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]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 983 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 984 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 985 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 986 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 987 | if (len > 0) { |
| 988 | char ch = namebuf[len-1]; |
| 989 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 990 | namebuf[len++] = '/'; |
| 991 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 992 | strcpy(namebuf + len, "*.*"); |
| 993 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 994 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 995 | return NULL; |
| 996 | |
| 997 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 998 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 999 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1000 | if (errno == ERROR_FILE_NOT_FOUND) |
| 1001 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1002 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1003 | } |
| 1004 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1005 | if (FileData.cFileName[0] == '.' && |
| 1006 | (FileData.cFileName[1] == '\0' || |
| 1007 | FileData.cFileName[1] == '.' && |
| 1008 | FileData.cFileName[2] == '\0')) |
| 1009 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1010 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1011 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1012 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1013 | d = NULL; |
| 1014 | break; |
| 1015 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1016 | if (PyList_Append(d, v) != 0) { |
| 1017 | Py_DECREF(v); |
| 1018 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1019 | d = NULL; |
| 1020 | break; |
| 1021 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1022 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1023 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1024 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1025 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1026 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1027 | |
| 1028 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1029 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1030 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1031 | |
| 1032 | #ifndef MAX_PATH |
| 1033 | #define MAX_PATH 250 |
| 1034 | #endif |
| 1035 | char *name, *pt; |
| 1036 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1037 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1038 | char namebuf[MAX_PATH+5]; |
| 1039 | struct _find_t ep; |
| 1040 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1041 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1042 | return NULL; |
| 1043 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1044 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1045 | return NULL; |
| 1046 | } |
| 1047 | strcpy(namebuf, name); |
| 1048 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1049 | if (*pt == ALTSEP) |
| 1050 | *pt = SEP; |
| 1051 | if (namebuf[len-1] != SEP) |
| 1052 | namebuf[len++] = SEP; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1053 | strcpy(namebuf + len, "*.*"); |
| 1054 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1055 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1056 | return NULL; |
| 1057 | |
| 1058 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1059 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 1060 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1061 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1062 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1063 | } |
| 1064 | do { |
| 1065 | if (ep.name[0] == '.' && |
| 1066 | (ep.name[1] == '\0' || |
| 1067 | ep.name[1] == '.' && |
| 1068 | ep.name[2] == '\0')) |
| 1069 | continue; |
| 1070 | strcpy(namebuf, ep.name); |
| 1071 | for (pt = namebuf; *pt; pt++) |
| 1072 | if (isupper(*pt)) |
| 1073 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1074 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1075 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1076 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1077 | d = NULL; |
| 1078 | break; |
| 1079 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1080 | if (PyList_Append(d, v) != 0) { |
| 1081 | Py_DECREF(v); |
| 1082 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1083 | d = NULL; |
| 1084 | break; |
| 1085 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1086 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1087 | } while (_dos_findnext(&ep) == 0); |
| 1088 | |
| 1089 | return d; |
| 1090 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1091 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1092 | |
| 1093 | #ifndef MAX_PATH |
| 1094 | #define MAX_PATH CCHMAXPATH |
| 1095 | #endif |
| 1096 | char *name, *pt; |
| 1097 | int len; |
| 1098 | PyObject *d, *v; |
| 1099 | char namebuf[MAX_PATH+5]; |
| 1100 | HDIR hdir = 1; |
| 1101 | ULONG srchcnt = 1; |
| 1102 | FILEFINDBUF3 ep; |
| 1103 | APIRET rc; |
| 1104 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1105 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1106 | return NULL; |
| 1107 | if (len >= MAX_PATH) { |
| 1108 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | strcpy(namebuf, name); |
| 1112 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1113 | if (*pt == ALTSEP) |
| 1114 | *pt = SEP; |
| 1115 | if (namebuf[len-1] != SEP) |
| 1116 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1117 | strcpy(namebuf + len, "*.*"); |
| 1118 | |
| 1119 | if ((d = PyList_New(0)) == NULL) |
| 1120 | return NULL; |
| 1121 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1122 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1123 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1124 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1125 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1126 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1127 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (rc != NO_ERROR) { |
| 1130 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1131 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1134 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1135 | do { |
| 1136 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1137 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1138 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1139 | |
| 1140 | strcpy(namebuf, ep.achName); |
| 1141 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1142 | /* Leave Case of Name Alone -- In Native Form */ |
| 1143 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1144 | |
| 1145 | v = PyString_FromString(namebuf); |
| 1146 | if (v == NULL) { |
| 1147 | Py_DECREF(d); |
| 1148 | d = NULL; |
| 1149 | break; |
| 1150 | } |
| 1151 | if (PyList_Append(d, v) != 0) { |
| 1152 | Py_DECREF(v); |
| 1153 | Py_DECREF(d); |
| 1154 | d = NULL; |
| 1155 | break; |
| 1156 | } |
| 1157 | Py_DECREF(v); |
| 1158 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1159 | } |
| 1160 | |
| 1161 | return d; |
| 1162 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1164 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1165 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1166 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1167 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1168 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1170 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1171 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1172 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1173 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1174 | closedir(dirp); |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1178 | if (ep->d_name[0] == '.' && |
| 1179 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1180 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1181 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1182 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1184 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1185 | d = NULL; |
| 1186 | break; |
| 1187 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1188 | if (PyList_Append(d, v) != 0) { |
| 1189 | Py_DECREF(v); |
| 1190 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1191 | d = NULL; |
| 1192 | break; |
| 1193 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1194 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1195 | } |
| 1196 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1198 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1199 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1200 | #endif /* which OS */ |
| 1201 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1202 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1203 | #ifdef MS_WIN32 |
| 1204 | /* A helper function for abspath on win32 */ |
| 1205 | static PyObject * |
| 1206 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1207 | { |
| 1208 | /* assume encoded strings wont more than double no of chars */ |
| 1209 | char inbuf[MAX_PATH*2]; |
| 1210 | char *inbufp = inbuf; |
| 1211 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1212 | char outbuf[MAX_PATH*2]; |
| 1213 | char *temp; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1214 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1215 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1216 | &insize)) |
| 1217 | return NULL; |
| 1218 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1219 | outbuf, &temp)) |
| 1220 | return win32_error("GetFullPathName", inbuf); |
| 1221 | return PyString_FromString(outbuf); |
| 1222 | } /* end of posix__getfullpathname */ |
| 1223 | #endif /* MS_WIN32 */ |
| 1224 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1225 | static char posix_mkdir__doc__[] = |
| 1226 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1227 | Create a directory."; |
| 1228 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1229 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1230 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1231 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1232 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1233 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1234 | int mode = 0777; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1235 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1236 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1237 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1238 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1239 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1240 | res = mkdir(path); |
| 1241 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1242 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1243 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1244 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1245 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1246 | return posix_error_with_allocated_filename(path); |
| 1247 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1248 | Py_INCREF(Py_None); |
| 1249 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1252 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1253 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1254 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1255 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1256 | #include <sys/resource.h> |
| 1257 | #endif |
| 1258 | #endif |
| 1259 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1260 | static char posix_nice__doc__[] = |
| 1261 | "nice(inc) -> new_priority\n\ |
| 1262 | Decrease the priority of process and return new priority."; |
| 1263 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1264 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1265 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1266 | { |
| 1267 | int increment, value; |
| 1268 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1269 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1270 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1271 | |
| 1272 | /* There are two flavours of 'nice': one that returns the new |
| 1273 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1274 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1275 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1276 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1277 | If we are of the nice family that returns the new priority, we |
| 1278 | need to clear errno before the call, and check if errno is filled |
| 1279 | before calling posix_error() on a returnvalue of -1, because the |
| 1280 | -1 may be the actual new priority! */ |
| 1281 | |
| 1282 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1283 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1284 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1285 | if (value == 0) |
| 1286 | value = getpriority(PRIO_PROCESS, 0); |
| 1287 | #endif |
| 1288 | if (value == -1 && errno != 0) |
| 1289 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1290 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1291 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1292 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1293 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1295 | |
| 1296 | static char posix_rename__doc__[] = |
| 1297 | "rename(old, new) -> None\n\ |
| 1298 | Rename a file or directory."; |
| 1299 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1300 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1301 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1302 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1303 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1306 | |
| 1307 | static char posix_rmdir__doc__[] = |
| 1308 | "rmdir(path) -> None\n\ |
| 1309 | Remove a directory."; |
| 1310 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1311 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1312 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1314 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1317 | |
| 1318 | static char posix_stat__doc__[] = |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 1319 | "stat(path) -> (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,\n\ |
| 1320 | st_size, st_atime, st_mtime, st_ctime)\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1321 | Perform a stat system call on the given path."; |
| 1322 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1323 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1324 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1325 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1326 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1330 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1331 | static char posix_system__doc__[] = |
| 1332 | "system(command) -> exit_status\n\ |
| 1333 | Execute the command (a string) in a subshell."; |
| 1334 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1335 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1336 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1337 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1338 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1339 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1340 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1342 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1343 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1344 | Py_END_ALLOW_THREADS |
| 1345 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1346 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1347 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1348 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1349 | |
| 1350 | static char posix_umask__doc__[] = |
| 1351 | "umask(new_mask) -> old_mask\n\ |
| 1352 | Set the current numeric umask and return the previous umask."; |
| 1353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1354 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1355 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1356 | { |
| 1357 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1358 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1359 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1360 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1361 | if (i < 0) |
| 1362 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1363 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1366 | |
| 1367 | static char posix_unlink__doc__[] = |
| 1368 | "unlink(path) -> None\n\ |
| 1369 | Remove a file (same as remove(path))."; |
| 1370 | |
| 1371 | static char posix_remove__doc__[] = |
| 1372 | "remove(path) -> None\n\ |
| 1373 | Remove a file (same as unlink(path))."; |
| 1374 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1375 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1376 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1377 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1378 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1381 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1382 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1383 | static char posix_uname__doc__[] = |
| 1384 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1385 | Return a tuple identifying the current operating system."; |
| 1386 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1387 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1388 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1389 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1390 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1391 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1392 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1393 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1394 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1395 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1396 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1397 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1398 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1399 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1400 | u.sysname, |
| 1401 | u.nodename, |
| 1402 | u.release, |
| 1403 | u.version, |
| 1404 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1405 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1406 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1407 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1408 | |
| 1409 | static char posix_utime__doc__[] = |
| 1410 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1411 | utime(path, None) -> None\n\ |
| 1412 | Set the access and modified time of the file to the given values. If the\n\ |
| 1413 | 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] | 1414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1415 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1416 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1417 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1418 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1419 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1420 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1421 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1422 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1423 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1424 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1425 | struct utimbuf buf; |
| 1426 | #define ATIME buf.actime |
| 1427 | #define MTIME buf.modtime |
| 1428 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1429 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1430 | time_t buf[2]; |
| 1431 | #define ATIME buf[0] |
| 1432 | #define MTIME buf[1] |
| 1433 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1434 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1435 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1436 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1437 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1438 | if (arg == Py_None) { |
| 1439 | /* optional time values not given */ |
| 1440 | Py_BEGIN_ALLOW_THREADS |
| 1441 | res = utime(path, NULL); |
| 1442 | Py_END_ALLOW_THREADS |
| 1443 | } |
| 1444 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1445 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1446 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | } |
| 1449 | else { |
| 1450 | ATIME = atime; |
| 1451 | MTIME = mtime; |
| 1452 | Py_BEGIN_ALLOW_THREADS |
| 1453 | res = utime(path, UTIME_ARG); |
| 1454 | Py_END_ALLOW_THREADS |
| 1455 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1456 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1457 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1458 | Py_INCREF(Py_None); |
| 1459 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1460 | #undef UTIME_ARG |
| 1461 | #undef ATIME |
| 1462 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1465 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1466 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1467 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1468 | static char posix__exit__doc__[] = |
| 1469 | "_exit(status)\n\ |
| 1470 | Exit to the system with specified status, without normal exit processing."; |
| 1471 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1473 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1474 | { |
| 1475 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1476 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1477 | return NULL; |
| 1478 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1479 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1482 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1483 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1484 | static char posix_execv__doc__[] = |
| 1485 | "execv(path, args)\n\ |
| 1486 | Execute an executable path with arguments, replacing current process.\n\ |
| 1487 | \n\ |
| 1488 | path: path of executable file\n\ |
| 1489 | args: tuple or list of strings"; |
| 1490 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1491 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1492 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1493 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1494 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1495 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1496 | char **argvlist; |
| 1497 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1498 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1499 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1500 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1501 | argv is a list or tuple of strings. */ |
| 1502 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1503 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1504 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1505 | if (PyList_Check(argv)) { |
| 1506 | argc = PyList_Size(argv); |
| 1507 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1508 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1509 | else if (PyTuple_Check(argv)) { |
| 1510 | argc = PyTuple_Size(argv); |
| 1511 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1512 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1513 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1514 | 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] | 1515 | return NULL; |
| 1516 | } |
| 1517 | |
| 1518 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1519 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1520 | return NULL; |
| 1521 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1522 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1523 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1524 | if (argvlist == NULL) |
| 1525 | return NULL; |
| 1526 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1527 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1528 | PyMem_DEL(argvlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1529 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1530 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1531 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1532 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1533 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1534 | } |
| 1535 | argvlist[argc] = NULL; |
| 1536 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1537 | #ifdef BAD_EXEC_PROTOTYPES |
| 1538 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1539 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1540 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1541 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1542 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1543 | /* If we get here it's definitely an error */ |
| 1544 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1545 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1546 | return posix_error(); |
| 1547 | } |
| 1548 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1549 | |
| 1550 | static char posix_execve__doc__[] = |
| 1551 | "execve(path, args, env)\n\ |
| 1552 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1553 | \n\ |
| 1554 | path: path of executable file\n\ |
| 1555 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1556 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1558 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1559 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1560 | { |
| 1561 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1562 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1563 | char **argvlist; |
| 1564 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1565 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1566 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1567 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1568 | |
| 1569 | /* execve has three arguments: (path, argv, env), where |
| 1570 | argv is a list or tuple of strings and env is a dictionary |
| 1571 | like posix.environ. */ |
| 1572 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1573 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1574 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1575 | if (PyList_Check(argv)) { |
| 1576 | argc = PyList_Size(argv); |
| 1577 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1578 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1579 | else if (PyTuple_Check(argv)) { |
| 1580 | argc = PyTuple_Size(argv); |
| 1581 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1582 | } |
| 1583 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1584 | 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] | 1585 | return NULL; |
| 1586 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1587 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1588 | 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] | 1589 | return NULL; |
| 1590 | } |
| 1591 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1592 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1593 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1594 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1595 | return NULL; |
| 1596 | } |
| 1597 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1598 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1599 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1600 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1601 | return NULL; |
| 1602 | } |
| 1603 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1604 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1605 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1606 | &argvlist[i])) |
| 1607 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1608 | goto fail_1; |
| 1609 | } |
| 1610 | } |
| 1611 | argvlist[argc] = NULL; |
| 1612 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1613 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1614 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1615 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1616 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1617 | goto fail_1; |
| 1618 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1619 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1620 | keys = PyMapping_Keys(env); |
| 1621 | vals = PyMapping_Values(env); |
| 1622 | if (!keys || !vals) |
| 1623 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1624 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1625 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1626 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1627 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1628 | |
| 1629 | key = PyList_GetItem(keys, pos); |
| 1630 | val = PyList_GetItem(vals, pos); |
| 1631 | if (!key || !val) |
| 1632 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1633 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1634 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1635 | !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] | 1636 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1637 | goto fail_2; |
| 1638 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1639 | |
| 1640 | #if defined(PYOS_OS2) |
| 1641 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1642 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1643 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1644 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1645 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1646 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1647 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1648 | goto fail_2; |
| 1649 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1650 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1651 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1652 | #if defined(PYOS_OS2) |
| 1653 | } |
| 1654 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1655 | } |
| 1656 | envlist[envc] = 0; |
| 1657 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1658 | |
| 1659 | #ifdef BAD_EXEC_PROTOTYPES |
| 1660 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1661 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1662 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1663 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1665 | /* If we get here it's definitely an error */ |
| 1666 | |
| 1667 | (void) posix_error(); |
| 1668 | |
| 1669 | fail_2: |
| 1670 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1671 | PyMem_DEL(envlist[envc]); |
| 1672 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1673 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1674 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1675 | Py_XDECREF(vals); |
| 1676 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1677 | return NULL; |
| 1678 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1679 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1680 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1681 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1682 | #ifdef HAVE_SPAWNV |
| 1683 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1684 | "spawnv(mode, path, args)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1685 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1686 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1687 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1688 | path: path of executable file\n\ |
| 1689 | args: tuple or list of strings"; |
| 1690 | |
| 1691 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1692 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1693 | { |
| 1694 | char *path; |
| 1695 | PyObject *argv; |
| 1696 | char **argvlist; |
| 1697 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1698 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1699 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1700 | |
| 1701 | /* spawnv has three arguments: (mode, path, argv), where |
| 1702 | argv is a list or tuple of strings. */ |
| 1703 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1704 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1705 | return NULL; |
| 1706 | if (PyList_Check(argv)) { |
| 1707 | argc = PyList_Size(argv); |
| 1708 | getitem = PyList_GetItem; |
| 1709 | } |
| 1710 | else if (PyTuple_Check(argv)) { |
| 1711 | argc = PyTuple_Size(argv); |
| 1712 | getitem = PyTuple_GetItem; |
| 1713 | } |
| 1714 | else { |
Martin v. Löwis | e75f0e4 | 2001-11-24 09:31:44 +0000 | [diff] [blame] | 1715 | 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] | 1716 | return NULL; |
| 1717 | } |
| 1718 | |
| 1719 | argvlist = PyMem_NEW(char *, argc+1); |
| 1720 | if (argvlist == NULL) |
| 1721 | return NULL; |
| 1722 | for (i = 0; i < argc; i++) { |
| 1723 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1724 | PyMem_DEL(argvlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1725 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1726 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1727 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | argvlist[argc] = NULL; |
| 1731 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1732 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1733 | Py_BEGIN_ALLOW_THREADS |
| 1734 | spawnval = spawnv(mode, path, argvlist); |
| 1735 | Py_END_ALLOW_THREADS |
| 1736 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1737 | if (mode == _OLD_P_OVERLAY) |
| 1738 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1739 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1740 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1741 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1742 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1743 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1744 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1745 | PyMem_DEL(argvlist); |
| 1746 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1747 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1748 | return posix_error(); |
| 1749 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1750 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1751 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1752 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1753 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1754 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1759 | "spawnve(mode, path, args, env)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1760 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1761 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1762 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1763 | path: path of executable file\n\ |
| 1764 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1765 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1766 | |
| 1767 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1768 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1769 | { |
| 1770 | char *path; |
| 1771 | PyObject *argv, *env; |
| 1772 | char **argvlist; |
| 1773 | char **envlist; |
| 1774 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1775 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1776 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1777 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1778 | |
| 1779 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1780 | argv is a list or tuple of strings and env is a dictionary |
| 1781 | like posix.environ. */ |
| 1782 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1783 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1784 | return NULL; |
| 1785 | if (PyList_Check(argv)) { |
| 1786 | argc = PyList_Size(argv); |
| 1787 | getitem = PyList_GetItem; |
| 1788 | } |
| 1789 | else if (PyTuple_Check(argv)) { |
| 1790 | argc = PyTuple_Size(argv); |
| 1791 | getitem = PyTuple_GetItem; |
| 1792 | } |
| 1793 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1794 | 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] | 1795 | return NULL; |
| 1796 | } |
| 1797 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1798 | 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] | 1799 | return NULL; |
| 1800 | } |
| 1801 | |
| 1802 | argvlist = PyMem_NEW(char *, argc+1); |
| 1803 | if (argvlist == NULL) { |
| 1804 | PyErr_NoMemory(); |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | for (i = 0; i < argc; i++) { |
| 1808 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1809 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1810 | &argvlist[i])) |
| 1811 | { |
| 1812 | goto fail_1; |
| 1813 | } |
| 1814 | } |
| 1815 | argvlist[argc] = NULL; |
| 1816 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1817 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1818 | envlist = PyMem_NEW(char *, i + 1); |
| 1819 | if (envlist == NULL) { |
| 1820 | PyErr_NoMemory(); |
| 1821 | goto fail_1; |
| 1822 | } |
| 1823 | envc = 0; |
| 1824 | keys = PyMapping_Keys(env); |
| 1825 | vals = PyMapping_Values(env); |
| 1826 | if (!keys || !vals) |
| 1827 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1828 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1829 | for (pos = 0; pos < i; pos++) { |
| 1830 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1831 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1832 | |
| 1833 | key = PyList_GetItem(keys, pos); |
| 1834 | val = PyList_GetItem(vals, pos); |
| 1835 | if (!key || !val) |
| 1836 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1837 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1838 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1839 | !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] | 1840 | { |
| 1841 | goto fail_2; |
| 1842 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1843 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1844 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1845 | if (p == NULL) { |
| 1846 | PyErr_NoMemory(); |
| 1847 | goto fail_2; |
| 1848 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1849 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1850 | envlist[envc++] = p; |
| 1851 | } |
| 1852 | envlist[envc] = 0; |
| 1853 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1854 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1855 | Py_BEGIN_ALLOW_THREADS |
| 1856 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 1857 | Py_END_ALLOW_THREADS |
| 1858 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1859 | if (mode == _OLD_P_OVERLAY) |
| 1860 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1861 | |
| 1862 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1863 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1864 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1865 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1866 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1867 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1868 | (void) posix_error(); |
| 1869 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1870 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1871 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1872 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1873 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1874 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1875 | |
| 1876 | fail_2: |
| 1877 | while (--envc >= 0) |
| 1878 | PyMem_DEL(envlist[envc]); |
| 1879 | PyMem_DEL(envlist); |
| 1880 | fail_1: |
| 1881 | PyMem_DEL(argvlist); |
| 1882 | Py_XDECREF(vals); |
| 1883 | Py_XDECREF(keys); |
| 1884 | return res; |
| 1885 | } |
| 1886 | #endif /* HAVE_SPAWNV */ |
| 1887 | |
| 1888 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1889 | #ifdef HAVE_FORK1 |
| 1890 | static char posix_fork1__doc__[] = |
| 1891 | "fork1() -> pid\n\ |
| 1892 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1893 | \n\ |
| 1894 | Return 0 to child process and PID of child to parent process."; |
| 1895 | |
| 1896 | static PyObject * |
| 1897 | posix_fork1(self, args) |
| 1898 | PyObject *self; |
| 1899 | PyObject *args; |
| 1900 | { |
| 1901 | int pid; |
| 1902 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1903 | return NULL; |
| 1904 | pid = fork1(); |
| 1905 | if (pid == -1) |
| 1906 | return posix_error(); |
| 1907 | PyOS_AfterFork(); |
| 1908 | return PyInt_FromLong((long)pid); |
| 1909 | } |
| 1910 | #endif |
| 1911 | |
| 1912 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1913 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1914 | static char posix_fork__doc__[] = |
| 1915 | "fork() -> pid\n\ |
| 1916 | Fork a child process.\n\ |
| 1917 | \n\ |
| 1918 | Return 0 to child process and PID of child to parent process."; |
| 1919 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1920 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1921 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1922 | { |
| 1923 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1924 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1925 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1926 | pid = fork(); |
| 1927 | if (pid == -1) |
| 1928 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1929 | if (pid == 0) |
| 1930 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1931 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1932 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1933 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1934 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1935 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1936 | #ifdef HAVE_PTY_H |
| 1937 | #include <pty.h> |
| 1938 | #else |
| 1939 | #ifdef HAVE_LIBUTIL_H |
| 1940 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1941 | #endif /* HAVE_LIBUTIL_H */ |
| 1942 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1943 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1944 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1945 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1946 | static char posix_openpty__doc__[] = |
| 1947 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1948 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1949 | |
| 1950 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1951 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1952 | { |
| 1953 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1954 | #ifndef HAVE_OPENPTY |
| 1955 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1956 | #endif |
| 1957 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1958 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1959 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1960 | |
| 1961 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1962 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1963 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1964 | #else |
| 1965 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1966 | if (slave_name == NULL) |
| 1967 | return posix_error(); |
| 1968 | |
| 1969 | slave_fd = open(slave_name, O_RDWR); |
| 1970 | if (slave_fd < 0) |
| 1971 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1972 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1973 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1974 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1975 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1976 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1977 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1978 | |
| 1979 | #ifdef HAVE_FORKPTY |
| 1980 | static char posix_forkpty__doc__[] = |
| 1981 | "forkpty() -> (pid, master_fd)\n\ |
| 1982 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1983 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1984 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1985 | |
| 1986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1987 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1988 | { |
| 1989 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1990 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1991 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1992 | return NULL; |
| 1993 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1994 | if (pid == -1) |
| 1995 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1996 | if (pid == 0) |
| 1997 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1998 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1999 | } |
| 2000 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2001 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2002 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2003 | static char posix_getegid__doc__[] = |
| 2004 | "getegid() -> egid\n\ |
| 2005 | Return the current process's effective group id."; |
| 2006 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2007 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2008 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2009 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2010 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2011 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2012 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2013 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2014 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2015 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2017 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2018 | static char posix_geteuid__doc__[] = |
| 2019 | "geteuid() -> euid\n\ |
| 2020 | Return the current process's effective user id."; |
| 2021 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2023 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2024 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2025 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2026 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2027 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2028 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2029 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2030 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2032 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2033 | static char posix_getgid__doc__[] = |
| 2034 | "getgid() -> gid\n\ |
| 2035 | Return the current process's group id."; |
| 2036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2037 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2038 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2039 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2040 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2041 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2042 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2043 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2044 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2045 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2046 | |
| 2047 | static char posix_getpid__doc__[] = |
| 2048 | "getpid() -> pid\n\ |
| 2049 | Return the current process id"; |
| 2050 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2051 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2052 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2053 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2054 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2055 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2056 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2059 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2060 | #ifdef HAVE_GETGROUPS |
| 2061 | static char posix_getgroups__doc__[] = "\ |
| 2062 | getgroups() -> list of group IDs\n\ |
| 2063 | Return list of supplemental group IDs for the process."; |
| 2064 | |
| 2065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2066 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2067 | { |
| 2068 | PyObject *result = NULL; |
| 2069 | |
| 2070 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2071 | #ifdef NGROUPS_MAX |
| 2072 | #define MAX_GROUPS NGROUPS_MAX |
| 2073 | #else |
| 2074 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2075 | #define MAX_GROUPS 64 |
| 2076 | #endif |
| 2077 | gid_t grouplist[MAX_GROUPS]; |
| 2078 | int n; |
| 2079 | |
| 2080 | n = getgroups(MAX_GROUPS, grouplist); |
| 2081 | if (n < 0) |
| 2082 | posix_error(); |
| 2083 | else { |
| 2084 | result = PyList_New(n); |
| 2085 | if (result != NULL) { |
| 2086 | PyObject *o; |
| 2087 | int i; |
| 2088 | for (i = 0; i < n; ++i) { |
| 2089 | o = PyInt_FromLong((long)grouplist[i]); |
| 2090 | if (o == NULL) { |
| 2091 | Py_DECREF(result); |
| 2092 | result = NULL; |
| 2093 | break; |
| 2094 | } |
| 2095 | PyList_SET_ITEM(result, i, o); |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | } |
| 2100 | return result; |
| 2101 | } |
| 2102 | #endif |
| 2103 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2104 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2105 | static char posix_getpgrp__doc__[] = |
| 2106 | "getpgrp() -> pgrp\n\ |
| 2107 | Return the current process group id."; |
| 2108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2110 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2111 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2112 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2113 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2114 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2115 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2116 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2117 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2118 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2119 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2120 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2123 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2124 | static char posix_setpgrp__doc__[] = |
| 2125 | "setpgrp() -> None\n\ |
| 2126 | Make this process a session leader."; |
| 2127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2128 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2129 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2130 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2131 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2132 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2133 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2134 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2135 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2136 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2137 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2138 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2139 | Py_INCREF(Py_None); |
| 2140 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2143 | #endif /* HAVE_SETPGRP */ |
| 2144 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2145 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2146 | static char posix_getppid__doc__[] = |
| 2147 | "getppid() -> ppid\n\ |
| 2148 | Return the parent's process id."; |
| 2149 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2150 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2151 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2152 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2153 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2154 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2155 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2156 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2157 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2158 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2159 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2160 | #ifdef HAVE_GETLOGIN |
| 2161 | static char posix_getlogin__doc__[] = "\ |
| 2162 | getlogin() -> string\n\ |
| 2163 | Return the actual login name."; |
| 2164 | |
| 2165 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2166 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2167 | { |
| 2168 | PyObject *result = NULL; |
| 2169 | |
| 2170 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2171 | char *name; |
| 2172 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2173 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2174 | errno = 0; |
| 2175 | name = getlogin(); |
| 2176 | if (name == NULL) { |
| 2177 | if (errno) |
| 2178 | posix_error(); |
| 2179 | else |
| 2180 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2181 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2182 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2183 | else |
| 2184 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2185 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2186 | } |
| 2187 | return result; |
| 2188 | } |
| 2189 | #endif |
| 2190 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2191 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2192 | static char posix_getuid__doc__[] = |
| 2193 | "getuid() -> uid\n\ |
| 2194 | Return the current process's user id."; |
| 2195 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2196 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2197 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2198 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2199 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2200 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2201 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2202 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2203 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2205 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2206 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2207 | static char posix_kill__doc__[] = |
| 2208 | "kill(pid, sig) -> None\n\ |
| 2209 | Kill a process with a signal."; |
| 2210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2212 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2213 | { |
| 2214 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2215 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2216 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2217 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2218 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2219 | APIRET rc; |
| 2220 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2221 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2222 | |
| 2223 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2224 | APIRET rc; |
| 2225 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2226 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2227 | |
| 2228 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2229 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2230 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2231 | if (kill(pid, sig) == -1) |
| 2232 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2233 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2234 | Py_INCREF(Py_None); |
| 2235 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2236 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2237 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2238 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2239 | #ifdef HAVE_KILLPG |
| 2240 | static char posix_killpg__doc__[] = |
| 2241 | "killpg(pgid, sig) -> None\n\ |
| 2242 | Kill a process group with a signal."; |
| 2243 | |
| 2244 | static PyObject * |
| 2245 | posix_killpg(PyObject *self, PyObject *args) |
| 2246 | { |
| 2247 | int pgid, sig; |
| 2248 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2249 | return NULL; |
| 2250 | if (killpg(pgid, sig) == -1) |
| 2251 | return posix_error(); |
| 2252 | Py_INCREF(Py_None); |
| 2253 | return Py_None; |
| 2254 | } |
| 2255 | #endif |
| 2256 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2257 | #ifdef HAVE_PLOCK |
| 2258 | |
| 2259 | #ifdef HAVE_SYS_LOCK_H |
| 2260 | #include <sys/lock.h> |
| 2261 | #endif |
| 2262 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2263 | static char posix_plock__doc__[] = |
| 2264 | "plock(op) -> None\n\ |
| 2265 | Lock program segments into memory."; |
| 2266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2267 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2268 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2269 | { |
| 2270 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2271 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2272 | return NULL; |
| 2273 | if (plock(op) == -1) |
| 2274 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2275 | Py_INCREF(Py_None); |
| 2276 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2277 | } |
| 2278 | #endif |
| 2279 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2280 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2281 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2282 | static char posix_popen__doc__[] = |
| 2283 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2284 | Open a pipe to/from a command returning a file object."; |
| 2285 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2286 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2287 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2288 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2289 | async_system(const char *command) |
| 2290 | { |
| 2291 | char *p, errormsg[256], args[1024]; |
| 2292 | RESULTCODES rcodes; |
| 2293 | APIRET rc; |
| 2294 | char *shell = getenv("COMSPEC"); |
| 2295 | if (!shell) |
| 2296 | shell = "cmd"; |
| 2297 | |
| 2298 | strcpy(args, shell); |
| 2299 | p = &args[ strlen(args)+1 ]; |
| 2300 | strcpy(p, "/c "); |
| 2301 | strcat(p, command); |
| 2302 | p += strlen(p) + 1; |
| 2303 | *p = '\0'; |
| 2304 | |
| 2305 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2306 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2307 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2308 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2309 | &rcodes, shell); |
| 2310 | return rc; |
| 2311 | } |
| 2312 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2313 | static FILE * |
| 2314 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2315 | { |
| 2316 | HFILE rhan, whan; |
| 2317 | FILE *retfd = NULL; |
| 2318 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2319 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2320 | if (rc != NO_ERROR) { |
| 2321 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2322 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2323 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2324 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2325 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2326 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2327 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2328 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2329 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2330 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2331 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2332 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2333 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2334 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2337 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2338 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2339 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2340 | if (rc == NO_ERROR) |
| 2341 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2342 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2343 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2344 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2345 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2346 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2347 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2348 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2349 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2350 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2351 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2352 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2353 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2354 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2355 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2358 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2359 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2360 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2361 | if (rc == NO_ERROR) |
| 2362 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2363 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2364 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2365 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2366 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2367 | } else { |
| 2368 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2369 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2370 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2374 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2375 | { |
| 2376 | char *name; |
| 2377 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2378 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2379 | FILE *fp; |
| 2380 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2381 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2382 | return NULL; |
| 2383 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2384 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2385 | Py_END_ALLOW_THREADS |
| 2386 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2387 | return os2_error(err); |
| 2388 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2389 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2390 | if (f != NULL) |
| 2391 | PyFile_SetBufSize(f, bufsize); |
| 2392 | return f; |
| 2393 | } |
| 2394 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2395 | #elif defined(PYCC_GCC) |
| 2396 | |
| 2397 | /* standard posix version of popen() support */ |
| 2398 | static PyObject * |
| 2399 | posix_popen(PyObject *self, PyObject *args) |
| 2400 | { |
| 2401 | char *name; |
| 2402 | char *mode = "r"; |
| 2403 | int bufsize = -1; |
| 2404 | FILE *fp; |
| 2405 | PyObject *f; |
| 2406 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 2407 | return NULL; |
| 2408 | Py_BEGIN_ALLOW_THREADS |
| 2409 | fp = popen(name, mode); |
| 2410 | Py_END_ALLOW_THREADS |
| 2411 | if (fp == NULL) |
| 2412 | return posix_error(); |
| 2413 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 2414 | if (f != NULL) |
| 2415 | PyFile_SetBufSize(f, bufsize); |
| 2416 | return f; |
| 2417 | } |
| 2418 | |
| 2419 | /* fork() under OS/2 has lots'o'warts |
| 2420 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 2421 | * most of this code is a ripoff of the win32 code, but using the |
| 2422 | * capabilities of EMX's C library routines |
| 2423 | */ |
| 2424 | |
| 2425 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 2426 | #define POPEN_1 1 |
| 2427 | #define POPEN_2 2 |
| 2428 | #define POPEN_3 3 |
| 2429 | #define POPEN_4 4 |
| 2430 | |
| 2431 | static PyObject *_PyPopen(char *, int, int, int); |
| 2432 | static int _PyPclose(FILE *file); |
| 2433 | |
| 2434 | /* |
| 2435 | * Internal dictionary mapping popen* file pointers to process handles, |
| 2436 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2437 | * for more information on this dictionary's use. |
| 2438 | */ |
| 2439 | static PyObject *_PyPopenProcs = NULL; |
| 2440 | |
| 2441 | /* os2emx version of popen2() |
| 2442 | * |
| 2443 | * The result of this function is a pipe (file) connected to the |
| 2444 | * process's stdin, and a pipe connected to the process's stdout. |
| 2445 | */ |
| 2446 | |
| 2447 | static PyObject * |
| 2448 | os2emx_popen2(PyObject *self, PyObject *args) |
| 2449 | { |
| 2450 | PyObject *f; |
| 2451 | int tm=0; |
| 2452 | |
| 2453 | char *cmdstring; |
| 2454 | char *mode = "t"; |
| 2455 | int bufsize = -1; |
| 2456 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 2457 | return NULL; |
| 2458 | |
| 2459 | if (*mode == 't') |
| 2460 | tm = O_TEXT; |
| 2461 | else if (*mode != 'b') { |
| 2462 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2463 | return NULL; |
| 2464 | } else |
| 2465 | tm = O_BINARY; |
| 2466 | |
| 2467 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 2468 | |
| 2469 | return f; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
| 2473 | * Variation on os2emx.popen2 |
| 2474 | * |
| 2475 | * The result of this function is 3 pipes - the process's stdin, |
| 2476 | * stdout and stderr |
| 2477 | */ |
| 2478 | |
| 2479 | static PyObject * |
| 2480 | os2emx_popen3(PyObject *self, PyObject *args) |
| 2481 | { |
| 2482 | PyObject *f; |
| 2483 | int tm = 0; |
| 2484 | |
| 2485 | char *cmdstring; |
| 2486 | char *mode = "t"; |
| 2487 | int bufsize = -1; |
| 2488 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 2489 | return NULL; |
| 2490 | |
| 2491 | if (*mode == 't') |
| 2492 | tm = O_TEXT; |
| 2493 | else if (*mode != 'b') { |
| 2494 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2495 | return NULL; |
| 2496 | } else |
| 2497 | tm = O_BINARY; |
| 2498 | |
| 2499 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 2500 | |
| 2501 | return f; |
| 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * Variation on os2emx.popen2 |
| 2506 | * |
| 2507 | * The result of this function is 2 pipes - the processes stdin, |
| 2508 | * and stdout+stderr combined as a single pipe. |
| 2509 | */ |
| 2510 | |
| 2511 | static PyObject * |
| 2512 | os2emx_popen4(PyObject *self, PyObject *args) |
| 2513 | { |
| 2514 | PyObject *f; |
| 2515 | int tm = 0; |
| 2516 | |
| 2517 | char *cmdstring; |
| 2518 | char *mode = "t"; |
| 2519 | int bufsize = -1; |
| 2520 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 2521 | return NULL; |
| 2522 | |
| 2523 | if (*mode == 't') |
| 2524 | tm = O_TEXT; |
| 2525 | else if (*mode != 'b') { |
| 2526 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2527 | return NULL; |
| 2528 | } else |
| 2529 | tm = O_BINARY; |
| 2530 | |
| 2531 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 2532 | |
| 2533 | return f; |
| 2534 | } |
| 2535 | |
| 2536 | /* a couple of structures for convenient handling of multiple |
| 2537 | * file handles and pipes |
| 2538 | */ |
| 2539 | struct file_ref |
| 2540 | { |
| 2541 | int handle; |
| 2542 | int flags; |
| 2543 | }; |
| 2544 | |
| 2545 | struct pipe_ref |
| 2546 | { |
| 2547 | int rd; |
| 2548 | int wr; |
| 2549 | }; |
| 2550 | |
| 2551 | /* The following code is derived from the win32 code */ |
| 2552 | |
| 2553 | static PyObject * |
| 2554 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 2555 | { |
| 2556 | struct file_ref stdio[3]; |
| 2557 | struct pipe_ref p_fd[3]; |
| 2558 | FILE *p_s[3]; |
| 2559 | int file_count, i, pipe_err, pipe_pid; |
| 2560 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 2561 | PyObject *f, *p_f[3]; |
| 2562 | |
| 2563 | /* file modes for subsequent fdopen's on pipe handles */ |
| 2564 | if (mode == O_TEXT) |
| 2565 | { |
| 2566 | rd_mode = "rt"; |
| 2567 | wr_mode = "wt"; |
| 2568 | } |
| 2569 | else |
| 2570 | { |
| 2571 | rd_mode = "rb"; |
| 2572 | wr_mode = "wb"; |
| 2573 | } |
| 2574 | |
| 2575 | /* prepare shell references */ |
| 2576 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 2577 | if ((shell = getenv("COMSPEC")) == NULL) |
| 2578 | { |
| 2579 | errno = ENOENT; |
| 2580 | return posix_error(); |
| 2581 | } |
| 2582 | |
| 2583 | sh_name = _getname(shell); |
| 2584 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 2585 | opt = "/c"; |
| 2586 | else |
| 2587 | opt = "-c"; |
| 2588 | |
| 2589 | /* save current stdio fds + their flags, and set not inheritable */ |
| 2590 | i = pipe_err = 0; |
| 2591 | while (pipe_err >= 0 && i < 3) |
| 2592 | { |
| 2593 | pipe_err = stdio[i].handle = dup(i); |
| 2594 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 2595 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 2596 | i++; |
| 2597 | } |
| 2598 | if (pipe_err < 0) |
| 2599 | { |
| 2600 | /* didn't get them all saved - clean up and bail out */ |
| 2601 | int saved_err = errno; |
| 2602 | while (i-- > 0) |
| 2603 | { |
| 2604 | close(stdio[i].handle); |
| 2605 | } |
| 2606 | errno = saved_err; |
| 2607 | return posix_error(); |
| 2608 | } |
| 2609 | |
| 2610 | /* create pipe ends */ |
| 2611 | file_count = 2; |
| 2612 | if (n == POPEN_3) |
| 2613 | file_count = 3; |
| 2614 | i = pipe_err = 0; |
| 2615 | while ((pipe_err == 0) && (i < file_count)) |
| 2616 | pipe_err = pipe((int *)&p_fd[i++]); |
| 2617 | if (pipe_err < 0) |
| 2618 | { |
| 2619 | /* didn't get them all made - clean up and bail out */ |
| 2620 | while (i-- > 0) |
| 2621 | { |
| 2622 | close(p_fd[i].wr); |
| 2623 | close(p_fd[i].rd); |
| 2624 | } |
| 2625 | errno = EPIPE; |
| 2626 | return posix_error(); |
| 2627 | } |
| 2628 | |
| 2629 | /* change the actual standard IO streams over temporarily, |
| 2630 | * making the retained pipe ends non-inheritable |
| 2631 | */ |
| 2632 | pipe_err = 0; |
| 2633 | |
| 2634 | /* - stdin */ |
| 2635 | if (dup2(p_fd[0].rd, 0) == 0) |
| 2636 | { |
| 2637 | close(p_fd[0].rd); |
| 2638 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 2639 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 2640 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 2641 | { |
| 2642 | close(p_fd[0].wr); |
| 2643 | pipe_err = -1; |
| 2644 | } |
| 2645 | } |
| 2646 | else |
| 2647 | { |
| 2648 | pipe_err = -1; |
| 2649 | } |
| 2650 | |
| 2651 | /* - stdout */ |
| 2652 | if (pipe_err == 0) |
| 2653 | { |
| 2654 | if (dup2(p_fd[1].wr, 1) == 1) |
| 2655 | { |
| 2656 | close(p_fd[1].wr); |
| 2657 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 2658 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 2659 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 2660 | { |
| 2661 | close(p_fd[1].rd); |
| 2662 | pipe_err = -1; |
| 2663 | } |
| 2664 | } |
| 2665 | else |
| 2666 | { |
| 2667 | pipe_err = -1; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | /* - stderr, as required */ |
| 2672 | if (pipe_err == 0) |
| 2673 | switch (n) |
| 2674 | { |
| 2675 | case POPEN_3: |
| 2676 | { |
| 2677 | if (dup2(p_fd[2].wr, 2) == 2) |
| 2678 | { |
| 2679 | close(p_fd[2].wr); |
| 2680 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 2681 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 2682 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 2683 | { |
| 2684 | close(p_fd[2].rd); |
| 2685 | pipe_err = -1; |
| 2686 | } |
| 2687 | } |
| 2688 | else |
| 2689 | { |
| 2690 | pipe_err = -1; |
| 2691 | } |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | case POPEN_4: |
| 2696 | { |
| 2697 | if (dup2(1, 2) != 2) |
| 2698 | { |
| 2699 | pipe_err = -1; |
| 2700 | } |
| 2701 | break; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | /* spawn the child process */ |
| 2706 | if (pipe_err == 0) |
| 2707 | { |
| 2708 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 2709 | if (pipe_pid == -1) |
| 2710 | { |
| 2711 | pipe_err = -1; |
| 2712 | } |
| 2713 | else |
| 2714 | { |
| 2715 | /* save the PID into the FILE structure |
| 2716 | * NOTE: this implementation doesn't actually |
| 2717 | * take advantage of this, but do it for |
| 2718 | * completeness - AIM Apr01 |
| 2719 | */ |
| 2720 | for (i = 0; i < file_count; i++) |
| 2721 | p_s[i]->_pid = pipe_pid; |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | /* reset standard IO to normal */ |
| 2726 | for (i = 0; i < 3; i++) |
| 2727 | { |
| 2728 | dup2(stdio[i].handle, i); |
| 2729 | fcntl(i, F_SETFD, stdio[i].flags); |
| 2730 | close(stdio[i].handle); |
| 2731 | } |
| 2732 | |
| 2733 | /* if any remnant problems, clean up and bail out */ |
| 2734 | if (pipe_err < 0) |
| 2735 | { |
| 2736 | for (i = 0; i < 3; i++) |
| 2737 | { |
| 2738 | close(p_fd[i].rd); |
| 2739 | close(p_fd[i].wr); |
| 2740 | } |
| 2741 | errno = EPIPE; |
| 2742 | return posix_error_with_filename(cmdstring); |
| 2743 | } |
| 2744 | |
| 2745 | /* build tuple of file objects to return */ |
| 2746 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 2747 | PyFile_SetBufSize(p_f[0], bufsize); |
| 2748 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 2749 | PyFile_SetBufSize(p_f[1], bufsize); |
| 2750 | if (n == POPEN_3) |
| 2751 | { |
| 2752 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 2753 | PyFile_SetBufSize(p_f[0], bufsize); |
| 2754 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 2755 | } |
| 2756 | else |
| 2757 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 2758 | |
| 2759 | /* |
| 2760 | * Insert the files we've created into the process dictionary |
| 2761 | * all referencing the list with the process handle and the |
| 2762 | * initial number of files (see description below in _PyPclose). |
| 2763 | * Since if _PyPclose later tried to wait on a process when all |
| 2764 | * handles weren't closed, it could create a deadlock with the |
| 2765 | * child, we spend some energy here to try to ensure that we |
| 2766 | * either insert all file handles into the dictionary or none |
| 2767 | * at all. It's a little clumsy with the various popen modes |
| 2768 | * and variable number of files involved. |
| 2769 | */ |
| 2770 | if (!_PyPopenProcs) |
| 2771 | { |
| 2772 | _PyPopenProcs = PyDict_New(); |
| 2773 | } |
| 2774 | |
| 2775 | if (_PyPopenProcs) |
| 2776 | { |
| 2777 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 2778 | int ins_rc[3]; |
| 2779 | |
| 2780 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2781 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2782 | |
| 2783 | procObj = PyList_New(2); |
| 2784 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 2785 | intObj = PyInt_FromLong((long) file_count); |
| 2786 | |
| 2787 | if (procObj && pidObj && intObj) |
| 2788 | { |
| 2789 | PyList_SetItem(procObj, 0, pidObj); |
| 2790 | PyList_SetItem(procObj, 1, intObj); |
| 2791 | |
| 2792 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 2793 | if (fileObj[0]) |
| 2794 | { |
| 2795 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2796 | fileObj[0], |
| 2797 | procObj); |
| 2798 | } |
| 2799 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 2800 | if (fileObj[1]) |
| 2801 | { |
| 2802 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2803 | fileObj[1], |
| 2804 | procObj); |
| 2805 | } |
| 2806 | if (file_count >= 3) |
| 2807 | { |
| 2808 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 2809 | if (fileObj[2]) |
| 2810 | { |
| 2811 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2812 | fileObj[2], |
| 2813 | procObj); |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2818 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2819 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 2820 | { |
| 2821 | /* Something failed - remove any dictionary |
| 2822 | * entries that did make it. |
| 2823 | */ |
| 2824 | if (!ins_rc[0] && fileObj[0]) |
| 2825 | { |
| 2826 | PyDict_DelItem(_PyPopenProcs, |
| 2827 | fileObj[0]); |
| 2828 | } |
| 2829 | if (!ins_rc[1] && fileObj[1]) |
| 2830 | { |
| 2831 | PyDict_DelItem(_PyPopenProcs, |
| 2832 | fileObj[1]); |
| 2833 | } |
| 2834 | if (!ins_rc[2] && fileObj[2]) |
| 2835 | { |
| 2836 | PyDict_DelItem(_PyPopenProcs, |
| 2837 | fileObj[2]); |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | /* |
| 2843 | * Clean up our localized references for the dictionary keys |
| 2844 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2845 | * that got placed in the dictionary. |
| 2846 | */ |
| 2847 | Py_XDECREF(procObj); |
| 2848 | Py_XDECREF(fileObj[0]); |
| 2849 | Py_XDECREF(fileObj[1]); |
| 2850 | Py_XDECREF(fileObj[2]); |
| 2851 | } |
| 2852 | |
| 2853 | /* Child is launched. */ |
| 2854 | return f; |
| 2855 | } |
| 2856 | |
| 2857 | /* |
| 2858 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2859 | * exit code for the child process and return as a result of the close. |
| 2860 | * |
| 2861 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2862 | * input file pointer to information about the process that was |
| 2863 | * originally created by the popen* call that created the file pointer. |
| 2864 | * The dictionary uses the file pointer as a key (with one entry |
| 2865 | * inserted for each file returned by the original popen* call) and a |
| 2866 | * single list object as the value for all files from a single call. |
| 2867 | * The list object contains the Win32 process handle at [0], and a file |
| 2868 | * count at [1], which is initialized to the total number of file |
| 2869 | * handles using that list. |
| 2870 | * |
| 2871 | * This function closes whichever handle it is passed, and decrements |
| 2872 | * the file count in the dictionary for the process handle pointed to |
| 2873 | * by this file. On the last close (when the file count reaches zero), |
| 2874 | * this function will wait for the child process and then return its |
| 2875 | * exit code as the result of the close() operation. This permits the |
| 2876 | * files to be closed in any order - it is always the close() of the |
| 2877 | * final handle that will return the exit code. |
| 2878 | */ |
| 2879 | |
| 2880 | /* RED_FLAG 31-Aug-2000 Tim |
| 2881 | * This is always called (today!) between a pair of |
| 2882 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2883 | * macros. So the thread running this has no valid thread state, as |
| 2884 | * far as Python is concerned. However, this calls some Python API |
| 2885 | * functions that cannot be called safely without a valid thread |
| 2886 | * state, in particular PyDict_GetItem. |
| 2887 | * As a temporary hack (although it may last for years ...), we |
| 2888 | * *rely* on not having a valid thread state in this function, in |
| 2889 | * order to create our own "from scratch". |
| 2890 | * This will deadlock if _PyPclose is ever called by a thread |
| 2891 | * holding the global lock. |
| 2892 | * (The OS/2 EMX thread support appears to cover the case where the |
| 2893 | * lock is already held - AIM Apr01) |
| 2894 | */ |
| 2895 | |
| 2896 | static int _PyPclose(FILE *file) |
| 2897 | { |
| 2898 | int result; |
| 2899 | int exit_code; |
| 2900 | int pipe_pid; |
| 2901 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 2902 | int file_count; |
| 2903 | #ifdef WITH_THREAD |
| 2904 | PyInterpreterState* pInterpreterState; |
| 2905 | PyThreadState* pThreadState; |
| 2906 | #endif |
| 2907 | |
| 2908 | /* Close the file handle first, to ensure it can't block the |
| 2909 | * child from exiting if it's the last handle. |
| 2910 | */ |
| 2911 | result = fclose(file); |
| 2912 | |
| 2913 | #ifdef WITH_THREAD |
| 2914 | /* Bootstrap a valid thread state into existence. */ |
| 2915 | pInterpreterState = PyInterpreterState_New(); |
| 2916 | if (!pInterpreterState) { |
| 2917 | /* Well, we're hosed now! We don't have a thread |
| 2918 | * state, so can't call a nice error routine, or raise |
| 2919 | * an exception. Just die. |
| 2920 | */ |
| 2921 | Py_FatalError("unable to allocate interpreter state " |
| 2922 | "when closing popen object."); |
| 2923 | return -1; /* unreachable */ |
| 2924 | } |
| 2925 | pThreadState = PyThreadState_New(pInterpreterState); |
| 2926 | if (!pThreadState) { |
| 2927 | Py_FatalError("unable to allocate thread state " |
| 2928 | "when closing popen object."); |
| 2929 | return -1; /* unreachable */ |
| 2930 | } |
| 2931 | /* Grab the global lock. Note that this will deadlock if the |
| 2932 | * current thread already has the lock! (see RED_FLAG comments |
| 2933 | * before this function) |
| 2934 | */ |
| 2935 | PyEval_RestoreThread(pThreadState); |
| 2936 | #endif |
| 2937 | |
| 2938 | if (_PyPopenProcs) |
| 2939 | { |
| 2940 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2941 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2942 | fileObj)) != NULL && |
| 2943 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 2944 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 2945 | { |
| 2946 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 2947 | file_count = (int) PyInt_AsLong(intObj); |
| 2948 | |
| 2949 | if (file_count > 1) |
| 2950 | { |
| 2951 | /* Still other files referencing process */ |
| 2952 | file_count--; |
| 2953 | PyList_SetItem(procObj,1, |
| 2954 | PyInt_FromLong((long) file_count)); |
| 2955 | } |
| 2956 | else |
| 2957 | { |
| 2958 | /* Last file for this process */ |
| 2959 | if (result != EOF && |
| 2960 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 2961 | { |
| 2962 | /* extract exit status */ |
| 2963 | if (WIFEXITED(exit_code)) |
| 2964 | { |
| 2965 | result = WEXITSTATUS(exit_code); |
| 2966 | } |
| 2967 | else |
| 2968 | { |
| 2969 | errno = EPIPE; |
| 2970 | result = -1; |
| 2971 | } |
| 2972 | } |
| 2973 | else |
| 2974 | { |
| 2975 | /* Indicate failure - this will cause the file object |
| 2976 | * to raise an I/O error and translate the last |
| 2977 | * error code from errno. We do have a problem with |
| 2978 | * last errors that overlap the normal errno table, |
| 2979 | * but that's a consistent problem with the file object. |
| 2980 | */ |
| 2981 | result = -1; |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | /* Remove this file pointer from dictionary */ |
| 2986 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2987 | |
| 2988 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 2989 | { |
| 2990 | Py_DECREF(_PyPopenProcs); |
| 2991 | _PyPopenProcs = NULL; |
| 2992 | } |
| 2993 | |
| 2994 | } /* if object retrieval ok */ |
| 2995 | |
| 2996 | Py_XDECREF(fileObj); |
| 2997 | } /* if _PyPopenProcs */ |
| 2998 | |
| 2999 | #ifdef WITH_THREAD |
| 3000 | /* Tear down the thread & interpreter states. |
| 3001 | * Note that interpreter state clear & delete functions automatically |
| 3002 | * call the thread clear & delete functions, and indeed insist on |
| 3003 | * doing that themselves. The lock must be held during the clear, but |
| 3004 | * need not be held during the delete. |
| 3005 | */ |
| 3006 | PyInterpreterState_Clear(pInterpreterState); |
| 3007 | PyEval_ReleaseThread(pThreadState); |
| 3008 | PyInterpreterState_Delete(pInterpreterState); |
| 3009 | #endif |
| 3010 | |
| 3011 | return result; |
| 3012 | } |
| 3013 | |
| 3014 | #endif /* PYCC_??? */ |
| 3015 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3016 | #elif defined(MS_WIN32) |
| 3017 | |
| 3018 | /* |
| 3019 | * Portable 'popen' replacement for Win32. |
| 3020 | * |
| 3021 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3022 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3023 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3024 | */ |
| 3025 | |
| 3026 | #include <malloc.h> |
| 3027 | #include <io.h> |
| 3028 | #include <fcntl.h> |
| 3029 | |
| 3030 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3031 | #define POPEN_1 1 |
| 3032 | #define POPEN_2 2 |
| 3033 | #define POPEN_3 3 |
| 3034 | #define POPEN_4 4 |
| 3035 | |
| 3036 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3037 | static int _PyPclose(FILE *file); |
| 3038 | |
| 3039 | /* |
| 3040 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3041 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3042 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3043 | */ |
| 3044 | static PyObject *_PyPopenProcs = NULL; |
| 3045 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3046 | |
| 3047 | /* popen that works from a GUI. |
| 3048 | * |
| 3049 | * The result of this function is a pipe (file) connected to the |
| 3050 | * processes stdin or stdout, depending on the requested mode. |
| 3051 | */ |
| 3052 | |
| 3053 | static PyObject * |
| 3054 | posix_popen(PyObject *self, PyObject *args) |
| 3055 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3056 | PyObject *f, *s; |
| 3057 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3058 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3059 | char *cmdstring; |
| 3060 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3061 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3062 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3063 | return NULL; |
| 3064 | |
| 3065 | s = PyTuple_New(0); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3066 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3067 | if (*mode == 'r') |
| 3068 | tm = _O_RDONLY; |
| 3069 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3070 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3071 | return NULL; |
| 3072 | } else |
| 3073 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3074 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3075 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3076 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3077 | return NULL; |
| 3078 | } |
| 3079 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3080 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3081 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3082 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3083 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3084 | else |
| 3085 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3086 | |
| 3087 | return f; |
| 3088 | } |
| 3089 | |
| 3090 | /* Variation on win32pipe.popen |
| 3091 | * |
| 3092 | * The result of this function is a pipe (file) connected to the |
| 3093 | * process's stdin, and a pipe connected to the process's stdout. |
| 3094 | */ |
| 3095 | |
| 3096 | static PyObject * |
| 3097 | win32_popen2(PyObject *self, PyObject *args) |
| 3098 | { |
| 3099 | PyObject *f; |
| 3100 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3101 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3102 | char *cmdstring; |
| 3103 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3104 | int bufsize = -1; |
| 3105 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3106 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3107 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3108 | if (*mode == 't') |
| 3109 | tm = _O_TEXT; |
| 3110 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3111 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3112 | return NULL; |
| 3113 | } else |
| 3114 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3115 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3116 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3117 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3118 | return NULL; |
| 3119 | } |
| 3120 | |
| 3121 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3122 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3123 | return f; |
| 3124 | } |
| 3125 | |
| 3126 | /* |
| 3127 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3128 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3129 | * The result of this function is 3 pipes - the process's stdin, |
| 3130 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3131 | */ |
| 3132 | |
| 3133 | static PyObject * |
| 3134 | win32_popen3(PyObject *self, PyObject *args) |
| 3135 | { |
| 3136 | PyObject *f; |
| 3137 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3138 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3139 | char *cmdstring; |
| 3140 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3141 | int bufsize = -1; |
| 3142 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3143 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3144 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3145 | if (*mode == 't') |
| 3146 | tm = _O_TEXT; |
| 3147 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3148 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3149 | return NULL; |
| 3150 | } else |
| 3151 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3152 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3153 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3154 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | } |
| 3157 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3158 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3159 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3160 | return f; |
| 3161 | } |
| 3162 | |
| 3163 | /* |
| 3164 | * Variation on win32pipe.popen |
| 3165 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3166 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3167 | * and stdout+stderr combined as a single pipe. |
| 3168 | */ |
| 3169 | |
| 3170 | static PyObject * |
| 3171 | win32_popen4(PyObject *self, PyObject *args) |
| 3172 | { |
| 3173 | PyObject *f; |
| 3174 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3175 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3176 | char *cmdstring; |
| 3177 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3178 | int bufsize = -1; |
| 3179 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3180 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3181 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3182 | if (*mode == 't') |
| 3183 | tm = _O_TEXT; |
| 3184 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3185 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3186 | return NULL; |
| 3187 | } else |
| 3188 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3189 | |
| 3190 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3191 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3192 | return NULL; |
| 3193 | } |
| 3194 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3195 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3196 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3197 | return f; |
| 3198 | } |
| 3199 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3200 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3201 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3202 | HANDLE hStdin, |
| 3203 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3204 | HANDLE hStderr, |
| 3205 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3206 | { |
| 3207 | PROCESS_INFORMATION piProcInfo; |
| 3208 | STARTUPINFO siStartInfo; |
| 3209 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3210 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3211 | int i; |
| 3212 | int x; |
| 3213 | |
| 3214 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3215 | char *comshell; |
| 3216 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3217 | s1 = (char *)_alloca(i); |
| 3218 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3219 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3220 | |
| 3221 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3222 | * then use the w9xpopen hack. |
| 3223 | */ |
| 3224 | comshell = s1 + x; |
| 3225 | while (comshell >= s1 && *comshell != '\\') |
| 3226 | --comshell; |
| 3227 | ++comshell; |
| 3228 | |
| 3229 | if (GetVersion() < 0x80000000 && |
| 3230 | _stricmp(comshell, "command.com") != 0) { |
| 3231 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3232 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 3233 | s2 = (char *)_alloca(x); |
| 3234 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3235 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3236 | } |
| 3237 | else { |
| 3238 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3239 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3240 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3241 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3242 | char modulepath[_MAX_PATH]; |
| 3243 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3244 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3245 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3246 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3247 | x = i+1; |
| 3248 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3249 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3250 | strncat(modulepath, |
| 3251 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3252 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3253 | -strlen(modulepath)); |
| 3254 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3255 | /* Eeek - file-not-found - possibly an embedding |
| 3256 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3257 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3258 | strncpy(modulepath, |
| 3259 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3260 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3261 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3262 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3263 | strncat(modulepath, |
| 3264 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3265 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3266 | -strlen(modulepath)); |
| 3267 | /* No where else to look - raise an easily identifiable |
| 3268 | error, rather than leaving Windows to report |
| 3269 | "file not found" - as the user is probably blissfully |
| 3270 | unaware this shim EXE is used, and it will confuse them. |
| 3271 | (well, it confused me for a while ;-) |
| 3272 | */ |
| 3273 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3274 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3275 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3276 | "for popen to work with your shell " |
| 3277 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3278 | szConsoleSpawn); |
| 3279 | return FALSE; |
| 3280 | } |
| 3281 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3282 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3283 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3284 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3285 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3286 | s2 = (char *)_alloca(x); |
| 3287 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3288 | /* To maintain correct argument passing semantics, |
| 3289 | we pass the command-line as it stands, and allow |
| 3290 | quoting to be applied. w9xpopen.exe will then |
| 3291 | use its argv vector, and re-quote the necessary |
| 3292 | args for the ultimate child process. |
| 3293 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3294 | PyOS_snprintf( |
| 3295 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3296 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3297 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3298 | s1, |
| 3299 | s3, |
| 3300 | cmdstring); |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3305 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3306 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3307 | PyErr_SetString(PyExc_RuntimeError, |
| 3308 | "Cannot locate a COMSPEC environment variable to " |
| 3309 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3310 | return FALSE; |
| 3311 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3312 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3313 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3314 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3315 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3316 | siStartInfo.hStdInput = hStdin; |
| 3317 | siStartInfo.hStdOutput = hStdout; |
| 3318 | siStartInfo.hStdError = hStderr; |
| 3319 | siStartInfo.wShowWindow = SW_HIDE; |
| 3320 | |
| 3321 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3322 | s2, |
| 3323 | NULL, |
| 3324 | NULL, |
| 3325 | TRUE, |
| 3326 | CREATE_NEW_CONSOLE, |
| 3327 | NULL, |
| 3328 | NULL, |
| 3329 | &siStartInfo, |
| 3330 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3331 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3332 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3333 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3334 | /* Return process handle */ |
| 3335 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3336 | return TRUE; |
| 3337 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3338 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3339 | return FALSE; |
| 3340 | } |
| 3341 | |
| 3342 | /* The following code is based off of KB: Q190351 */ |
| 3343 | |
| 3344 | static PyObject * |
| 3345 | _PyPopen(char *cmdstring, int mode, int n) |
| 3346 | { |
| 3347 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3348 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3349 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3350 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3351 | SECURITY_ATTRIBUTES saAttr; |
| 3352 | BOOL fSuccess; |
| 3353 | int fd1, fd2, fd3; |
| 3354 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3355 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3356 | PyObject *f; |
| 3357 | |
| 3358 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3359 | saAttr.bInheritHandle = TRUE; |
| 3360 | saAttr.lpSecurityDescriptor = NULL; |
| 3361 | |
| 3362 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 3363 | return win32_error("CreatePipe", NULL); |
| 3364 | |
| 3365 | /* Create new output read handle and the input write handle. Set |
| 3366 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 3367 | * the these handles; resulting in non-closeable handles to the pipes |
| 3368 | * being created. */ |
| 3369 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3370 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 3371 | FALSE, |
| 3372 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3373 | if (!fSuccess) |
| 3374 | return win32_error("DuplicateHandle", NULL); |
| 3375 | |
| 3376 | /* Close the inheritable version of ChildStdin |
| 3377 | that we're using. */ |
| 3378 | CloseHandle(hChildStdinWr); |
| 3379 | |
| 3380 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 3381 | return win32_error("CreatePipe", NULL); |
| 3382 | |
| 3383 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3384 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 3385 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3386 | if (!fSuccess) |
| 3387 | return win32_error("DuplicateHandle", NULL); |
| 3388 | |
| 3389 | /* Close the inheritable version of ChildStdout |
| 3390 | that we're using. */ |
| 3391 | CloseHandle(hChildStdoutRd); |
| 3392 | |
| 3393 | if (n != POPEN_4) { |
| 3394 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 3395 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3396 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 3397 | hChildStderrRd, |
| 3398 | GetCurrentProcess(), |
| 3399 | &hChildStderrRdDup, 0, |
| 3400 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3401 | if (!fSuccess) |
| 3402 | return win32_error("DuplicateHandle", NULL); |
| 3403 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 3404 | CloseHandle(hChildStderrRd); |
| 3405 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3406 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3407 | switch (n) { |
| 3408 | case POPEN_1: |
| 3409 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 3410 | case _O_WRONLY | _O_TEXT: |
| 3411 | /* Case for writing to child Stdin in text mode. */ |
| 3412 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3413 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3414 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3415 | PyFile_SetBufSize(f, 0); |
| 3416 | /* We don't care about these pipes anymore, so close them. */ |
| 3417 | CloseHandle(hChildStdoutRdDup); |
| 3418 | CloseHandle(hChildStderrRdDup); |
| 3419 | break; |
| 3420 | |
| 3421 | case _O_RDONLY | _O_TEXT: |
| 3422 | /* Case for reading from child Stdout in text mode. */ |
| 3423 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3424 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3425 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3426 | PyFile_SetBufSize(f, 0); |
| 3427 | /* We don't care about these pipes anymore, so close them. */ |
| 3428 | CloseHandle(hChildStdinWrDup); |
| 3429 | CloseHandle(hChildStderrRdDup); |
| 3430 | break; |
| 3431 | |
| 3432 | case _O_RDONLY | _O_BINARY: |
| 3433 | /* Case for readinig from child Stdout in binary mode. */ |
| 3434 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3435 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3436 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3437 | PyFile_SetBufSize(f, 0); |
| 3438 | /* We don't care about these pipes anymore, so close them. */ |
| 3439 | CloseHandle(hChildStdinWrDup); |
| 3440 | CloseHandle(hChildStderrRdDup); |
| 3441 | break; |
| 3442 | |
| 3443 | case _O_WRONLY | _O_BINARY: |
| 3444 | /* Case for writing to child Stdin in binary mode. */ |
| 3445 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3446 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3447 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3448 | PyFile_SetBufSize(f, 0); |
| 3449 | /* We don't care about these pipes anymore, so close them. */ |
| 3450 | CloseHandle(hChildStdoutRdDup); |
| 3451 | CloseHandle(hChildStderrRdDup); |
| 3452 | break; |
| 3453 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3454 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3455 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3456 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3457 | case POPEN_2: |
| 3458 | case POPEN_4: |
| 3459 | { |
| 3460 | char *m1, *m2; |
| 3461 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3462 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3463 | if (mode && _O_TEXT) { |
| 3464 | m1 = "r"; |
| 3465 | m2 = "w"; |
| 3466 | } else { |
| 3467 | m1 = "rb"; |
| 3468 | m2 = "wb"; |
| 3469 | } |
| 3470 | |
| 3471 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3472 | f1 = _fdopen(fd1, m2); |
| 3473 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3474 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3475 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3476 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3477 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3478 | PyFile_SetBufSize(p2, 0); |
| 3479 | |
| 3480 | if (n != 4) |
| 3481 | CloseHandle(hChildStderrRdDup); |
| 3482 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3483 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3484 | Py_XDECREF(p1); |
| 3485 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3486 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3487 | break; |
| 3488 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3489 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3490 | case POPEN_3: |
| 3491 | { |
| 3492 | char *m1, *m2; |
| 3493 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3494 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3495 | if (mode && _O_TEXT) { |
| 3496 | m1 = "r"; |
| 3497 | m2 = "w"; |
| 3498 | } else { |
| 3499 | m1 = "rb"; |
| 3500 | m2 = "wb"; |
| 3501 | } |
| 3502 | |
| 3503 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3504 | f1 = _fdopen(fd1, m2); |
| 3505 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3506 | f2 = _fdopen(fd2, m1); |
| 3507 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 3508 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3509 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3510 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 3511 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3512 | PyFile_SetBufSize(p1, 0); |
| 3513 | PyFile_SetBufSize(p2, 0); |
| 3514 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3515 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3516 | Py_XDECREF(p1); |
| 3517 | Py_XDECREF(p2); |
| 3518 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3519 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | if (n == POPEN_4) { |
| 3525 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3526 | hChildStdinRd, |
| 3527 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3528 | hChildStdoutWr, |
| 3529 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3530 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3531 | } |
| 3532 | else { |
| 3533 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3534 | hChildStdinRd, |
| 3535 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3536 | hChildStderrWr, |
| 3537 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3538 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3541 | /* |
| 3542 | * Insert the files we've created into the process dictionary |
| 3543 | * all referencing the list with the process handle and the |
| 3544 | * initial number of files (see description below in _PyPclose). |
| 3545 | * Since if _PyPclose later tried to wait on a process when all |
| 3546 | * handles weren't closed, it could create a deadlock with the |
| 3547 | * child, we spend some energy here to try to ensure that we |
| 3548 | * either insert all file handles into the dictionary or none |
| 3549 | * at all. It's a little clumsy with the various popen modes |
| 3550 | * and variable number of files involved. |
| 3551 | */ |
| 3552 | if (!_PyPopenProcs) { |
| 3553 | _PyPopenProcs = PyDict_New(); |
| 3554 | } |
| 3555 | |
| 3556 | if (_PyPopenProcs) { |
| 3557 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 3558 | int ins_rc[3]; |
| 3559 | |
| 3560 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3561 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3562 | |
| 3563 | procObj = PyList_New(2); |
| 3564 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 3565 | intObj = PyInt_FromLong(file_count); |
| 3566 | |
| 3567 | if (procObj && hProcessObj && intObj) { |
| 3568 | PyList_SetItem(procObj,0,hProcessObj); |
| 3569 | PyList_SetItem(procObj,1,intObj); |
| 3570 | |
| 3571 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 3572 | if (fileObj[0]) { |
| 3573 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3574 | fileObj[0], |
| 3575 | procObj); |
| 3576 | } |
| 3577 | if (file_count >= 2) { |
| 3578 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 3579 | if (fileObj[1]) { |
| 3580 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3581 | fileObj[1], |
| 3582 | procObj); |
| 3583 | } |
| 3584 | } |
| 3585 | if (file_count >= 3) { |
| 3586 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 3587 | if (fileObj[2]) { |
| 3588 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3589 | fileObj[2], |
| 3590 | procObj); |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3595 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3596 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 3597 | /* Something failed - remove any dictionary |
| 3598 | * entries that did make it. |
| 3599 | */ |
| 3600 | if (!ins_rc[0] && fileObj[0]) { |
| 3601 | PyDict_DelItem(_PyPopenProcs, |
| 3602 | fileObj[0]); |
| 3603 | } |
| 3604 | if (!ins_rc[1] && fileObj[1]) { |
| 3605 | PyDict_DelItem(_PyPopenProcs, |
| 3606 | fileObj[1]); |
| 3607 | } |
| 3608 | if (!ins_rc[2] && fileObj[2]) { |
| 3609 | PyDict_DelItem(_PyPopenProcs, |
| 3610 | fileObj[2]); |
| 3611 | } |
| 3612 | } |
| 3613 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3614 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3615 | /* |
| 3616 | * Clean up our localized references for the dictionary keys |
| 3617 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3618 | * that got placed in the dictionary. |
| 3619 | */ |
| 3620 | Py_XDECREF(procObj); |
| 3621 | Py_XDECREF(fileObj[0]); |
| 3622 | Py_XDECREF(fileObj[1]); |
| 3623 | Py_XDECREF(fileObj[2]); |
| 3624 | } |
| 3625 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3626 | /* Child is launched. Close the parents copy of those pipe |
| 3627 | * handles that only the child should have open. You need to |
| 3628 | * make sure that no handles to the write end of the output pipe |
| 3629 | * are maintained in this process or else the pipe will not close |
| 3630 | * when the child process exits and the ReadFile will hang. */ |
| 3631 | |
| 3632 | if (!CloseHandle(hChildStdinRd)) |
| 3633 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3634 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3635 | if (!CloseHandle(hChildStdoutWr)) |
| 3636 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3637 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3638 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 3639 | return win32_error("CloseHandle", NULL); |
| 3640 | |
| 3641 | return f; |
| 3642 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* |
| 3645 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3646 | * 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] | 3647 | * |
| 3648 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3649 | * input file pointer to information about the process that was |
| 3650 | * originally created by the popen* call that created the file pointer. |
| 3651 | * The dictionary uses the file pointer as a key (with one entry |
| 3652 | * inserted for each file returned by the original popen* call) and a |
| 3653 | * single list object as the value for all files from a single call. |
| 3654 | * The list object contains the Win32 process handle at [0], and a file |
| 3655 | * count at [1], which is initialized to the total number of file |
| 3656 | * handles using that list. |
| 3657 | * |
| 3658 | * This function closes whichever handle it is passed, and decrements |
| 3659 | * the file count in the dictionary for the process handle pointed to |
| 3660 | * by this file. On the last close (when the file count reaches zero), |
| 3661 | * this function will wait for the child process and then return its |
| 3662 | * exit code as the result of the close() operation. This permits the |
| 3663 | * files to be closed in any order - it is always the close() of the |
| 3664 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3665 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* RED_FLAG 31-Aug-2000 Tim |
| 3668 | * This is always called (today!) between a pair of |
| 3669 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3670 | * macros. So the thread running this has no valid thread state, as |
| 3671 | * far as Python is concerned. However, this calls some Python API |
| 3672 | * functions that cannot be called safely without a valid thread |
| 3673 | * state, in particular PyDict_GetItem. |
| 3674 | * As a temporary hack (although it may last for years ...), we |
| 3675 | * *rely* on not having a valid thread state in this function, in |
| 3676 | * order to create our own "from scratch". |
| 3677 | * This will deadlock if _PyPclose is ever called by a thread |
| 3678 | * holding the global lock. |
| 3679 | */ |
| 3680 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3681 | static int _PyPclose(FILE *file) |
| 3682 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3683 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3684 | DWORD exit_code; |
| 3685 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3686 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 3687 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3688 | #ifdef WITH_THREAD |
| 3689 | PyInterpreterState* pInterpreterState; |
| 3690 | PyThreadState* pThreadState; |
| 3691 | #endif |
| 3692 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3693 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3694 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3695 | */ |
| 3696 | result = fclose(file); |
| 3697 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3698 | #ifdef WITH_THREAD |
| 3699 | /* Bootstrap a valid thread state into existence. */ |
| 3700 | pInterpreterState = PyInterpreterState_New(); |
| 3701 | if (!pInterpreterState) { |
| 3702 | /* Well, we're hosed now! We don't have a thread |
| 3703 | * state, so can't call a nice error routine, or raise |
| 3704 | * an exception. Just die. |
| 3705 | */ |
| 3706 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3707 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3708 | return -1; /* unreachable */ |
| 3709 | } |
| 3710 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3711 | if (!pThreadState) { |
| 3712 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3713 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3714 | return -1; /* unreachable */ |
| 3715 | } |
| 3716 | /* Grab the global lock. Note that this will deadlock if the |
| 3717 | * current thread already has the lock! (see RED_FLAG comments |
| 3718 | * before this function) |
| 3719 | */ |
| 3720 | PyEval_RestoreThread(pThreadState); |
| 3721 | #endif |
| 3722 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3723 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3724 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3725 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3726 | fileObj)) != NULL && |
| 3727 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 3728 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 3729 | |
| 3730 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 3731 | file_count = PyInt_AsLong(intObj); |
| 3732 | |
| 3733 | if (file_count > 1) { |
| 3734 | /* Still other files referencing process */ |
| 3735 | file_count--; |
| 3736 | PyList_SetItem(procObj,1, |
| 3737 | PyInt_FromLong(file_count)); |
| 3738 | } else { |
| 3739 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3740 | if (result != EOF && |
| 3741 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 3742 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3743 | /* Possible truncation here in 16-bit environments, but |
| 3744 | * real exit codes are just the lower byte in any event. |
| 3745 | */ |
| 3746 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3747 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3748 | /* Indicate failure - this will cause the file object |
| 3749 | * to raise an I/O error and translate the last Win32 |
| 3750 | * error code from errno. We do have a problem with |
| 3751 | * last errors that overlap the normal errno table, |
| 3752 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3753 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3754 | if (result != EOF) { |
| 3755 | /* If the error wasn't from the fclose(), then |
| 3756 | * set errno for the file object error handling. |
| 3757 | */ |
| 3758 | errno = GetLastError(); |
| 3759 | } |
| 3760 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | /* Free up the native handle at this point */ |
| 3764 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3765 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3766 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3767 | /* Remove this file pointer from dictionary */ |
| 3768 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3769 | |
| 3770 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 3771 | Py_DECREF(_PyPopenProcs); |
| 3772 | _PyPopenProcs = NULL; |
| 3773 | } |
| 3774 | |
| 3775 | } /* if object retrieval ok */ |
| 3776 | |
| 3777 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3778 | } /* if _PyPopenProcs */ |
| 3779 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3780 | #ifdef WITH_THREAD |
| 3781 | /* Tear down the thread & interpreter states. |
| 3782 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3783 | * call the thread clear & delete functions, and indeed insist on |
| 3784 | * doing that themselves. The lock must be held during the clear, but |
| 3785 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3786 | */ |
| 3787 | PyInterpreterState_Clear(pInterpreterState); |
| 3788 | PyEval_ReleaseThread(pThreadState); |
| 3789 | PyInterpreterState_Delete(pInterpreterState); |
| 3790 | #endif |
| 3791 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3792 | return result; |
| 3793 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3794 | |
| 3795 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3796 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3797 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3798 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3799 | char *name; |
| 3800 | char *mode = "r"; |
| 3801 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3802 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3803 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3804 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3805 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3806 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3807 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3808 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3809 | if (fp == NULL) |
| 3810 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3811 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3812 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3813 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3814 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3815 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3816 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3817 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3818 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3819 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3820 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3821 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3822 | static char posix_setuid__doc__[] = |
| 3823 | "setuid(uid) -> None\n\ |
| 3824 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3825 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3826 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3827 | { |
| 3828 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3829 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3830 | return NULL; |
| 3831 | if (setuid(uid) < 0) |
| 3832 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3833 | Py_INCREF(Py_None); |
| 3834 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3835 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3836 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3838 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3839 | #ifdef HAVE_SETEUID |
| 3840 | static char posix_seteuid__doc__[] = |
| 3841 | "seteuid(uid) -> None\n\ |
| 3842 | Set the current process's effective user id."; |
| 3843 | static PyObject * |
| 3844 | posix_seteuid (PyObject *self, PyObject *args) |
| 3845 | { |
| 3846 | int euid; |
| 3847 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3848 | return NULL; |
| 3849 | } else if (seteuid(euid) < 0) { |
| 3850 | return posix_error(); |
| 3851 | } else { |
| 3852 | Py_INCREF(Py_None); |
| 3853 | return Py_None; |
| 3854 | } |
| 3855 | } |
| 3856 | #endif /* HAVE_SETEUID */ |
| 3857 | |
| 3858 | #ifdef HAVE_SETEGID |
| 3859 | static char posix_setegid__doc__[] = |
| 3860 | "setegid(gid) -> None\n\ |
| 3861 | Set the current process's effective group id."; |
| 3862 | static PyObject * |
| 3863 | posix_setegid (PyObject *self, PyObject *args) |
| 3864 | { |
| 3865 | int egid; |
| 3866 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3867 | return NULL; |
| 3868 | } else if (setegid(egid) < 0) { |
| 3869 | return posix_error(); |
| 3870 | } else { |
| 3871 | Py_INCREF(Py_None); |
| 3872 | return Py_None; |
| 3873 | } |
| 3874 | } |
| 3875 | #endif /* HAVE_SETEGID */ |
| 3876 | |
| 3877 | #ifdef HAVE_SETREUID |
| 3878 | static char posix_setreuid__doc__[] = |
| 3879 | "seteuid(ruid, euid) -> None\n\ |
| 3880 | Set the current process's real and effective user ids."; |
| 3881 | static PyObject * |
| 3882 | posix_setreuid (PyObject *self, PyObject *args) |
| 3883 | { |
| 3884 | int ruid, euid; |
| 3885 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3886 | return NULL; |
| 3887 | } else if (setreuid(ruid, euid) < 0) { |
| 3888 | return posix_error(); |
| 3889 | } else { |
| 3890 | Py_INCREF(Py_None); |
| 3891 | return Py_None; |
| 3892 | } |
| 3893 | } |
| 3894 | #endif /* HAVE_SETREUID */ |
| 3895 | |
| 3896 | #ifdef HAVE_SETREGID |
| 3897 | static char posix_setregid__doc__[] = |
| 3898 | "setegid(rgid, egid) -> None\n\ |
| 3899 | Set the current process's real and effective group ids."; |
| 3900 | static PyObject * |
| 3901 | posix_setregid (PyObject *self, PyObject *args) |
| 3902 | { |
| 3903 | int rgid, egid; |
| 3904 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3905 | return NULL; |
| 3906 | } else if (setregid(rgid, egid) < 0) { |
| 3907 | return posix_error(); |
| 3908 | } else { |
| 3909 | Py_INCREF(Py_None); |
| 3910 | return Py_None; |
| 3911 | } |
| 3912 | } |
| 3913 | #endif /* HAVE_SETREGID */ |
| 3914 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3915 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3916 | static char posix_setgid__doc__[] = |
| 3917 | "setgid(gid) -> None\n\ |
| 3918 | Set the current process's group id."; |
| 3919 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3920 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3921 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3922 | { |
| 3923 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3924 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3925 | return NULL; |
| 3926 | if (setgid(gid) < 0) |
| 3927 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3928 | Py_INCREF(Py_None); |
| 3929 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3930 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3931 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3932 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3933 | #ifdef HAVE_SETGROUPS |
| 3934 | static char posix_setgroups__doc__[] = |
| 3935 | "setgroups(list) -> None\n\ |
| 3936 | Set the groups of the current process to list."; |
| 3937 | |
| 3938 | static PyObject * |
| 3939 | posix_setgroups(PyObject *self, PyObject *args) |
| 3940 | { |
| 3941 | PyObject *groups; |
| 3942 | int i, len; |
| 3943 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3944 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3945 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 3946 | return NULL; |
| 3947 | if (!PySequence_Check(groups)) { |
| 3948 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 3949 | return NULL; |
| 3950 | } |
| 3951 | len = PySequence_Size(groups); |
| 3952 | if (len > MAX_GROUPS) { |
| 3953 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 3954 | return NULL; |
| 3955 | } |
| 3956 | for(i = 0; i < len; i++) { |
| 3957 | PyObject *elem; |
| 3958 | elem = PySequence_GetItem(groups, i); |
| 3959 | if (!elem) |
| 3960 | return NULL; |
| 3961 | if (!PyInt_Check(elem)) { |
| 3962 | PyErr_SetString(PyExc_TypeError, |
| 3963 | "groups must be integers"); |
| 3964 | Py_DECREF(elem); |
| 3965 | return NULL; |
| 3966 | } |
| 3967 | /* XXX: check that value fits into gid_t. */ |
| 3968 | grouplist[i] = PyInt_AsLong(elem); |
| 3969 | Py_DECREF(elem); |
| 3970 | } |
| 3971 | |
| 3972 | if (setgroups(len, grouplist) < 0) |
| 3973 | return posix_error(); |
| 3974 | Py_INCREF(Py_None); |
| 3975 | return Py_None; |
| 3976 | } |
| 3977 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3978 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3979 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3980 | static char posix_waitpid__doc__[] = |
| 3981 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3982 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3983 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3984 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3985 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3986 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3987 | int pid, options; |
| 3988 | #ifdef UNION_WAIT |
| 3989 | union wait status; |
| 3990 | #define status_i (status.w_status) |
| 3991 | #else |
| 3992 | int status; |
| 3993 | #define status_i status |
| 3994 | #endif |
| 3995 | status_i = 0; |
| 3996 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3997 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3998 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3999 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4000 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4001 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4002 | if (pid == -1) |
| 4003 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4004 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4005 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4008 | #elif defined(HAVE_CWAIT) |
| 4009 | |
| 4010 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
| 4011 | static char posix_waitpid__doc__[] = |
| 4012 | "waitpid(pid, options) -> (pid, status << 8)\n" |
| 4013 | "Wait for completion of a given process. options is ignored on Windows."; |
| 4014 | |
| 4015 | static PyObject * |
| 4016 | posix_waitpid(PyObject *self, PyObject *args) |
| 4017 | { |
| 4018 | int pid, options; |
| 4019 | int status; |
| 4020 | |
| 4021 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4022 | return NULL; |
| 4023 | Py_BEGIN_ALLOW_THREADS |
| 4024 | pid = _cwait(&status, pid, options); |
| 4025 | Py_END_ALLOW_THREADS |
| 4026 | if (pid == -1) |
| 4027 | return posix_error(); |
| 4028 | else |
| 4029 | /* shift the status left a byte so this is more like the |
| 4030 | POSIX waitpid */ |
| 4031 | return Py_BuildValue("ii", pid, status << 8); |
| 4032 | } |
| 4033 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4034 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4035 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4036 | static char posix_wait__doc__[] = |
| 4037 | "wait() -> (pid, status)\n\ |
| 4038 | Wait for completion of a child process."; |
| 4039 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4040 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4041 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4042 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4043 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4044 | #ifdef UNION_WAIT |
| 4045 | union wait status; |
| 4046 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4047 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4048 | int status; |
| 4049 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4050 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4051 | if (!PyArg_ParseTuple(args, ":wait")) |
| 4052 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4053 | status_i = 0; |
| 4054 | Py_BEGIN_ALLOW_THREADS |
| 4055 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4056 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4057 | if (pid == -1) |
| 4058 | return posix_error(); |
| 4059 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4060 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4061 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4062 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4063 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4064 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4065 | |
| 4066 | static char posix_lstat__doc__[] = |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 4067 | "lstat(path) -> (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid,\n\ |
| 4068 | st_size, st_atime, st_mtime, st_ctime)\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4069 | Like stat(path), but do not follow symbolic links."; |
| 4070 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4071 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4072 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4073 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4074 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4075 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4076 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4077 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4078 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4081 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4082 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4083 | static char posix_readlink__doc__[] = |
| 4084 | "readlink(path) -> path\n\ |
| 4085 | Return a string representing the path to which the symbolic link points."; |
| 4086 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4087 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4088 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4089 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4090 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4091 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4092 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4093 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4094 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4095 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4096 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4097 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4098 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4099 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4100 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4101 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4102 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4104 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4105 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4106 | static char posix_symlink__doc__[] = |
| 4107 | "symlink(src, dst) -> None\n\ |
| 4108 | Create a symbolic link."; |
| 4109 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4111 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4112 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4113 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4114 | } |
| 4115 | #endif /* HAVE_SYMLINK */ |
| 4116 | |
| 4117 | |
| 4118 | #ifdef HAVE_TIMES |
| 4119 | #ifndef HZ |
| 4120 | #define HZ 60 /* Universal constant :-) */ |
| 4121 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4122 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4123 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4124 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4125 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4126 | { |
| 4127 | ULONG value = 0; |
| 4128 | |
| 4129 | Py_BEGIN_ALLOW_THREADS |
| 4130 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4131 | Py_END_ALLOW_THREADS |
| 4132 | |
| 4133 | return value; |
| 4134 | } |
| 4135 | |
| 4136 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4137 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4138 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4139 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4140 | return NULL; |
| 4141 | |
| 4142 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4143 | return Py_BuildValue("ddddd", |
| 4144 | (double)0 /* t.tms_utime / HZ */, |
| 4145 | (double)0 /* t.tms_stime / HZ */, |
| 4146 | (double)0 /* t.tms_cutime / HZ */, |
| 4147 | (double)0 /* t.tms_cstime / HZ */, |
| 4148 | (double)system_uptime() / 1000); |
| 4149 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4150 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4151 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4152 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4153 | { |
| 4154 | struct tms t; |
| 4155 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4156 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4157 | return NULL; |
| 4158 | errno = 0; |
| 4159 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4160 | if (c == (clock_t) -1) |
| 4161 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4162 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4163 | (double)t.tms_utime / HZ, |
| 4164 | (double)t.tms_stime / HZ, |
| 4165 | (double)t.tms_cutime / HZ, |
| 4166 | (double)t.tms_cstime / HZ, |
| 4167 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4168 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4169 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4170 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4171 | |
| 4172 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 4173 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4174 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4175 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4176 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4177 | { |
| 4178 | FILETIME create, exit, kernel, user; |
| 4179 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4180 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4181 | return NULL; |
| 4182 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4183 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4184 | /* The fields of a FILETIME structure are the hi and lo part |
| 4185 | of a 64-bit value expressed in 100 nanosecond units. |
| 4186 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4187 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4188 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4189 | return Py_BuildValue( |
| 4190 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4191 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4192 | kernel.dwLowDateTime*1e-7), |
| 4193 | (double)(user.dwHighDateTime*429.4967296 + |
| 4194 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4195 | (double)0, |
| 4196 | (double)0, |
| 4197 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4198 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4199 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4200 | |
| 4201 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 4202 | static char posix_times__doc__[] = |
| 4203 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 4204 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4205 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4207 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4208 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4209 | static char posix_setsid__doc__[] = |
| 4210 | "setsid() -> None\n\ |
| 4211 | Call the system call setsid()."; |
| 4212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4214 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4215 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4216 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4217 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4218 | if (setsid() < 0) |
| 4219 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4220 | Py_INCREF(Py_None); |
| 4221 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4222 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4223 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4224 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4225 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4226 | static char posix_setpgid__doc__[] = |
| 4227 | "setpgid(pid, pgrp) -> None\n\ |
| 4228 | Call the system call setpgid()."; |
| 4229 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4230 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4231 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4232 | { |
| 4233 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4234 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4235 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4236 | if (setpgid(pid, pgrp) < 0) |
| 4237 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4238 | Py_INCREF(Py_None); |
| 4239 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4240 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4241 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4242 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4244 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4245 | static char posix_tcgetpgrp__doc__[] = |
| 4246 | "tcgetpgrp(fd) -> pgid\n\ |
| 4247 | Return the process group associated with the terminal given by a fd."; |
| 4248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4250 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4251 | { |
| 4252 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4253 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4254 | return NULL; |
| 4255 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4256 | if (pgid < 0) |
| 4257 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4258 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4259 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4260 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4261 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4262 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4263 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4264 | static char posix_tcsetpgrp__doc__[] = |
| 4265 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 4266 | Set the process group associated with the terminal given by a fd."; |
| 4267 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4268 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4269 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4270 | { |
| 4271 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4272 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4273 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4274 | if (tcsetpgrp(fd, pgid) < 0) |
| 4275 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4276 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4277 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4278 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4279 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4280 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4281 | /* Functions acting on file descriptors */ |
| 4282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4283 | static char posix_open__doc__[] = |
| 4284 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 4285 | Open a file (for low level IO)."; |
| 4286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4287 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4288 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4289 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4290 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4291 | int flag; |
| 4292 | int mode = 0777; |
| 4293 | int fd; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4294 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4295 | Py_FileSystemDefaultEncoding, &file, |
| 4296 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4297 | return NULL; |
| 4298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4299 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4300 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4301 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4302 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4303 | return posix_error_with_allocated_filename(file); |
| 4304 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4305 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4308 | |
| 4309 | static char posix_close__doc__[] = |
| 4310 | "close(fd) -> None\n\ |
| 4311 | Close a file descriptor (for low level IO)."; |
| 4312 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4313 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4314 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4315 | { |
| 4316 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4317 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4318 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4319 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4320 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4321 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4322 | if (res < 0) |
| 4323 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4324 | Py_INCREF(Py_None); |
| 4325 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4328 | |
| 4329 | static char posix_dup__doc__[] = |
| 4330 | "dup(fd) -> fd2\n\ |
| 4331 | Return a duplicate of a file descriptor."; |
| 4332 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4333 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4334 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4335 | { |
| 4336 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4337 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4338 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4339 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4340 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4341 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4342 | if (fd < 0) |
| 4343 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4344 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4347 | |
| 4348 | static char posix_dup2__doc__[] = |
| 4349 | "dup2(fd, fd2) -> None\n\ |
| 4350 | Duplicate file descriptor."; |
| 4351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4352 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4353 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4354 | { |
| 4355 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4356 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4357 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4358 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4359 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4360 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4361 | if (res < 0) |
| 4362 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4363 | Py_INCREF(Py_None); |
| 4364 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4367 | |
| 4368 | static char posix_lseek__doc__[] = |
| 4369 | "lseek(fd, pos, how) -> newpos\n\ |
| 4370 | Set the current position of a file descriptor."; |
| 4371 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4373 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4374 | { |
| 4375 | int fd, how; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 4376 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4377 | LONG_LONG pos, res; |
| 4378 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4379 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4380 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4381 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4382 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4383 | return NULL; |
| 4384 | #ifdef SEEK_SET |
| 4385 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4386 | switch (how) { |
| 4387 | case 0: how = SEEK_SET; break; |
| 4388 | case 1: how = SEEK_CUR; break; |
| 4389 | case 2: how = SEEK_END; break; |
| 4390 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4391 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4392 | |
| 4393 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4394 | pos = PyInt_AsLong(posobj); |
| 4395 | #else |
| 4396 | pos = PyLong_Check(posobj) ? |
| 4397 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4398 | #endif |
| 4399 | if (PyErr_Occurred()) |
| 4400 | return NULL; |
| 4401 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4402 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 4403 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4404 | res = _lseeki64(fd, pos, how); |
| 4405 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4406 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4407 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4408 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4409 | if (res < 0) |
| 4410 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4411 | |
| 4412 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4413 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4414 | #else |
| 4415 | return PyLong_FromLongLong(res); |
| 4416 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4419 | |
| 4420 | static char posix_read__doc__[] = |
| 4421 | "read(fd, buffersize) -> string\n\ |
| 4422 | Read a file descriptor."; |
| 4423 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4424 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4425 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4426 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4427 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4428 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4429 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4430 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4431 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4432 | if (buffer == NULL) |
| 4433 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4434 | Py_BEGIN_ALLOW_THREADS |
| 4435 | n = read(fd, PyString_AsString(buffer), size); |
| 4436 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4437 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4438 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4439 | return posix_error(); |
| 4440 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4441 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4442 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4443 | return buffer; |
| 4444 | } |
| 4445 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4446 | |
| 4447 | static char posix_write__doc__[] = |
| 4448 | "write(fd, string) -> byteswritten\n\ |
| 4449 | Write a string to a file descriptor."; |
| 4450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4451 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4452 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4453 | { |
| 4454 | int fd, size; |
| 4455 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4456 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4457 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4458 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4459 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4460 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4461 | if (size < 0) |
| 4462 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4463 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4466 | |
| 4467 | static char posix_fstat__doc__[]= |
| 4468 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 4469 | Like stat(), but for an open file descriptor."; |
| 4470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4471 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4472 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4473 | { |
| 4474 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4475 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4476 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4477 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4478 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4479 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4480 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4481 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4482 | if (res != 0) |
| 4483 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4484 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4485 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4488 | |
| 4489 | static char posix_fdopen__doc__[] = |
| 4490 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 4491 | Return an open file object connected to a file descriptor."; |
| 4492 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4493 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4494 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4495 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4496 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4497 | char *mode = "r"; |
| 4498 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4499 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4500 | PyObject *f; |
| 4501 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4502 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4503 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4504 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4505 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4506 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4507 | if (fp == NULL) |
| 4508 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4509 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4510 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4511 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4512 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4515 | static char posix_isatty__doc__[] = |
| 4516 | "isatty(fd) -> Boolean\n\ |
| 4517 | 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] | 4518 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4519 | |
| 4520 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4521 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4522 | { |
| 4523 | int fd; |
| 4524 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 4525 | return NULL; |
| 4526 | return Py_BuildValue("i", isatty(fd)); |
| 4527 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4528 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4529 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4530 | static char posix_pipe__doc__[] = |
| 4531 | "pipe() -> (read_end, write_end)\n\ |
| 4532 | Create a pipe."; |
| 4533 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4534 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4535 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4536 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4537 | #if defined(PYOS_OS2) |
| 4538 | HFILE read, write; |
| 4539 | APIRET rc; |
| 4540 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4541 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4542 | return NULL; |
| 4543 | |
| 4544 | Py_BEGIN_ALLOW_THREADS |
| 4545 | rc = DosCreatePipe( &read, &write, 4096); |
| 4546 | Py_END_ALLOW_THREADS |
| 4547 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4548 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4549 | |
| 4550 | return Py_BuildValue("(ii)", read, write); |
| 4551 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4552 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4553 | int fds[2]; |
| 4554 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4555 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4556 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4557 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4558 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4559 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4560 | if (res != 0) |
| 4561 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4562 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4563 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4564 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4565 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4566 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4567 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4568 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4569 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4570 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4571 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 4572 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4573 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 4574 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 4575 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 4576 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4577 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4578 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4579 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4580 | #endif /* HAVE_PIPE */ |
| 4581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4582 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4583 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4584 | static char posix_mkfifo__doc__[] = |
| 4585 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 4586 | Create a FIFO (a POSIX named pipe)."; |
| 4587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4588 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4589 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4590 | { |
| 4591 | char *file; |
| 4592 | int mode = 0666; |
| 4593 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4594 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4595 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4596 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4597 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4598 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4599 | if (res < 0) |
| 4600 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4601 | Py_INCREF(Py_None); |
| 4602 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4603 | } |
| 4604 | #endif |
| 4605 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4606 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4607 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4608 | static char posix_ftruncate__doc__[] = |
| 4609 | "ftruncate(fd, length) -> None\n\ |
| 4610 | Truncate a file to a specified length."; |
| 4611 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4612 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4613 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4614 | { |
| 4615 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4616 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4617 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4618 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4619 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4620 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4621 | return NULL; |
| 4622 | |
| 4623 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4624 | length = PyInt_AsLong(lenobj); |
| 4625 | #else |
| 4626 | length = PyLong_Check(lenobj) ? |
| 4627 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 4628 | #endif |
| 4629 | if (PyErr_Occurred()) |
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_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4633 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4634 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4635 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4636 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4637 | return NULL; |
| 4638 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4639 | Py_INCREF(Py_None); |
| 4640 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4641 | } |
| 4642 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4643 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4644 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4645 | static char posix_putenv__doc__[] = |
| 4646 | "putenv(key, value) -> None\n\ |
| 4647 | Change or add an environment variable."; |
| 4648 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4649 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4650 | * get re-set with another call for the same key. */ |
| 4651 | static PyObject *posix_putenv_garbage; |
| 4652 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4653 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4654 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4655 | { |
| 4656 | char *s1, *s2; |
| 4657 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4658 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4659 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4660 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4661 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4662 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4663 | |
| 4664 | #if defined(PYOS_OS2) |
| 4665 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4666 | APIRET rc; |
| 4667 | |
| 4668 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4669 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4670 | |
| 4671 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4672 | if (rc != NO_ERROR) |
| 4673 | return os2_error(rc); |
| 4674 | |
| 4675 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4676 | APIRET rc; |
| 4677 | |
| 4678 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4679 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4680 | |
| 4681 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4682 | if (rc != NO_ERROR) |
| 4683 | return os2_error(rc); |
| 4684 | } else { |
| 4685 | #endif |
| 4686 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4687 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4688 | len = strlen(s1) + strlen(s2) + 2; |
| 4689 | /* len includes space for a trailing \0; the size arg to |
| 4690 | PyString_FromStringAndSize does not count that */ |
| 4691 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4692 | if (newstr == NULL) |
| 4693 | return PyErr_NoMemory(); |
| 4694 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4695 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4696 | if (putenv(new)) { |
| 4697 | posix_error(); |
| 4698 | return NULL; |
| 4699 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4700 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 4701 | * this will cause previous value to be collected. This has to |
| 4702 | * happen after the real putenv() call because the old value |
| 4703 | * was still accessible until then. */ |
| 4704 | if (PyDict_SetItem(posix_putenv_garbage, |
| 4705 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 4706 | /* really not much we can do; just leak */ |
| 4707 | PyErr_Clear(); |
| 4708 | } |
| 4709 | else { |
| 4710 | Py_DECREF(newstr); |
| 4711 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4712 | |
| 4713 | #if defined(PYOS_OS2) |
| 4714 | } |
| 4715 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4716 | Py_INCREF(Py_None); |
| 4717 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4718 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4719 | #endif /* putenv */ |
| 4720 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4721 | #ifdef HAVE_UNSETENV |
| 4722 | static char posix_unsetenv__doc__[] = |
| 4723 | "unsetenv(key) -> None\n\ |
| 4724 | Delete an environment variable."; |
| 4725 | |
| 4726 | static PyObject * |
| 4727 | posix_unsetenv(PyObject *self, PyObject *args) |
| 4728 | { |
| 4729 | char *s1; |
| 4730 | |
| 4731 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 4732 | return NULL; |
| 4733 | |
| 4734 | unsetenv(s1); |
| 4735 | |
| 4736 | /* Remove the key from posix_putenv_garbage; |
| 4737 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4738 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4739 | * old value was still accessible until then. |
| 4740 | */ |
| 4741 | if (PyDict_DelItem(posix_putenv_garbage, |
| 4742 | PyTuple_GET_ITEM(args, 0))) { |
| 4743 | /* really not much we can do; just leak */ |
| 4744 | PyErr_Clear(); |
| 4745 | } |
| 4746 | |
| 4747 | Py_INCREF(Py_None); |
| 4748 | return Py_None; |
| 4749 | } |
| 4750 | #endif /* unsetenv */ |
| 4751 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4752 | #ifdef HAVE_STRERROR |
| 4753 | static char posix_strerror__doc__[] = |
| 4754 | "strerror(code) -> string\n\ |
| 4755 | Translate an error code to a message string."; |
| 4756 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 4757 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4758 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4759 | { |
| 4760 | int code; |
| 4761 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4762 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4763 | return NULL; |
| 4764 | message = strerror(code); |
| 4765 | if (message == NULL) { |
| 4766 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4767 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4768 | return NULL; |
| 4769 | } |
| 4770 | return PyString_FromString(message); |
| 4771 | } |
| 4772 | #endif /* strerror */ |
| 4773 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4774 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4775 | #ifdef HAVE_SYS_WAIT_H |
| 4776 | |
| 4777 | #ifdef WIFSTOPPED |
| 4778 | static char posix_WIFSTOPPED__doc__[] = |
| 4779 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4780 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4781 | |
| 4782 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4783 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4784 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4785 | #ifdef UNION_WAIT |
| 4786 | union wait status; |
| 4787 | #define status_i (status.w_status) |
| 4788 | #else |
| 4789 | int status; |
| 4790 | #define status_i status |
| 4791 | #endif |
| 4792 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4793 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4794 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4795 | { |
| 4796 | return NULL; |
| 4797 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4798 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4799 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4800 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4801 | } |
| 4802 | #endif /* WIFSTOPPED */ |
| 4803 | |
| 4804 | #ifdef WIFSIGNALED |
| 4805 | static char posix_WIFSIGNALED__doc__[] = |
| 4806 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 4807 | 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] | 4808 | |
| 4809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4810 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4811 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4812 | #ifdef UNION_WAIT |
| 4813 | union wait status; |
| 4814 | #define status_i (status.w_status) |
| 4815 | #else |
| 4816 | int status; |
| 4817 | #define status_i status |
| 4818 | #endif |
| 4819 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4820 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4821 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4822 | { |
| 4823 | return NULL; |
| 4824 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4825 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4826 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4827 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4828 | } |
| 4829 | #endif /* WIFSIGNALED */ |
| 4830 | |
| 4831 | #ifdef WIFEXITED |
| 4832 | static char posix_WIFEXITED__doc__[] = |
| 4833 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4834 | Return true if the process returning 'status' exited using the exit()\n\ |
| 4835 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4836 | |
| 4837 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4838 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4839 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4840 | #ifdef UNION_WAIT |
| 4841 | union wait status; |
| 4842 | #define status_i (status.w_status) |
| 4843 | #else |
| 4844 | int status; |
| 4845 | #define status_i status |
| 4846 | #endif |
| 4847 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4848 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4849 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4850 | { |
| 4851 | return NULL; |
| 4852 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4853 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4854 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4855 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4856 | } |
| 4857 | #endif /* WIFEXITED */ |
| 4858 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4859 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4860 | static char posix_WEXITSTATUS__doc__[] = |
| 4861 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4862 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4863 | |
| 4864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4865 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4866 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4867 | #ifdef UNION_WAIT |
| 4868 | union wait status; |
| 4869 | #define status_i (status.w_status) |
| 4870 | #else |
| 4871 | int status; |
| 4872 | #define status_i status |
| 4873 | #endif |
| 4874 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4875 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4876 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4877 | { |
| 4878 | return NULL; |
| 4879 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4880 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4881 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4882 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4883 | } |
| 4884 | #endif /* WEXITSTATUS */ |
| 4885 | |
| 4886 | #ifdef WTERMSIG |
| 4887 | static char posix_WTERMSIG__doc__[] = |
| 4888 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4889 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4890 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4891 | |
| 4892 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4893 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4894 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4895 | #ifdef UNION_WAIT |
| 4896 | union wait status; |
| 4897 | #define status_i (status.w_status) |
| 4898 | #else |
| 4899 | int status; |
| 4900 | #define status_i status |
| 4901 | #endif |
| 4902 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4903 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4904 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4905 | { |
| 4906 | return NULL; |
| 4907 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4908 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4909 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4910 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4911 | } |
| 4912 | #endif /* WTERMSIG */ |
| 4913 | |
| 4914 | #ifdef WSTOPSIG |
| 4915 | static char posix_WSTOPSIG__doc__[] = |
| 4916 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4917 | 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] | 4918 | |
| 4919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4920 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4921 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4922 | #ifdef UNION_WAIT |
| 4923 | union wait status; |
| 4924 | #define status_i (status.w_status) |
| 4925 | #else |
| 4926 | int status; |
| 4927 | #define status_i status |
| 4928 | #endif |
| 4929 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4930 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4931 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4932 | { |
| 4933 | return NULL; |
| 4934 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4935 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4936 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4937 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4938 | } |
| 4939 | #endif /* WSTOPSIG */ |
| 4940 | |
| 4941 | #endif /* HAVE_SYS_WAIT_H */ |
| 4942 | |
| 4943 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4944 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4945 | #ifdef _SCO_DS |
| 4946 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4947 | needed definitions in sys/statvfs.h */ |
| 4948 | #define _SVID3 |
| 4949 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4950 | #include <sys/statvfs.h> |
| 4951 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4952 | static PyObject* |
| 4953 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 4954 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 4955 | if (v == NULL) |
| 4956 | return NULL; |
| 4957 | |
| 4958 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4959 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4960 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4961 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 4962 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 4963 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 4964 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 4965 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 4966 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 4967 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4968 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4969 | #else |
| 4970 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4971 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4972 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4973 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4974 | PyStructSequence_SET_ITEM(v, 3, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4975 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 4976 | PyStructSequence_SET_ITEM(v, 4, |
| 4977 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4978 | PyStructSequence_SET_ITEM(v, 5, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4979 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4980 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4981 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4982 | PyStructSequence_SET_ITEM(v, 7, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4983 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 4984 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4985 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4986 | #endif |
| 4987 | |
| 4988 | return v; |
| 4989 | } |
| 4990 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4991 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4992 | "fstatvfs(fd) -> \n\ |
| 4993 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4994 | Perform an fstatvfs system call on the given fd."; |
| 4995 | |
| 4996 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4997 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4998 | { |
| 4999 | int fd, res; |
| 5000 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5001 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5002 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5003 | return NULL; |
| 5004 | Py_BEGIN_ALLOW_THREADS |
| 5005 | res = fstatvfs(fd, &st); |
| 5006 | Py_END_ALLOW_THREADS |
| 5007 | if (res != 0) |
| 5008 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5009 | |
| 5010 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5011 | } |
| 5012 | #endif /* HAVE_FSTATVFS */ |
| 5013 | |
| 5014 | |
| 5015 | #if defined(HAVE_STATVFS) |
| 5016 | #include <sys/statvfs.h> |
| 5017 | |
| 5018 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 5019 | "statvfs(path) -> \n\ |
| 5020 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5021 | Perform a statvfs system call on the given path."; |
| 5022 | |
| 5023 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5024 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5025 | { |
| 5026 | char *path; |
| 5027 | int res; |
| 5028 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5029 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5030 | return NULL; |
| 5031 | Py_BEGIN_ALLOW_THREADS |
| 5032 | res = statvfs(path, &st); |
| 5033 | Py_END_ALLOW_THREADS |
| 5034 | if (res != 0) |
| 5035 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5036 | |
| 5037 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5038 | } |
| 5039 | #endif /* HAVE_STATVFS */ |
| 5040 | |
| 5041 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5042 | #ifdef HAVE_TEMPNAM |
| 5043 | static char posix_tempnam__doc__[] = "\ |
| 5044 | tempnam([dir[, prefix]]) -> string\n\ |
| 5045 | Return a unique name for a temporary file.\n\ |
| 5046 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 5047 | or None if not needed."; |
| 5048 | |
| 5049 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5050 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5051 | { |
| 5052 | PyObject *result = NULL; |
| 5053 | char *dir = NULL; |
| 5054 | char *pfx = NULL; |
| 5055 | char *name; |
| 5056 | |
| 5057 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5058 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5059 | |
| 5060 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5061 | "tempnam is a potential security risk to your program") < 0) |
| 5062 | return NULL; |
| 5063 | |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5064 | #ifdef MS_WIN32 |
| 5065 | name = _tempnam(dir, pfx); |
| 5066 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5067 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5068 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5069 | if (name == NULL) |
| 5070 | return PyErr_NoMemory(); |
| 5071 | result = PyString_FromString(name); |
| 5072 | free(name); |
| 5073 | return result; |
| 5074 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5075 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5076 | |
| 5077 | |
| 5078 | #ifdef HAVE_TMPFILE |
| 5079 | static char posix_tmpfile__doc__[] = "\ |
| 5080 | tmpfile() -> file object\n\ |
| 5081 | Create a temporary file with no directory entries."; |
| 5082 | |
| 5083 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5084 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5085 | { |
| 5086 | FILE *fp; |
| 5087 | |
| 5088 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 5089 | return NULL; |
| 5090 | fp = tmpfile(); |
| 5091 | if (fp == NULL) |
| 5092 | return posix_error(); |
| 5093 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 5094 | } |
| 5095 | #endif |
| 5096 | |
| 5097 | |
| 5098 | #ifdef HAVE_TMPNAM |
| 5099 | static char posix_tmpnam__doc__[] = "\ |
| 5100 | tmpnam() -> string\n\ |
| 5101 | Return a unique name for a temporary file."; |
| 5102 | |
| 5103 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5104 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5105 | { |
| 5106 | char buffer[L_tmpnam]; |
| 5107 | char *name; |
| 5108 | |
| 5109 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 5110 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5111 | |
| 5112 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5113 | "tmpnam is a potential security risk to your program") < 0) |
| 5114 | return NULL; |
| 5115 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5116 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5117 | name = tmpnam_r(buffer); |
| 5118 | #else |
| 5119 | name = tmpnam(buffer); |
| 5120 | #endif |
| 5121 | if (name == NULL) { |
| 5122 | PyErr_SetObject(PyExc_OSError, |
| 5123 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5124 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5125 | "unexpected NULL from tmpnam_r" |
| 5126 | #else |
| 5127 | "unexpected NULL from tmpnam" |
| 5128 | #endif |
| 5129 | )); |
| 5130 | return NULL; |
| 5131 | } |
| 5132 | return PyString_FromString(buffer); |
| 5133 | } |
| 5134 | #endif |
| 5135 | |
| 5136 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5137 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5138 | * It maps strings representing configuration variable names to |
| 5139 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5140 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5141 | * rarely-used constants. There are three separate tables that use |
| 5142 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5143 | * |
| 5144 | * This code is always included, even if none of the interfaces that |
| 5145 | * need it are included. The #if hackery needed to avoid it would be |
| 5146 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5147 | */ |
| 5148 | struct constdef { |
| 5149 | char *name; |
| 5150 | long value; |
| 5151 | }; |
| 5152 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5153 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5154 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5155 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5156 | { |
| 5157 | if (PyInt_Check(arg)) { |
| 5158 | *valuep = PyInt_AS_LONG(arg); |
| 5159 | return 1; |
| 5160 | } |
| 5161 | if (PyString_Check(arg)) { |
| 5162 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5163 | size_t lo = 0; |
| 5164 | size_t mid; |
| 5165 | size_t hi = tablesize; |
| 5166 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5167 | char *confname = PyString_AS_STRING(arg); |
| 5168 | while (lo < hi) { |
| 5169 | mid = (lo + hi) / 2; |
| 5170 | cmp = strcmp(confname, table[mid].name); |
| 5171 | if (cmp < 0) |
| 5172 | hi = mid; |
| 5173 | else if (cmp > 0) |
| 5174 | lo = mid + 1; |
| 5175 | else { |
| 5176 | *valuep = table[mid].value; |
| 5177 | return 1; |
| 5178 | } |
| 5179 | } |
| 5180 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5181 | } |
| 5182 | else |
| 5183 | PyErr_SetString(PyExc_TypeError, |
| 5184 | "configuration names must be strings or integers"); |
| 5185 | return 0; |
| 5186 | } |
| 5187 | |
| 5188 | |
| 5189 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5190 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5191 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5192 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5193 | #endif |
| 5194 | #ifdef _PC_ABI_ASYNC_IO |
| 5195 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5197 | #ifdef _PC_ASYNC_IO |
| 5198 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5199 | #endif |
| 5200 | #ifdef _PC_CHOWN_RESTRICTED |
| 5201 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5202 | #endif |
| 5203 | #ifdef _PC_FILESIZEBITS |
| 5204 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5205 | #endif |
| 5206 | #ifdef _PC_LAST |
| 5207 | {"PC_LAST", _PC_LAST}, |
| 5208 | #endif |
| 5209 | #ifdef _PC_LINK_MAX |
| 5210 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5211 | #endif |
| 5212 | #ifdef _PC_MAX_CANON |
| 5213 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5214 | #endif |
| 5215 | #ifdef _PC_MAX_INPUT |
| 5216 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5217 | #endif |
| 5218 | #ifdef _PC_NAME_MAX |
| 5219 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5220 | #endif |
| 5221 | #ifdef _PC_NO_TRUNC |
| 5222 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5223 | #endif |
| 5224 | #ifdef _PC_PATH_MAX |
| 5225 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5226 | #endif |
| 5227 | #ifdef _PC_PIPE_BUF |
| 5228 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5229 | #endif |
| 5230 | #ifdef _PC_PRIO_IO |
| 5231 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5232 | #endif |
| 5233 | #ifdef _PC_SOCK_MAXBUF |
| 5234 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5235 | #endif |
| 5236 | #ifdef _PC_SYNC_IO |
| 5237 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5238 | #endif |
| 5239 | #ifdef _PC_VDISABLE |
| 5240 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5241 | #endif |
| 5242 | }; |
| 5243 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5244 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5245 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5246 | { |
| 5247 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5248 | sizeof(posix_constants_pathconf) |
| 5249 | / sizeof(struct constdef)); |
| 5250 | } |
| 5251 | #endif |
| 5252 | |
| 5253 | #ifdef HAVE_FPATHCONF |
| 5254 | static char posix_fpathconf__doc__[] = "\ |
| 5255 | fpathconf(fd, name) -> integer\n\ |
| 5256 | Return the configuration limit name for the file descriptor fd.\n\ |
| 5257 | If there is no limit, return -1."; |
| 5258 | |
| 5259 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5260 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5261 | { |
| 5262 | PyObject *result = NULL; |
| 5263 | int name, fd; |
| 5264 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5265 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5266 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5267 | long limit; |
| 5268 | |
| 5269 | errno = 0; |
| 5270 | limit = fpathconf(fd, name); |
| 5271 | if (limit == -1 && errno != 0) |
| 5272 | posix_error(); |
| 5273 | else |
| 5274 | result = PyInt_FromLong(limit); |
| 5275 | } |
| 5276 | return result; |
| 5277 | } |
| 5278 | #endif |
| 5279 | |
| 5280 | |
| 5281 | #ifdef HAVE_PATHCONF |
| 5282 | static char posix_pathconf__doc__[] = "\ |
| 5283 | pathconf(path, name) -> integer\n\ |
| 5284 | Return the configuration limit name for the file or directory path.\n\ |
| 5285 | If there is no limit, return -1."; |
| 5286 | |
| 5287 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5288 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5289 | { |
| 5290 | PyObject *result = NULL; |
| 5291 | int name; |
| 5292 | char *path; |
| 5293 | |
| 5294 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5295 | conv_path_confname, &name)) { |
| 5296 | long limit; |
| 5297 | |
| 5298 | errno = 0; |
| 5299 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5300 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5301 | if (errno == EINVAL) |
| 5302 | /* could be a path or name problem */ |
| 5303 | posix_error(); |
| 5304 | else |
| 5305 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5306 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5307 | else |
| 5308 | result = PyInt_FromLong(limit); |
| 5309 | } |
| 5310 | return result; |
| 5311 | } |
| 5312 | #endif |
| 5313 | |
| 5314 | #ifdef HAVE_CONFSTR |
| 5315 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5316 | #ifdef _CS_ARCHITECTURE |
| 5317 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5318 | #endif |
| 5319 | #ifdef _CS_HOSTNAME |
| 5320 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5321 | #endif |
| 5322 | #ifdef _CS_HW_PROVIDER |
| 5323 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5324 | #endif |
| 5325 | #ifdef _CS_HW_SERIAL |
| 5326 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5327 | #endif |
| 5328 | #ifdef _CS_INITTAB_NAME |
| 5329 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5330 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5331 | #ifdef _CS_LFS64_CFLAGS |
| 5332 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5333 | #endif |
| 5334 | #ifdef _CS_LFS64_LDFLAGS |
| 5335 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5336 | #endif |
| 5337 | #ifdef _CS_LFS64_LIBS |
| 5338 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5339 | #endif |
| 5340 | #ifdef _CS_LFS64_LINTFLAGS |
| 5341 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5342 | #endif |
| 5343 | #ifdef _CS_LFS_CFLAGS |
| 5344 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5345 | #endif |
| 5346 | #ifdef _CS_LFS_LDFLAGS |
| 5347 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5348 | #endif |
| 5349 | #ifdef _CS_LFS_LIBS |
| 5350 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5351 | #endif |
| 5352 | #ifdef _CS_LFS_LINTFLAGS |
| 5353 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5354 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5355 | #ifdef _CS_MACHINE |
| 5356 | {"CS_MACHINE", _CS_MACHINE}, |
| 5357 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5358 | #ifdef _CS_PATH |
| 5359 | {"CS_PATH", _CS_PATH}, |
| 5360 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5361 | #ifdef _CS_RELEASE |
| 5362 | {"CS_RELEASE", _CS_RELEASE}, |
| 5363 | #endif |
| 5364 | #ifdef _CS_SRPC_DOMAIN |
| 5365 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5366 | #endif |
| 5367 | #ifdef _CS_SYSNAME |
| 5368 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5369 | #endif |
| 5370 | #ifdef _CS_VERSION |
| 5371 | {"CS_VERSION", _CS_VERSION}, |
| 5372 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5373 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5374 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5375 | #endif |
| 5376 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5377 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5378 | #endif |
| 5379 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5380 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5381 | #endif |
| 5382 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5383 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5384 | #endif |
| 5385 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5386 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5387 | #endif |
| 5388 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5389 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5390 | #endif |
| 5391 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5392 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5393 | #endif |
| 5394 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5395 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5396 | #endif |
| 5397 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5398 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5399 | #endif |
| 5400 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5401 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5402 | #endif |
| 5403 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5404 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5405 | #endif |
| 5406 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5407 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5408 | #endif |
| 5409 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5410 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5411 | #endif |
| 5412 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5413 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5414 | #endif |
| 5415 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5416 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5417 | #endif |
| 5418 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5419 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5420 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5421 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5422 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5423 | #endif |
| 5424 | #ifdef _MIPS_CS_BASE |
| 5425 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5426 | #endif |
| 5427 | #ifdef _MIPS_CS_HOSTID |
| 5428 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5429 | #endif |
| 5430 | #ifdef _MIPS_CS_HW_NAME |
| 5431 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5432 | #endif |
| 5433 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5434 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5435 | #endif |
| 5436 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5437 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 5438 | #endif |
| 5439 | #ifdef _MIPS_CS_OSREL_MIN |
| 5440 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 5441 | #endif |
| 5442 | #ifdef _MIPS_CS_OSREL_PATCH |
| 5443 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 5444 | #endif |
| 5445 | #ifdef _MIPS_CS_OS_NAME |
| 5446 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 5447 | #endif |
| 5448 | #ifdef _MIPS_CS_OS_PROVIDER |
| 5449 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 5450 | #endif |
| 5451 | #ifdef _MIPS_CS_PROCESSORS |
| 5452 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 5453 | #endif |
| 5454 | #ifdef _MIPS_CS_SERIAL |
| 5455 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 5456 | #endif |
| 5457 | #ifdef _MIPS_CS_VENDOR |
| 5458 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 5459 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5460 | }; |
| 5461 | |
| 5462 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5463 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5464 | { |
| 5465 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 5466 | sizeof(posix_constants_confstr) |
| 5467 | / sizeof(struct constdef)); |
| 5468 | } |
| 5469 | |
| 5470 | static char posix_confstr__doc__[] = "\ |
| 5471 | confstr(name) -> string\n\ |
| 5472 | Return a string-valued system configuration variable."; |
| 5473 | |
| 5474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5475 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5476 | { |
| 5477 | PyObject *result = NULL; |
| 5478 | int name; |
| 5479 | char buffer[64]; |
| 5480 | |
| 5481 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 5482 | int len = confstr(name, buffer, sizeof(buffer)); |
| 5483 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5484 | errno = 0; |
| 5485 | if (len == 0) { |
| 5486 | if (errno != 0) |
| 5487 | posix_error(); |
| 5488 | else |
| 5489 | result = PyString_FromString(""); |
| 5490 | } |
| 5491 | else { |
| 5492 | if (len >= sizeof(buffer)) { |
| 5493 | result = PyString_FromStringAndSize(NULL, len); |
| 5494 | if (result != NULL) |
| 5495 | confstr(name, PyString_AS_STRING(result), len+1); |
| 5496 | } |
| 5497 | else |
| 5498 | result = PyString_FromString(buffer); |
| 5499 | } |
| 5500 | } |
| 5501 | return result; |
| 5502 | } |
| 5503 | #endif |
| 5504 | |
| 5505 | |
| 5506 | #ifdef HAVE_SYSCONF |
| 5507 | static struct constdef posix_constants_sysconf[] = { |
| 5508 | #ifdef _SC_2_CHAR_TERM |
| 5509 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 5510 | #endif |
| 5511 | #ifdef _SC_2_C_BIND |
| 5512 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 5513 | #endif |
| 5514 | #ifdef _SC_2_C_DEV |
| 5515 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 5516 | #endif |
| 5517 | #ifdef _SC_2_C_VERSION |
| 5518 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 5519 | #endif |
| 5520 | #ifdef _SC_2_FORT_DEV |
| 5521 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 5522 | #endif |
| 5523 | #ifdef _SC_2_FORT_RUN |
| 5524 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 5525 | #endif |
| 5526 | #ifdef _SC_2_LOCALEDEF |
| 5527 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 5528 | #endif |
| 5529 | #ifdef _SC_2_SW_DEV |
| 5530 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 5531 | #endif |
| 5532 | #ifdef _SC_2_UPE |
| 5533 | {"SC_2_UPE", _SC_2_UPE}, |
| 5534 | #endif |
| 5535 | #ifdef _SC_2_VERSION |
| 5536 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 5537 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5538 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 5539 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 5540 | #endif |
| 5541 | #ifdef _SC_ACL |
| 5542 | {"SC_ACL", _SC_ACL}, |
| 5543 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5544 | #ifdef _SC_AIO_LISTIO_MAX |
| 5545 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 5546 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5547 | #ifdef _SC_AIO_MAX |
| 5548 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 5549 | #endif |
| 5550 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 5551 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 5552 | #endif |
| 5553 | #ifdef _SC_ARG_MAX |
| 5554 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 5555 | #endif |
| 5556 | #ifdef _SC_ASYNCHRONOUS_IO |
| 5557 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 5558 | #endif |
| 5559 | #ifdef _SC_ATEXIT_MAX |
| 5560 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 5561 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5562 | #ifdef _SC_AUDIT |
| 5563 | {"SC_AUDIT", _SC_AUDIT}, |
| 5564 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5565 | #ifdef _SC_AVPHYS_PAGES |
| 5566 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 5567 | #endif |
| 5568 | #ifdef _SC_BC_BASE_MAX |
| 5569 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 5570 | #endif |
| 5571 | #ifdef _SC_BC_DIM_MAX |
| 5572 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 5573 | #endif |
| 5574 | #ifdef _SC_BC_SCALE_MAX |
| 5575 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 5576 | #endif |
| 5577 | #ifdef _SC_BC_STRING_MAX |
| 5578 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 5579 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5580 | #ifdef _SC_CAP |
| 5581 | {"SC_CAP", _SC_CAP}, |
| 5582 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5583 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 5584 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 5585 | #endif |
| 5586 | #ifdef _SC_CHAR_BIT |
| 5587 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 5588 | #endif |
| 5589 | #ifdef _SC_CHAR_MAX |
| 5590 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 5591 | #endif |
| 5592 | #ifdef _SC_CHAR_MIN |
| 5593 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 5594 | #endif |
| 5595 | #ifdef _SC_CHILD_MAX |
| 5596 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 5597 | #endif |
| 5598 | #ifdef _SC_CLK_TCK |
| 5599 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 5600 | #endif |
| 5601 | #ifdef _SC_COHER_BLKSZ |
| 5602 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 5603 | #endif |
| 5604 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 5605 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 5606 | #endif |
| 5607 | #ifdef _SC_DCACHE_ASSOC |
| 5608 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5609 | #endif |
| 5610 | #ifdef _SC_DCACHE_BLKSZ |
| 5611 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5612 | #endif |
| 5613 | #ifdef _SC_DCACHE_LINESZ |
| 5614 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5615 | #endif |
| 5616 | #ifdef _SC_DCACHE_SZ |
| 5617 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5618 | #endif |
| 5619 | #ifdef _SC_DCACHE_TBLKSZ |
| 5620 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5621 | #endif |
| 5622 | #ifdef _SC_DELAYTIMER_MAX |
| 5623 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5624 | #endif |
| 5625 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5626 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5627 | #endif |
| 5628 | #ifdef _SC_EXPR_NEST_MAX |
| 5629 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5630 | #endif |
| 5631 | #ifdef _SC_FSYNC |
| 5632 | {"SC_FSYNC", _SC_FSYNC}, |
| 5633 | #endif |
| 5634 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5635 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5636 | #endif |
| 5637 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5638 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5639 | #endif |
| 5640 | #ifdef _SC_ICACHE_ASSOC |
| 5641 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5642 | #endif |
| 5643 | #ifdef _SC_ICACHE_BLKSZ |
| 5644 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5645 | #endif |
| 5646 | #ifdef _SC_ICACHE_LINESZ |
| 5647 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5648 | #endif |
| 5649 | #ifdef _SC_ICACHE_SZ |
| 5650 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5651 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5652 | #ifdef _SC_INF |
| 5653 | {"SC_INF", _SC_INF}, |
| 5654 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5655 | #ifdef _SC_INT_MAX |
| 5656 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5657 | #endif |
| 5658 | #ifdef _SC_INT_MIN |
| 5659 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5660 | #endif |
| 5661 | #ifdef _SC_IOV_MAX |
| 5662 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5663 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5664 | #ifdef _SC_IP_SECOPTS |
| 5665 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5666 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5667 | #ifdef _SC_JOB_CONTROL |
| 5668 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5669 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5670 | #ifdef _SC_KERN_POINTERS |
| 5671 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5672 | #endif |
| 5673 | #ifdef _SC_KERN_SIM |
| 5674 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5675 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5676 | #ifdef _SC_LINE_MAX |
| 5677 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5678 | #endif |
| 5679 | #ifdef _SC_LOGIN_NAME_MAX |
| 5680 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5681 | #endif |
| 5682 | #ifdef _SC_LOGNAME_MAX |
| 5683 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5684 | #endif |
| 5685 | #ifdef _SC_LONG_BIT |
| 5686 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5687 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5688 | #ifdef _SC_MAC |
| 5689 | {"SC_MAC", _SC_MAC}, |
| 5690 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5691 | #ifdef _SC_MAPPED_FILES |
| 5692 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5693 | #endif |
| 5694 | #ifdef _SC_MAXPID |
| 5695 | {"SC_MAXPID", _SC_MAXPID}, |
| 5696 | #endif |
| 5697 | #ifdef _SC_MB_LEN_MAX |
| 5698 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5699 | #endif |
| 5700 | #ifdef _SC_MEMLOCK |
| 5701 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5702 | #endif |
| 5703 | #ifdef _SC_MEMLOCK_RANGE |
| 5704 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5705 | #endif |
| 5706 | #ifdef _SC_MEMORY_PROTECTION |
| 5707 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5708 | #endif |
| 5709 | #ifdef _SC_MESSAGE_PASSING |
| 5710 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5711 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5712 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5713 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5714 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5715 | #ifdef _SC_MQ_OPEN_MAX |
| 5716 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5717 | #endif |
| 5718 | #ifdef _SC_MQ_PRIO_MAX |
| 5719 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5720 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5721 | #ifdef _SC_NACLS_MAX |
| 5722 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5723 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5724 | #ifdef _SC_NGROUPS_MAX |
| 5725 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5726 | #endif |
| 5727 | #ifdef _SC_NL_ARGMAX |
| 5728 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5729 | #endif |
| 5730 | #ifdef _SC_NL_LANGMAX |
| 5731 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5732 | #endif |
| 5733 | #ifdef _SC_NL_MSGMAX |
| 5734 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5735 | #endif |
| 5736 | #ifdef _SC_NL_NMAX |
| 5737 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5738 | #endif |
| 5739 | #ifdef _SC_NL_SETMAX |
| 5740 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5741 | #endif |
| 5742 | #ifdef _SC_NL_TEXTMAX |
| 5743 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5744 | #endif |
| 5745 | #ifdef _SC_NPROCESSORS_CONF |
| 5746 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5747 | #endif |
| 5748 | #ifdef _SC_NPROCESSORS_ONLN |
| 5749 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5750 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5751 | #ifdef _SC_NPROC_CONF |
| 5752 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5753 | #endif |
| 5754 | #ifdef _SC_NPROC_ONLN |
| 5755 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5756 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5757 | #ifdef _SC_NZERO |
| 5758 | {"SC_NZERO", _SC_NZERO}, |
| 5759 | #endif |
| 5760 | #ifdef _SC_OPEN_MAX |
| 5761 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5762 | #endif |
| 5763 | #ifdef _SC_PAGESIZE |
| 5764 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5765 | #endif |
| 5766 | #ifdef _SC_PAGE_SIZE |
| 5767 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5768 | #endif |
| 5769 | #ifdef _SC_PASS_MAX |
| 5770 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5771 | #endif |
| 5772 | #ifdef _SC_PHYS_PAGES |
| 5773 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5774 | #endif |
| 5775 | #ifdef _SC_PII |
| 5776 | {"SC_PII", _SC_PII}, |
| 5777 | #endif |
| 5778 | #ifdef _SC_PII_INTERNET |
| 5779 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5780 | #endif |
| 5781 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5782 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5783 | #endif |
| 5784 | #ifdef _SC_PII_INTERNET_STREAM |
| 5785 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5786 | #endif |
| 5787 | #ifdef _SC_PII_OSI |
| 5788 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 5789 | #endif |
| 5790 | #ifdef _SC_PII_OSI_CLTS |
| 5791 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 5792 | #endif |
| 5793 | #ifdef _SC_PII_OSI_COTS |
| 5794 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 5795 | #endif |
| 5796 | #ifdef _SC_PII_OSI_M |
| 5797 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 5798 | #endif |
| 5799 | #ifdef _SC_PII_SOCKET |
| 5800 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 5801 | #endif |
| 5802 | #ifdef _SC_PII_XTI |
| 5803 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 5804 | #endif |
| 5805 | #ifdef _SC_POLL |
| 5806 | {"SC_POLL", _SC_POLL}, |
| 5807 | #endif |
| 5808 | #ifdef _SC_PRIORITIZED_IO |
| 5809 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 5810 | #endif |
| 5811 | #ifdef _SC_PRIORITY_SCHEDULING |
| 5812 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 5813 | #endif |
| 5814 | #ifdef _SC_REALTIME_SIGNALS |
| 5815 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 5816 | #endif |
| 5817 | #ifdef _SC_RE_DUP_MAX |
| 5818 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 5819 | #endif |
| 5820 | #ifdef _SC_RTSIG_MAX |
| 5821 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 5822 | #endif |
| 5823 | #ifdef _SC_SAVED_IDS |
| 5824 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 5825 | #endif |
| 5826 | #ifdef _SC_SCHAR_MAX |
| 5827 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 5828 | #endif |
| 5829 | #ifdef _SC_SCHAR_MIN |
| 5830 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 5831 | #endif |
| 5832 | #ifdef _SC_SELECT |
| 5833 | {"SC_SELECT", _SC_SELECT}, |
| 5834 | #endif |
| 5835 | #ifdef _SC_SEMAPHORES |
| 5836 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 5837 | #endif |
| 5838 | #ifdef _SC_SEM_NSEMS_MAX |
| 5839 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 5840 | #endif |
| 5841 | #ifdef _SC_SEM_VALUE_MAX |
| 5842 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 5843 | #endif |
| 5844 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 5845 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 5846 | #endif |
| 5847 | #ifdef _SC_SHRT_MAX |
| 5848 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 5849 | #endif |
| 5850 | #ifdef _SC_SHRT_MIN |
| 5851 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 5852 | #endif |
| 5853 | #ifdef _SC_SIGQUEUE_MAX |
| 5854 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5855 | #endif |
| 5856 | #ifdef _SC_SIGRT_MAX |
| 5857 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5858 | #endif |
| 5859 | #ifdef _SC_SIGRT_MIN |
| 5860 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5861 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5862 | #ifdef _SC_SOFTPOWER |
| 5863 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5864 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5865 | #ifdef _SC_SPLIT_CACHE |
| 5866 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5867 | #endif |
| 5868 | #ifdef _SC_SSIZE_MAX |
| 5869 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5870 | #endif |
| 5871 | #ifdef _SC_STACK_PROT |
| 5872 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5873 | #endif |
| 5874 | #ifdef _SC_STREAM_MAX |
| 5875 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5876 | #endif |
| 5877 | #ifdef _SC_SYNCHRONIZED_IO |
| 5878 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5879 | #endif |
| 5880 | #ifdef _SC_THREADS |
| 5881 | {"SC_THREADS", _SC_THREADS}, |
| 5882 | #endif |
| 5883 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5884 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5885 | #endif |
| 5886 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5887 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5888 | #endif |
| 5889 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5890 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5891 | #endif |
| 5892 | #ifdef _SC_THREAD_KEYS_MAX |
| 5893 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5894 | #endif |
| 5895 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5896 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5897 | #endif |
| 5898 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5899 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5900 | #endif |
| 5901 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5902 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5903 | #endif |
| 5904 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5905 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5906 | #endif |
| 5907 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5908 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5909 | #endif |
| 5910 | #ifdef _SC_THREAD_STACK_MIN |
| 5911 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5912 | #endif |
| 5913 | #ifdef _SC_THREAD_THREADS_MAX |
| 5914 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5915 | #endif |
| 5916 | #ifdef _SC_TIMERS |
| 5917 | {"SC_TIMERS", _SC_TIMERS}, |
| 5918 | #endif |
| 5919 | #ifdef _SC_TIMER_MAX |
| 5920 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5921 | #endif |
| 5922 | #ifdef _SC_TTY_NAME_MAX |
| 5923 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5924 | #endif |
| 5925 | #ifdef _SC_TZNAME_MAX |
| 5926 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5927 | #endif |
| 5928 | #ifdef _SC_T_IOV_MAX |
| 5929 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5930 | #endif |
| 5931 | #ifdef _SC_UCHAR_MAX |
| 5932 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5933 | #endif |
| 5934 | #ifdef _SC_UINT_MAX |
| 5935 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5936 | #endif |
| 5937 | #ifdef _SC_UIO_MAXIOV |
| 5938 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5939 | #endif |
| 5940 | #ifdef _SC_ULONG_MAX |
| 5941 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5942 | #endif |
| 5943 | #ifdef _SC_USHRT_MAX |
| 5944 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5945 | #endif |
| 5946 | #ifdef _SC_VERSION |
| 5947 | {"SC_VERSION", _SC_VERSION}, |
| 5948 | #endif |
| 5949 | #ifdef _SC_WORD_BIT |
| 5950 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5951 | #endif |
| 5952 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5953 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5954 | #endif |
| 5955 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5956 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5957 | #endif |
| 5958 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5959 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5960 | #endif |
| 5961 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5962 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5963 | #endif |
| 5964 | #ifdef _SC_XOPEN_CRYPT |
| 5965 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5966 | #endif |
| 5967 | #ifdef _SC_XOPEN_ENH_I18N |
| 5968 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5969 | #endif |
| 5970 | #ifdef _SC_XOPEN_LEGACY |
| 5971 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5972 | #endif |
| 5973 | #ifdef _SC_XOPEN_REALTIME |
| 5974 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5975 | #endif |
| 5976 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5977 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5978 | #endif |
| 5979 | #ifdef _SC_XOPEN_SHM |
| 5980 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5981 | #endif |
| 5982 | #ifdef _SC_XOPEN_UNIX |
| 5983 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5984 | #endif |
| 5985 | #ifdef _SC_XOPEN_VERSION |
| 5986 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5987 | #endif |
| 5988 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5989 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5990 | #endif |
| 5991 | #ifdef _SC_XOPEN_XPG2 |
| 5992 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5993 | #endif |
| 5994 | #ifdef _SC_XOPEN_XPG3 |
| 5995 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5996 | #endif |
| 5997 | #ifdef _SC_XOPEN_XPG4 |
| 5998 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5999 | #endif |
| 6000 | }; |
| 6001 | |
| 6002 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6003 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6004 | { |
| 6005 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6006 | sizeof(posix_constants_sysconf) |
| 6007 | / sizeof(struct constdef)); |
| 6008 | } |
| 6009 | |
| 6010 | static char posix_sysconf__doc__[] = "\ |
| 6011 | sysconf(name) -> integer\n\ |
| 6012 | Return an integer-valued system configuration variable."; |
| 6013 | |
| 6014 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6015 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6016 | { |
| 6017 | PyObject *result = NULL; |
| 6018 | int name; |
| 6019 | |
| 6020 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6021 | int value; |
| 6022 | |
| 6023 | errno = 0; |
| 6024 | value = sysconf(name); |
| 6025 | if (value == -1 && errno != 0) |
| 6026 | posix_error(); |
| 6027 | else |
| 6028 | result = PyInt_FromLong(value); |
| 6029 | } |
| 6030 | return result; |
| 6031 | } |
| 6032 | #endif |
| 6033 | |
| 6034 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6035 | /* This code is used to ensure that the tables of configuration value names |
| 6036 | * are in sorted order as required by conv_confname(), and also to build the |
| 6037 | * the exported dictionaries that are used to publish information about the |
| 6038 | * names available on the host platform. |
| 6039 | * |
| 6040 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6041 | * when used, even for platforms we're not able to test on. It also makes |
| 6042 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6043 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6044 | |
| 6045 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6046 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6047 | { |
| 6048 | const struct constdef *c1 = |
| 6049 | (const struct constdef *) v1; |
| 6050 | const struct constdef *c2 = |
| 6051 | (const struct constdef *) v2; |
| 6052 | |
| 6053 | return strcmp(c1->name, c2->name); |
| 6054 | } |
| 6055 | |
| 6056 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6057 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 6058 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6059 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6060 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6061 | size_t i; |
| 6062 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6063 | |
| 6064 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6065 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6066 | if (d == NULL) |
| 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 | for (i=0; i < tablesize; ++i) { |
| 6070 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6071 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6072 | Py_XDECREF(o); |
| 6073 | Py_DECREF(d); |
| 6074 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6075 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6076 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6077 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6078 | status = PyDict_SetItemString(moddict, tablename, d); |
| 6079 | Py_DECREF(d); |
| 6080 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6081 | } |
| 6082 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6083 | /* Return -1 on failure, 0 on success. */ |
| 6084 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6085 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6086 | { |
| 6087 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6088 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6089 | sizeof(posix_constants_pathconf) |
| 6090 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6091 | "pathconf_names", moddict)) |
| 6092 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6093 | #endif |
| 6094 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6095 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6096 | sizeof(posix_constants_confstr) |
| 6097 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6098 | "confstr_names", moddict)) |
| 6099 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6100 | #endif |
| 6101 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6102 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6103 | sizeof(posix_constants_sysconf) |
| 6104 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6105 | "sysconf_names", moddict)) |
| 6106 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6107 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6108 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6109 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6110 | |
| 6111 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6112 | static char posix_abort__doc__[] = "\ |
| 6113 | abort() -> does not return!\n\ |
| 6114 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 6115 | in the hardest way possible on the hosting operating system."; |
| 6116 | |
| 6117 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6118 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6119 | { |
| 6120 | if (!PyArg_ParseTuple(args, ":abort")) |
| 6121 | return NULL; |
| 6122 | abort(); |
| 6123 | /*NOTREACHED*/ |
| 6124 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6125 | return NULL; |
| 6126 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6127 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6128 | #ifdef MS_WIN32 |
| 6129 | static char win32_startfile__doc__[] = "\ |
| 6130 | startfile(filepath) - Start a file with its associated application.\n\ |
| 6131 | \n\ |
| 6132 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6133 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6134 | with whatever application (if any) its extension is associated.\n\ |
| 6135 | \n\ |
| 6136 | startfile returns as soon as the associated application is launched.\n\ |
| 6137 | There is no option to wait for the application to close, and no way\n\ |
| 6138 | to retrieve the application's exit status.\n\ |
| 6139 | \n\ |
| 6140 | The filepath is relative to the current directory. If you want to use\n\ |
| 6141 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 6142 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 6143 | |
| 6144 | static PyObject * |
| 6145 | win32_startfile(PyObject *self, PyObject *args) |
| 6146 | { |
| 6147 | char *filepath; |
| 6148 | HINSTANCE rc; |
| 6149 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6150 | return NULL; |
| 6151 | Py_BEGIN_ALLOW_THREADS |
| 6152 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6153 | Py_END_ALLOW_THREADS |
| 6154 | if (rc <= (HINSTANCE)32) |
| 6155 | return win32_error("startfile", filepath); |
| 6156 | Py_INCREF(Py_None); |
| 6157 | return Py_None; |
| 6158 | } |
| 6159 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6160 | |
| 6161 | static PyMethodDef posix_methods[] = { |
| 6162 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6163 | #ifdef HAVE_TTYNAME |
| 6164 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6165 | #endif |
| 6166 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6167 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6168 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6169 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6170 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6171 | #ifdef HAVE_CHROOT |
| 6172 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6173 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6174 | #ifdef HAVE_CTERMID |
| 6175 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 6176 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6177 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6178 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6179 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6180 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6181 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6182 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6183 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6184 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6185 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6186 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6187 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6188 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6189 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6190 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6191 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6192 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6193 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6194 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6195 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6196 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6197 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6198 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6199 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6200 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6201 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6202 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6203 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6204 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6205 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6206 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6207 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6208 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6209 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6210 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6211 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6212 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6213 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6214 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6215 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6216 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6217 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6218 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6219 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6220 | #ifdef HAVE_FORK1 |
| 6221 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 6222 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6223 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6224 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6225 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6226 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6227 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6228 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6229 | #ifdef HAVE_FORKPTY |
| 6230 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 6231 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6232 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6233 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6234 | #endif /* HAVE_GETEGID */ |
| 6235 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6236 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6237 | #endif /* HAVE_GETEUID */ |
| 6238 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6239 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6240 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6241 | #ifdef HAVE_GETGROUPS |
| 6242 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 6243 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6244 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6245 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6246 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6247 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6248 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6249 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6250 | #endif /* HAVE_GETPPID */ |
| 6251 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6252 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6253 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6254 | #ifdef HAVE_GETLOGIN |
| 6255 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 6256 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6257 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6258 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6259 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6260 | #ifdef HAVE_KILLPG |
| 6261 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6262 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6263 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6264 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6265 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6266 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6267 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6268 | #ifdef MS_WIN32 |
| 6269 | {"popen2", win32_popen2, METH_VARARGS}, |
| 6270 | {"popen3", win32_popen3, METH_VARARGS}, |
| 6271 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6272 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6273 | #else |
| 6274 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6275 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 6276 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 6277 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 6278 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6279 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6280 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6281 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6282 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6283 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6284 | #ifdef HAVE_SETEUID |
| 6285 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6286 | #endif /* HAVE_SETEUID */ |
| 6287 | #ifdef HAVE_SETEGID |
| 6288 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6289 | #endif /* HAVE_SETEGID */ |
| 6290 | #ifdef HAVE_SETREUID |
| 6291 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6292 | #endif /* HAVE_SETREUID */ |
| 6293 | #ifdef HAVE_SETREGID |
| 6294 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6295 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6296 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6297 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6298 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6299 | #ifdef HAVE_SETGROUPS |
| 6300 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 6301 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6302 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6303 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6304 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6305 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6306 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6307 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6308 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6309 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6310 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6311 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6312 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6313 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6314 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6315 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6316 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6317 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6318 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6319 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6320 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6321 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6322 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6323 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6324 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 6325 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6326 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6327 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6328 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6329 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6330 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 6331 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6332 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6333 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6334 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6335 | #endif |
| 6336 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6337 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6338 | #endif |
| 6339 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6340 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6341 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6342 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6343 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6344 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6345 | #ifdef HAVE_UNSETENV |
| 6346 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6347 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6348 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6349 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6350 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6351 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6352 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6353 | #endif |
| 6354 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6355 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6356 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6357 | #ifdef HAVE_SYS_WAIT_H |
| 6358 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6359 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6360 | #endif /* WIFSTOPPED */ |
| 6361 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6362 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6363 | #endif /* WIFSIGNALED */ |
| 6364 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6365 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6366 | #endif /* WIFEXITED */ |
| 6367 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6368 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6369 | #endif /* WEXITSTATUS */ |
| 6370 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6371 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6372 | #endif /* WTERMSIG */ |
| 6373 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6374 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6375 | #endif /* WSTOPSIG */ |
| 6376 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6377 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6378 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6379 | #endif |
| 6380 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6381 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6382 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 6383 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6384 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 6385 | #endif |
| 6386 | #ifdef HAVE_TEMPNAM |
| 6387 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 6388 | #endif |
| 6389 | #ifdef HAVE_TMPNAM |
| 6390 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 6391 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6392 | #ifdef HAVE_CONFSTR |
| 6393 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6394 | #endif |
| 6395 | #ifdef HAVE_SYSCONF |
| 6396 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6397 | #endif |
| 6398 | #ifdef HAVE_FPATHCONF |
| 6399 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6400 | #endif |
| 6401 | #ifdef HAVE_PATHCONF |
| 6402 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6403 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6404 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6405 | #ifdef MS_WIN32 |
| 6406 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6407 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6408 | {NULL, NULL} /* Sentinel */ |
| 6409 | }; |
| 6410 | |
| 6411 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6412 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6413 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6414 | { |
| 6415 | PyObject* v = PyInt_FromLong(value); |
| 6416 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 6417 | return -1; /* triggers fatal error */ |
| 6418 | |
| 6419 | Py_DECREF(v); |
| 6420 | return 0; |
| 6421 | } |
| 6422 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6423 | #if defined(PYOS_OS2) |
| 6424 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 6425 | static int insertvalues(PyObject *d) |
| 6426 | { |
| 6427 | APIRET rc; |
| 6428 | ULONG values[QSV_MAX+1]; |
| 6429 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 6430 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6431 | |
| 6432 | Py_BEGIN_ALLOW_THREADS |
| 6433 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 6434 | Py_END_ALLOW_THREADS |
| 6435 | |
| 6436 | if (rc != NO_ERROR) { |
| 6437 | os2_error(rc); |
| 6438 | return -1; |
| 6439 | } |
| 6440 | |
| 6441 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 6442 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 6443 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 6444 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 6445 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 6446 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 6447 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 6448 | |
| 6449 | switch (values[QSV_VERSION_MINOR]) { |
| 6450 | case 0: ver = "2.00"; break; |
| 6451 | case 10: ver = "2.10"; break; |
| 6452 | case 11: ver = "2.11"; break; |
| 6453 | case 30: ver = "3.00"; break; |
| 6454 | case 40: ver = "4.00"; break; |
| 6455 | case 50: ver = "5.00"; break; |
| 6456 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 6457 | PyOS_snprintf(tmp, sizeof(tmp), |
| 6458 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 6459 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6460 | ver = &tmp[0]; |
| 6461 | } |
| 6462 | |
| 6463 | /* Add Indicator of the Version of the Operating System */ |
| 6464 | v = PyString_FromString(ver); |
| 6465 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 6466 | return -1; |
| 6467 | Py_DECREF(v); |
| 6468 | |
| 6469 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 6470 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 6471 | tmp[1] = ':'; |
| 6472 | tmp[2] = '\0'; |
| 6473 | |
| 6474 | v = PyString_FromString(tmp); |
| 6475 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 6476 | return -1; |
| 6477 | Py_DECREF(v); |
| 6478 | |
| 6479 | return 0; |
| 6480 | } |
| 6481 | #endif |
| 6482 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6483 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6484 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6485 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6486 | #ifdef F_OK |
| 6487 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6488 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6489 | #ifdef R_OK |
| 6490 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6491 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6492 | #ifdef W_OK |
| 6493 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6494 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6495 | #ifdef X_OK |
| 6496 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6497 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6498 | #ifdef NGROUPS_MAX |
| 6499 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 6500 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6501 | #ifdef TMP_MAX |
| 6502 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 6503 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6504 | #ifdef WNOHANG |
| 6505 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6506 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6507 | #ifdef O_RDONLY |
| 6508 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 6509 | #endif |
| 6510 | #ifdef O_WRONLY |
| 6511 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 6512 | #endif |
| 6513 | #ifdef O_RDWR |
| 6514 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 6515 | #endif |
| 6516 | #ifdef O_NDELAY |
| 6517 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 6518 | #endif |
| 6519 | #ifdef O_NONBLOCK |
| 6520 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 6521 | #endif |
| 6522 | #ifdef O_APPEND |
| 6523 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 6524 | #endif |
| 6525 | #ifdef O_DSYNC |
| 6526 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 6527 | #endif |
| 6528 | #ifdef O_RSYNC |
| 6529 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 6530 | #endif |
| 6531 | #ifdef O_SYNC |
| 6532 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 6533 | #endif |
| 6534 | #ifdef O_NOCTTY |
| 6535 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 6536 | #endif |
| 6537 | #ifdef O_CREAT |
| 6538 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 6539 | #endif |
| 6540 | #ifdef O_EXCL |
| 6541 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 6542 | #endif |
| 6543 | #ifdef O_TRUNC |
| 6544 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 6545 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 6546 | #ifdef O_BINARY |
| 6547 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 6548 | #endif |
| 6549 | #ifdef O_TEXT |
| 6550 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 6551 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 6552 | #ifdef O_LARGEFILE |
| 6553 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 6554 | #endif |
| 6555 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6556 | /* MS Windows */ |
| 6557 | #ifdef O_NOINHERIT |
| 6558 | /* Don't inherit in child processes. */ |
| 6559 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 6560 | #endif |
| 6561 | #ifdef _O_SHORT_LIVED |
| 6562 | /* Optimize for short life (keep in memory). */ |
| 6563 | /* MS forgot to define this one with a non-underscore form too. */ |
| 6564 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 6565 | #endif |
| 6566 | #ifdef O_TEMPORARY |
| 6567 | /* Automatically delete when last handle is closed. */ |
| 6568 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 6569 | #endif |
| 6570 | #ifdef O_RANDOM |
| 6571 | /* Optimize for random access. */ |
| 6572 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 6573 | #endif |
| 6574 | #ifdef O_SEQUENTIAL |
| 6575 | /* Optimize for sequential access. */ |
| 6576 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 6577 | #endif |
| 6578 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 6579 | /* GNU extensions. */ |
| 6580 | #ifdef O_DIRECT |
| 6581 | /* Direct disk access. */ |
| 6582 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 6583 | #endif |
| 6584 | #ifdef O_DIRECTORY |
| 6585 | /* Must be a directory. */ |
| 6586 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 6587 | #endif |
| 6588 | #ifdef O_NOFOLLOW |
| 6589 | /* Do not follow links. */ |
| 6590 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 6591 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6592 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6593 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6594 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6595 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 6596 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 6597 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 6598 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 6599 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 6600 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 6601 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 6602 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 6603 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 6604 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 6605 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 6606 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 6607 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 6608 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 6609 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 6610 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 6611 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 6612 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 6613 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 6614 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 6615 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 6616 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 6617 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 6618 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 6619 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 6620 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6621 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6622 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 6623 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6624 | #if defined(PYOS_OS2) |
| 6625 | if (insertvalues(d)) return -1; |
| 6626 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6627 | return 0; |
| 6628 | } |
| 6629 | |
| 6630 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6631 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6632 | #define INITFUNC initnt |
| 6633 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 6634 | |
| 6635 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6636 | #define INITFUNC initos2 |
| 6637 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 6638 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6639 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6640 | #define INITFUNC initposix |
| 6641 | #define MODNAME "posix" |
| 6642 | #endif |
| 6643 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 6644 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6645 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6646 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6647 | PyObject *m, *d, *v; |
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 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6650 | posix_methods, |
| 6651 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6652 | (PyObject *)NULL, |
| 6653 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6654 | d = PyModule_GetDict(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6655 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6656 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6657 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6658 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 6659 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6660 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6661 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6662 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6663 | return; |
| 6664 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6665 | if (setup_confname_tables(d)) |
| 6666 | return; |
| 6667 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 6668 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6669 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6670 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 6671 | if (posix_putenv_garbage == NULL) |
| 6672 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6673 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6674 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6675 | stat_result_desc.name = MODNAME ".stat_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6676 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 6677 | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
| 6678 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6679 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6680 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Guido van Rossum | bb2501f | 2001-12-27 16:23:28 +0000 | [diff] [blame] | 6681 | PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6682 | } |