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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT and MS-Win. In that case the module |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5 | actually calls itself 'nt', not 'posix', and a few functions are |
| 6 | 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 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 13 | static char posix__doc__ [] = |
| 14 | "This module provides access to operating system functionality that is\n\ |
| 15 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 16 | disguised Unix interface). Refer to the library manual and\n\ |
| 17 | corresponding Unix manual entries for more information on calls."; |
| 18 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 21 | #if defined(PYOS_OS2) |
| 22 | #define INCL_DOS |
| 23 | #define INCL_DOSERRORS |
| 24 | #define INCL_DOSPROCESS |
| 25 | #define INCL_NOPMAPI |
| 26 | #include <os2.h> |
| 27 | #endif |
| 28 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 31 | #ifdef HAVE_SYS_WAIT_H |
| 32 | #include <sys/wait.h> /* For WNOHANG */ |
| 33 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 35 | #ifdef HAVE_SIGNAL_H |
| 36 | #include <signal.h> |
| 37 | #endif |
| 38 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | #ifdef HAVE_FCNTL_H |
| 40 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 41 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 42 | |
Skip Montanaro | 8216c18 | 2001-02-27 17:04:34 +0000 | [diff] [blame] | 43 | /* pick up declaration of confstr on some systems? */ |
| 44 | #ifdef HAVE_UNISTD_H |
| 45 | #include <unistd.h> |
| 46 | #endif /* HAVE_UNISTD_H */ |
| 47 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 48 | /* Various compilers have only certain posix functions */ |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 49 | /* XXX Gosh I wish these were all moved into config.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 50 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 51 | #include <process.h> |
| 52 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 53 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 54 | #define HAVE_GETCWD 1 |
| 55 | #define HAVE_OPENDIR 1 |
| 56 | #define HAVE_SYSTEM 1 |
| 57 | #if defined(__OS2__) |
| 58 | #define HAVE_EXECV 1 |
| 59 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 60 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 61 | #include <process.h> |
| 62 | #else |
| 63 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 64 | #define HAVE_EXECV 1 |
| 65 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 66 | #define HAVE_OPENDIR 1 |
| 67 | #define HAVE_PIPE 1 |
| 68 | #define HAVE_POPEN 1 |
| 69 | #define HAVE_SYSTEM 1 |
| 70 | #define HAVE_WAIT 1 |
| 71 | #else |
| 72 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 73 | #define HAVE_GETCWD 1 |
| 74 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 75 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 76 | #define HAVE_EXECV 1 |
| 77 | #define HAVE_PIPE 1 |
| 78 | #define HAVE_POPEN 1 |
| 79 | #define HAVE_SYSTEM 1 |
| 80 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 81 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 82 | #else /* all other compilers */ |
| 83 | /* Unix functions that the configure script doesn't check for */ |
| 84 | #define HAVE_EXECV 1 |
| 85 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 86 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 87 | #define HAVE_FORK1 1 |
| 88 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 89 | #define HAVE_GETCWD 1 |
| 90 | #define HAVE_GETEGID 1 |
| 91 | #define HAVE_GETEUID 1 |
| 92 | #define HAVE_GETGID 1 |
| 93 | #define HAVE_GETPPID 1 |
| 94 | #define HAVE_GETUID 1 |
| 95 | #define HAVE_KILL 1 |
| 96 | #define HAVE_OPENDIR 1 |
| 97 | #define HAVE_PIPE 1 |
| 98 | #define HAVE_POPEN 1 |
| 99 | #define HAVE_SYSTEM 1 |
| 100 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 101 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 102 | #endif /* _MSC_VER */ |
| 103 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 105 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 107 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 109 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 110 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 113 | #if defined(sun) && !defined(__SVR4) |
| 114 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 115 | extern int rename(const char *, const char *); |
| 116 | extern int pclose(FILE *); |
| 117 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 118 | extern int fsync(int); |
| 119 | extern int lstat(const char *, struct stat *); |
| 120 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 123 | #ifdef NeXT |
| 124 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 125 | #undef HAVE_UNISTD_H |
| 126 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 127 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 128 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 129 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 132 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 133 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 134 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 135 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 136 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 137 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 138 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 139 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 141 | #endif |
| 142 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 143 | extern int chdir(char *); |
| 144 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 145 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 146 | extern int chdir(const char *); |
| 147 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 148 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 149 | #ifdef __BORLANDC__ |
| 150 | extern int chmod(const char *, int); |
| 151 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 153 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int chown(const char *, uid_t, gid_t); |
| 155 | extern char *getcwd(char *, int); |
| 156 | extern char *strerror(int); |
| 157 | extern int link(const char *, const char *); |
| 158 | extern int rename(const char *, const char *); |
| 159 | extern int stat(const char *, struct stat *); |
| 160 | extern int unlink(const char *); |
| 161 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 163 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 164 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 166 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 167 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 170 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | #ifdef HAVE_UTIME_H |
| 173 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 174 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 176 | #ifdef HAVE_SYS_UTIME_H |
| 177 | #include <sys/utime.h> |
| 178 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 179 | #endif /* HAVE_SYS_UTIME_H */ |
| 180 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | #ifdef HAVE_SYS_TIMES_H |
| 182 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 183 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 184 | |
| 185 | #ifdef HAVE_SYS_PARAM_H |
| 186 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 187 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | |
| 189 | #ifdef HAVE_SYS_UTSNAME_H |
| 190 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 191 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | |
| 193 | #ifndef MAXPATHLEN |
| 194 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 195 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 197 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 199 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 200 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 201 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 202 | #include <direct.h> |
| 203 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 204 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 206 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 207 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 208 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #endif |
| 211 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 213 | #endif |
| 214 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #endif |
| 217 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 219 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | #include <direct.h> |
| 221 | #include <io.h> |
| 222 | #include <process.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 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 238 | #ifdef UNION_WAIT |
| 239 | /* Emulate some macros on systems that have a union instead of macros */ |
| 240 | |
| 241 | #ifndef WIFEXITED |
| 242 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 243 | #endif |
| 244 | |
| 245 | #ifndef WEXITSTATUS |
| 246 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 247 | #endif |
| 248 | |
| 249 | #ifndef WTERMSIG |
| 250 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 251 | #endif |
| 252 | |
| 253 | #endif /* UNION_WAIT */ |
| 254 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 255 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 256 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 257 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 258 | #define USE_CTERMID_R |
| 259 | #endif |
| 260 | |
| 261 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 262 | #define USE_TMPNAM_R |
| 263 | #endif |
| 264 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 265 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 266 | #undef STAT |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 267 | #ifdef MS_WIN64 |
| 268 | # define STAT _stati64 |
| 269 | # define FSTAT _fstati64 |
| 270 | # define STRUCT_STAT struct _stati64 |
| 271 | #else |
| 272 | # define STAT stat |
| 273 | # define FSTAT fstat |
| 274 | # define STRUCT_STAT struct stat |
| 275 | #endif |
| 276 | |
| 277 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 279 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 280 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 282 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 284 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 285 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 287 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 289 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | if (d == NULL) |
| 291 | return NULL; |
| 292 | if (environ == NULL) |
| 293 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 294 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 295 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 296 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 297 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | char *p = strchr(*e, '='); |
| 299 | if (p == NULL) |
| 300 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 301 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 302 | if (k == NULL) { |
| 303 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 305 | } |
| 306 | v = PyString_FromString(p+1); |
| 307 | if (v == NULL) { |
| 308 | PyErr_Clear(); |
| 309 | Py_DECREF(k); |
| 310 | continue; |
| 311 | } |
| 312 | if (PyDict_GetItem(d, k) == NULL) { |
| 313 | if (PyDict_SetItem(d, k, v) != 0) |
| 314 | PyErr_Clear(); |
| 315 | } |
| 316 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 317 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 319 | #if defined(PYOS_OS2) |
| 320 | { |
| 321 | APIRET rc; |
| 322 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 323 | |
| 324 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 325 | 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] | 326 | PyObject *v = PyString_FromString(buffer); |
| 327 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 328 | Py_DECREF(v); |
| 329 | } |
| 330 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 331 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 332 | PyObject *v = PyString_FromString(buffer); |
| 333 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 334 | Py_DECREF(v); |
| 335 | } |
| 336 | } |
| 337 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | return d; |
| 339 | } |
| 340 | |
| 341 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 343 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 344 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 345 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 346 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 347 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 349 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 350 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 351 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 352 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 355 | static PyObject * |
| 356 | posix_error_with_allocated_filename(char* name) |
| 357 | { |
| 358 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 359 | PyMem_Free(name); |
| 360 | return rc; |
| 361 | } |
| 362 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 363 | #ifdef MS_WIN32 |
| 364 | static PyObject * |
| 365 | win32_error(char* function, char* filename) |
| 366 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 367 | /* XXX We should pass the function name along in the future. |
| 368 | (_winreg.c also wants to pass the function name.) |
| 369 | This would however require an additional param to the |
| 370 | Windows error object, which is non-trivial. |
| 371 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 372 | errno = GetLastError(); |
| 373 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 374 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 375 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 376 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 377 | } |
| 378 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 380 | #if defined(PYOS_OS2) |
| 381 | /********************************************************************** |
| 382 | * Helper Function to Trim and Format OS/2 Messages |
| 383 | **********************************************************************/ |
| 384 | static void |
| 385 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 386 | { |
| 387 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 388 | |
| 389 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 390 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 391 | |
| 392 | while (lastc > msgbuf && isspace(*lastc)) |
| 393 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 394 | } |
| 395 | |
| 396 | /* Add Optional Reason Text */ |
| 397 | if (reason) { |
| 398 | strcat(msgbuf, " : "); |
| 399 | strcat(msgbuf, reason); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /********************************************************************** |
| 404 | * Decode an OS/2 Operating System Error Code |
| 405 | * |
| 406 | * A convenience function to lookup an OS/2 error code and return a |
| 407 | * text message we can use to raise a Python exception. |
| 408 | * |
| 409 | * Notes: |
| 410 | * The messages for errors returned from the OS/2 kernel reside in |
| 411 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 412 | * |
| 413 | **********************************************************************/ |
| 414 | static char * |
| 415 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 416 | { |
| 417 | APIRET rc; |
| 418 | ULONG msglen; |
| 419 | |
| 420 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 421 | Py_BEGIN_ALLOW_THREADS |
| 422 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 423 | errorcode, "oso001.msg", &msglen); |
| 424 | Py_END_ALLOW_THREADS |
| 425 | |
| 426 | if (rc == NO_ERROR) |
| 427 | os2_formatmsg(msgbuf, msglen, reason); |
| 428 | else |
| 429 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 430 | |
| 431 | return msgbuf; |
| 432 | } |
| 433 | |
| 434 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 435 | errors are not in a global variable e.g. 'errno' nor are |
| 436 | they congruent with posix error numbers. */ |
| 437 | |
| 438 | static PyObject * os2_error(int code) |
| 439 | { |
| 440 | char text[1024]; |
| 441 | PyObject *v; |
| 442 | |
| 443 | os2_strerror(text, sizeof(text), code, ""); |
| 444 | |
| 445 | v = Py_BuildValue("(is)", code, text); |
| 446 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 447 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 448 | Py_DECREF(v); |
| 449 | } |
| 450 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 451 | } |
| 452 | |
| 453 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 454 | |
| 455 | /* POSIX generic methods */ |
| 456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 458 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 459 | { |
| 460 | int fd; |
| 461 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 462 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 463 | return NULL; |
| 464 | Py_BEGIN_ALLOW_THREADS |
| 465 | res = (*func)(fd); |
| 466 | Py_END_ALLOW_THREADS |
| 467 | if (res < 0) |
| 468 | return posix_error(); |
| 469 | Py_INCREF(Py_None); |
| 470 | return Py_None; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 475 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 476 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 477 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 478 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 479 | if (!PyArg_ParseTuple(args, format, |
| 480 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 481 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 482 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 483 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 484 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 485 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 486 | return posix_error_with_allocated_filename(path1); |
| 487 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 488 | Py_INCREF(Py_None); |
| 489 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 492 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 493 | posix_2str(PyObject *args, char *format, |
| 494 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 495 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 496 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 497 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 498 | if (!PyArg_ParseTuple(args, format, |
| 499 | Py_FileSystemDefaultEncoding, &path1, |
| 500 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 502 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 503 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 504 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 505 | PyMem_Free(path1); |
| 506 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 507 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 508 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 509 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 510 | Py_INCREF(Py_None); |
| 511 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 514 | /* pack a system stat C structure into the Python stat tuple |
| 515 | (used by posix_stat() and posix_fstat()) */ |
| 516 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 517 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 518 | { |
| 519 | PyObject *v = PyTuple_New(10); |
| 520 | if (v == NULL) |
| 521 | return NULL; |
| 522 | |
| 523 | PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode)); |
| 524 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 525 | PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
| 526 | #else |
| 527 | PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino)); |
| 528 | #endif |
| 529 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
| 530 | PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
| 531 | #else |
| 532 | PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev)); |
| 533 | #endif |
| 534 | PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 535 | PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 536 | PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid)); |
| 537 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 538 | PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size)); |
| 539 | #else |
| 540 | PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size)); |
| 541 | #endif |
| 542 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 543 | PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 544 | PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 545 | PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
| 546 | #else |
| 547 | PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 548 | PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 549 | PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 550 | #endif |
| 551 | |
| 552 | if (PyErr_Occurred()) { |
| 553 | Py_DECREF(v); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | return v; |
| 558 | } |
| 559 | |
| 560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 562 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 563 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 565 | STRUCT_STAT st; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 566 | char *path = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 567 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 568 | |
| 569 | #ifdef MS_WIN32 |
| 570 | int pathlen; |
| 571 | char pathcopy[MAX_PATH]; |
| 572 | #endif /* MS_WIN32 */ |
| 573 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 574 | if (!PyArg_ParseTuple(args, format, |
| 575 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 577 | |
| 578 | #ifdef MS_WIN32 |
| 579 | pathlen = strlen(path); |
| 580 | /* the library call can blow up if the file name is too long! */ |
| 581 | if (pathlen > MAX_PATH) { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 582 | PyMem_Free(path); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 583 | errno = ENAMETOOLONG; |
| 584 | return posix_error(); |
| 585 | } |
| 586 | |
| 587 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 588 | /* exception for specific or current drive root */ |
| 589 | if (!((pathlen == 1) || |
| 590 | ((pathlen == 3) && |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 591 | (path[1] == ':') && |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 592 | (path[2] == '\\' || path[2] == '/')))) |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 593 | { |
| 594 | strncpy(pathcopy, path, pathlen); |
| 595 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 596 | path = pathcopy; |
| 597 | } |
| 598 | } |
| 599 | #endif /* MS_WIN32 */ |
| 600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 601 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 602 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 603 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 604 | if (res != 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 605 | return posix_error_with_allocated_filename(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 606 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 607 | PyMem_Free(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 608 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
| 612 | /* POSIX methods */ |
| 613 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 614 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 615 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 616 | Test for access to a file."; |
| 617 | |
| 618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 619 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 620 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 621 | char *path; |
| 622 | int mode; |
| 623 | int res; |
| 624 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 625 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 626 | return NULL; |
| 627 | Py_BEGIN_ALLOW_THREADS |
| 628 | res = access(path, mode); |
| 629 | Py_END_ALLOW_THREADS |
| 630 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 633 | #ifndef F_OK |
| 634 | #define F_OK 0 |
| 635 | #endif |
| 636 | #ifndef R_OK |
| 637 | #define R_OK 4 |
| 638 | #endif |
| 639 | #ifndef W_OK |
| 640 | #define W_OK 2 |
| 641 | #endif |
| 642 | #ifndef X_OK |
| 643 | #define X_OK 1 |
| 644 | #endif |
| 645 | |
| 646 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 647 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 648 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 649 | Return the name of the terminal device connected to 'fd'."; |
| 650 | |
| 651 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 652 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 653 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 654 | int id; |
| 655 | char *ret; |
| 656 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 657 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 658 | return NULL; |
| 659 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 660 | ret = ttyname(id); |
| 661 | if (ret == NULL) |
| 662 | return(posix_error()); |
| 663 | return(PyString_FromString(ret)); |
| 664 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 665 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 666 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 667 | #ifdef HAVE_CTERMID |
| 668 | static char posix_ctermid__doc__[] = |
| 669 | "ctermid() -> String\n\ |
| 670 | Return the name of the controlling terminal for this process."; |
| 671 | |
| 672 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 673 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 674 | { |
| 675 | char *ret; |
| 676 | char buffer[L_ctermid]; |
| 677 | |
| 678 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 679 | return NULL; |
| 680 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 681 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 682 | ret = ctermid_r(buffer); |
| 683 | #else |
| 684 | ret = ctermid(buffer); |
| 685 | #endif |
| 686 | if (ret == NULL) |
| 687 | return(posix_error()); |
| 688 | return(PyString_FromString(buffer)); |
| 689 | } |
| 690 | #endif |
| 691 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 692 | static char posix_chdir__doc__[] = |
| 693 | "chdir(path) -> None\n\ |
| 694 | Change the current working directory to the specified path."; |
| 695 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 696 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 697 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 698 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 699 | return posix_1str(args, "et:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 702 | |
| 703 | static char posix_chmod__doc__[] = |
| 704 | "chmod(path, mode) -> None\n\ |
| 705 | Change the access permissions of a file."; |
| 706 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 707 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 708 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 709 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 710 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 711 | int i; |
| 712 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 713 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
| 714 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 715 | return NULL; |
| 716 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 717 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 718 | Py_END_ALLOW_THREADS |
| 719 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 720 | return posix_error_with_allocated_filename(path); |
| 721 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 722 | Py_INCREF(Py_None); |
| 723 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 727 | #ifdef HAVE_FSYNC |
| 728 | static char posix_fsync__doc__[] = |
| 729 | "fsync(fildes) -> None\n\ |
| 730 | force write of file with filedescriptor to disk."; |
| 731 | |
| 732 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 733 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 734 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 735 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 736 | } |
| 737 | #endif /* HAVE_FSYNC */ |
| 738 | |
| 739 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 741 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 742 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 743 | #endif |
| 744 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 745 | static char posix_fdatasync__doc__[] = |
| 746 | "fdatasync(fildes) -> None\n\ |
| 747 | force write of file with filedescriptor to disk.\n\ |
| 748 | does not force update of metadata."; |
| 749 | |
| 750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 751 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 752 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 753 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 754 | } |
| 755 | #endif /* HAVE_FDATASYNC */ |
| 756 | |
| 757 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 758 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 759 | static char posix_chown__doc__[] = |
| 760 | "chown(path, uid, gid) -> None\n\ |
| 761 | Change the owner and group id of path to the numeric uid and gid."; |
| 762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 764 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 765 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 766 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 767 | int uid, gid; |
| 768 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 769 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 770 | Py_FileSystemDefaultEncoding, &path, |
| 771 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 772 | return NULL; |
| 773 | Py_BEGIN_ALLOW_THREADS |
| 774 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 775 | Py_END_ALLOW_THREADS |
| 776 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 777 | return posix_error_with_allocated_filename(path); |
| 778 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 779 | Py_INCREF(Py_None); |
| 780 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 781 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 782 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 785 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 786 | static char posix_getcwd__doc__[] = |
| 787 | "getcwd() -> path\n\ |
| 788 | Return a string representing the current working directory."; |
| 789 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 790 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 791 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | { |
| 793 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 794 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 795 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 796 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 797 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 798 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 799 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 800 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 801 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 802 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 804 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 805 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 806 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 807 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 808 | static char posix_link__doc__[] = |
| 809 | "link(src, dst) -> None\n\ |
| 810 | Create a hard link to a file."; |
| 811 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 812 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 813 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 814 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 815 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 816 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 817 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 819 | |
| 820 | static char posix_listdir__doc__[] = |
| 821 | "listdir(path) -> list_of_strings\n\ |
| 822 | Return a list containing the names of the entries in the directory.\n\ |
| 823 | \n\ |
| 824 | path: path of directory to list\n\ |
| 825 | \n\ |
| 826 | The list is in arbitrary order. It does not include the special\n\ |
| 827 | entries '.' and '..' even if they are present in the directory."; |
| 828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 830 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 831 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 832 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 833 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 834 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 835 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 836 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 837 | HANDLE hFindFile; |
| 838 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 839 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 840 | char namebuf[MAX_PATH*2+5]; |
| 841 | char *bufptr = namebuf; |
| 842 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 843 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 844 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 845 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 846 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 847 | return NULL; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 848 | ch = namebuf[len-1]; |
| 849 | if (ch != '/' && ch != '\\' && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 850 | namebuf[len++] = '/'; |
| 851 | strcpy(namebuf + len, "*.*"); |
| 852 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 853 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 854 | return NULL; |
| 855 | |
| 856 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 857 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 858 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 859 | if (errno == ERROR_FILE_NOT_FOUND) |
| 860 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 861 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 862 | } |
| 863 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 864 | if (FileData.cFileName[0] == '.' && |
| 865 | (FileData.cFileName[1] == '\0' || |
| 866 | FileData.cFileName[1] == '.' && |
| 867 | FileData.cFileName[2] == '\0')) |
| 868 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 869 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 870 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 871 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 872 | d = NULL; |
| 873 | break; |
| 874 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 875 | if (PyList_Append(d, v) != 0) { |
| 876 | Py_DECREF(v); |
| 877 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 878 | d = NULL; |
| 879 | break; |
| 880 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 881 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 882 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 883 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 884 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 885 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 886 | |
| 887 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 888 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 889 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 890 | |
| 891 | #ifndef MAX_PATH |
| 892 | #define MAX_PATH 250 |
| 893 | #endif |
| 894 | char *name, *pt; |
| 895 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 896 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 897 | char namebuf[MAX_PATH+5]; |
| 898 | struct _find_t ep; |
| 899 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 900 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 901 | return NULL; |
| 902 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 903 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 904 | return NULL; |
| 905 | } |
| 906 | strcpy(namebuf, name); |
| 907 | for (pt = namebuf; *pt; pt++) |
| 908 | if (*pt == '/') |
| 909 | *pt = '\\'; |
| 910 | if (namebuf[len-1] != '\\') |
| 911 | namebuf[len++] = '\\'; |
| 912 | strcpy(namebuf + len, "*.*"); |
| 913 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 914 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 915 | return NULL; |
| 916 | |
| 917 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 918 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 919 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 920 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 921 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 922 | } |
| 923 | do { |
| 924 | if (ep.name[0] == '.' && |
| 925 | (ep.name[1] == '\0' || |
| 926 | ep.name[1] == '.' && |
| 927 | ep.name[2] == '\0')) |
| 928 | continue; |
| 929 | strcpy(namebuf, ep.name); |
| 930 | for (pt = namebuf; *pt; pt++) |
| 931 | if (isupper(*pt)) |
| 932 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 933 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 934 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 935 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 936 | d = NULL; |
| 937 | break; |
| 938 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 939 | if (PyList_Append(d, v) != 0) { |
| 940 | Py_DECREF(v); |
| 941 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 942 | d = NULL; |
| 943 | break; |
| 944 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 945 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 946 | } while (_dos_findnext(&ep) == 0); |
| 947 | |
| 948 | return d; |
| 949 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 950 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 951 | |
| 952 | #ifndef MAX_PATH |
| 953 | #define MAX_PATH CCHMAXPATH |
| 954 | #endif |
| 955 | char *name, *pt; |
| 956 | int len; |
| 957 | PyObject *d, *v; |
| 958 | char namebuf[MAX_PATH+5]; |
| 959 | HDIR hdir = 1; |
| 960 | ULONG srchcnt = 1; |
| 961 | FILEFINDBUF3 ep; |
| 962 | APIRET rc; |
| 963 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 964 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 965 | return NULL; |
| 966 | if (len >= MAX_PATH) { |
| 967 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 968 | return NULL; |
| 969 | } |
| 970 | strcpy(namebuf, name); |
| 971 | for (pt = namebuf; *pt; pt++) |
| 972 | if (*pt == '/') |
| 973 | *pt = '\\'; |
| 974 | if (namebuf[len-1] != '\\') |
| 975 | namebuf[len++] = '\\'; |
| 976 | strcpy(namebuf + len, "*.*"); |
| 977 | |
| 978 | if ((d = PyList_New(0)) == NULL) |
| 979 | return NULL; |
| 980 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 981 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 982 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 983 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 984 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 985 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 986 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 987 | |
| 988 | if (rc != NO_ERROR) { |
| 989 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 990 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 993 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 994 | do { |
| 995 | if (ep.achName[0] == '.' |
| 996 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 997 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 998 | |
| 999 | strcpy(namebuf, ep.achName); |
| 1000 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1001 | /* Leave Case of Name Alone -- In Native Form */ |
| 1002 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1003 | |
| 1004 | v = PyString_FromString(namebuf); |
| 1005 | if (v == NULL) { |
| 1006 | Py_DECREF(d); |
| 1007 | d = NULL; |
| 1008 | break; |
| 1009 | } |
| 1010 | if (PyList_Append(d, v) != 0) { |
| 1011 | Py_DECREF(v); |
| 1012 | Py_DECREF(d); |
| 1013 | d = NULL; |
| 1014 | break; |
| 1015 | } |
| 1016 | Py_DECREF(v); |
| 1017 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1018 | } |
| 1019 | |
| 1020 | return d; |
| 1021 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1022 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1023 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1024 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1025 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1026 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1027 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1028 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1029 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1030 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1031 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1032 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1033 | closedir(dirp); |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1037 | if (ep->d_name[0] == '.' && |
| 1038 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1039 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1040 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1041 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1042 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1043 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1044 | d = NULL; |
| 1045 | break; |
| 1046 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1047 | if (PyList_Append(d, v) != 0) { |
| 1048 | Py_DECREF(v); |
| 1049 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1050 | d = NULL; |
| 1051 | break; |
| 1052 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1053 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1054 | } |
| 1055 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1056 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1057 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1058 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1059 | #endif /* which OS */ |
| 1060 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1061 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1062 | #ifdef MS_WIN32 |
| 1063 | /* A helper function for abspath on win32 */ |
| 1064 | static PyObject * |
| 1065 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1066 | { |
| 1067 | /* assume encoded strings wont more than double no of chars */ |
| 1068 | char inbuf[MAX_PATH*2]; |
| 1069 | char *inbufp = inbuf; |
| 1070 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1071 | char outbuf[MAX_PATH*2]; |
| 1072 | char *temp; |
| 1073 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1074 | Py_FileSystemDefaultEncoding, &inbufp, |
| 1075 | &insize)) |
| 1076 | return NULL; |
| 1077 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1078 | outbuf, &temp)) |
| 1079 | return win32_error("GetFullPathName", inbuf); |
| 1080 | return PyString_FromString(outbuf); |
| 1081 | } /* end of posix__getfullpathname */ |
| 1082 | #endif /* MS_WIN32 */ |
| 1083 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1084 | static char posix_mkdir__doc__[] = |
| 1085 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1086 | Create a directory."; |
| 1087 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1088 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1089 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1090 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1091 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1092 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1093 | int mode = 0777; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1094 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 1095 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1096 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1097 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1098 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1099 | res = mkdir(path); |
| 1100 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1101 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1102 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1103 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1104 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1105 | return posix_error_with_allocated_filename(path); |
| 1106 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1107 | Py_INCREF(Py_None); |
| 1108 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1112 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1113 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1114 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1115 | #include <sys/resource.h> |
| 1116 | #endif |
| 1117 | #endif |
| 1118 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1119 | static char posix_nice__doc__[] = |
| 1120 | "nice(inc) -> new_priority\n\ |
| 1121 | Decrease the priority of process and return new priority."; |
| 1122 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1123 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1124 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1125 | { |
| 1126 | int increment, value; |
| 1127 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1128 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1129 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* There are two flavours of 'nice': one that returns the new |
| 1132 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1133 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1134 | the use of getpriority() to get the new priority. |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1135 | |
| 1136 | If we are of the nice family that returns the new priority, we |
| 1137 | need to clear errno before the call, and check if errno is filled |
| 1138 | before calling posix_error() on a returnvalue of -1, because the |
| 1139 | -1 may be the actual new priority! */ |
| 1140 | |
| 1141 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1142 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1143 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1144 | if (value == 0) |
| 1145 | value = getpriority(PRIO_PROCESS, 0); |
| 1146 | #endif |
| 1147 | if (value == -1 && errno != 0) |
| 1148 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1149 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1150 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1151 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1152 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1154 | |
| 1155 | static char posix_rename__doc__[] = |
| 1156 | "rename(old, new) -> None\n\ |
| 1157 | Rename a file or directory."; |
| 1158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1160 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1162 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1165 | |
| 1166 | static char posix_rmdir__doc__[] = |
| 1167 | "rmdir(path) -> None\n\ |
| 1168 | Remove a directory."; |
| 1169 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1170 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1171 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1172 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1173 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1176 | |
| 1177 | static char posix_stat__doc__[] = |
| 1178 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1179 | Perform a stat system call on the given path."; |
| 1180 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1181 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1182 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1184 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1187 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1188 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1189 | static char posix_system__doc__[] = |
| 1190 | "system(command) -> exit_status\n\ |
| 1191 | Execute the command (a string) in a subshell."; |
| 1192 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1193 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1194 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1195 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1196 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1197 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1198 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1199 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1200 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1201 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1202 | Py_END_ALLOW_THREADS |
| 1203 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1204 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1205 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1207 | |
| 1208 | static char posix_umask__doc__[] = |
| 1209 | "umask(new_mask) -> old_mask\n\ |
| 1210 | Set the current numeric umask and return the previous umask."; |
| 1211 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1212 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1213 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1214 | { |
| 1215 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1216 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1217 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1218 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | if (i < 0) |
| 1220 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1221 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1224 | |
| 1225 | static char posix_unlink__doc__[] = |
| 1226 | "unlink(path) -> None\n\ |
| 1227 | Remove a file (same as remove(path))."; |
| 1228 | |
| 1229 | static char posix_remove__doc__[] = |
| 1230 | "remove(path) -> None\n\ |
| 1231 | Remove a file (same as unlink(path))."; |
| 1232 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1234 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1235 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1236 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1240 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1241 | static char posix_uname__doc__[] = |
| 1242 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1243 | Return a tuple identifying the current operating system."; |
| 1244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1246 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1247 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1248 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1249 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1250 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1251 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1252 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1253 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1254 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1255 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1256 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1257 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1258 | u.sysname, |
| 1259 | u.nodename, |
| 1260 | u.release, |
| 1261 | u.version, |
| 1262 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1263 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1264 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1266 | |
| 1267 | static char posix_utime__doc__[] = |
| 1268 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1269 | utime(path, None) -> None\n\ |
| 1270 | Set the access and modified time of the file to the given values. If the\n\ |
| 1271 | 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] | 1272 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1274 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1275 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1276 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1277 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1278 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1279 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1280 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1281 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1282 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1283 | struct utimbuf buf; |
| 1284 | #define ATIME buf.actime |
| 1285 | #define MTIME buf.modtime |
| 1286 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1287 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1288 | time_t buf[2]; |
| 1289 | #define ATIME buf[0] |
| 1290 | #define MTIME buf[1] |
| 1291 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1292 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1293 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1294 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1295 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1296 | if (arg == Py_None) { |
| 1297 | /* optional time values not given */ |
| 1298 | Py_BEGIN_ALLOW_THREADS |
| 1299 | res = utime(path, NULL); |
| 1300 | Py_END_ALLOW_THREADS |
| 1301 | } |
| 1302 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1303 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1304 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1305 | return NULL; |
| 1306 | } |
| 1307 | else { |
| 1308 | ATIME = atime; |
| 1309 | MTIME = mtime; |
| 1310 | Py_BEGIN_ALLOW_THREADS |
| 1311 | res = utime(path, UTIME_ARG); |
| 1312 | Py_END_ALLOW_THREADS |
| 1313 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1314 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1315 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1316 | Py_INCREF(Py_None); |
| 1317 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1318 | #undef UTIME_ARG |
| 1319 | #undef ATIME |
| 1320 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1323 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1324 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1326 | static char posix__exit__doc__[] = |
| 1327 | "_exit(status)\n\ |
| 1328 | Exit to the system with specified status, without normal exit processing."; |
| 1329 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1330 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1331 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1332 | { |
| 1333 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1334 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1335 | return NULL; |
| 1336 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1337 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1340 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1341 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1342 | static char posix_execv__doc__[] = |
| 1343 | "execv(path, args)\n\ |
| 1344 | Execute an executable path with arguments, replacing current process.\n\ |
| 1345 | \n\ |
| 1346 | path: path of executable file\n\ |
| 1347 | args: tuple or list of strings"; |
| 1348 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1349 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1350 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1351 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1352 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1353 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1354 | char **argvlist; |
| 1355 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1356 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1357 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1358 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1359 | argv is a list or tuple of strings. */ |
| 1360 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1361 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1362 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1363 | if (PyList_Check(argv)) { |
| 1364 | argc = PyList_Size(argv); |
| 1365 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1366 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1367 | else if (PyTuple_Check(argv)) { |
| 1368 | argc = PyTuple_Size(argv); |
| 1369 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1370 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1371 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1372 | 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] | 1373 | return NULL; |
| 1374 | } |
| 1375 | |
| 1376 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1377 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1380 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1381 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1382 | if (argvlist == NULL) |
| 1383 | return NULL; |
| 1384 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1385 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1386 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1387 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1388 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1389 | return NULL; |
| 1390 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1391 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1392 | } |
| 1393 | argvlist[argc] = NULL; |
| 1394 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1395 | #ifdef BAD_EXEC_PROTOTYPES |
| 1396 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1397 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1398 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1399 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1400 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1401 | /* If we get here it's definitely an error */ |
| 1402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1403 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1404 | return posix_error(); |
| 1405 | } |
| 1406 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1407 | |
| 1408 | static char posix_execve__doc__[] = |
| 1409 | "execve(path, args, env)\n\ |
| 1410 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1411 | \n\ |
| 1412 | path: path of executable file\n\ |
| 1413 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1414 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1415 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1416 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1417 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1418 | { |
| 1419 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1420 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1421 | char **argvlist; |
| 1422 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1423 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1424 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1425 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1426 | |
| 1427 | /* execve has three arguments: (path, argv, env), where |
| 1428 | argv is a list or tuple of strings and env is a dictionary |
| 1429 | like posix.environ. */ |
| 1430 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1431 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1432 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1433 | if (PyList_Check(argv)) { |
| 1434 | argc = PyList_Size(argv); |
| 1435 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1436 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1437 | else if (PyTuple_Check(argv)) { |
| 1438 | argc = PyTuple_Size(argv); |
| 1439 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1440 | } |
| 1441 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1442 | 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] | 1443 | return NULL; |
| 1444 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1445 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1446 | 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] | 1447 | return NULL; |
| 1448 | } |
| 1449 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1450 | if (argc == 0) { |
| 1451 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1452 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1456 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1457 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1458 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1459 | return NULL; |
| 1460 | } |
| 1461 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1462 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1463 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1464 | &argvlist[i])) |
| 1465 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1466 | goto fail_1; |
| 1467 | } |
| 1468 | } |
| 1469 | argvlist[argc] = NULL; |
| 1470 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1471 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1472 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1473 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1474 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1475 | goto fail_1; |
| 1476 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1477 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1478 | keys = PyMapping_Keys(env); |
| 1479 | vals = PyMapping_Values(env); |
| 1480 | if (!keys || !vals) |
| 1481 | goto fail_2; |
| 1482 | |
| 1483 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1484 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1485 | |
| 1486 | key = PyList_GetItem(keys, pos); |
| 1487 | val = PyList_GetItem(vals, pos); |
| 1488 | if (!key || !val) |
| 1489 | goto fail_2; |
| 1490 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1491 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1492 | !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] | 1493 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1494 | goto fail_2; |
| 1495 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1496 | |
| 1497 | #if defined(PYOS_OS2) |
| 1498 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1499 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1500 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1501 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1502 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1503 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1504 | goto fail_2; |
| 1505 | } |
| 1506 | sprintf(p, "%s=%s", k, v); |
| 1507 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1508 | #if defined(PYOS_OS2) |
| 1509 | } |
| 1510 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1511 | } |
| 1512 | envlist[envc] = 0; |
| 1513 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1514 | |
| 1515 | #ifdef BAD_EXEC_PROTOTYPES |
| 1516 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1517 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1518 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1519 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1520 | |
| 1521 | /* If we get here it's definitely an error */ |
| 1522 | |
| 1523 | (void) posix_error(); |
| 1524 | |
| 1525 | fail_2: |
| 1526 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1527 | PyMem_DEL(envlist[envc]); |
| 1528 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1529 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1530 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1531 | Py_XDECREF(vals); |
| 1532 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1533 | return NULL; |
| 1534 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1535 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1536 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1537 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1538 | #ifdef HAVE_SPAWNV |
| 1539 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1540 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1541 | Execute an executable path with arguments, replacing current process.\n\ |
| 1542 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1543 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1544 | path: path of executable file\n\ |
| 1545 | args: tuple or list of strings"; |
| 1546 | |
| 1547 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1548 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1549 | { |
| 1550 | char *path; |
| 1551 | PyObject *argv; |
| 1552 | char **argvlist; |
| 1553 | int mode, i, argc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1554 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1555 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1556 | |
| 1557 | /* spawnv has three arguments: (mode, path, argv), where |
| 1558 | argv is a list or tuple of strings. */ |
| 1559 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1560 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1561 | return NULL; |
| 1562 | if (PyList_Check(argv)) { |
| 1563 | argc = PyList_Size(argv); |
| 1564 | getitem = PyList_GetItem; |
| 1565 | } |
| 1566 | else if (PyTuple_Check(argv)) { |
| 1567 | argc = PyTuple_Size(argv); |
| 1568 | getitem = PyTuple_GetItem; |
| 1569 | } |
| 1570 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1571 | PyErr_SetString(PyExc_TypeError, "spawmv() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1572 | return NULL; |
| 1573 | } |
| 1574 | |
| 1575 | argvlist = PyMem_NEW(char *, argc+1); |
| 1576 | if (argvlist == NULL) |
| 1577 | return NULL; |
| 1578 | for (i = 0; i < argc; i++) { |
| 1579 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1580 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1581 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1582 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1583 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | argvlist[argc] = NULL; |
| 1587 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1588 | if (mode == _OLD_P_OVERLAY) |
| 1589 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1590 | spawnval = _spawnv(mode, path, argvlist); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1591 | |
| 1592 | PyMem_DEL(argvlist); |
| 1593 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1594 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1595 | return posix_error(); |
| 1596 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1597 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1598 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1599 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1600 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1601 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1606 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1607 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1608 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1609 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1610 | path: path of executable file\n\ |
| 1611 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1612 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1613 | |
| 1614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1615 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1616 | { |
| 1617 | char *path; |
| 1618 | PyObject *argv, *env; |
| 1619 | char **argvlist; |
| 1620 | char **envlist; |
| 1621 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1622 | int mode, i, pos, argc, envc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1623 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1624 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1625 | |
| 1626 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1627 | argv is a list or tuple of strings and env is a dictionary |
| 1628 | like posix.environ. */ |
| 1629 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1630 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1631 | return NULL; |
| 1632 | if (PyList_Check(argv)) { |
| 1633 | argc = PyList_Size(argv); |
| 1634 | getitem = PyList_GetItem; |
| 1635 | } |
| 1636 | else if (PyTuple_Check(argv)) { |
| 1637 | argc = PyTuple_Size(argv); |
| 1638 | getitem = PyTuple_GetItem; |
| 1639 | } |
| 1640 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1641 | 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] | 1642 | return NULL; |
| 1643 | } |
| 1644 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1645 | 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] | 1646 | return NULL; |
| 1647 | } |
| 1648 | |
| 1649 | argvlist = PyMem_NEW(char *, argc+1); |
| 1650 | if (argvlist == NULL) { |
| 1651 | PyErr_NoMemory(); |
| 1652 | return NULL; |
| 1653 | } |
| 1654 | for (i = 0; i < argc; i++) { |
| 1655 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1656 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1657 | &argvlist[i])) |
| 1658 | { |
| 1659 | goto fail_1; |
| 1660 | } |
| 1661 | } |
| 1662 | argvlist[argc] = NULL; |
| 1663 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1664 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1665 | envlist = PyMem_NEW(char *, i + 1); |
| 1666 | if (envlist == NULL) { |
| 1667 | PyErr_NoMemory(); |
| 1668 | goto fail_1; |
| 1669 | } |
| 1670 | envc = 0; |
| 1671 | keys = PyMapping_Keys(env); |
| 1672 | vals = PyMapping_Values(env); |
| 1673 | if (!keys || !vals) |
| 1674 | goto fail_2; |
| 1675 | |
| 1676 | for (pos = 0; pos < i; pos++) { |
| 1677 | char *p, *k, *v; |
| 1678 | |
| 1679 | key = PyList_GetItem(keys, pos); |
| 1680 | val = PyList_GetItem(vals, pos); |
| 1681 | if (!key || !val) |
| 1682 | goto fail_2; |
| 1683 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1684 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1685 | !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] | 1686 | { |
| 1687 | goto fail_2; |
| 1688 | } |
| 1689 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1690 | if (p == NULL) { |
| 1691 | PyErr_NoMemory(); |
| 1692 | goto fail_2; |
| 1693 | } |
| 1694 | sprintf(p, "%s=%s", k, v); |
| 1695 | envlist[envc++] = p; |
| 1696 | } |
| 1697 | envlist[envc] = 0; |
| 1698 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1699 | if (mode == _OLD_P_OVERLAY) |
| 1700 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1701 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 1702 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1703 | (void) posix_error(); |
| 1704 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1705 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1706 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1707 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1708 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1709 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1710 | |
| 1711 | fail_2: |
| 1712 | while (--envc >= 0) |
| 1713 | PyMem_DEL(envlist[envc]); |
| 1714 | PyMem_DEL(envlist); |
| 1715 | fail_1: |
| 1716 | PyMem_DEL(argvlist); |
| 1717 | Py_XDECREF(vals); |
| 1718 | Py_XDECREF(keys); |
| 1719 | return res; |
| 1720 | } |
| 1721 | #endif /* HAVE_SPAWNV */ |
| 1722 | |
| 1723 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1724 | #ifdef HAVE_FORK1 |
| 1725 | static char posix_fork1__doc__[] = |
| 1726 | "fork1() -> pid\n\ |
| 1727 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1728 | \n\ |
| 1729 | Return 0 to child process and PID of child to parent process."; |
| 1730 | |
| 1731 | static PyObject * |
| 1732 | posix_fork1(self, args) |
| 1733 | PyObject *self; |
| 1734 | PyObject *args; |
| 1735 | { |
| 1736 | int pid; |
| 1737 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1738 | return NULL; |
| 1739 | pid = fork1(); |
| 1740 | if (pid == -1) |
| 1741 | return posix_error(); |
| 1742 | PyOS_AfterFork(); |
| 1743 | return PyInt_FromLong((long)pid); |
| 1744 | } |
| 1745 | #endif |
| 1746 | |
| 1747 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1749 | static char posix_fork__doc__[] = |
| 1750 | "fork() -> pid\n\ |
| 1751 | Fork a child process.\n\ |
| 1752 | \n\ |
| 1753 | Return 0 to child process and PID of child to parent process."; |
| 1754 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1755 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1756 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1757 | { |
| 1758 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1759 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1760 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1761 | pid = fork(); |
| 1762 | if (pid == -1) |
| 1763 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1764 | if (pid == 0) |
| 1765 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1766 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1767 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1768 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1769 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1770 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1771 | #ifdef HAVE_PTY_H |
| 1772 | #include <pty.h> |
| 1773 | #else |
| 1774 | #ifdef HAVE_LIBUTIL_H |
| 1775 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1776 | #endif /* HAVE_LIBUTIL_H */ |
| 1777 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1778 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1779 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1780 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1781 | static char posix_openpty__doc__[] = |
| 1782 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1783 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1784 | |
| 1785 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1786 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1787 | { |
| 1788 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1789 | #ifndef HAVE_OPENPTY |
| 1790 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1791 | #endif |
| 1792 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1793 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1794 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1795 | |
| 1796 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1797 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1798 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1799 | #else |
| 1800 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1801 | if (slave_name == NULL) |
| 1802 | return posix_error(); |
| 1803 | |
| 1804 | slave_fd = open(slave_name, O_RDWR); |
| 1805 | if (slave_fd < 0) |
| 1806 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1807 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1808 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1809 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1810 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1811 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1812 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1813 | |
| 1814 | #ifdef HAVE_FORKPTY |
| 1815 | static char posix_forkpty__doc__[] = |
| 1816 | "forkpty() -> (pid, master_fd)\n\ |
| 1817 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1818 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1819 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1820 | |
| 1821 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1822 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1823 | { |
| 1824 | int master_fd, pid; |
| 1825 | |
| 1826 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1827 | return NULL; |
| 1828 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1829 | if (pid == -1) |
| 1830 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1831 | if (pid == 0) |
| 1832 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1833 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1834 | } |
| 1835 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1836 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1837 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 | static char posix_getegid__doc__[] = |
| 1839 | "getegid() -> egid\n\ |
| 1840 | Return the current process's effective group id."; |
| 1841 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1842 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1843 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1844 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1845 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1846 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1847 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1848 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1849 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1850 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1851 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1852 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1853 | static char posix_geteuid__doc__[] = |
| 1854 | "geteuid() -> euid\n\ |
| 1855 | Return the current process's effective user id."; |
| 1856 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1858 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1859 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1860 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1861 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1862 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1863 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1864 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1865 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1866 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1867 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1868 | static char posix_getgid__doc__[] = |
| 1869 | "getgid() -> gid\n\ |
| 1870 | Return the current process's group id."; |
| 1871 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1872 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1873 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1874 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1875 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1876 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1877 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1878 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1879 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1880 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1881 | |
| 1882 | static char posix_getpid__doc__[] = |
| 1883 | "getpid() -> pid\n\ |
| 1884 | Return the current process id"; |
| 1885 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1886 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1887 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1888 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1889 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1890 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1891 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1894 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1895 | #ifdef HAVE_GETGROUPS |
| 1896 | static char posix_getgroups__doc__[] = "\ |
| 1897 | getgroups() -> list of group IDs\n\ |
| 1898 | Return list of supplemental group IDs for the process."; |
| 1899 | |
| 1900 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1901 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1902 | { |
| 1903 | PyObject *result = NULL; |
| 1904 | |
| 1905 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 1906 | #ifdef NGROUPS_MAX |
| 1907 | #define MAX_GROUPS NGROUPS_MAX |
| 1908 | #else |
| 1909 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 1910 | #define MAX_GROUPS 64 |
| 1911 | #endif |
| 1912 | gid_t grouplist[MAX_GROUPS]; |
| 1913 | int n; |
| 1914 | |
| 1915 | n = getgroups(MAX_GROUPS, grouplist); |
| 1916 | if (n < 0) |
| 1917 | posix_error(); |
| 1918 | else { |
| 1919 | result = PyList_New(n); |
| 1920 | if (result != NULL) { |
| 1921 | PyObject *o; |
| 1922 | int i; |
| 1923 | for (i = 0; i < n; ++i) { |
| 1924 | o = PyInt_FromLong((long)grouplist[i]); |
| 1925 | if (o == NULL) { |
| 1926 | Py_DECREF(result); |
| 1927 | result = NULL; |
| 1928 | break; |
| 1929 | } |
| 1930 | PyList_SET_ITEM(result, i, o); |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | return result; |
| 1936 | } |
| 1937 | #endif |
| 1938 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1939 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1940 | static char posix_getpgrp__doc__[] = |
| 1941 | "getpgrp() -> pgrp\n\ |
| 1942 | Return the current process group id."; |
| 1943 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1944 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1945 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1946 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1947 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1948 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1949 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1950 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1951 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1952 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1953 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1954 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1955 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1956 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1957 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1958 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1959 | static char posix_setpgrp__doc__[] = |
| 1960 | "setpgrp() -> None\n\ |
| 1961 | Make this process a session leader."; |
| 1962 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1963 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1964 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1965 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1966 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1967 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1968 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1969 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1970 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1971 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1972 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1973 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1974 | Py_INCREF(Py_None); |
| 1975 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1978 | #endif /* HAVE_SETPGRP */ |
| 1979 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1980 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | static char posix_getppid__doc__[] = |
| 1982 | "getppid() -> ppid\n\ |
| 1983 | Return the parent's process id."; |
| 1984 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1985 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1986 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1987 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1988 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1989 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1990 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1991 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1992 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1993 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1994 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1995 | #ifdef HAVE_GETLOGIN |
| 1996 | static char posix_getlogin__doc__[] = "\ |
| 1997 | getlogin() -> string\n\ |
| 1998 | Return the actual login name."; |
| 1999 | |
| 2000 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2001 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2002 | { |
| 2003 | PyObject *result = NULL; |
| 2004 | |
| 2005 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2006 | char *name; |
| 2007 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2008 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2009 | errno = 0; |
| 2010 | name = getlogin(); |
| 2011 | if (name == NULL) { |
| 2012 | if (errno) |
| 2013 | posix_error(); |
| 2014 | else |
| 2015 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2016 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2017 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2018 | else |
| 2019 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2020 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2021 | } |
| 2022 | return result; |
| 2023 | } |
| 2024 | #endif |
| 2025 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2026 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2027 | static char posix_getuid__doc__[] = |
| 2028 | "getuid() -> uid\n\ |
| 2029 | Return the current process's user id."; |
| 2030 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2031 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2032 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2033 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2034 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2035 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2036 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2037 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2038 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2039 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2040 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2041 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2042 | static char posix_kill__doc__[] = |
| 2043 | "kill(pid, sig) -> None\n\ |
| 2044 | Kill a process with a signal."; |
| 2045 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2046 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2047 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2048 | { |
| 2049 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2050 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2051 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2052 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2053 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2054 | APIRET rc; |
| 2055 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2056 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2057 | |
| 2058 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2059 | APIRET rc; |
| 2060 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2061 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2062 | |
| 2063 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2064 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2065 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2066 | if (kill(pid, sig) == -1) |
| 2067 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2068 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2069 | Py_INCREF(Py_None); |
| 2070 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2071 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2072 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2073 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2074 | #ifdef HAVE_PLOCK |
| 2075 | |
| 2076 | #ifdef HAVE_SYS_LOCK_H |
| 2077 | #include <sys/lock.h> |
| 2078 | #endif |
| 2079 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2080 | static char posix_plock__doc__[] = |
| 2081 | "plock(op) -> None\n\ |
| 2082 | Lock program segments into memory."; |
| 2083 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2084 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2085 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2086 | { |
| 2087 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2088 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2089 | return NULL; |
| 2090 | if (plock(op) == -1) |
| 2091 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2092 | Py_INCREF(Py_None); |
| 2093 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2094 | } |
| 2095 | #endif |
| 2096 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2097 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2098 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2099 | static char posix_popen__doc__[] = |
| 2100 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2101 | Open a pipe to/from a command returning a file object."; |
| 2102 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2103 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2104 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2105 | async_system(const char *command) |
| 2106 | { |
| 2107 | char *p, errormsg[256], args[1024]; |
| 2108 | RESULTCODES rcodes; |
| 2109 | APIRET rc; |
| 2110 | char *shell = getenv("COMSPEC"); |
| 2111 | if (!shell) |
| 2112 | shell = "cmd"; |
| 2113 | |
| 2114 | strcpy(args, shell); |
| 2115 | p = &args[ strlen(args)+1 ]; |
| 2116 | strcpy(p, "/c "); |
| 2117 | strcat(p, command); |
| 2118 | p += strlen(p) + 1; |
| 2119 | *p = '\0'; |
| 2120 | |
| 2121 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2122 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2123 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2124 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2125 | &rcodes, shell); |
| 2126 | return rc; |
| 2127 | } |
| 2128 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2129 | static FILE * |
| 2130 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2131 | { |
| 2132 | HFILE rhan, whan; |
| 2133 | FILE *retfd = NULL; |
| 2134 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2135 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2136 | if (rc != NO_ERROR) { |
| 2137 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2138 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2139 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2140 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2141 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2142 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2144 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2145 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2147 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2148 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2151 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2154 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2155 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2156 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2157 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2158 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2160 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2161 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2162 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2163 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2164 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2165 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2166 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2167 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2170 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2173 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2174 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2175 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2176 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2177 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2178 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2179 | } else { |
| 2180 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2181 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2186 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2187 | { |
| 2188 | char *name; |
| 2189 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2190 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2191 | FILE *fp; |
| 2192 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2193 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2194 | return NULL; |
| 2195 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2196 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2197 | Py_END_ALLOW_THREADS |
| 2198 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2199 | return os2_error(err); |
| 2200 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2201 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2202 | if (f != NULL) |
| 2203 | PyFile_SetBufSize(f, bufsize); |
| 2204 | return f; |
| 2205 | } |
| 2206 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2207 | #elif defined(MS_WIN32) |
| 2208 | |
| 2209 | /* |
| 2210 | * Portable 'popen' replacement for Win32. |
| 2211 | * |
| 2212 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2213 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2214 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2215 | */ |
| 2216 | |
| 2217 | #include <malloc.h> |
| 2218 | #include <io.h> |
| 2219 | #include <fcntl.h> |
| 2220 | |
| 2221 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2222 | #define POPEN_1 1 |
| 2223 | #define POPEN_2 2 |
| 2224 | #define POPEN_3 3 |
| 2225 | #define POPEN_4 4 |
| 2226 | |
| 2227 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2228 | static int _PyPclose(FILE *file); |
| 2229 | |
| 2230 | /* |
| 2231 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2232 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2233 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2234 | */ |
| 2235 | static PyObject *_PyPopenProcs = NULL; |
| 2236 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2237 | |
| 2238 | /* popen that works from a GUI. |
| 2239 | * |
| 2240 | * The result of this function is a pipe (file) connected to the |
| 2241 | * processes stdin or stdout, depending on the requested mode. |
| 2242 | */ |
| 2243 | |
| 2244 | static PyObject * |
| 2245 | posix_popen(PyObject *self, PyObject *args) |
| 2246 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2247 | PyObject *f, *s; |
| 2248 | int tm = 0; |
| 2249 | |
| 2250 | char *cmdstring; |
| 2251 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2252 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2253 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2254 | return NULL; |
| 2255 | |
| 2256 | s = PyTuple_New(0); |
| 2257 | |
| 2258 | if (*mode == 'r') |
| 2259 | tm = _O_RDONLY; |
| 2260 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2261 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } else |
| 2264 | tm = _O_WRONLY; |
| 2265 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2266 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2267 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2268 | return NULL; |
| 2269 | } |
| 2270 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2271 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2272 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2273 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2274 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2275 | else |
| 2276 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2277 | |
| 2278 | return f; |
| 2279 | } |
| 2280 | |
| 2281 | /* Variation on win32pipe.popen |
| 2282 | * |
| 2283 | * The result of this function is a pipe (file) connected to the |
| 2284 | * process's stdin, and a pipe connected to the process's stdout. |
| 2285 | */ |
| 2286 | |
| 2287 | static PyObject * |
| 2288 | win32_popen2(PyObject *self, PyObject *args) |
| 2289 | { |
| 2290 | PyObject *f; |
| 2291 | int tm=0; |
| 2292 | |
| 2293 | char *cmdstring; |
| 2294 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2295 | int bufsize = -1; |
| 2296 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2297 | return NULL; |
| 2298 | |
| 2299 | if (*mode == 't') |
| 2300 | tm = _O_TEXT; |
| 2301 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2302 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2303 | return NULL; |
| 2304 | } else |
| 2305 | tm = _O_BINARY; |
| 2306 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2307 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2308 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2309 | return NULL; |
| 2310 | } |
| 2311 | |
| 2312 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2313 | |
| 2314 | return f; |
| 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2319 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2320 | * The result of this function is 3 pipes - the process's stdin, |
| 2321 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2322 | */ |
| 2323 | |
| 2324 | static PyObject * |
| 2325 | win32_popen3(PyObject *self, PyObject *args) |
| 2326 | { |
| 2327 | PyObject *f; |
| 2328 | int tm = 0; |
| 2329 | |
| 2330 | char *cmdstring; |
| 2331 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2332 | int bufsize = -1; |
| 2333 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2334 | return NULL; |
| 2335 | |
| 2336 | if (*mode == 't') |
| 2337 | tm = _O_TEXT; |
| 2338 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2339 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2340 | return NULL; |
| 2341 | } else |
| 2342 | tm = _O_BINARY; |
| 2343 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2344 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2345 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2346 | return NULL; |
| 2347 | } |
| 2348 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2349 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2350 | |
| 2351 | return f; |
| 2352 | } |
| 2353 | |
| 2354 | /* |
| 2355 | * Variation on win32pipe.popen |
| 2356 | * |
| 2357 | * The result of this function is 2 pipes - the processes stdin, |
| 2358 | * and stdout+stderr combined as a single pipe. |
| 2359 | */ |
| 2360 | |
| 2361 | static PyObject * |
| 2362 | win32_popen4(PyObject *self, PyObject *args) |
| 2363 | { |
| 2364 | PyObject *f; |
| 2365 | int tm = 0; |
| 2366 | |
| 2367 | char *cmdstring; |
| 2368 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2369 | int bufsize = -1; |
| 2370 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2371 | return NULL; |
| 2372 | |
| 2373 | if (*mode == 't') |
| 2374 | tm = _O_TEXT; |
| 2375 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2376 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2377 | return NULL; |
| 2378 | } else |
| 2379 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2380 | |
| 2381 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2382 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2383 | return NULL; |
| 2384 | } |
| 2385 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2386 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2387 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2388 | return f; |
| 2389 | } |
| 2390 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2391 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2392 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2393 | HANDLE hStdin, |
| 2394 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2395 | HANDLE hStderr, |
| 2396 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2397 | { |
| 2398 | PROCESS_INFORMATION piProcInfo; |
| 2399 | STARTUPINFO siStartInfo; |
| 2400 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2401 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2402 | int i; |
| 2403 | int x; |
| 2404 | |
| 2405 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 2406 | s1 = (char *)_alloca(i); |
| 2407 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2408 | return x; |
| 2409 | if (GetVersion() < 0x80000000) { |
| 2410 | /* |
| 2411 | * NT/2000 |
| 2412 | */ |
| 2413 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2414 | s2 = (char *)_alloca(x); |
| 2415 | ZeroMemory(s2, x); |
| 2416 | sprintf(s2, "%s%s%s", s1, s3, cmdstring); |
| 2417 | } |
| 2418 | else { |
| 2419 | /* |
| 2420 | * Oh gag, we're on Win9x. Use the workaround listed in |
| 2421 | * KB: Q150956 |
| 2422 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2423 | char modulepath[_MAX_PATH]; |
| 2424 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2425 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2426 | for (i = x = 0; modulepath[i]; i++) |
| 2427 | if (modulepath[i] == '\\') |
| 2428 | x = i+1; |
| 2429 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2430 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 2431 | strncat(modulepath, |
| 2432 | szConsoleSpawn, |
| 2433 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2434 | -strlen(modulepath)); |
| 2435 | if (stat(modulepath, &statinfo) != 0) { |
| 2436 | /* Eeek - file-not-found - possibly an embedding |
| 2437 | situation - see if we can locate it in sys.prefix |
| 2438 | */ |
| 2439 | strncpy(modulepath, |
| 2440 | Py_GetExecPrefix(), |
| 2441 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 2442 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 2443 | strcat(modulepath, "\\"); |
| 2444 | strncat(modulepath, |
| 2445 | szConsoleSpawn, |
| 2446 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2447 | -strlen(modulepath)); |
| 2448 | /* No where else to look - raise an easily identifiable |
| 2449 | error, rather than leaving Windows to report |
| 2450 | "file not found" - as the user is probably blissfully |
| 2451 | unaware this shim EXE is used, and it will confuse them. |
| 2452 | (well, it confused me for a while ;-) |
| 2453 | */ |
| 2454 | if (stat(modulepath, &statinfo) != 0) { |
| 2455 | PyErr_Format(PyExc_RuntimeError, |
| 2456 | "Can not locate '%s' which is needed " |
| 2457 | "for popen to work on this platform.", |
| 2458 | szConsoleSpawn); |
| 2459 | return FALSE; |
| 2460 | } |
| 2461 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2462 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2463 | strlen(modulepath) + |
| 2464 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2465 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2466 | s2 = (char *)_alloca(x); |
| 2467 | ZeroMemory(s2, x); |
| 2468 | sprintf( |
| 2469 | s2, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2470 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2471 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2472 | s1, |
| 2473 | s3, |
| 2474 | cmdstring); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2479 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2480 | else { |
| 2481 | PyErr_SetString(PyExc_RuntimeError, "Can not locate a COMSPEC environment variable to use as the shell"); |
| 2482 | return FALSE; |
| 2483 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2484 | |
| 2485 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2486 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2487 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2488 | siStartInfo.hStdInput = hStdin; |
| 2489 | siStartInfo.hStdOutput = hStdout; |
| 2490 | siStartInfo.hStdError = hStderr; |
| 2491 | siStartInfo.wShowWindow = SW_HIDE; |
| 2492 | |
| 2493 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2494 | s2, |
| 2495 | NULL, |
| 2496 | NULL, |
| 2497 | TRUE, |
| 2498 | CREATE_NEW_CONSOLE, |
| 2499 | NULL, |
| 2500 | NULL, |
| 2501 | &siStartInfo, |
| 2502 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2503 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2504 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2505 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2506 | /* Return process handle */ |
| 2507 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2508 | return TRUE; |
| 2509 | } |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2510 | win32_error("CreateProcess", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2511 | return FALSE; |
| 2512 | } |
| 2513 | |
| 2514 | /* The following code is based off of KB: Q190351 */ |
| 2515 | |
| 2516 | static PyObject * |
| 2517 | _PyPopen(char *cmdstring, int mode, int n) |
| 2518 | { |
| 2519 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2520 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2521 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2522 | |
| 2523 | SECURITY_ATTRIBUTES saAttr; |
| 2524 | BOOL fSuccess; |
| 2525 | int fd1, fd2, fd3; |
| 2526 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2527 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2528 | PyObject *f; |
| 2529 | |
| 2530 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2531 | saAttr.bInheritHandle = TRUE; |
| 2532 | saAttr.lpSecurityDescriptor = NULL; |
| 2533 | |
| 2534 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2535 | return win32_error("CreatePipe", NULL); |
| 2536 | |
| 2537 | /* Create new output read handle and the input write handle. Set |
| 2538 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2539 | * the these handles; resulting in non-closeable handles to the pipes |
| 2540 | * being created. */ |
| 2541 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2542 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2543 | FALSE, |
| 2544 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2545 | if (!fSuccess) |
| 2546 | return win32_error("DuplicateHandle", NULL); |
| 2547 | |
| 2548 | /* Close the inheritable version of ChildStdin |
| 2549 | that we're using. */ |
| 2550 | CloseHandle(hChildStdinWr); |
| 2551 | |
| 2552 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2553 | return win32_error("CreatePipe", NULL); |
| 2554 | |
| 2555 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2556 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2557 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2558 | if (!fSuccess) |
| 2559 | return win32_error("DuplicateHandle", NULL); |
| 2560 | |
| 2561 | /* Close the inheritable version of ChildStdout |
| 2562 | that we're using. */ |
| 2563 | CloseHandle(hChildStdoutRd); |
| 2564 | |
| 2565 | if (n != POPEN_4) { |
| 2566 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2567 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2568 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2569 | hChildStderrRd, |
| 2570 | GetCurrentProcess(), |
| 2571 | &hChildStderrRdDup, 0, |
| 2572 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2573 | if (!fSuccess) |
| 2574 | return win32_error("DuplicateHandle", NULL); |
| 2575 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2576 | CloseHandle(hChildStderrRd); |
| 2577 | } |
| 2578 | |
| 2579 | switch (n) { |
| 2580 | case POPEN_1: |
| 2581 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2582 | case _O_WRONLY | _O_TEXT: |
| 2583 | /* Case for writing to child Stdin in text mode. */ |
| 2584 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2585 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2586 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2587 | PyFile_SetBufSize(f, 0); |
| 2588 | /* We don't care about these pipes anymore, so close them. */ |
| 2589 | CloseHandle(hChildStdoutRdDup); |
| 2590 | CloseHandle(hChildStderrRdDup); |
| 2591 | break; |
| 2592 | |
| 2593 | case _O_RDONLY | _O_TEXT: |
| 2594 | /* Case for reading from child Stdout in text mode. */ |
| 2595 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2596 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2597 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2598 | PyFile_SetBufSize(f, 0); |
| 2599 | /* We don't care about these pipes anymore, so close them. */ |
| 2600 | CloseHandle(hChildStdinWrDup); |
| 2601 | CloseHandle(hChildStderrRdDup); |
| 2602 | break; |
| 2603 | |
| 2604 | case _O_RDONLY | _O_BINARY: |
| 2605 | /* Case for readinig from child Stdout in binary mode. */ |
| 2606 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2607 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2608 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2609 | PyFile_SetBufSize(f, 0); |
| 2610 | /* We don't care about these pipes anymore, so close them. */ |
| 2611 | CloseHandle(hChildStdinWrDup); |
| 2612 | CloseHandle(hChildStderrRdDup); |
| 2613 | break; |
| 2614 | |
| 2615 | case _O_WRONLY | _O_BINARY: |
| 2616 | /* Case for writing to child Stdin in binary mode. */ |
| 2617 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2618 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2619 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2620 | PyFile_SetBufSize(f, 0); |
| 2621 | /* We don't care about these pipes anymore, so close them. */ |
| 2622 | CloseHandle(hChildStdoutRdDup); |
| 2623 | CloseHandle(hChildStderrRdDup); |
| 2624 | break; |
| 2625 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2626 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2627 | break; |
| 2628 | |
| 2629 | case POPEN_2: |
| 2630 | case POPEN_4: |
| 2631 | { |
| 2632 | char *m1, *m2; |
| 2633 | PyObject *p1, *p2; |
| 2634 | |
| 2635 | if (mode && _O_TEXT) { |
| 2636 | m1 = "r"; |
| 2637 | m2 = "w"; |
| 2638 | } else { |
| 2639 | m1 = "rb"; |
| 2640 | m2 = "wb"; |
| 2641 | } |
| 2642 | |
| 2643 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2644 | f1 = _fdopen(fd1, m2); |
| 2645 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2646 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2647 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2648 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2649 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2650 | PyFile_SetBufSize(p2, 0); |
| 2651 | |
| 2652 | if (n != 4) |
| 2653 | CloseHandle(hChildStderrRdDup); |
| 2654 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2655 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2656 | Py_XDECREF(p1); |
| 2657 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2658 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2659 | break; |
| 2660 | } |
| 2661 | |
| 2662 | case POPEN_3: |
| 2663 | { |
| 2664 | char *m1, *m2; |
| 2665 | PyObject *p1, *p2, *p3; |
| 2666 | |
| 2667 | if (mode && _O_TEXT) { |
| 2668 | m1 = "r"; |
| 2669 | m2 = "w"; |
| 2670 | } else { |
| 2671 | m1 = "rb"; |
| 2672 | m2 = "wb"; |
| 2673 | } |
| 2674 | |
| 2675 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2676 | f1 = _fdopen(fd1, m2); |
| 2677 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2678 | f2 = _fdopen(fd2, m1); |
| 2679 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2680 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2681 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2682 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2683 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2684 | PyFile_SetBufSize(p1, 0); |
| 2685 | PyFile_SetBufSize(p2, 0); |
| 2686 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2687 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2688 | Py_XDECREF(p1); |
| 2689 | Py_XDECREF(p2); |
| 2690 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2691 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2692 | break; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (n == POPEN_4) { |
| 2697 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2698 | hChildStdinRd, |
| 2699 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2700 | hChildStdoutWr, |
| 2701 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2702 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2703 | } |
| 2704 | else { |
| 2705 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2706 | hChildStdinRd, |
| 2707 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2708 | hChildStderrWr, |
| 2709 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2710 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2713 | /* |
| 2714 | * Insert the files we've created into the process dictionary |
| 2715 | * all referencing the list with the process handle and the |
| 2716 | * initial number of files (see description below in _PyPclose). |
| 2717 | * Since if _PyPclose later tried to wait on a process when all |
| 2718 | * handles weren't closed, it could create a deadlock with the |
| 2719 | * child, we spend some energy here to try to ensure that we |
| 2720 | * either insert all file handles into the dictionary or none |
| 2721 | * at all. It's a little clumsy with the various popen modes |
| 2722 | * and variable number of files involved. |
| 2723 | */ |
| 2724 | if (!_PyPopenProcs) { |
| 2725 | _PyPopenProcs = PyDict_New(); |
| 2726 | } |
| 2727 | |
| 2728 | if (_PyPopenProcs) { |
| 2729 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2730 | int ins_rc[3]; |
| 2731 | |
| 2732 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2733 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2734 | |
| 2735 | procObj = PyList_New(2); |
| 2736 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2737 | intObj = PyInt_FromLong(file_count); |
| 2738 | |
| 2739 | if (procObj && hProcessObj && intObj) { |
| 2740 | PyList_SetItem(procObj,0,hProcessObj); |
| 2741 | PyList_SetItem(procObj,1,intObj); |
| 2742 | |
| 2743 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2744 | if (fileObj[0]) { |
| 2745 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2746 | fileObj[0], |
| 2747 | procObj); |
| 2748 | } |
| 2749 | if (file_count >= 2) { |
| 2750 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2751 | if (fileObj[1]) { |
| 2752 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2753 | fileObj[1], |
| 2754 | procObj); |
| 2755 | } |
| 2756 | } |
| 2757 | if (file_count >= 3) { |
| 2758 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2759 | if (fileObj[2]) { |
| 2760 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2761 | fileObj[2], |
| 2762 | procObj); |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2767 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2768 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2769 | /* Something failed - remove any dictionary |
| 2770 | * entries that did make it. |
| 2771 | */ |
| 2772 | if (!ins_rc[0] && fileObj[0]) { |
| 2773 | PyDict_DelItem(_PyPopenProcs, |
| 2774 | fileObj[0]); |
| 2775 | } |
| 2776 | if (!ins_rc[1] && fileObj[1]) { |
| 2777 | PyDict_DelItem(_PyPopenProcs, |
| 2778 | fileObj[1]); |
| 2779 | } |
| 2780 | if (!ins_rc[2] && fileObj[2]) { |
| 2781 | PyDict_DelItem(_PyPopenProcs, |
| 2782 | fileObj[2]); |
| 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | /* |
| 2788 | * Clean up our localized references for the dictionary keys |
| 2789 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2790 | * that got placed in the dictionary. |
| 2791 | */ |
| 2792 | Py_XDECREF(procObj); |
| 2793 | Py_XDECREF(fileObj[0]); |
| 2794 | Py_XDECREF(fileObj[1]); |
| 2795 | Py_XDECREF(fileObj[2]); |
| 2796 | } |
| 2797 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2798 | /* Child is launched. Close the parents copy of those pipe |
| 2799 | * handles that only the child should have open. You need to |
| 2800 | * make sure that no handles to the write end of the output pipe |
| 2801 | * are maintained in this process or else the pipe will not close |
| 2802 | * when the child process exits and the ReadFile will hang. */ |
| 2803 | |
| 2804 | if (!CloseHandle(hChildStdinRd)) |
| 2805 | return win32_error("CloseHandle", NULL); |
| 2806 | |
| 2807 | if (!CloseHandle(hChildStdoutWr)) |
| 2808 | return win32_error("CloseHandle", NULL); |
| 2809 | |
| 2810 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2811 | return win32_error("CloseHandle", NULL); |
| 2812 | |
| 2813 | return f; |
| 2814 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2815 | |
| 2816 | /* |
| 2817 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2818 | * 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] | 2819 | * |
| 2820 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2821 | * input file pointer to information about the process that was |
| 2822 | * originally created by the popen* call that created the file pointer. |
| 2823 | * The dictionary uses the file pointer as a key (with one entry |
| 2824 | * inserted for each file returned by the original popen* call) and a |
| 2825 | * single list object as the value for all files from a single call. |
| 2826 | * The list object contains the Win32 process handle at [0], and a file |
| 2827 | * count at [1], which is initialized to the total number of file |
| 2828 | * handles using that list. |
| 2829 | * |
| 2830 | * This function closes whichever handle it is passed, and decrements |
| 2831 | * the file count in the dictionary for the process handle pointed to |
| 2832 | * by this file. On the last close (when the file count reaches zero), |
| 2833 | * this function will wait for the child process and then return its |
| 2834 | * exit code as the result of the close() operation. This permits the |
| 2835 | * files to be closed in any order - it is always the close() of the |
| 2836 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2837 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2838 | |
| 2839 | /* RED_FLAG 31-Aug-2000 Tim |
| 2840 | * This is always called (today!) between a pair of |
| 2841 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2842 | * macros. So the thread running this has no valid thread state, as |
| 2843 | * far as Python is concerned. However, this calls some Python API |
| 2844 | * functions that cannot be called safely without a valid thread |
| 2845 | * state, in particular PyDict_GetItem. |
| 2846 | * As a temporary hack (although it may last for years ...), we |
| 2847 | * *rely* on not having a valid thread state in this function, in |
| 2848 | * order to create our own "from scratch". |
| 2849 | * This will deadlock if _PyPclose is ever called by a thread |
| 2850 | * holding the global lock. |
| 2851 | */ |
| 2852 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2853 | static int _PyPclose(FILE *file) |
| 2854 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2855 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2856 | DWORD exit_code; |
| 2857 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2858 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 2859 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2860 | #ifdef WITH_THREAD |
| 2861 | PyInterpreterState* pInterpreterState; |
| 2862 | PyThreadState* pThreadState; |
| 2863 | #endif |
| 2864 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2865 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2866 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2867 | */ |
| 2868 | result = fclose(file); |
| 2869 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2870 | #ifdef WITH_THREAD |
| 2871 | /* Bootstrap a valid thread state into existence. */ |
| 2872 | pInterpreterState = PyInterpreterState_New(); |
| 2873 | if (!pInterpreterState) { |
| 2874 | /* Well, we're hosed now! We don't have a thread |
| 2875 | * state, so can't call a nice error routine, or raise |
| 2876 | * an exception. Just die. |
| 2877 | */ |
| 2878 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2879 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2880 | return -1; /* unreachable */ |
| 2881 | } |
| 2882 | pThreadState = PyThreadState_New(pInterpreterState); |
| 2883 | if (!pThreadState) { |
| 2884 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2885 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2886 | return -1; /* unreachable */ |
| 2887 | } |
| 2888 | /* Grab the global lock. Note that this will deadlock if the |
| 2889 | * current thread already has the lock! (see RED_FLAG comments |
| 2890 | * before this function) |
| 2891 | */ |
| 2892 | PyEval_RestoreThread(pThreadState); |
| 2893 | #endif |
| 2894 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2895 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2896 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2897 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2898 | fileObj)) != NULL && |
| 2899 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 2900 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 2901 | |
| 2902 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 2903 | file_count = PyInt_AsLong(intObj); |
| 2904 | |
| 2905 | if (file_count > 1) { |
| 2906 | /* Still other files referencing process */ |
| 2907 | file_count--; |
| 2908 | PyList_SetItem(procObj,1, |
| 2909 | PyInt_FromLong(file_count)); |
| 2910 | } else { |
| 2911 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2912 | if (result != EOF && |
| 2913 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 2914 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2915 | /* Possible truncation here in 16-bit environments, but |
| 2916 | * real exit codes are just the lower byte in any event. |
| 2917 | */ |
| 2918 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2919 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2920 | /* Indicate failure - this will cause the file object |
| 2921 | * to raise an I/O error and translate the last Win32 |
| 2922 | * error code from errno. We do have a problem with |
| 2923 | * last errors that overlap the normal errno table, |
| 2924 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2925 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2926 | if (result != EOF) { |
| 2927 | /* If the error wasn't from the fclose(), then |
| 2928 | * set errno for the file object error handling. |
| 2929 | */ |
| 2930 | errno = GetLastError(); |
| 2931 | } |
| 2932 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | /* Free up the native handle at this point */ |
| 2936 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2937 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2938 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2939 | /* Remove this file pointer from dictionary */ |
| 2940 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2941 | |
| 2942 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 2943 | Py_DECREF(_PyPopenProcs); |
| 2944 | _PyPopenProcs = NULL; |
| 2945 | } |
| 2946 | |
| 2947 | } /* if object retrieval ok */ |
| 2948 | |
| 2949 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2950 | } /* if _PyPopenProcs */ |
| 2951 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2952 | #ifdef WITH_THREAD |
| 2953 | /* Tear down the thread & interpreter states. |
| 2954 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2955 | * call the thread clear & delete functions, and indeed insist on |
| 2956 | * doing that themselves. The lock must be held during the clear, but |
| 2957 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2958 | */ |
| 2959 | PyInterpreterState_Clear(pInterpreterState); |
| 2960 | PyEval_ReleaseThread(pThreadState); |
| 2961 | PyInterpreterState_Delete(pInterpreterState); |
| 2962 | #endif |
| 2963 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2964 | return result; |
| 2965 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2966 | |
| 2967 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2968 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2969 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2970 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2971 | char *name; |
| 2972 | char *mode = "r"; |
| 2973 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2974 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2975 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2976 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2977 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2978 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2979 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2980 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2981 | if (fp == NULL) |
| 2982 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2983 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2984 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2985 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2986 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2987 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2988 | #endif |
| 2989 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2990 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2992 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2993 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2994 | static char posix_setuid__doc__[] = |
| 2995 | "setuid(uid) -> None\n\ |
| 2996 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2997 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2998 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2999 | { |
| 3000 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3001 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3002 | return NULL; |
| 3003 | if (setuid(uid) < 0) |
| 3004 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3005 | Py_INCREF(Py_None); |
| 3006 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3007 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3008 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3009 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3010 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3011 | #ifdef HAVE_SETEUID |
| 3012 | static char posix_seteuid__doc__[] = |
| 3013 | "seteuid(uid) -> None\n\ |
| 3014 | Set the current process's effective user id."; |
| 3015 | static PyObject * |
| 3016 | posix_seteuid (PyObject *self, PyObject *args) |
| 3017 | { |
| 3018 | int euid; |
| 3019 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3020 | return NULL; |
| 3021 | } else if (seteuid(euid) < 0) { |
| 3022 | return posix_error(); |
| 3023 | } else { |
| 3024 | Py_INCREF(Py_None); |
| 3025 | return Py_None; |
| 3026 | } |
| 3027 | } |
| 3028 | #endif /* HAVE_SETEUID */ |
| 3029 | |
| 3030 | #ifdef HAVE_SETEGID |
| 3031 | static char posix_setegid__doc__[] = |
| 3032 | "setegid(gid) -> None\n\ |
| 3033 | Set the current process's effective group id."; |
| 3034 | static PyObject * |
| 3035 | posix_setegid (PyObject *self, PyObject *args) |
| 3036 | { |
| 3037 | int egid; |
| 3038 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3039 | return NULL; |
| 3040 | } else if (setegid(egid) < 0) { |
| 3041 | return posix_error(); |
| 3042 | } else { |
| 3043 | Py_INCREF(Py_None); |
| 3044 | return Py_None; |
| 3045 | } |
| 3046 | } |
| 3047 | #endif /* HAVE_SETEGID */ |
| 3048 | |
| 3049 | #ifdef HAVE_SETREUID |
| 3050 | static char posix_setreuid__doc__[] = |
| 3051 | "seteuid(ruid, euid) -> None\n\ |
| 3052 | Set the current process's real and effective user ids."; |
| 3053 | static PyObject * |
| 3054 | posix_setreuid (PyObject *self, PyObject *args) |
| 3055 | { |
| 3056 | int ruid, euid; |
| 3057 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3058 | return NULL; |
| 3059 | } else if (setreuid(ruid, euid) < 0) { |
| 3060 | return posix_error(); |
| 3061 | } else { |
| 3062 | Py_INCREF(Py_None); |
| 3063 | return Py_None; |
| 3064 | } |
| 3065 | } |
| 3066 | #endif /* HAVE_SETREUID */ |
| 3067 | |
| 3068 | #ifdef HAVE_SETREGID |
| 3069 | static char posix_setregid__doc__[] = |
| 3070 | "setegid(rgid, egid) -> None\n\ |
| 3071 | Set the current process's real and effective group ids."; |
| 3072 | static PyObject * |
| 3073 | posix_setregid (PyObject *self, PyObject *args) |
| 3074 | { |
| 3075 | int rgid, egid; |
| 3076 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3077 | return NULL; |
| 3078 | } else if (setregid(rgid, egid) < 0) { |
| 3079 | return posix_error(); |
| 3080 | } else { |
| 3081 | Py_INCREF(Py_None); |
| 3082 | return Py_None; |
| 3083 | } |
| 3084 | } |
| 3085 | #endif /* HAVE_SETREGID */ |
| 3086 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3087 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3088 | static char posix_setgid__doc__[] = |
| 3089 | "setgid(gid) -> None\n\ |
| 3090 | Set the current process's group id."; |
| 3091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3093 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3094 | { |
| 3095 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3096 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3097 | return NULL; |
| 3098 | if (setgid(gid) < 0) |
| 3099 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3100 | Py_INCREF(Py_None); |
| 3101 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3102 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3103 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3104 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3105 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3106 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3107 | static char posix_waitpid__doc__[] = |
| 3108 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3109 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3110 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3112 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3113 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3114 | int pid, options; |
| 3115 | #ifdef UNION_WAIT |
| 3116 | union wait status; |
| 3117 | #define status_i (status.w_status) |
| 3118 | #else |
| 3119 | int status; |
| 3120 | #define status_i status |
| 3121 | #endif |
| 3122 | status_i = 0; |
| 3123 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3124 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3125 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3126 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3127 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3128 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3129 | #else |
| 3130 | pid = waitpid(pid, &status, options); |
| 3131 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3133 | if (pid == -1) |
| 3134 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3135 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3136 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3137 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3138 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3141 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3142 | static char posix_wait__doc__[] = |
| 3143 | "wait() -> (pid, status)\n\ |
| 3144 | Wait for completion of a child process."; |
| 3145 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3146 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3147 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3148 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3149 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3150 | #ifdef UNION_WAIT |
| 3151 | union wait status; |
| 3152 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3153 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3154 | int status; |
| 3155 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3156 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3157 | if (!PyArg_ParseTuple(args, ":wait")) |
| 3158 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3159 | status_i = 0; |
| 3160 | Py_BEGIN_ALLOW_THREADS |
| 3161 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3162 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3163 | if (pid == -1) |
| 3164 | return posix_error(); |
| 3165 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3166 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3167 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3168 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3169 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3170 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3171 | |
| 3172 | static char posix_lstat__doc__[] = |
| 3173 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3174 | Like stat(path), but do not follow symbolic links."; |
| 3175 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3176 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3177 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3178 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3179 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3180 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3181 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3182 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3183 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3187 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3188 | static char posix_readlink__doc__[] = |
| 3189 | "readlink(path) -> path\n\ |
| 3190 | Return a string representing the path to which the symbolic link points."; |
| 3191 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3192 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3193 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3194 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3195 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3196 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3197 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3198 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3199 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3200 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3201 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3202 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3203 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3204 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3205 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3206 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3207 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3208 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3209 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3210 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3211 | static char posix_symlink__doc__[] = |
| 3212 | "symlink(src, dst) -> None\n\ |
| 3213 | Create a symbolic link."; |
| 3214 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3215 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3216 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3217 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3218 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3219 | } |
| 3220 | #endif /* HAVE_SYMLINK */ |
| 3221 | |
| 3222 | |
| 3223 | #ifdef HAVE_TIMES |
| 3224 | #ifndef HZ |
| 3225 | #define HZ 60 /* Universal constant :-) */ |
| 3226 | #endif /* HZ */ |
| 3227 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3228 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3229 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3230 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3231 | { |
| 3232 | ULONG value = 0; |
| 3233 | |
| 3234 | Py_BEGIN_ALLOW_THREADS |
| 3235 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3236 | Py_END_ALLOW_THREADS |
| 3237 | |
| 3238 | return value; |
| 3239 | } |
| 3240 | |
| 3241 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3242 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3243 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3244 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3245 | return NULL; |
| 3246 | |
| 3247 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3248 | return Py_BuildValue("ddddd", |
| 3249 | (double)0 /* t.tms_utime / HZ */, |
| 3250 | (double)0 /* t.tms_stime / HZ */, |
| 3251 | (double)0 /* t.tms_cutime / HZ */, |
| 3252 | (double)0 /* t.tms_cstime / HZ */, |
| 3253 | (double)system_uptime() / 1000); |
| 3254 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3255 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3256 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3257 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3258 | { |
| 3259 | struct tms t; |
| 3260 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3261 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3262 | return NULL; |
| 3263 | errno = 0; |
| 3264 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3265 | if (c == (clock_t) -1) |
| 3266 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3268 | (double)t.tms_utime / HZ, |
| 3269 | (double)t.tms_stime / HZ, |
| 3270 | (double)t.tms_cutime / HZ, |
| 3271 | (double)t.tms_cstime / HZ, |
| 3272 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3273 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3274 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3275 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3276 | |
| 3277 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3278 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3279 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3280 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3281 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3282 | { |
| 3283 | FILETIME create, exit, kernel, user; |
| 3284 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3285 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3286 | return NULL; |
| 3287 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3288 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3289 | /* The fields of a FILETIME structure are the hi and lo part |
| 3290 | of a 64-bit value expressed in 100 nanosecond units. |
| 3291 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3292 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3293 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3294 | return Py_BuildValue( |
| 3295 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3296 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3297 | kernel.dwLowDateTime*1e-7), |
| 3298 | (double)(user.dwHighDateTime*429.4967296 + |
| 3299 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3300 | (double)0, |
| 3301 | (double)0, |
| 3302 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3303 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3304 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3305 | |
| 3306 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3307 | static char posix_times__doc__[] = |
| 3308 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3309 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3310 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3312 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3313 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3314 | static char posix_setsid__doc__[] = |
| 3315 | "setsid() -> None\n\ |
| 3316 | Call the system call setsid()."; |
| 3317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3318 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3319 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3320 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3321 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3322 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3323 | if (setsid() < 0) |
| 3324 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3325 | Py_INCREF(Py_None); |
| 3326 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3327 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3328 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3329 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3330 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3331 | static char posix_setpgid__doc__[] = |
| 3332 | "setpgid(pid, pgrp) -> None\n\ |
| 3333 | Call the system call setpgid()."; |
| 3334 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3335 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3336 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3337 | { |
| 3338 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3339 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3340 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3341 | if (setpgid(pid, pgrp) < 0) |
| 3342 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3343 | Py_INCREF(Py_None); |
| 3344 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3345 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3346 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3347 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3348 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3349 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3350 | static char posix_tcgetpgrp__doc__[] = |
| 3351 | "tcgetpgrp(fd) -> pgid\n\ |
| 3352 | Return the process group associated with the terminal given by a fd."; |
| 3353 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3354 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3355 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3356 | { |
| 3357 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3358 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3359 | return NULL; |
| 3360 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3361 | if (pgid < 0) |
| 3362 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3363 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3364 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3365 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3366 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3367 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3368 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3369 | static char posix_tcsetpgrp__doc__[] = |
| 3370 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3371 | Set the process group associated with the terminal given by a fd."; |
| 3372 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3373 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3374 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3375 | { |
| 3376 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3377 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3378 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3379 | if (tcsetpgrp(fd, pgid) < 0) |
| 3380 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3381 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3382 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3383 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3384 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3385 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3386 | /* Functions acting on file descriptors */ |
| 3387 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3388 | static char posix_open__doc__[] = |
| 3389 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3390 | Open a file (for low level IO)."; |
| 3391 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3392 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3393 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3394 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3395 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3396 | int flag; |
| 3397 | int mode = 0777; |
| 3398 | int fd; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3399 | if (!PyArg_ParseTuple(args, "eti|i", |
| 3400 | Py_FileSystemDefaultEncoding, &file, |
| 3401 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3402 | return NULL; |
| 3403 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3404 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3405 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3406 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3407 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3408 | return posix_error_with_allocated_filename(file); |
| 3409 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3410 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3413 | |
| 3414 | static char posix_close__doc__[] = |
| 3415 | "close(fd) -> None\n\ |
| 3416 | Close a file descriptor (for low level IO)."; |
| 3417 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3418 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3419 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3420 | { |
| 3421 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3422 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3423 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3424 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3425 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3426 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3427 | if (res < 0) |
| 3428 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3429 | Py_INCREF(Py_None); |
| 3430 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3433 | |
| 3434 | static char posix_dup__doc__[] = |
| 3435 | "dup(fd) -> fd2\n\ |
| 3436 | Return a duplicate of a file descriptor."; |
| 3437 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3438 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3439 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3440 | { |
| 3441 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3442 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3443 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3444 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3445 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3446 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3447 | if (fd < 0) |
| 3448 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3449 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3452 | |
| 3453 | static char posix_dup2__doc__[] = |
| 3454 | "dup2(fd, fd2) -> None\n\ |
| 3455 | Duplicate file descriptor."; |
| 3456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3458 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3459 | { |
| 3460 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3461 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3462 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3463 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3464 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3465 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3466 | if (res < 0) |
| 3467 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3468 | Py_INCREF(Py_None); |
| 3469 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3472 | |
| 3473 | static char posix_lseek__doc__[] = |
| 3474 | "lseek(fd, pos, how) -> newpos\n\ |
| 3475 | Set the current position of a file descriptor."; |
| 3476 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3477 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3478 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3479 | { |
| 3480 | int fd, how; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3481 | #ifdef MS_WIN64 |
| 3482 | LONG_LONG pos, res; |
| 3483 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3484 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3485 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3486 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3487 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3488 | return NULL; |
| 3489 | #ifdef SEEK_SET |
| 3490 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3491 | switch (how) { |
| 3492 | case 0: how = SEEK_SET; break; |
| 3493 | case 1: how = SEEK_CUR; break; |
| 3494 | case 2: how = SEEK_END; break; |
| 3495 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3496 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3497 | |
| 3498 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3499 | pos = PyInt_AsLong(posobj); |
| 3500 | #else |
| 3501 | pos = PyLong_Check(posobj) ? |
| 3502 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3503 | #endif |
| 3504 | if (PyErr_Occurred()) |
| 3505 | return NULL; |
| 3506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3507 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3508 | #ifdef MS_WIN64 |
| 3509 | res = _lseeki64(fd, pos, how); |
| 3510 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3511 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3512 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3513 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3514 | if (res < 0) |
| 3515 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3516 | |
| 3517 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3518 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3519 | #else |
| 3520 | return PyLong_FromLongLong(res); |
| 3521 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3524 | |
| 3525 | static char posix_read__doc__[] = |
| 3526 | "read(fd, buffersize) -> string\n\ |
| 3527 | Read a file descriptor."; |
| 3528 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3529 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3530 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3531 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3532 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3533 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3534 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3535 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3536 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3537 | if (buffer == NULL) |
| 3538 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3539 | Py_BEGIN_ALLOW_THREADS |
| 3540 | n = read(fd, PyString_AsString(buffer), size); |
| 3541 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3542 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3543 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3544 | return posix_error(); |
| 3545 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3546 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3547 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3548 | return buffer; |
| 3549 | } |
| 3550 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3551 | |
| 3552 | static char posix_write__doc__[] = |
| 3553 | "write(fd, string) -> byteswritten\n\ |
| 3554 | Write a string to a file descriptor."; |
| 3555 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3556 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3557 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3558 | { |
| 3559 | int fd, size; |
| 3560 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3561 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3562 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3563 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3564 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3565 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3566 | if (size < 0) |
| 3567 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3568 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3571 | |
| 3572 | static char posix_fstat__doc__[]= |
| 3573 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3574 | Like stat(), but for an open file descriptor."; |
| 3575 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3577 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3578 | { |
| 3579 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3580 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3581 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3582 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3583 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3584 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3585 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3586 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3587 | if (res != 0) |
| 3588 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3589 | |
| 3590 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3593 | |
| 3594 | static char posix_fdopen__doc__[] = |
| 3595 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3596 | Return an open file object connected to a file descriptor."; |
| 3597 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3598 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3599 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3600 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3601 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3602 | char *mode = "r"; |
| 3603 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3604 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3605 | PyObject *f; |
| 3606 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3607 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3608 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3609 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3610 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3611 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3612 | if (fp == NULL) |
| 3613 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3614 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3615 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3616 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3617 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3620 | static char posix_isatty__doc__[] = |
| 3621 | "isatty(fd) -> Boolean\n\ |
| 3622 | 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] | 3623 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3624 | |
| 3625 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3626 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3627 | { |
| 3628 | int fd; |
| 3629 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3630 | return NULL; |
| 3631 | return Py_BuildValue("i", isatty(fd)); |
| 3632 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3633 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3634 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3635 | static char posix_pipe__doc__[] = |
| 3636 | "pipe() -> (read_end, write_end)\n\ |
| 3637 | Create a pipe."; |
| 3638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3639 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3640 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3641 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3642 | #if defined(PYOS_OS2) |
| 3643 | HFILE read, write; |
| 3644 | APIRET rc; |
| 3645 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3646 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3647 | return NULL; |
| 3648 | |
| 3649 | Py_BEGIN_ALLOW_THREADS |
| 3650 | rc = DosCreatePipe( &read, &write, 4096); |
| 3651 | Py_END_ALLOW_THREADS |
| 3652 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3653 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3654 | |
| 3655 | return Py_BuildValue("(ii)", read, write); |
| 3656 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3657 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3658 | int fds[2]; |
| 3659 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3660 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3661 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3662 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3663 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3664 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3665 | if (res != 0) |
| 3666 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3667 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3668 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3669 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3670 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3671 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3672 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3673 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3674 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3675 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3676 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3677 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3678 | return win32_error("CreatePipe", NULL); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3679 | read_fd = _open_osfhandle((intptr_t)read, 0); |
| 3680 | write_fd = _open_osfhandle((intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3681 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3682 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3683 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3684 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3685 | #endif /* HAVE_PIPE */ |
| 3686 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3687 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3688 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3689 | static char posix_mkfifo__doc__[] = |
| 3690 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3691 | Create a FIFO (a POSIX named pipe)."; |
| 3692 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3693 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3694 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3695 | { |
| 3696 | char *file; |
| 3697 | int mode = 0666; |
| 3698 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3699 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3700 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3701 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3702 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3703 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3704 | if (res < 0) |
| 3705 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3706 | Py_INCREF(Py_None); |
| 3707 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3708 | } |
| 3709 | #endif |
| 3710 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3711 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3712 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3713 | static char posix_ftruncate__doc__[] = |
| 3714 | "ftruncate(fd, length) -> None\n\ |
| 3715 | Truncate a file to a specified length."; |
| 3716 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3717 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3718 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3719 | { |
| 3720 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3721 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3722 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3723 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3724 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3725 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3726 | return NULL; |
| 3727 | |
| 3728 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3729 | length = PyInt_AsLong(lenobj); |
| 3730 | #else |
| 3731 | length = PyLong_Check(lenobj) ? |
| 3732 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3733 | #endif |
| 3734 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3735 | return NULL; |
| 3736 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3737 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3738 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3739 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3740 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3742 | return NULL; |
| 3743 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3744 | Py_INCREF(Py_None); |
| 3745 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3746 | } |
| 3747 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3748 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3749 | #ifdef NeXT |
| 3750 | #define HAVE_PUTENV |
| 3751 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3752 | static int |
| 3753 | putenv(char *newval) |
| 3754 | { |
| 3755 | extern char **environ; |
| 3756 | |
| 3757 | static int firstTime = 1; |
| 3758 | char **ep; |
| 3759 | char *cp; |
| 3760 | int esiz; |
| 3761 | char *np; |
| 3762 | |
| 3763 | if (!(np = strchr(newval, '='))) |
| 3764 | return 1; |
| 3765 | *np = '\0'; |
| 3766 | |
| 3767 | /* look it up */ |
| 3768 | for (ep=environ ; *ep ; ep++) |
| 3769 | { |
| 3770 | /* this should always be true... */ |
| 3771 | if (cp = strchr(*ep, '=')) |
| 3772 | { |
| 3773 | *cp = '\0'; |
| 3774 | if (!strcmp(*ep, newval)) |
| 3775 | { |
| 3776 | /* got it! */ |
| 3777 | *cp = '='; |
| 3778 | break; |
| 3779 | } |
| 3780 | *cp = '='; |
| 3781 | } |
| 3782 | else |
| 3783 | { |
| 3784 | *np = '='; |
| 3785 | return 1; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | *np = '='; |
| 3790 | if (*ep) |
| 3791 | { |
| 3792 | /* the string was already there: |
| 3793 | just replace it with the new one */ |
| 3794 | *ep = newval; |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
| 3798 | /* expand environ by one */ |
| 3799 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3800 | esiz++; |
| 3801 | if (firstTime) |
| 3802 | { |
| 3803 | char **epp; |
| 3804 | char **newenv; |
| 3805 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 3806 | return 1; |
| 3807 | |
| 3808 | for (ep=environ, epp=newenv ; *ep ;) |
| 3809 | *epp++ = *ep++; |
| 3810 | *epp++ = newval; |
| 3811 | *epp = (char *) 0; |
| 3812 | environ = newenv; |
| 3813 | } |
| 3814 | else |
| 3815 | { |
| 3816 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 3817 | return 1; |
| 3818 | environ[esiz - 2] = newval; |
| 3819 | environ[esiz - 1] = (char *) 0; |
| 3820 | firstTime = 0; |
| 3821 | } |
| 3822 | |
| 3823 | return 0; |
| 3824 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 3825 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3826 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3827 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3828 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3829 | static char posix_putenv__doc__[] = |
| 3830 | "putenv(key, value) -> None\n\ |
| 3831 | Change or add an environment variable."; |
| 3832 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3833 | /* Save putenv() parameters as values here, so we can collect them when they |
| 3834 | * get re-set with another call for the same key. */ |
| 3835 | static PyObject *posix_putenv_garbage; |
| 3836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3837 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3838 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3839 | { |
| 3840 | char *s1, *s2; |
| 3841 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3842 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3843 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3844 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3845 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3846 | |
| 3847 | #if defined(PYOS_OS2) |
| 3848 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 3849 | APIRET rc; |
| 3850 | |
| 3851 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3852 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3853 | |
| 3854 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 3855 | if (rc != NO_ERROR) |
| 3856 | return os2_error(rc); |
| 3857 | |
| 3858 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 3859 | APIRET rc; |
| 3860 | |
| 3861 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3862 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3863 | |
| 3864 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 3865 | if (rc != NO_ERROR) |
| 3866 | return os2_error(rc); |
| 3867 | } else { |
| 3868 | #endif |
| 3869 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3870 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 3871 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 3872 | if (newstr == NULL) |
| 3873 | return PyErr_NoMemory(); |
| 3874 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3875 | (void) sprintf(new, "%s=%s", s1, s2); |
| 3876 | if (putenv(new)) { |
| 3877 | posix_error(); |
| 3878 | return NULL; |
| 3879 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3880 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 3881 | * this will cause previous value to be collected. This has to |
| 3882 | * happen after the real putenv() call because the old value |
| 3883 | * was still accessible until then. */ |
| 3884 | if (PyDict_SetItem(posix_putenv_garbage, |
| 3885 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 3886 | /* really not much we can do; just leak */ |
| 3887 | PyErr_Clear(); |
| 3888 | } |
| 3889 | else { |
| 3890 | Py_DECREF(newstr); |
| 3891 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3892 | |
| 3893 | #if defined(PYOS_OS2) |
| 3894 | } |
| 3895 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3896 | Py_INCREF(Py_None); |
| 3897 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3898 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3899 | #endif /* putenv */ |
| 3900 | |
| 3901 | #ifdef HAVE_STRERROR |
| 3902 | static char posix_strerror__doc__[] = |
| 3903 | "strerror(code) -> string\n\ |
| 3904 | Translate an error code to a message string."; |
| 3905 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 3906 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3907 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3908 | { |
| 3909 | int code; |
| 3910 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3911 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3912 | return NULL; |
| 3913 | message = strerror(code); |
| 3914 | if (message == NULL) { |
| 3915 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3916 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3917 | return NULL; |
| 3918 | } |
| 3919 | return PyString_FromString(message); |
| 3920 | } |
| 3921 | #endif /* strerror */ |
| 3922 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3923 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3924 | #ifdef HAVE_SYS_WAIT_H |
| 3925 | |
| 3926 | #ifdef WIFSTOPPED |
| 3927 | static char posix_WIFSTOPPED__doc__[] = |
| 3928 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3929 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3930 | |
| 3931 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3932 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3933 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3934 | #ifdef UNION_WAIT |
| 3935 | union wait status; |
| 3936 | #define status_i (status.w_status) |
| 3937 | #else |
| 3938 | int status; |
| 3939 | #define status_i status |
| 3940 | #endif |
| 3941 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3942 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3943 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3944 | { |
| 3945 | return NULL; |
| 3946 | } |
| 3947 | |
| 3948 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3949 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3950 | } |
| 3951 | #endif /* WIFSTOPPED */ |
| 3952 | |
| 3953 | #ifdef WIFSIGNALED |
| 3954 | static char posix_WIFSIGNALED__doc__[] = |
| 3955 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 3956 | 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] | 3957 | |
| 3958 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3959 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3960 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3961 | #ifdef UNION_WAIT |
| 3962 | union wait status; |
| 3963 | #define status_i (status.w_status) |
| 3964 | #else |
| 3965 | int status; |
| 3966 | #define status_i status |
| 3967 | #endif |
| 3968 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3969 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3970 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3971 | { |
| 3972 | return NULL; |
| 3973 | } |
| 3974 | |
| 3975 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3976 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3977 | } |
| 3978 | #endif /* WIFSIGNALED */ |
| 3979 | |
| 3980 | #ifdef WIFEXITED |
| 3981 | static char posix_WIFEXITED__doc__[] = |
| 3982 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3983 | Return true if the process returning 'status' exited using the exit()\n\ |
| 3984 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3985 | |
| 3986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3987 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3988 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3989 | #ifdef UNION_WAIT |
| 3990 | union wait status; |
| 3991 | #define status_i (status.w_status) |
| 3992 | #else |
| 3993 | int status; |
| 3994 | #define status_i status |
| 3995 | #endif |
| 3996 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3997 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3998 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3999 | { |
| 4000 | return NULL; |
| 4001 | } |
| 4002 | |
| 4003 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4004 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4005 | } |
| 4006 | #endif /* WIFEXITED */ |
| 4007 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4008 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4009 | static char posix_WEXITSTATUS__doc__[] = |
| 4010 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4011 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4012 | |
| 4013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4014 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4015 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4016 | #ifdef UNION_WAIT |
| 4017 | union wait status; |
| 4018 | #define status_i (status.w_status) |
| 4019 | #else |
| 4020 | int status; |
| 4021 | #define status_i status |
| 4022 | #endif |
| 4023 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4024 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4025 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4026 | { |
| 4027 | return NULL; |
| 4028 | } |
| 4029 | |
| 4030 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4031 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4032 | } |
| 4033 | #endif /* WEXITSTATUS */ |
| 4034 | |
| 4035 | #ifdef WTERMSIG |
| 4036 | static char posix_WTERMSIG__doc__[] = |
| 4037 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4038 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4039 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4040 | |
| 4041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4042 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4043 | { |
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) |
| 4047 | #else |
| 4048 | int status; |
| 4049 | #define status_i status |
| 4050 | #endif |
| 4051 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4052 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4053 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4054 | { |
| 4055 | return NULL; |
| 4056 | } |
| 4057 | |
| 4058 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4059 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4060 | } |
| 4061 | #endif /* WTERMSIG */ |
| 4062 | |
| 4063 | #ifdef WSTOPSIG |
| 4064 | static char posix_WSTOPSIG__doc__[] = |
| 4065 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4066 | 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] | 4067 | |
| 4068 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4069 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4070 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4071 | #ifdef UNION_WAIT |
| 4072 | union wait status; |
| 4073 | #define status_i (status.w_status) |
| 4074 | #else |
| 4075 | int status; |
| 4076 | #define status_i status |
| 4077 | #endif |
| 4078 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4079 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4080 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4081 | { |
| 4082 | return NULL; |
| 4083 | } |
| 4084 | |
| 4085 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4086 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4087 | } |
| 4088 | #endif /* WSTOPSIG */ |
| 4089 | |
| 4090 | #endif /* HAVE_SYS_WAIT_H */ |
| 4091 | |
| 4092 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4093 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4094 | #ifdef _SCO_DS |
| 4095 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4096 | needed definitions in sys/statvfs.h */ |
| 4097 | #define _SVID3 |
| 4098 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4099 | #include <sys/statvfs.h> |
| 4100 | |
| 4101 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4102 | "fstatvfs(fd) -> \n\ |
| 4103 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4104 | Perform an fstatvfs system call on the given fd."; |
| 4105 | |
| 4106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4107 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4108 | { |
| 4109 | int fd, res; |
| 4110 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4111 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4112 | return NULL; |
| 4113 | Py_BEGIN_ALLOW_THREADS |
| 4114 | res = fstatvfs(fd, &st); |
| 4115 | Py_END_ALLOW_THREADS |
| 4116 | if (res != 0) |
| 4117 | return posix_error(); |
| 4118 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4119 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4120 | (long) st.f_bsize, |
| 4121 | (long) st.f_frsize, |
| 4122 | (long) st.f_blocks, |
| 4123 | (long) st.f_bfree, |
| 4124 | (long) st.f_bavail, |
| 4125 | (long) st.f_files, |
| 4126 | (long) st.f_ffree, |
| 4127 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4128 | (long) st.f_flag, |
| 4129 | (long) st.f_namemax); |
| 4130 | #else |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4131 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4132 | (long) st.f_bsize, |
| 4133 | (long) st.f_frsize, |
| 4134 | (LONG_LONG) st.f_blocks, |
| 4135 | (LONG_LONG) st.f_bfree, |
| 4136 | (LONG_LONG) st.f_bavail, |
| 4137 | (LONG_LONG) st.f_files, |
| 4138 | (LONG_LONG) st.f_ffree, |
| 4139 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4140 | (long) st.f_flag, |
| 4141 | (long) st.f_namemax); |
| 4142 | #endif |
| 4143 | } |
| 4144 | #endif /* HAVE_FSTATVFS */ |
| 4145 | |
| 4146 | |
| 4147 | #if defined(HAVE_STATVFS) |
| 4148 | #include <sys/statvfs.h> |
| 4149 | |
| 4150 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4151 | "statvfs(path) -> \n\ |
| 4152 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4153 | Perform a statvfs system call on the given path."; |
| 4154 | |
| 4155 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4156 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4157 | { |
| 4158 | char *path; |
| 4159 | int res; |
| 4160 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4161 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4162 | return NULL; |
| 4163 | Py_BEGIN_ALLOW_THREADS |
| 4164 | res = statvfs(path, &st); |
| 4165 | Py_END_ALLOW_THREADS |
| 4166 | if (res != 0) |
| 4167 | return posix_error_with_filename(path); |
| 4168 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4169 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4170 | (long) st.f_bsize, |
| 4171 | (long) st.f_frsize, |
| 4172 | (long) st.f_blocks, |
| 4173 | (long) st.f_bfree, |
| 4174 | (long) st.f_bavail, |
| 4175 | (long) st.f_files, |
| 4176 | (long) st.f_ffree, |
| 4177 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4178 | (long) st.f_flag, |
| 4179 | (long) st.f_namemax); |
| 4180 | #else /* HAVE_LARGEFILE_SUPPORT */ |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4181 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4182 | (long) st.f_bsize, |
| 4183 | (long) st.f_frsize, |
| 4184 | (LONG_LONG) st.f_blocks, |
| 4185 | (LONG_LONG) st.f_bfree, |
| 4186 | (LONG_LONG) st.f_bavail, |
| 4187 | (LONG_LONG) st.f_files, |
| 4188 | (LONG_LONG) st.f_ffree, |
| 4189 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4190 | (long) st.f_flag, |
| 4191 | (long) st.f_namemax); |
| 4192 | #endif |
| 4193 | } |
| 4194 | #endif /* HAVE_STATVFS */ |
| 4195 | |
| 4196 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4197 | #ifdef HAVE_TEMPNAM |
| 4198 | static char posix_tempnam__doc__[] = "\ |
| 4199 | tempnam([dir[, prefix]]) -> string\n\ |
| 4200 | Return a unique name for a temporary file.\n\ |
| 4201 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4202 | or None if not needed."; |
| 4203 | |
| 4204 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4205 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4206 | { |
| 4207 | PyObject *result = NULL; |
| 4208 | char *dir = NULL; |
| 4209 | char *pfx = NULL; |
| 4210 | char *name; |
| 4211 | |
| 4212 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4213 | return NULL; |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4214 | #ifdef MS_WIN32 |
| 4215 | name = _tempnam(dir, pfx); |
| 4216 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4217 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4218 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4219 | if (name == NULL) |
| 4220 | return PyErr_NoMemory(); |
| 4221 | result = PyString_FromString(name); |
| 4222 | free(name); |
| 4223 | return result; |
| 4224 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4225 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4226 | |
| 4227 | |
| 4228 | #ifdef HAVE_TMPFILE |
| 4229 | static char posix_tmpfile__doc__[] = "\ |
| 4230 | tmpfile() -> file object\n\ |
| 4231 | Create a temporary file with no directory entries."; |
| 4232 | |
| 4233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4234 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4235 | { |
| 4236 | FILE *fp; |
| 4237 | |
| 4238 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4239 | return NULL; |
| 4240 | fp = tmpfile(); |
| 4241 | if (fp == NULL) |
| 4242 | return posix_error(); |
| 4243 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4244 | } |
| 4245 | #endif |
| 4246 | |
| 4247 | |
| 4248 | #ifdef HAVE_TMPNAM |
| 4249 | static char posix_tmpnam__doc__[] = "\ |
| 4250 | tmpnam() -> string\n\ |
| 4251 | Return a unique name for a temporary file."; |
| 4252 | |
| 4253 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4254 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4255 | { |
| 4256 | char buffer[L_tmpnam]; |
| 4257 | char *name; |
| 4258 | |
| 4259 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4260 | return NULL; |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4261 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4262 | name = tmpnam_r(buffer); |
| 4263 | #else |
| 4264 | name = tmpnam(buffer); |
| 4265 | #endif |
| 4266 | if (name == NULL) { |
| 4267 | PyErr_SetObject(PyExc_OSError, |
| 4268 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4269 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4270 | "unexpected NULL from tmpnam_r" |
| 4271 | #else |
| 4272 | "unexpected NULL from tmpnam" |
| 4273 | #endif |
| 4274 | )); |
| 4275 | return NULL; |
| 4276 | } |
| 4277 | return PyString_FromString(buffer); |
| 4278 | } |
| 4279 | #endif |
| 4280 | |
| 4281 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4282 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4283 | * It maps strings representing configuration variable names to |
| 4284 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4285 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4286 | * rarely-used constants. There are three separate tables that use |
| 4287 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4288 | * |
| 4289 | * This code is always included, even if none of the interfaces that |
| 4290 | * need it are included. The #if hackery needed to avoid it would be |
| 4291 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4292 | */ |
| 4293 | struct constdef { |
| 4294 | char *name; |
| 4295 | long value; |
| 4296 | }; |
| 4297 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4298 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4299 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4300 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4301 | { |
| 4302 | if (PyInt_Check(arg)) { |
| 4303 | *valuep = PyInt_AS_LONG(arg); |
| 4304 | return 1; |
| 4305 | } |
| 4306 | if (PyString_Check(arg)) { |
| 4307 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4308 | size_t lo = 0; |
| 4309 | size_t mid; |
| 4310 | size_t hi = tablesize; |
| 4311 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4312 | char *confname = PyString_AS_STRING(arg); |
| 4313 | while (lo < hi) { |
| 4314 | mid = (lo + hi) / 2; |
| 4315 | cmp = strcmp(confname, table[mid].name); |
| 4316 | if (cmp < 0) |
| 4317 | hi = mid; |
| 4318 | else if (cmp > 0) |
| 4319 | lo = mid + 1; |
| 4320 | else { |
| 4321 | *valuep = table[mid].value; |
| 4322 | return 1; |
| 4323 | } |
| 4324 | } |
| 4325 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4326 | } |
| 4327 | else |
| 4328 | PyErr_SetString(PyExc_TypeError, |
| 4329 | "configuration names must be strings or integers"); |
| 4330 | return 0; |
| 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4335 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4336 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4337 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4338 | #endif |
| 4339 | #ifdef _PC_ABI_ASYNC_IO |
| 4340 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4341 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4342 | #ifdef _PC_ASYNC_IO |
| 4343 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4344 | #endif |
| 4345 | #ifdef _PC_CHOWN_RESTRICTED |
| 4346 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4347 | #endif |
| 4348 | #ifdef _PC_FILESIZEBITS |
| 4349 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4350 | #endif |
| 4351 | #ifdef _PC_LAST |
| 4352 | {"PC_LAST", _PC_LAST}, |
| 4353 | #endif |
| 4354 | #ifdef _PC_LINK_MAX |
| 4355 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4356 | #endif |
| 4357 | #ifdef _PC_MAX_CANON |
| 4358 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4359 | #endif |
| 4360 | #ifdef _PC_MAX_INPUT |
| 4361 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4362 | #endif |
| 4363 | #ifdef _PC_NAME_MAX |
| 4364 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4365 | #endif |
| 4366 | #ifdef _PC_NO_TRUNC |
| 4367 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4368 | #endif |
| 4369 | #ifdef _PC_PATH_MAX |
| 4370 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4371 | #endif |
| 4372 | #ifdef _PC_PIPE_BUF |
| 4373 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4374 | #endif |
| 4375 | #ifdef _PC_PRIO_IO |
| 4376 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4377 | #endif |
| 4378 | #ifdef _PC_SOCK_MAXBUF |
| 4379 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4380 | #endif |
| 4381 | #ifdef _PC_SYNC_IO |
| 4382 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4383 | #endif |
| 4384 | #ifdef _PC_VDISABLE |
| 4385 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4386 | #endif |
| 4387 | }; |
| 4388 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4389 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4390 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4391 | { |
| 4392 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4393 | sizeof(posix_constants_pathconf) |
| 4394 | / sizeof(struct constdef)); |
| 4395 | } |
| 4396 | #endif |
| 4397 | |
| 4398 | #ifdef HAVE_FPATHCONF |
| 4399 | static char posix_fpathconf__doc__[] = "\ |
| 4400 | fpathconf(fd, name) -> integer\n\ |
| 4401 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4402 | If there is no limit, return -1."; |
| 4403 | |
| 4404 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4405 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4406 | { |
| 4407 | PyObject *result = NULL; |
| 4408 | int name, fd; |
| 4409 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4410 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4411 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4412 | long limit; |
| 4413 | |
| 4414 | errno = 0; |
| 4415 | limit = fpathconf(fd, name); |
| 4416 | if (limit == -1 && errno != 0) |
| 4417 | posix_error(); |
| 4418 | else |
| 4419 | result = PyInt_FromLong(limit); |
| 4420 | } |
| 4421 | return result; |
| 4422 | } |
| 4423 | #endif |
| 4424 | |
| 4425 | |
| 4426 | #ifdef HAVE_PATHCONF |
| 4427 | static char posix_pathconf__doc__[] = "\ |
| 4428 | pathconf(path, name) -> integer\n\ |
| 4429 | Return the configuration limit name for the file or directory path.\n\ |
| 4430 | If there is no limit, return -1."; |
| 4431 | |
| 4432 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4433 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4434 | { |
| 4435 | PyObject *result = NULL; |
| 4436 | int name; |
| 4437 | char *path; |
| 4438 | |
| 4439 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4440 | conv_path_confname, &name)) { |
| 4441 | long limit; |
| 4442 | |
| 4443 | errno = 0; |
| 4444 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4445 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4446 | if (errno == EINVAL) |
| 4447 | /* could be a path or name problem */ |
| 4448 | posix_error(); |
| 4449 | else |
| 4450 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4451 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4452 | else |
| 4453 | result = PyInt_FromLong(limit); |
| 4454 | } |
| 4455 | return result; |
| 4456 | } |
| 4457 | #endif |
| 4458 | |
| 4459 | #ifdef HAVE_CONFSTR |
| 4460 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4461 | #ifdef _CS_ARCHITECTURE |
| 4462 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4463 | #endif |
| 4464 | #ifdef _CS_HOSTNAME |
| 4465 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4466 | #endif |
| 4467 | #ifdef _CS_HW_PROVIDER |
| 4468 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4469 | #endif |
| 4470 | #ifdef _CS_HW_SERIAL |
| 4471 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4472 | #endif |
| 4473 | #ifdef _CS_INITTAB_NAME |
| 4474 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4475 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4476 | #ifdef _CS_LFS64_CFLAGS |
| 4477 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4478 | #endif |
| 4479 | #ifdef _CS_LFS64_LDFLAGS |
| 4480 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4481 | #endif |
| 4482 | #ifdef _CS_LFS64_LIBS |
| 4483 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4484 | #endif |
| 4485 | #ifdef _CS_LFS64_LINTFLAGS |
| 4486 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4487 | #endif |
| 4488 | #ifdef _CS_LFS_CFLAGS |
| 4489 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4490 | #endif |
| 4491 | #ifdef _CS_LFS_LDFLAGS |
| 4492 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4493 | #endif |
| 4494 | #ifdef _CS_LFS_LIBS |
| 4495 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4496 | #endif |
| 4497 | #ifdef _CS_LFS_LINTFLAGS |
| 4498 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4499 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4500 | #ifdef _CS_MACHINE |
| 4501 | {"CS_MACHINE", _CS_MACHINE}, |
| 4502 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4503 | #ifdef _CS_PATH |
| 4504 | {"CS_PATH", _CS_PATH}, |
| 4505 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4506 | #ifdef _CS_RELEASE |
| 4507 | {"CS_RELEASE", _CS_RELEASE}, |
| 4508 | #endif |
| 4509 | #ifdef _CS_SRPC_DOMAIN |
| 4510 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4511 | #endif |
| 4512 | #ifdef _CS_SYSNAME |
| 4513 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4514 | #endif |
| 4515 | #ifdef _CS_VERSION |
| 4516 | {"CS_VERSION", _CS_VERSION}, |
| 4517 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4518 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4519 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4520 | #endif |
| 4521 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4522 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4523 | #endif |
| 4524 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4525 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4526 | #endif |
| 4527 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4528 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4529 | #endif |
| 4530 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4531 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4532 | #endif |
| 4533 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4534 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4535 | #endif |
| 4536 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4537 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4538 | #endif |
| 4539 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4540 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4541 | #endif |
| 4542 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4543 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4544 | #endif |
| 4545 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4546 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4547 | #endif |
| 4548 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4549 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4550 | #endif |
| 4551 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4552 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4553 | #endif |
| 4554 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4555 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4556 | #endif |
| 4557 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4558 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4559 | #endif |
| 4560 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4561 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4562 | #endif |
| 4563 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4564 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4565 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4566 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4567 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4568 | #endif |
| 4569 | #ifdef _MIPS_CS_BASE |
| 4570 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4571 | #endif |
| 4572 | #ifdef _MIPS_CS_HOSTID |
| 4573 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4574 | #endif |
| 4575 | #ifdef _MIPS_CS_HW_NAME |
| 4576 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4577 | #endif |
| 4578 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4579 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4580 | #endif |
| 4581 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4582 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4583 | #endif |
| 4584 | #ifdef _MIPS_CS_OSREL_MIN |
| 4585 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4586 | #endif |
| 4587 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4588 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4589 | #endif |
| 4590 | #ifdef _MIPS_CS_OS_NAME |
| 4591 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4592 | #endif |
| 4593 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4594 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4595 | #endif |
| 4596 | #ifdef _MIPS_CS_PROCESSORS |
| 4597 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4598 | #endif |
| 4599 | #ifdef _MIPS_CS_SERIAL |
| 4600 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4601 | #endif |
| 4602 | #ifdef _MIPS_CS_VENDOR |
| 4603 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4604 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4605 | }; |
| 4606 | |
| 4607 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4608 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4609 | { |
| 4610 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4611 | sizeof(posix_constants_confstr) |
| 4612 | / sizeof(struct constdef)); |
| 4613 | } |
| 4614 | |
| 4615 | static char posix_confstr__doc__[] = "\ |
| 4616 | confstr(name) -> string\n\ |
| 4617 | Return a string-valued system configuration variable."; |
| 4618 | |
| 4619 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4620 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4621 | { |
| 4622 | PyObject *result = NULL; |
| 4623 | int name; |
| 4624 | char buffer[64]; |
| 4625 | |
| 4626 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4627 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4628 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4629 | errno = 0; |
| 4630 | if (len == 0) { |
| 4631 | if (errno != 0) |
| 4632 | posix_error(); |
| 4633 | else |
| 4634 | result = PyString_FromString(""); |
| 4635 | } |
| 4636 | else { |
| 4637 | if (len >= sizeof(buffer)) { |
| 4638 | result = PyString_FromStringAndSize(NULL, len); |
| 4639 | if (result != NULL) |
| 4640 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4641 | } |
| 4642 | else |
| 4643 | result = PyString_FromString(buffer); |
| 4644 | } |
| 4645 | } |
| 4646 | return result; |
| 4647 | } |
| 4648 | #endif |
| 4649 | |
| 4650 | |
| 4651 | #ifdef HAVE_SYSCONF |
| 4652 | static struct constdef posix_constants_sysconf[] = { |
| 4653 | #ifdef _SC_2_CHAR_TERM |
| 4654 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4655 | #endif |
| 4656 | #ifdef _SC_2_C_BIND |
| 4657 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4658 | #endif |
| 4659 | #ifdef _SC_2_C_DEV |
| 4660 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4661 | #endif |
| 4662 | #ifdef _SC_2_C_VERSION |
| 4663 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4664 | #endif |
| 4665 | #ifdef _SC_2_FORT_DEV |
| 4666 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4667 | #endif |
| 4668 | #ifdef _SC_2_FORT_RUN |
| 4669 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4670 | #endif |
| 4671 | #ifdef _SC_2_LOCALEDEF |
| 4672 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4673 | #endif |
| 4674 | #ifdef _SC_2_SW_DEV |
| 4675 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4676 | #endif |
| 4677 | #ifdef _SC_2_UPE |
| 4678 | {"SC_2_UPE", _SC_2_UPE}, |
| 4679 | #endif |
| 4680 | #ifdef _SC_2_VERSION |
| 4681 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4682 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4683 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4684 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4685 | #endif |
| 4686 | #ifdef _SC_ACL |
| 4687 | {"SC_ACL", _SC_ACL}, |
| 4688 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4689 | #ifdef _SC_AIO_LISTIO_MAX |
| 4690 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4691 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4692 | #ifdef _SC_AIO_MAX |
| 4693 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4694 | #endif |
| 4695 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4696 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4697 | #endif |
| 4698 | #ifdef _SC_ARG_MAX |
| 4699 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4700 | #endif |
| 4701 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4702 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4703 | #endif |
| 4704 | #ifdef _SC_ATEXIT_MAX |
| 4705 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4706 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4707 | #ifdef _SC_AUDIT |
| 4708 | {"SC_AUDIT", _SC_AUDIT}, |
| 4709 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4710 | #ifdef _SC_AVPHYS_PAGES |
| 4711 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4712 | #endif |
| 4713 | #ifdef _SC_BC_BASE_MAX |
| 4714 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4715 | #endif |
| 4716 | #ifdef _SC_BC_DIM_MAX |
| 4717 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4718 | #endif |
| 4719 | #ifdef _SC_BC_SCALE_MAX |
| 4720 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4721 | #endif |
| 4722 | #ifdef _SC_BC_STRING_MAX |
| 4723 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4724 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4725 | #ifdef _SC_CAP |
| 4726 | {"SC_CAP", _SC_CAP}, |
| 4727 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4728 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4729 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4730 | #endif |
| 4731 | #ifdef _SC_CHAR_BIT |
| 4732 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4733 | #endif |
| 4734 | #ifdef _SC_CHAR_MAX |
| 4735 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4736 | #endif |
| 4737 | #ifdef _SC_CHAR_MIN |
| 4738 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4739 | #endif |
| 4740 | #ifdef _SC_CHILD_MAX |
| 4741 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4742 | #endif |
| 4743 | #ifdef _SC_CLK_TCK |
| 4744 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4745 | #endif |
| 4746 | #ifdef _SC_COHER_BLKSZ |
| 4747 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4748 | #endif |
| 4749 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4750 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4751 | #endif |
| 4752 | #ifdef _SC_DCACHE_ASSOC |
| 4753 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4754 | #endif |
| 4755 | #ifdef _SC_DCACHE_BLKSZ |
| 4756 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4757 | #endif |
| 4758 | #ifdef _SC_DCACHE_LINESZ |
| 4759 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4760 | #endif |
| 4761 | #ifdef _SC_DCACHE_SZ |
| 4762 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 4763 | #endif |
| 4764 | #ifdef _SC_DCACHE_TBLKSZ |
| 4765 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 4766 | #endif |
| 4767 | #ifdef _SC_DELAYTIMER_MAX |
| 4768 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 4769 | #endif |
| 4770 | #ifdef _SC_EQUIV_CLASS_MAX |
| 4771 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 4772 | #endif |
| 4773 | #ifdef _SC_EXPR_NEST_MAX |
| 4774 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 4775 | #endif |
| 4776 | #ifdef _SC_FSYNC |
| 4777 | {"SC_FSYNC", _SC_FSYNC}, |
| 4778 | #endif |
| 4779 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 4780 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 4781 | #endif |
| 4782 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 4783 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 4784 | #endif |
| 4785 | #ifdef _SC_ICACHE_ASSOC |
| 4786 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 4787 | #endif |
| 4788 | #ifdef _SC_ICACHE_BLKSZ |
| 4789 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 4790 | #endif |
| 4791 | #ifdef _SC_ICACHE_LINESZ |
| 4792 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 4793 | #endif |
| 4794 | #ifdef _SC_ICACHE_SZ |
| 4795 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 4796 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4797 | #ifdef _SC_INF |
| 4798 | {"SC_INF", _SC_INF}, |
| 4799 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4800 | #ifdef _SC_INT_MAX |
| 4801 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 4802 | #endif |
| 4803 | #ifdef _SC_INT_MIN |
| 4804 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 4805 | #endif |
| 4806 | #ifdef _SC_IOV_MAX |
| 4807 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 4808 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4809 | #ifdef _SC_IP_SECOPTS |
| 4810 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 4811 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4812 | #ifdef _SC_JOB_CONTROL |
| 4813 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 4814 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4815 | #ifdef _SC_KERN_POINTERS |
| 4816 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 4817 | #endif |
| 4818 | #ifdef _SC_KERN_SIM |
| 4819 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 4820 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4821 | #ifdef _SC_LINE_MAX |
| 4822 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 4823 | #endif |
| 4824 | #ifdef _SC_LOGIN_NAME_MAX |
| 4825 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 4826 | #endif |
| 4827 | #ifdef _SC_LOGNAME_MAX |
| 4828 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 4829 | #endif |
| 4830 | #ifdef _SC_LONG_BIT |
| 4831 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 4832 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4833 | #ifdef _SC_MAC |
| 4834 | {"SC_MAC", _SC_MAC}, |
| 4835 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4836 | #ifdef _SC_MAPPED_FILES |
| 4837 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 4838 | #endif |
| 4839 | #ifdef _SC_MAXPID |
| 4840 | {"SC_MAXPID", _SC_MAXPID}, |
| 4841 | #endif |
| 4842 | #ifdef _SC_MB_LEN_MAX |
| 4843 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 4844 | #endif |
| 4845 | #ifdef _SC_MEMLOCK |
| 4846 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 4847 | #endif |
| 4848 | #ifdef _SC_MEMLOCK_RANGE |
| 4849 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 4850 | #endif |
| 4851 | #ifdef _SC_MEMORY_PROTECTION |
| 4852 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 4853 | #endif |
| 4854 | #ifdef _SC_MESSAGE_PASSING |
| 4855 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 4856 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4857 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 4858 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 4859 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4860 | #ifdef _SC_MQ_OPEN_MAX |
| 4861 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 4862 | #endif |
| 4863 | #ifdef _SC_MQ_PRIO_MAX |
| 4864 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 4865 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4866 | #ifdef _SC_NACLS_MAX |
| 4867 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 4868 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4869 | #ifdef _SC_NGROUPS_MAX |
| 4870 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 4871 | #endif |
| 4872 | #ifdef _SC_NL_ARGMAX |
| 4873 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 4874 | #endif |
| 4875 | #ifdef _SC_NL_LANGMAX |
| 4876 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 4877 | #endif |
| 4878 | #ifdef _SC_NL_MSGMAX |
| 4879 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 4880 | #endif |
| 4881 | #ifdef _SC_NL_NMAX |
| 4882 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 4883 | #endif |
| 4884 | #ifdef _SC_NL_SETMAX |
| 4885 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 4886 | #endif |
| 4887 | #ifdef _SC_NL_TEXTMAX |
| 4888 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 4889 | #endif |
| 4890 | #ifdef _SC_NPROCESSORS_CONF |
| 4891 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 4892 | #endif |
| 4893 | #ifdef _SC_NPROCESSORS_ONLN |
| 4894 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 4895 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4896 | #ifdef _SC_NPROC_CONF |
| 4897 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 4898 | #endif |
| 4899 | #ifdef _SC_NPROC_ONLN |
| 4900 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 4901 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4902 | #ifdef _SC_NZERO |
| 4903 | {"SC_NZERO", _SC_NZERO}, |
| 4904 | #endif |
| 4905 | #ifdef _SC_OPEN_MAX |
| 4906 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 4907 | #endif |
| 4908 | #ifdef _SC_PAGESIZE |
| 4909 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 4910 | #endif |
| 4911 | #ifdef _SC_PAGE_SIZE |
| 4912 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 4913 | #endif |
| 4914 | #ifdef _SC_PASS_MAX |
| 4915 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 4916 | #endif |
| 4917 | #ifdef _SC_PHYS_PAGES |
| 4918 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 4919 | #endif |
| 4920 | #ifdef _SC_PII |
| 4921 | {"SC_PII", _SC_PII}, |
| 4922 | #endif |
| 4923 | #ifdef _SC_PII_INTERNET |
| 4924 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 4925 | #endif |
| 4926 | #ifdef _SC_PII_INTERNET_DGRAM |
| 4927 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 4928 | #endif |
| 4929 | #ifdef _SC_PII_INTERNET_STREAM |
| 4930 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 4931 | #endif |
| 4932 | #ifdef _SC_PII_OSI |
| 4933 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 4934 | #endif |
| 4935 | #ifdef _SC_PII_OSI_CLTS |
| 4936 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 4937 | #endif |
| 4938 | #ifdef _SC_PII_OSI_COTS |
| 4939 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 4940 | #endif |
| 4941 | #ifdef _SC_PII_OSI_M |
| 4942 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 4943 | #endif |
| 4944 | #ifdef _SC_PII_SOCKET |
| 4945 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 4946 | #endif |
| 4947 | #ifdef _SC_PII_XTI |
| 4948 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 4949 | #endif |
| 4950 | #ifdef _SC_POLL |
| 4951 | {"SC_POLL", _SC_POLL}, |
| 4952 | #endif |
| 4953 | #ifdef _SC_PRIORITIZED_IO |
| 4954 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 4955 | #endif |
| 4956 | #ifdef _SC_PRIORITY_SCHEDULING |
| 4957 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 4958 | #endif |
| 4959 | #ifdef _SC_REALTIME_SIGNALS |
| 4960 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 4961 | #endif |
| 4962 | #ifdef _SC_RE_DUP_MAX |
| 4963 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 4964 | #endif |
| 4965 | #ifdef _SC_RTSIG_MAX |
| 4966 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 4967 | #endif |
| 4968 | #ifdef _SC_SAVED_IDS |
| 4969 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 4970 | #endif |
| 4971 | #ifdef _SC_SCHAR_MAX |
| 4972 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 4973 | #endif |
| 4974 | #ifdef _SC_SCHAR_MIN |
| 4975 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 4976 | #endif |
| 4977 | #ifdef _SC_SELECT |
| 4978 | {"SC_SELECT", _SC_SELECT}, |
| 4979 | #endif |
| 4980 | #ifdef _SC_SEMAPHORES |
| 4981 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 4982 | #endif |
| 4983 | #ifdef _SC_SEM_NSEMS_MAX |
| 4984 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 4985 | #endif |
| 4986 | #ifdef _SC_SEM_VALUE_MAX |
| 4987 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 4988 | #endif |
| 4989 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 4990 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 4991 | #endif |
| 4992 | #ifdef _SC_SHRT_MAX |
| 4993 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 4994 | #endif |
| 4995 | #ifdef _SC_SHRT_MIN |
| 4996 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 4997 | #endif |
| 4998 | #ifdef _SC_SIGQUEUE_MAX |
| 4999 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5000 | #endif |
| 5001 | #ifdef _SC_SIGRT_MAX |
| 5002 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5003 | #endif |
| 5004 | #ifdef _SC_SIGRT_MIN |
| 5005 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5006 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5007 | #ifdef _SC_SOFTPOWER |
| 5008 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5009 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5010 | #ifdef _SC_SPLIT_CACHE |
| 5011 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5012 | #endif |
| 5013 | #ifdef _SC_SSIZE_MAX |
| 5014 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5015 | #endif |
| 5016 | #ifdef _SC_STACK_PROT |
| 5017 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5018 | #endif |
| 5019 | #ifdef _SC_STREAM_MAX |
| 5020 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5021 | #endif |
| 5022 | #ifdef _SC_SYNCHRONIZED_IO |
| 5023 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5024 | #endif |
| 5025 | #ifdef _SC_THREADS |
| 5026 | {"SC_THREADS", _SC_THREADS}, |
| 5027 | #endif |
| 5028 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5029 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5030 | #endif |
| 5031 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5032 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5033 | #endif |
| 5034 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5035 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5036 | #endif |
| 5037 | #ifdef _SC_THREAD_KEYS_MAX |
| 5038 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5039 | #endif |
| 5040 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5041 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5042 | #endif |
| 5043 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5044 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5045 | #endif |
| 5046 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5047 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5048 | #endif |
| 5049 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5050 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5051 | #endif |
| 5052 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5053 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5054 | #endif |
| 5055 | #ifdef _SC_THREAD_STACK_MIN |
| 5056 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5057 | #endif |
| 5058 | #ifdef _SC_THREAD_THREADS_MAX |
| 5059 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5060 | #endif |
| 5061 | #ifdef _SC_TIMERS |
| 5062 | {"SC_TIMERS", _SC_TIMERS}, |
| 5063 | #endif |
| 5064 | #ifdef _SC_TIMER_MAX |
| 5065 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5066 | #endif |
| 5067 | #ifdef _SC_TTY_NAME_MAX |
| 5068 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5069 | #endif |
| 5070 | #ifdef _SC_TZNAME_MAX |
| 5071 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5072 | #endif |
| 5073 | #ifdef _SC_T_IOV_MAX |
| 5074 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5075 | #endif |
| 5076 | #ifdef _SC_UCHAR_MAX |
| 5077 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5078 | #endif |
| 5079 | #ifdef _SC_UINT_MAX |
| 5080 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5081 | #endif |
| 5082 | #ifdef _SC_UIO_MAXIOV |
| 5083 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5084 | #endif |
| 5085 | #ifdef _SC_ULONG_MAX |
| 5086 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5087 | #endif |
| 5088 | #ifdef _SC_USHRT_MAX |
| 5089 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5090 | #endif |
| 5091 | #ifdef _SC_VERSION |
| 5092 | {"SC_VERSION", _SC_VERSION}, |
| 5093 | #endif |
| 5094 | #ifdef _SC_WORD_BIT |
| 5095 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5096 | #endif |
| 5097 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5098 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5099 | #endif |
| 5100 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5101 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5102 | #endif |
| 5103 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5104 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5105 | #endif |
| 5106 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5107 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5108 | #endif |
| 5109 | #ifdef _SC_XOPEN_CRYPT |
| 5110 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5111 | #endif |
| 5112 | #ifdef _SC_XOPEN_ENH_I18N |
| 5113 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5114 | #endif |
| 5115 | #ifdef _SC_XOPEN_LEGACY |
| 5116 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5117 | #endif |
| 5118 | #ifdef _SC_XOPEN_REALTIME |
| 5119 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5120 | #endif |
| 5121 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5122 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5123 | #endif |
| 5124 | #ifdef _SC_XOPEN_SHM |
| 5125 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5126 | #endif |
| 5127 | #ifdef _SC_XOPEN_UNIX |
| 5128 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5129 | #endif |
| 5130 | #ifdef _SC_XOPEN_VERSION |
| 5131 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5132 | #endif |
| 5133 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5134 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5135 | #endif |
| 5136 | #ifdef _SC_XOPEN_XPG2 |
| 5137 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5138 | #endif |
| 5139 | #ifdef _SC_XOPEN_XPG3 |
| 5140 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5141 | #endif |
| 5142 | #ifdef _SC_XOPEN_XPG4 |
| 5143 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5144 | #endif |
| 5145 | }; |
| 5146 | |
| 5147 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5148 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5149 | { |
| 5150 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5151 | sizeof(posix_constants_sysconf) |
| 5152 | / sizeof(struct constdef)); |
| 5153 | } |
| 5154 | |
| 5155 | static char posix_sysconf__doc__[] = "\ |
| 5156 | sysconf(name) -> integer\n\ |
| 5157 | Return an integer-valued system configuration variable."; |
| 5158 | |
| 5159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5160 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5161 | { |
| 5162 | PyObject *result = NULL; |
| 5163 | int name; |
| 5164 | |
| 5165 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5166 | int value; |
| 5167 | |
| 5168 | errno = 0; |
| 5169 | value = sysconf(name); |
| 5170 | if (value == -1 && errno != 0) |
| 5171 | posix_error(); |
| 5172 | else |
| 5173 | result = PyInt_FromLong(value); |
| 5174 | } |
| 5175 | return result; |
| 5176 | } |
| 5177 | #endif |
| 5178 | |
| 5179 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5180 | /* This code is used to ensure that the tables of configuration value names |
| 5181 | * are in sorted order as required by conv_confname(), and also to build the |
| 5182 | * the exported dictionaries that are used to publish information about the |
| 5183 | * names available on the host platform. |
| 5184 | * |
| 5185 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5186 | * when used, even for platforms we're not able to test on. It also makes |
| 5187 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5188 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5189 | |
| 5190 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5191 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5192 | { |
| 5193 | const struct constdef *c1 = |
| 5194 | (const struct constdef *) v1; |
| 5195 | const struct constdef *c2 = |
| 5196 | (const struct constdef *) v2; |
| 5197 | |
| 5198 | return strcmp(c1->name, c2->name); |
| 5199 | } |
| 5200 | |
| 5201 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5202 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5203 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5204 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5205 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5206 | size_t i; |
| 5207 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5208 | |
| 5209 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5210 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5211 | if (d == NULL) |
| 5212 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5213 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5214 | for (i=0; i < tablesize; ++i) { |
| 5215 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5216 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5217 | Py_XDECREF(o); |
| 5218 | Py_DECREF(d); |
| 5219 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5220 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5221 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5222 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5223 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5224 | Py_DECREF(d); |
| 5225 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5228 | /* Return -1 on failure, 0 on success. */ |
| 5229 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5230 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5231 | { |
| 5232 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5233 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5234 | sizeof(posix_constants_pathconf) |
| 5235 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5236 | "pathconf_names", moddict)) |
| 5237 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5238 | #endif |
| 5239 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5240 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5241 | sizeof(posix_constants_confstr) |
| 5242 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5243 | "confstr_names", moddict)) |
| 5244 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5245 | #endif |
| 5246 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5247 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5248 | sizeof(posix_constants_sysconf) |
| 5249 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5250 | "sysconf_names", moddict)) |
| 5251 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5252 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5253 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5254 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5255 | |
| 5256 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5257 | static char posix_abort__doc__[] = "\ |
| 5258 | abort() -> does not return!\n\ |
| 5259 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5260 | in the hardest way possible on the hosting operating system."; |
| 5261 | |
| 5262 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5263 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5264 | { |
| 5265 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5266 | return NULL; |
| 5267 | abort(); |
| 5268 | /*NOTREACHED*/ |
| 5269 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5270 | return NULL; |
| 5271 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5272 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5273 | #ifdef MS_WIN32 |
| 5274 | static char win32_startfile__doc__[] = "\ |
| 5275 | startfile(filepath) - Start a file with its associated application.\n\ |
| 5276 | \n\ |
| 5277 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 5278 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 5279 | with whatever application (if any) its extension is associated.\n\ |
| 5280 | \n\ |
| 5281 | startfile returns as soon as the associated application is launched.\n\ |
| 5282 | There is no option to wait for the application to close, and no way\n\ |
| 5283 | to retrieve the application's exit status.\n\ |
| 5284 | \n\ |
| 5285 | The filepath is relative to the current directory. If you want to use\n\ |
| 5286 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 5287 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 5288 | |
| 5289 | static PyObject * |
| 5290 | win32_startfile(PyObject *self, PyObject *args) |
| 5291 | { |
| 5292 | char *filepath; |
| 5293 | HINSTANCE rc; |
| 5294 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 5295 | return NULL; |
| 5296 | Py_BEGIN_ALLOW_THREADS |
| 5297 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 5298 | Py_END_ALLOW_THREADS |
| 5299 | if (rc <= (HINSTANCE)32) |
| 5300 | return win32_error("startfile", filepath); |
| 5301 | Py_INCREF(Py_None); |
| 5302 | return Py_None; |
| 5303 | } |
| 5304 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5305 | |
| 5306 | static PyMethodDef posix_methods[] = { |
| 5307 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5308 | #ifdef HAVE_TTYNAME |
| 5309 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5310 | #endif |
| 5311 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5312 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5313 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5314 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5315 | #endif /* HAVE_CHOWN */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5316 | #ifdef HAVE_CTERMID |
| 5317 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5318 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5319 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5320 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5321 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5322 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5323 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5324 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5325 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5326 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5327 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5328 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5329 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5330 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5331 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5332 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5333 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5334 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5335 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5336 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5337 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5338 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5339 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5340 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5341 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5342 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5343 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5344 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5345 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5346 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5347 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5348 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5349 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5350 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5351 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5352 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5353 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5354 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5355 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5356 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5357 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5358 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5359 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5360 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5361 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5362 | #ifdef HAVE_FORK1 |
| 5363 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 5364 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5365 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5366 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5367 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5368 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5369 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5370 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5371 | #ifdef HAVE_FORKPTY |
| 5372 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5373 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5374 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5375 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5376 | #endif /* HAVE_GETEGID */ |
| 5377 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5378 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5379 | #endif /* HAVE_GETEUID */ |
| 5380 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5381 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5382 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5383 | #ifdef HAVE_GETGROUPS |
| 5384 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5385 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5386 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5387 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5388 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5389 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5390 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5391 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5392 | #endif /* HAVE_GETPPID */ |
| 5393 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5394 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5395 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5396 | #ifdef HAVE_GETLOGIN |
| 5397 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5398 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5399 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5400 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5401 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5402 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5403 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5404 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5405 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5406 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5407 | #ifdef MS_WIN32 |
| 5408 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5409 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5410 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5411 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5412 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5413 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5414 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5415 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5416 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5417 | #ifdef HAVE_SETEUID |
| 5418 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5419 | #endif /* HAVE_SETEUID */ |
| 5420 | #ifdef HAVE_SETEGID |
| 5421 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5422 | #endif /* HAVE_SETEGID */ |
| 5423 | #ifdef HAVE_SETREUID |
| 5424 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5425 | #endif /* HAVE_SETREUID */ |
| 5426 | #ifdef HAVE_SETREGID |
| 5427 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5428 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5429 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5430 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5431 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5432 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5433 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5434 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5435 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5436 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5437 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5438 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5439 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5440 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5441 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5442 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5443 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5444 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5445 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5446 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5447 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5448 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5449 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5450 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5451 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5452 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5453 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5454 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5455 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5456 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5457 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5458 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5459 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5460 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5461 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5462 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5463 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5464 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5465 | #endif |
| 5466 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5467 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5468 | #endif |
| 5469 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5470 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5471 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5472 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5473 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5474 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5475 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5476 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5477 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5478 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5479 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5480 | #endif |
| 5481 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5482 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5483 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5484 | #ifdef HAVE_SYS_WAIT_H |
| 5485 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5486 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5487 | #endif /* WIFSTOPPED */ |
| 5488 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5489 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5490 | #endif /* WIFSIGNALED */ |
| 5491 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5492 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5493 | #endif /* WIFEXITED */ |
| 5494 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5495 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5496 | #endif /* WEXITSTATUS */ |
| 5497 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5498 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5499 | #endif /* WTERMSIG */ |
| 5500 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5501 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5502 | #endif /* WSTOPSIG */ |
| 5503 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5504 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5505 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5506 | #endif |
| 5507 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5508 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5509 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 5510 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5511 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5512 | #endif |
| 5513 | #ifdef HAVE_TEMPNAM |
| 5514 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5515 | #endif |
| 5516 | #ifdef HAVE_TMPNAM |
| 5517 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5518 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5519 | #ifdef HAVE_CONFSTR |
| 5520 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5521 | #endif |
| 5522 | #ifdef HAVE_SYSCONF |
| 5523 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5524 | #endif |
| 5525 | #ifdef HAVE_FPATHCONF |
| 5526 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5527 | #endif |
| 5528 | #ifdef HAVE_PATHCONF |
| 5529 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5530 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5531 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5532 | #ifdef MS_WIN32 |
| 5533 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 5534 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5535 | {NULL, NULL} /* Sentinel */ |
| 5536 | }; |
| 5537 | |
| 5538 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5539 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5540 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5541 | { |
| 5542 | PyObject* v = PyInt_FromLong(value); |
| 5543 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5544 | return -1; /* triggers fatal error */ |
| 5545 | |
| 5546 | Py_DECREF(v); |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5550 | #if defined(PYOS_OS2) |
| 5551 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5552 | static int insertvalues(PyObject *d) |
| 5553 | { |
| 5554 | APIRET rc; |
| 5555 | ULONG values[QSV_MAX+1]; |
| 5556 | PyObject *v; |
| 5557 | char *ver, tmp[10]; |
| 5558 | |
| 5559 | Py_BEGIN_ALLOW_THREADS |
| 5560 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5561 | Py_END_ALLOW_THREADS |
| 5562 | |
| 5563 | if (rc != NO_ERROR) { |
| 5564 | os2_error(rc); |
| 5565 | return -1; |
| 5566 | } |
| 5567 | |
| 5568 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5569 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5570 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5571 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5572 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5573 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5574 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5575 | |
| 5576 | switch (values[QSV_VERSION_MINOR]) { |
| 5577 | case 0: ver = "2.00"; break; |
| 5578 | case 10: ver = "2.10"; break; |
| 5579 | case 11: ver = "2.11"; break; |
| 5580 | case 30: ver = "3.00"; break; |
| 5581 | case 40: ver = "4.00"; break; |
| 5582 | case 50: ver = "5.00"; break; |
| 5583 | default: |
| 5584 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 5585 | values[QSV_VERSION_MINOR]); |
| 5586 | ver = &tmp[0]; |
| 5587 | } |
| 5588 | |
| 5589 | /* Add Indicator of the Version of the Operating System */ |
| 5590 | v = PyString_FromString(ver); |
| 5591 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5592 | return -1; |
| 5593 | Py_DECREF(v); |
| 5594 | |
| 5595 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5596 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5597 | tmp[1] = ':'; |
| 5598 | tmp[2] = '\0'; |
| 5599 | |
| 5600 | v = PyString_FromString(tmp); |
| 5601 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5602 | return -1; |
| 5603 | Py_DECREF(v); |
| 5604 | |
| 5605 | return 0; |
| 5606 | } |
| 5607 | #endif |
| 5608 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5609 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5610 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5611 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5612 | #ifdef F_OK |
| 5613 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5614 | #endif |
| 5615 | #ifdef R_OK |
| 5616 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5617 | #endif |
| 5618 | #ifdef W_OK |
| 5619 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5620 | #endif |
| 5621 | #ifdef X_OK |
| 5622 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5623 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5624 | #ifdef NGROUPS_MAX |
| 5625 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5626 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5627 | #ifdef TMP_MAX |
| 5628 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5629 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5630 | #ifdef WNOHANG |
| 5631 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5632 | #endif |
| 5633 | #ifdef O_RDONLY |
| 5634 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5635 | #endif |
| 5636 | #ifdef O_WRONLY |
| 5637 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5638 | #endif |
| 5639 | #ifdef O_RDWR |
| 5640 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5641 | #endif |
| 5642 | #ifdef O_NDELAY |
| 5643 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5644 | #endif |
| 5645 | #ifdef O_NONBLOCK |
| 5646 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5647 | #endif |
| 5648 | #ifdef O_APPEND |
| 5649 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5650 | #endif |
| 5651 | #ifdef O_DSYNC |
| 5652 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5653 | #endif |
| 5654 | #ifdef O_RSYNC |
| 5655 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5656 | #endif |
| 5657 | #ifdef O_SYNC |
| 5658 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5659 | #endif |
| 5660 | #ifdef O_NOCTTY |
| 5661 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5662 | #endif |
| 5663 | #ifdef O_CREAT |
| 5664 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5665 | #endif |
| 5666 | #ifdef O_EXCL |
| 5667 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5668 | #endif |
| 5669 | #ifdef O_TRUNC |
| 5670 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5671 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5672 | #ifdef O_BINARY |
| 5673 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5674 | #endif |
| 5675 | #ifdef O_TEXT |
| 5676 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5677 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5678 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5679 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5680 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5681 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5682 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5683 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5684 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5685 | #endif |
| 5686 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5687 | #if defined(PYOS_OS2) |
| 5688 | if (insertvalues(d)) return -1; |
| 5689 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5690 | return 0; |
| 5691 | } |
| 5692 | |
| 5693 | |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5694 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5695 | #define INITFUNC initnt |
| 5696 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5697 | |
| 5698 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5699 | #define INITFUNC initos2 |
| 5700 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5701 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5702 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5703 | #define INITFUNC initposix |
| 5704 | #define MODNAME "posix" |
| 5705 | #endif |
| 5706 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5707 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5708 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5709 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5710 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5711 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5712 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5713 | posix_methods, |
| 5714 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5715 | (PyObject *)NULL, |
| 5716 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5717 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5718 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5719 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5720 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5721 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5722 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5723 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5724 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5725 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5726 | return; |
| 5727 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5728 | if (setup_confname_tables(d)) |
| 5729 | return; |
| 5730 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5731 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5732 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5733 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 5734 | if (posix_putenv_garbage == NULL) |
| 5735 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5736 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5737 | } |