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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 43 | /* Various compilers have only certain posix functions */ |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 44 | /* XXX Gosh I wish these were all moved into config.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 45 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #include <process.h> |
| 47 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 48 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 49 | #define HAVE_GETCWD 1 |
| 50 | #define HAVE_OPENDIR 1 |
| 51 | #define HAVE_SYSTEM 1 |
| 52 | #if defined(__OS2__) |
| 53 | #define HAVE_EXECV 1 |
| 54 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 55 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 56 | #include <process.h> |
| 57 | #else |
| 58 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 59 | #define HAVE_EXECV 1 |
| 60 | #define HAVE_GETCWD 1 |
| 61 | #define HAVE_GETEGID 1 |
| 62 | #define HAVE_GETEUID 1 |
| 63 | #define HAVE_GETGID 1 |
| 64 | #define HAVE_GETPPID 1 |
| 65 | #define HAVE_GETUID 1 |
| 66 | #define HAVE_KILL 1 |
| 67 | #define HAVE_OPENDIR 1 |
| 68 | #define HAVE_PIPE 1 |
| 69 | #define HAVE_POPEN 1 |
| 70 | #define HAVE_SYSTEM 1 |
| 71 | #define HAVE_WAIT 1 |
| 72 | #else |
| 73 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 74 | #define HAVE_GETCWD 1 |
| 75 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 76 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 77 | #define HAVE_EXECV 1 |
| 78 | #define HAVE_PIPE 1 |
| 79 | #define HAVE_POPEN 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 82 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 83 | #else /* all other compilers */ |
| 84 | /* Unix functions that the configure script doesn't check for */ |
| 85 | #define HAVE_EXECV 1 |
| 86 | #define HAVE_FORK 1 |
| 87 | #define HAVE_GETCWD 1 |
| 88 | #define HAVE_GETEGID 1 |
| 89 | #define HAVE_GETEUID 1 |
| 90 | #define HAVE_GETGID 1 |
| 91 | #define HAVE_GETPPID 1 |
| 92 | #define HAVE_GETUID 1 |
| 93 | #define HAVE_KILL 1 |
| 94 | #define HAVE_OPENDIR 1 |
| 95 | #define HAVE_PIPE 1 |
| 96 | #define HAVE_POPEN 1 |
| 97 | #define HAVE_SYSTEM 1 |
| 98 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 99 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 100 | #endif /* _MSC_VER */ |
| 101 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 102 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 103 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 107 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 108 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 111 | #if defined(sun) && !defined(__SVR4) |
| 112 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 113 | extern int rename(const char *, const char *); |
| 114 | extern int pclose(FILE *); |
| 115 | extern int fclose(FILE *); |
| 116 | #endif |
| 117 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 118 | #ifdef NeXT |
| 119 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 120 | #undef HAVE_UNISTD_H |
| 121 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 122 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 123 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 124 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 127 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 128 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 129 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 130 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 131 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 132 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 134 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 135 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 136 | #endif |
| 137 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 138 | extern int chdir(char *); |
| 139 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 140 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 141 | extern int chdir(const char *); |
| 142 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 143 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 144 | extern int chmod(const char *, mode_t); |
| 145 | extern int chown(const char *, uid_t, gid_t); |
| 146 | extern char *getcwd(char *, int); |
| 147 | extern char *strerror(int); |
| 148 | extern int link(const char *, const char *); |
| 149 | extern int rename(const char *, const char *); |
| 150 | extern int stat(const char *, struct stat *); |
| 151 | extern int unlink(const char *); |
| 152 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 153 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 154 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 155 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 156 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 157 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 158 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 159 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 163 | #ifdef HAVE_UTIME_H |
| 164 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 165 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 167 | #ifdef HAVE_SYS_UTIME_H |
| 168 | #include <sys/utime.h> |
| 169 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 170 | #endif /* HAVE_SYS_UTIME_H */ |
| 171 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | #ifdef HAVE_SYS_TIMES_H |
| 173 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 174 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | |
| 176 | #ifdef HAVE_SYS_PARAM_H |
| 177 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 178 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 179 | |
| 180 | #ifdef HAVE_SYS_UTSNAME_H |
| 181 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 182 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | |
| 184 | #ifndef MAXPATHLEN |
| 185 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 186 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 188 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 190 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 191 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 192 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | #include <direct.h> |
| 194 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 195 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 197 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 198 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 199 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 200 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 201 | #endif |
| 202 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 204 | #endif |
| 205 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 207 | #endif |
| 208 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #include <direct.h> |
| 212 | #include <io.h> |
| 213 | #include <process.h> |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 214 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 216 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 217 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 218 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 219 | #else /* 16-bit Windows */ |
| 220 | #include <dos.h> |
| 221 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 222 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 223 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 225 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 229 | #ifdef UNION_WAIT |
| 230 | /* Emulate some macros on systems that have a union instead of macros */ |
| 231 | |
| 232 | #ifndef WIFEXITED |
| 233 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 234 | #endif |
| 235 | |
| 236 | #ifndef WEXITSTATUS |
| 237 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 238 | #endif |
| 239 | |
| 240 | #ifndef WTERMSIG |
| 241 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 242 | #endif |
| 243 | |
| 244 | #endif /* UNION_WAIT */ |
| 245 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 246 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 247 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 248 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 249 | #define USE_CTERMID_R |
| 250 | #endif |
| 251 | |
| 252 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 253 | #define USE_TMPNAM_R |
| 254 | #endif |
| 255 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 256 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 257 | #undef STAT |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 258 | #ifdef MS_WIN64 |
| 259 | # define STAT _stati64 |
| 260 | # define FSTAT _fstati64 |
| 261 | # define STRUCT_STAT struct _stati64 |
| 262 | #else |
| 263 | # define STAT stat |
| 264 | # define FSTAT fstat |
| 265 | # define STRUCT_STAT struct stat |
| 266 | #endif |
| 267 | |
| 268 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 269 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 270 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 271 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 272 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 273 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 275 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 276 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 277 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 278 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 279 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 280 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | if (d == NULL) |
| 282 | return NULL; |
| 283 | if (environ == NULL) |
| 284 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 285 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 287 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 288 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | char *p = strchr(*e, '='); |
| 290 | if (p == NULL) |
| 291 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 292 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 293 | if (k == NULL) { |
| 294 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 295 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 296 | } |
| 297 | v = PyString_FromString(p+1); |
| 298 | if (v == NULL) { |
| 299 | PyErr_Clear(); |
| 300 | Py_DECREF(k); |
| 301 | continue; |
| 302 | } |
| 303 | if (PyDict_GetItem(d, k) == NULL) { |
| 304 | if (PyDict_SetItem(d, k, v) != 0) |
| 305 | PyErr_Clear(); |
| 306 | } |
| 307 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 308 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 310 | #if defined(PYOS_OS2) |
| 311 | { |
| 312 | APIRET rc; |
| 313 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 314 | |
| 315 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 316 | 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] | 317 | PyObject *v = PyString_FromString(buffer); |
| 318 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 319 | Py_DECREF(v); |
| 320 | } |
| 321 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 322 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 323 | PyObject *v = PyString_FromString(buffer); |
| 324 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 325 | Py_DECREF(v); |
| 326 | } |
| 327 | } |
| 328 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | return d; |
| 330 | } |
| 331 | |
| 332 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 334 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 335 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 336 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 337 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 338 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 341 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 342 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 343 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 346 | #ifdef MS_WIN32 |
| 347 | static PyObject * |
| 348 | win32_error(char* function, char* filename) |
| 349 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 350 | /* XXX We should pass the function name along in the future. |
| 351 | (_winreg.c also wants to pass the function name.) |
| 352 | This would however require an additional param to the |
| 353 | Windows error object, which is non-trivial. |
| 354 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 355 | errno = GetLastError(); |
| 356 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 357 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 358 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 359 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 360 | } |
| 361 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 363 | #if defined(PYOS_OS2) |
| 364 | /********************************************************************** |
| 365 | * Helper Function to Trim and Format OS/2 Messages |
| 366 | **********************************************************************/ |
| 367 | static void |
| 368 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 369 | { |
| 370 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 371 | |
| 372 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 373 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 374 | |
| 375 | while (lastc > msgbuf && isspace(*lastc)) |
| 376 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 377 | } |
| 378 | |
| 379 | /* Add Optional Reason Text */ |
| 380 | if (reason) { |
| 381 | strcat(msgbuf, " : "); |
| 382 | strcat(msgbuf, reason); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /********************************************************************** |
| 387 | * Decode an OS/2 Operating System Error Code |
| 388 | * |
| 389 | * A convenience function to lookup an OS/2 error code and return a |
| 390 | * text message we can use to raise a Python exception. |
| 391 | * |
| 392 | * Notes: |
| 393 | * The messages for errors returned from the OS/2 kernel reside in |
| 394 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 395 | * |
| 396 | **********************************************************************/ |
| 397 | static char * |
| 398 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 399 | { |
| 400 | APIRET rc; |
| 401 | ULONG msglen; |
| 402 | |
| 403 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 404 | Py_BEGIN_ALLOW_THREADS |
| 405 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 406 | errorcode, "oso001.msg", &msglen); |
| 407 | Py_END_ALLOW_THREADS |
| 408 | |
| 409 | if (rc == NO_ERROR) |
| 410 | os2_formatmsg(msgbuf, msglen, reason); |
| 411 | else |
| 412 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 413 | |
| 414 | return msgbuf; |
| 415 | } |
| 416 | |
| 417 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 418 | errors are not in a global variable e.g. 'errno' nor are |
| 419 | they congruent with posix error numbers. */ |
| 420 | |
| 421 | static PyObject * os2_error(int code) |
| 422 | { |
| 423 | char text[1024]; |
| 424 | PyObject *v; |
| 425 | |
| 426 | os2_strerror(text, sizeof(text), code, ""); |
| 427 | |
| 428 | v = Py_BuildValue("(is)", code, text); |
| 429 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 430 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 431 | Py_DECREF(v); |
| 432 | } |
| 433 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 434 | } |
| 435 | |
| 436 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | |
| 438 | /* POSIX generic methods */ |
| 439 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 440 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 441 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 442 | { |
| 443 | int fd; |
| 444 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 445 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 446 | return NULL; |
| 447 | Py_BEGIN_ALLOW_THREADS |
| 448 | res = (*func)(fd); |
| 449 | Py_END_ALLOW_THREADS |
| 450 | if (res < 0) |
| 451 | return posix_error(); |
| 452 | Py_INCREF(Py_None); |
| 453 | return Py_None; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 458 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 459 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 460 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 461 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 462 | if (!PyArg_ParseTuple(args, format, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 464 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 465 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 466 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 467 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 468 | return posix_error_with_filename(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 469 | Py_INCREF(Py_None); |
| 470 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 473 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 474 | posix_2str(PyObject *args, char *format, |
| 475 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 476 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 477 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 478 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 479 | if (!PyArg_ParseTuple(args, format, &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 480 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 481 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 482 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 483 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 484 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 485 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 486 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 487 | Py_INCREF(Py_None); |
| 488 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 491 | /* pack a system stat C structure into the Python stat tuple |
| 492 | (used by posix_stat() and posix_fstat()) */ |
| 493 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 494 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 495 | { |
| 496 | PyObject *v = PyTuple_New(10); |
| 497 | if (v == NULL) |
| 498 | return NULL; |
| 499 | |
| 500 | PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode)); |
| 501 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 502 | PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
| 503 | #else |
| 504 | PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino)); |
| 505 | #endif |
| 506 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
| 507 | PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
| 508 | #else |
| 509 | PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev)); |
| 510 | #endif |
| 511 | PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 512 | PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 513 | PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid)); |
| 514 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 515 | PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size)); |
| 516 | #else |
| 517 | PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size)); |
| 518 | #endif |
| 519 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 520 | PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 521 | PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 522 | PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
| 523 | #else |
| 524 | PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 525 | PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 526 | PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 527 | #endif |
| 528 | |
| 529 | if (PyErr_Occurred()) { |
| 530 | Py_DECREF(v); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | return v; |
| 535 | } |
| 536 | |
| 537 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 539 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 540 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 542 | STRUCT_STAT st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 543 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 544 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 545 | |
| 546 | #ifdef MS_WIN32 |
| 547 | int pathlen; |
| 548 | char pathcopy[MAX_PATH]; |
| 549 | #endif /* MS_WIN32 */ |
| 550 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 551 | if (!PyArg_ParseTuple(args, format, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 552 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 553 | |
| 554 | #ifdef MS_WIN32 |
| 555 | pathlen = strlen(path); |
| 556 | /* the library call can blow up if the file name is too long! */ |
| 557 | if (pathlen > MAX_PATH) { |
| 558 | errno = ENAMETOOLONG; |
| 559 | return posix_error(); |
| 560 | } |
| 561 | |
| 562 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 563 | /* exception for specific or current drive root */ |
| 564 | if (!((pathlen == 1) || |
| 565 | ((pathlen == 3) && |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 566 | (path[1] == ':') && |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 567 | (path[2] == '\\' || path[2] == '/')))) |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 568 | { |
| 569 | strncpy(pathcopy, path, pathlen); |
| 570 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 571 | path = pathcopy; |
| 572 | } |
| 573 | } |
| 574 | #endif /* MS_WIN32 */ |
| 575 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 576 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 577 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 578 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 579 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 580 | return posix_error_with_filename(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 581 | |
| 582 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
| 586 | /* POSIX methods */ |
| 587 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 588 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 589 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 590 | Test for access to a file."; |
| 591 | |
| 592 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 593 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 594 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 595 | char *path; |
| 596 | int mode; |
| 597 | int res; |
| 598 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 599 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 600 | return NULL; |
| 601 | Py_BEGIN_ALLOW_THREADS |
| 602 | res = access(path, mode); |
| 603 | Py_END_ALLOW_THREADS |
| 604 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 607 | #ifndef F_OK |
| 608 | #define F_OK 0 |
| 609 | #endif |
| 610 | #ifndef R_OK |
| 611 | #define R_OK 4 |
| 612 | #endif |
| 613 | #ifndef W_OK |
| 614 | #define W_OK 2 |
| 615 | #endif |
| 616 | #ifndef X_OK |
| 617 | #define X_OK 1 |
| 618 | #endif |
| 619 | |
| 620 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 621 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 622 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 623 | Return the name of the terminal device connected to 'fd'."; |
| 624 | |
| 625 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 626 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 627 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 628 | int id; |
| 629 | char *ret; |
| 630 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 631 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 632 | return NULL; |
| 633 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 634 | ret = ttyname(id); |
| 635 | if (ret == NULL) |
| 636 | return(posix_error()); |
| 637 | return(PyString_FromString(ret)); |
| 638 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 639 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 640 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 641 | #ifdef HAVE_CTERMID |
| 642 | static char posix_ctermid__doc__[] = |
| 643 | "ctermid() -> String\n\ |
| 644 | Return the name of the controlling terminal for this process."; |
| 645 | |
| 646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 647 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 648 | { |
| 649 | char *ret; |
| 650 | char buffer[L_ctermid]; |
| 651 | |
| 652 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 653 | return NULL; |
| 654 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 655 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 656 | ret = ctermid_r(buffer); |
| 657 | #else |
| 658 | ret = ctermid(buffer); |
| 659 | #endif |
| 660 | if (ret == NULL) |
| 661 | return(posix_error()); |
| 662 | return(PyString_FromString(buffer)); |
| 663 | } |
| 664 | #endif |
| 665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 666 | static char posix_chdir__doc__[] = |
| 667 | "chdir(path) -> None\n\ |
| 668 | Change the current working directory to the specified path."; |
| 669 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 670 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 671 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 673 | return posix_1str(args, "s:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 676 | |
| 677 | static char posix_chmod__doc__[] = |
| 678 | "chmod(path, mode) -> None\n\ |
| 679 | Change the access permissions of a file."; |
| 680 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 681 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 682 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 683 | { |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 684 | char *path; |
| 685 | int i; |
| 686 | int res; |
Guido van Rossum | 49679b4 | 2000-03-31 00:48:21 +0000 | [diff] [blame] | 687 | if (!PyArg_ParseTuple(args, "si", &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 688 | return NULL; |
| 689 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 690 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 691 | Py_END_ALLOW_THREADS |
| 692 | if (res < 0) |
| 693 | return posix_error_with_filename(path); |
| 694 | Py_INCREF(Py_None); |
| 695 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 699 | #ifdef HAVE_FSYNC |
| 700 | static char posix_fsync__doc__[] = |
| 701 | "fsync(fildes) -> None\n\ |
| 702 | force write of file with filedescriptor to disk."; |
| 703 | |
| 704 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 705 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 706 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 707 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 708 | } |
| 709 | #endif /* HAVE_FSYNC */ |
| 710 | |
| 711 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 713 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 714 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 715 | #endif |
| 716 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 717 | static char posix_fdatasync__doc__[] = |
| 718 | "fdatasync(fildes) -> None\n\ |
| 719 | force write of file with filedescriptor to disk.\n\ |
| 720 | does not force update of metadata."; |
| 721 | |
| 722 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 723 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 724 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 725 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 726 | } |
| 727 | #endif /* HAVE_FDATASYNC */ |
| 728 | |
| 729 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 730 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 731 | static char posix_chown__doc__[] = |
| 732 | "chown(path, uid, gid) -> None\n\ |
| 733 | Change the owner and group id of path to the numeric uid and gid."; |
| 734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 736 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 737 | { |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 738 | char *path; |
| 739 | int uid, gid; |
| 740 | int res; |
| 741 | if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid)) |
| 742 | return NULL; |
| 743 | Py_BEGIN_ALLOW_THREADS |
| 744 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 745 | Py_END_ALLOW_THREADS |
| 746 | if (res < 0) |
| 747 | return posix_error_with_filename(path); |
| 748 | Py_INCREF(Py_None); |
| 749 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 750 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 751 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 752 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 754 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 755 | static char posix_getcwd__doc__[] = |
| 756 | "getcwd() -> path\n\ |
| 757 | Return a string representing the current working directory."; |
| 758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 760 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 761 | { |
| 762 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 763 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 764 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 765 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 766 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 767 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 768 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 769 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 771 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 772 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 773 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 776 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 777 | static char posix_link__doc__[] = |
| 778 | "link(src, dst) -> None\n\ |
| 779 | Create a hard link to a file."; |
| 780 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 781 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 782 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 783 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 784 | return posix_2str(args, "ss:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 785 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 786 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 788 | |
| 789 | static char posix_listdir__doc__[] = |
| 790 | "listdir(path) -> list_of_strings\n\ |
| 791 | Return a list containing the names of the entries in the directory.\n\ |
| 792 | \n\ |
| 793 | path: path of directory to list\n\ |
| 794 | \n\ |
| 795 | The list is in arbitrary order. It does not include the special\n\ |
| 796 | entries '.' and '..' even if they are present in the directory."; |
| 797 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 798 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 799 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 800 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 801 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 802 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 803 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 804 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 805 | char *name; |
| 806 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 807 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 808 | HANDLE hFindFile; |
| 809 | WIN32_FIND_DATA FileData; |
| 810 | char namebuf[MAX_PATH+5]; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 811 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 812 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 813 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 814 | return NULL; |
| 815 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 816 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 817 | return NULL; |
| 818 | } |
| 819 | strcpy(namebuf, name); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 820 | ch = namebuf[len-1]; |
| 821 | if (ch != '/' && ch != '\\' && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 822 | namebuf[len++] = '/'; |
| 823 | strcpy(namebuf + len, "*.*"); |
| 824 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 825 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 826 | return NULL; |
| 827 | |
| 828 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 829 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 830 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 831 | if (errno == ERROR_FILE_NOT_FOUND) |
| 832 | return PyList_New(0); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 833 | return win32_error("FindFirstFile", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 834 | } |
| 835 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 836 | if (FileData.cFileName[0] == '.' && |
| 837 | (FileData.cFileName[1] == '\0' || |
| 838 | FileData.cFileName[1] == '.' && |
| 839 | FileData.cFileName[2] == '\0')) |
| 840 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 841 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 842 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 843 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 844 | d = NULL; |
| 845 | break; |
| 846 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 847 | if (PyList_Append(d, v) != 0) { |
| 848 | Py_DECREF(v); |
| 849 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 850 | d = NULL; |
| 851 | break; |
| 852 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 853 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 854 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 855 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 856 | if (FindClose(hFindFile) == FALSE) |
| 857 | return win32_error("FindClose", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 858 | |
| 859 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 860 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 861 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 862 | |
| 863 | #ifndef MAX_PATH |
| 864 | #define MAX_PATH 250 |
| 865 | #endif |
| 866 | char *name, *pt; |
| 867 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 868 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 869 | char namebuf[MAX_PATH+5]; |
| 870 | struct _find_t ep; |
| 871 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 872 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 873 | return NULL; |
| 874 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 875 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 876 | return NULL; |
| 877 | } |
| 878 | strcpy(namebuf, name); |
| 879 | for (pt = namebuf; *pt; pt++) |
| 880 | if (*pt == '/') |
| 881 | *pt = '\\'; |
| 882 | if (namebuf[len-1] != '\\') |
| 883 | namebuf[len++] = '\\'; |
| 884 | strcpy(namebuf + len, "*.*"); |
| 885 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 886 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 887 | return NULL; |
| 888 | |
| 889 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 890 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 891 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 892 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 893 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 894 | } |
| 895 | do { |
| 896 | if (ep.name[0] == '.' && |
| 897 | (ep.name[1] == '\0' || |
| 898 | ep.name[1] == '.' && |
| 899 | ep.name[2] == '\0')) |
| 900 | continue; |
| 901 | strcpy(namebuf, ep.name); |
| 902 | for (pt = namebuf; *pt; pt++) |
| 903 | if (isupper(*pt)) |
| 904 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 905 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 906 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 907 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 908 | d = NULL; |
| 909 | break; |
| 910 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 911 | if (PyList_Append(d, v) != 0) { |
| 912 | Py_DECREF(v); |
| 913 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 914 | d = NULL; |
| 915 | break; |
| 916 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 917 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 918 | } while (_dos_findnext(&ep) == 0); |
| 919 | |
| 920 | return d; |
| 921 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 922 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 923 | |
| 924 | #ifndef MAX_PATH |
| 925 | #define MAX_PATH CCHMAXPATH |
| 926 | #endif |
| 927 | char *name, *pt; |
| 928 | int len; |
| 929 | PyObject *d, *v; |
| 930 | char namebuf[MAX_PATH+5]; |
| 931 | HDIR hdir = 1; |
| 932 | ULONG srchcnt = 1; |
| 933 | FILEFINDBUF3 ep; |
| 934 | APIRET rc; |
| 935 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 936 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 937 | return NULL; |
| 938 | if (len >= MAX_PATH) { |
| 939 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 940 | return NULL; |
| 941 | } |
| 942 | strcpy(namebuf, name); |
| 943 | for (pt = namebuf; *pt; pt++) |
| 944 | if (*pt == '/') |
| 945 | *pt = '\\'; |
| 946 | if (namebuf[len-1] != '\\') |
| 947 | namebuf[len++] = '\\'; |
| 948 | strcpy(namebuf + len, "*.*"); |
| 949 | |
| 950 | if ((d = PyList_New(0)) == NULL) |
| 951 | return NULL; |
| 952 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 953 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 954 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 955 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 956 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 957 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 958 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 959 | |
| 960 | if (rc != NO_ERROR) { |
| 961 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 962 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 965 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 966 | do { |
| 967 | if (ep.achName[0] == '.' |
| 968 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 969 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 970 | |
| 971 | strcpy(namebuf, ep.achName); |
| 972 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 973 | /* Leave Case of Name Alone -- In Native Form */ |
| 974 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 975 | |
| 976 | v = PyString_FromString(namebuf); |
| 977 | if (v == NULL) { |
| 978 | Py_DECREF(d); |
| 979 | d = NULL; |
| 980 | break; |
| 981 | } |
| 982 | if (PyList_Append(d, v) != 0) { |
| 983 | Py_DECREF(v); |
| 984 | Py_DECREF(d); |
| 985 | d = NULL; |
| 986 | break; |
| 987 | } |
| 988 | Py_DECREF(v); |
| 989 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 990 | } |
| 991 | |
| 992 | return d; |
| 993 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 994 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 995 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 996 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 997 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 998 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 999 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1000 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1001 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1002 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1003 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1004 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1005 | closedir(dirp); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1009 | if (ep->d_name[0] == '.' && |
| 1010 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1011 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1012 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1013 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1014 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1015 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1016 | d = NULL; |
| 1017 | break; |
| 1018 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1019 | if (PyList_Append(d, v) != 0) { |
| 1020 | Py_DECREF(v); |
| 1021 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1022 | d = NULL; |
| 1023 | break; |
| 1024 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1025 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1026 | } |
| 1027 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1028 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1029 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1030 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1031 | #endif /* which OS */ |
| 1032 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1034 | static char posix_mkdir__doc__[] = |
| 1035 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1036 | Create a directory."; |
| 1037 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1039 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1040 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1041 | int res; |
| 1042 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1043 | int mode = 0777; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1044 | if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1045 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1046 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1047 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1048 | res = mkdir(path); |
| 1049 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1050 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1051 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1052 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1053 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1054 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1055 | Py_INCREF(Py_None); |
| 1056 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1060 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1061 | static char posix_nice__doc__[] = |
| 1062 | "nice(inc) -> new_priority\n\ |
| 1063 | Decrease the priority of process and return new priority."; |
| 1064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1066 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1067 | { |
| 1068 | int increment, value; |
| 1069 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1070 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1071 | return NULL; |
| 1072 | value = nice(increment); |
| 1073 | if (value == -1) |
| 1074 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1075 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1076 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1077 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1078 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1079 | |
| 1080 | static char posix_rename__doc__[] = |
| 1081 | "rename(old, new) -> None\n\ |
| 1082 | Rename a file or directory."; |
| 1083 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1084 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1085 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1086 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1087 | return posix_2str(args, "ss:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1090 | |
| 1091 | static char posix_rmdir__doc__[] = |
| 1092 | "rmdir(path) -> None\n\ |
| 1093 | Remove a directory."; |
| 1094 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1095 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1096 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1097 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1098 | return posix_1str(args, "s:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1101 | |
| 1102 | static char posix_stat__doc__[] = |
| 1103 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1104 | Perform a stat system call on the given path."; |
| 1105 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1107 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1108 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1109 | return posix_do_stat(self, args, "s:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1112 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1113 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1114 | static char posix_system__doc__[] = |
| 1115 | "system(command) -> exit_status\n\ |
| 1116 | Execute the command (a string) in a subshell."; |
| 1117 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1118 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1119 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1120 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1121 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1122 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1123 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1124 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1125 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1126 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1127 | Py_END_ALLOW_THREADS |
| 1128 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1129 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1130 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1131 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1132 | |
| 1133 | static char posix_umask__doc__[] = |
| 1134 | "umask(new_mask) -> old_mask\n\ |
| 1135 | Set the current numeric umask and return the previous umask."; |
| 1136 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1137 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1138 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | { |
| 1140 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1141 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1142 | return NULL; |
| 1143 | i = umask(i); |
| 1144 | if (i < 0) |
| 1145 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1146 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1149 | |
| 1150 | static char posix_unlink__doc__[] = |
| 1151 | "unlink(path) -> None\n\ |
| 1152 | Remove a file (same as remove(path))."; |
| 1153 | |
| 1154 | static char posix_remove__doc__[] = |
| 1155 | "remove(path) -> None\n\ |
| 1156 | Remove a file (same as unlink(path))."; |
| 1157 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1158 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1159 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1160 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1161 | return posix_1str(args, "s:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1165 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1166 | static char posix_uname__doc__[] = |
| 1167 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1168 | Return a tuple identifying the current operating system."; |
| 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_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1172 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1173 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1174 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1175 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1176 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1177 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1178 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1179 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1180 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1181 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1182 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1183 | u.sysname, |
| 1184 | u.nodename, |
| 1185 | u.release, |
| 1186 | u.version, |
| 1187 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1188 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1189 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1190 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1191 | |
| 1192 | static char posix_utime__doc__[] = |
| 1193 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1194 | utime(path, None) -> None\n\ |
| 1195 | Set the access and modified time of the file to the given values. If the\n\ |
| 1196 | 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] | 1197 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1198 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1199 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1200 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1201 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1202 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1203 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1204 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1206 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1207 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1208 | struct utimbuf buf; |
| 1209 | #define ATIME buf.actime |
| 1210 | #define MTIME buf.modtime |
| 1211 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1212 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1213 | time_t buf[2]; |
| 1214 | #define ATIME buf[0] |
| 1215 | #define MTIME buf[1] |
| 1216 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1217 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1218 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1219 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1220 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1221 | if (arg == Py_None) { |
| 1222 | /* optional time values not given */ |
| 1223 | Py_BEGIN_ALLOW_THREADS |
| 1224 | res = utime(path, NULL); |
| 1225 | Py_END_ALLOW_THREADS |
| 1226 | } |
| 1227 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1228 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1229 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1230 | return NULL; |
| 1231 | } |
| 1232 | else { |
| 1233 | ATIME = atime; |
| 1234 | MTIME = mtime; |
| 1235 | Py_BEGIN_ALLOW_THREADS |
| 1236 | res = utime(path, UTIME_ARG); |
| 1237 | Py_END_ALLOW_THREADS |
| 1238 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1239 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1240 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1241 | Py_INCREF(Py_None); |
| 1242 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1243 | #undef UTIME_ARG |
| 1244 | #undef ATIME |
| 1245 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1248 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1249 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1251 | static char posix__exit__doc__[] = |
| 1252 | "_exit(status)\n\ |
| 1253 | Exit to the system with specified status, without normal exit processing."; |
| 1254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1256 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1257 | { |
| 1258 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1259 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1260 | return NULL; |
| 1261 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1262 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1266 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1267 | static char posix_execv__doc__[] = |
| 1268 | "execv(path, args)\n\ |
| 1269 | Execute an executable path with arguments, replacing current process.\n\ |
| 1270 | \n\ |
| 1271 | path: path of executable file\n\ |
| 1272 | args: tuple or list of strings"; |
| 1273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1274 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1275 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1276 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1277 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1278 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1279 | char **argvlist; |
| 1280 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1281 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1282 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1283 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1284 | argv is a list or tuple of strings. */ |
| 1285 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1286 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1287 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1288 | if (PyList_Check(argv)) { |
| 1289 | argc = PyList_Size(argv); |
| 1290 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1291 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1292 | else if (PyTuple_Check(argv)) { |
| 1293 | argc = PyTuple_Size(argv); |
| 1294 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1295 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1296 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1297 | 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] | 1298 | return NULL; |
| 1299 | } |
| 1300 | |
| 1301 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1302 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1303 | return NULL; |
| 1304 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1306 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1307 | if (argvlist == NULL) |
| 1308 | return NULL; |
| 1309 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1310 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1311 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1312 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1313 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1316 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1317 | } |
| 1318 | argvlist[argc] = NULL; |
| 1319 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1320 | #ifdef BAD_EXEC_PROTOTYPES |
| 1321 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1322 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1323 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1324 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1325 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1326 | /* If we get here it's definitely an error */ |
| 1327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1328 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1329 | return posix_error(); |
| 1330 | } |
| 1331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1332 | |
| 1333 | static char posix_execve__doc__[] = |
| 1334 | "execve(path, args, env)\n\ |
| 1335 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1336 | \n\ |
| 1337 | path: path of executable file\n\ |
| 1338 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1339 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1340 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1341 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1342 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1343 | { |
| 1344 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1345 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1346 | char **argvlist; |
| 1347 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1348 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1349 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1350 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1351 | |
| 1352 | /* execve has three arguments: (path, argv, env), where |
| 1353 | argv is a list or tuple of strings and env is a dictionary |
| 1354 | like posix.environ. */ |
| 1355 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1356 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1357 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1358 | if (PyList_Check(argv)) { |
| 1359 | argc = PyList_Size(argv); |
| 1360 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1361 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1362 | else if (PyTuple_Check(argv)) { |
| 1363 | argc = PyTuple_Size(argv); |
| 1364 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1365 | } |
| 1366 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1367 | 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] | 1368 | return NULL; |
| 1369 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1370 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1371 | 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] | 1372 | return NULL; |
| 1373 | } |
| 1374 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1375 | if (argc == 0) { |
| 1376 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1377 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | } |
| 1380 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1381 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1382 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1383 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1384 | return NULL; |
| 1385 | } |
| 1386 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1387 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1388 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1389 | &argvlist[i])) |
| 1390 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1391 | goto fail_1; |
| 1392 | } |
| 1393 | } |
| 1394 | argvlist[argc] = NULL; |
| 1395 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1396 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1397 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1398 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1399 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1400 | goto fail_1; |
| 1401 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1402 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1403 | keys = PyMapping_Keys(env); |
| 1404 | vals = PyMapping_Values(env); |
| 1405 | if (!keys || !vals) |
| 1406 | goto fail_2; |
| 1407 | |
| 1408 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1409 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1410 | |
| 1411 | key = PyList_GetItem(keys, pos); |
| 1412 | val = PyList_GetItem(vals, pos); |
| 1413 | if (!key || !val) |
| 1414 | goto fail_2; |
| 1415 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1416 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1417 | !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] | 1418 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1419 | goto fail_2; |
| 1420 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1421 | |
| 1422 | #if defined(PYOS_OS2) |
| 1423 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1424 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1425 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1426 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1427 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1428 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1429 | goto fail_2; |
| 1430 | } |
| 1431 | sprintf(p, "%s=%s", k, v); |
| 1432 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1433 | #if defined(PYOS_OS2) |
| 1434 | } |
| 1435 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1436 | } |
| 1437 | envlist[envc] = 0; |
| 1438 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1439 | |
| 1440 | #ifdef BAD_EXEC_PROTOTYPES |
| 1441 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1442 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1443 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1444 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1445 | |
| 1446 | /* If we get here it's definitely an error */ |
| 1447 | |
| 1448 | (void) posix_error(); |
| 1449 | |
| 1450 | fail_2: |
| 1451 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1452 | PyMem_DEL(envlist[envc]); |
| 1453 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1454 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1455 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1456 | Py_XDECREF(vals); |
| 1457 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1458 | return NULL; |
| 1459 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1460 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1462 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1463 | #ifdef HAVE_SPAWNV |
| 1464 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1465 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1466 | Execute an executable path with arguments, replacing current process.\n\ |
| 1467 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1468 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1469 | path: path of executable file\n\ |
| 1470 | args: tuple or list of strings"; |
| 1471 | |
| 1472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1473 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1474 | { |
| 1475 | char *path; |
| 1476 | PyObject *argv; |
| 1477 | char **argvlist; |
| 1478 | int mode, i, argc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1479 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1480 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1481 | |
| 1482 | /* spawnv has three arguments: (mode, path, argv), where |
| 1483 | argv is a list or tuple of strings. */ |
| 1484 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1485 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1486 | return NULL; |
| 1487 | if (PyList_Check(argv)) { |
| 1488 | argc = PyList_Size(argv); |
| 1489 | getitem = PyList_GetItem; |
| 1490 | } |
| 1491 | else if (PyTuple_Check(argv)) { |
| 1492 | argc = PyTuple_Size(argv); |
| 1493 | getitem = PyTuple_GetItem; |
| 1494 | } |
| 1495 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1496 | 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] | 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | argvlist = PyMem_NEW(char *, argc+1); |
| 1501 | if (argvlist == NULL) |
| 1502 | return NULL; |
| 1503 | for (i = 0; i < argc; i++) { |
| 1504 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1505 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1506 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1507 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1508 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | argvlist[argc] = NULL; |
| 1512 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1513 | if (mode == _OLD_P_OVERLAY) |
| 1514 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1515 | spawnval = _spawnv(mode, path, argvlist); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1516 | |
| 1517 | PyMem_DEL(argvlist); |
| 1518 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1519 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1520 | return posix_error(); |
| 1521 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1522 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1523 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1524 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1525 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1526 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | |
| 1530 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1531 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1532 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1533 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1534 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1535 | path: path of executable file\n\ |
| 1536 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1537 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1538 | |
| 1539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1540 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1541 | { |
| 1542 | char *path; |
| 1543 | PyObject *argv, *env; |
| 1544 | char **argvlist; |
| 1545 | char **envlist; |
| 1546 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1547 | int mode, i, pos, argc, envc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1548 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1549 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1550 | |
| 1551 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1552 | argv is a list or tuple of strings and env is a dictionary |
| 1553 | like posix.environ. */ |
| 1554 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1555 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1556 | return NULL; |
| 1557 | if (PyList_Check(argv)) { |
| 1558 | argc = PyList_Size(argv); |
| 1559 | getitem = PyList_GetItem; |
| 1560 | } |
| 1561 | else if (PyTuple_Check(argv)) { |
| 1562 | argc = PyTuple_Size(argv); |
| 1563 | getitem = PyTuple_GetItem; |
| 1564 | } |
| 1565 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1566 | 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] | 1567 | return NULL; |
| 1568 | } |
| 1569 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1570 | 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] | 1571 | return NULL; |
| 1572 | } |
| 1573 | |
| 1574 | argvlist = PyMem_NEW(char *, argc+1); |
| 1575 | if (argvlist == NULL) { |
| 1576 | PyErr_NoMemory(); |
| 1577 | return NULL; |
| 1578 | } |
| 1579 | for (i = 0; i < argc; i++) { |
| 1580 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1581 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1582 | &argvlist[i])) |
| 1583 | { |
| 1584 | goto fail_1; |
| 1585 | } |
| 1586 | } |
| 1587 | argvlist[argc] = NULL; |
| 1588 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1589 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1590 | envlist = PyMem_NEW(char *, i + 1); |
| 1591 | if (envlist == NULL) { |
| 1592 | PyErr_NoMemory(); |
| 1593 | goto fail_1; |
| 1594 | } |
| 1595 | envc = 0; |
| 1596 | keys = PyMapping_Keys(env); |
| 1597 | vals = PyMapping_Values(env); |
| 1598 | if (!keys || !vals) |
| 1599 | goto fail_2; |
| 1600 | |
| 1601 | for (pos = 0; pos < i; pos++) { |
| 1602 | char *p, *k, *v; |
| 1603 | |
| 1604 | key = PyList_GetItem(keys, pos); |
| 1605 | val = PyList_GetItem(vals, pos); |
| 1606 | if (!key || !val) |
| 1607 | goto fail_2; |
| 1608 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1609 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1610 | !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] | 1611 | { |
| 1612 | goto fail_2; |
| 1613 | } |
| 1614 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1615 | if (p == NULL) { |
| 1616 | PyErr_NoMemory(); |
| 1617 | goto fail_2; |
| 1618 | } |
| 1619 | sprintf(p, "%s=%s", k, v); |
| 1620 | envlist[envc++] = p; |
| 1621 | } |
| 1622 | envlist[envc] = 0; |
| 1623 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1624 | if (mode == _OLD_P_OVERLAY) |
| 1625 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1626 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 1627 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1628 | (void) posix_error(); |
| 1629 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1630 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1631 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1632 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1633 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1634 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1635 | |
| 1636 | fail_2: |
| 1637 | while (--envc >= 0) |
| 1638 | PyMem_DEL(envlist[envc]); |
| 1639 | PyMem_DEL(envlist); |
| 1640 | fail_1: |
| 1641 | PyMem_DEL(argvlist); |
| 1642 | Py_XDECREF(vals); |
| 1643 | Py_XDECREF(keys); |
| 1644 | return res; |
| 1645 | } |
| 1646 | #endif /* HAVE_SPAWNV */ |
| 1647 | |
| 1648 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1649 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1650 | static char posix_fork__doc__[] = |
| 1651 | "fork() -> pid\n\ |
| 1652 | Fork a child process.\n\ |
| 1653 | \n\ |
| 1654 | Return 0 to child process and PID of child to parent process."; |
| 1655 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1656 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1657 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1658 | { |
| 1659 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1660 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1661 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1662 | pid = fork(); |
| 1663 | if (pid == -1) |
| 1664 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1665 | if (pid == 0) |
| 1666 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1667 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1668 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1669 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1670 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1671 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1672 | #ifdef HAVE_PTY_H |
| 1673 | #include <pty.h> |
| 1674 | #else |
| 1675 | #ifdef HAVE_LIBUTIL_H |
| 1676 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1677 | #endif /* HAVE_LIBUTIL_H */ |
| 1678 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1679 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1680 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1681 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1682 | static char posix_openpty__doc__[] = |
| 1683 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1684 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1685 | |
| 1686 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1687 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1688 | { |
| 1689 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1690 | #ifndef HAVE_OPENPTY |
| 1691 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1692 | #endif |
| 1693 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1694 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1695 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1696 | |
| 1697 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1698 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1699 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1700 | #else |
| 1701 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1702 | if (slave_name == NULL) |
| 1703 | return posix_error(); |
| 1704 | |
| 1705 | slave_fd = open(slave_name, O_RDWR); |
| 1706 | if (slave_fd < 0) |
| 1707 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1708 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1709 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1710 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1711 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1712 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1713 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1714 | |
| 1715 | #ifdef HAVE_FORKPTY |
| 1716 | static char posix_forkpty__doc__[] = |
| 1717 | "forkpty() -> (pid, master_fd)\n\ |
| 1718 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1719 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1720 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1721 | |
| 1722 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1723 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1724 | { |
| 1725 | int master_fd, pid; |
| 1726 | |
| 1727 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1728 | return NULL; |
| 1729 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1730 | if (pid == -1) |
| 1731 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1732 | if (pid == 0) |
| 1733 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1734 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1735 | } |
| 1736 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1738 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1739 | static char posix_getegid__doc__[] = |
| 1740 | "getegid() -> egid\n\ |
| 1741 | Return the current process's effective group id."; |
| 1742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1743 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1744 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1745 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1746 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1747 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1748 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1749 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1750 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1751 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1752 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1753 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1754 | static char posix_geteuid__doc__[] = |
| 1755 | "geteuid() -> euid\n\ |
| 1756 | Return the current process's effective user id."; |
| 1757 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1758 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1759 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1760 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1761 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1762 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1763 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1764 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1765 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1766 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1767 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1768 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1769 | static char posix_getgid__doc__[] = |
| 1770 | "getgid() -> gid\n\ |
| 1771 | Return the current process's group id."; |
| 1772 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1773 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1774 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1775 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1776 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1777 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1778 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1779 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1780 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1781 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1782 | |
| 1783 | static char posix_getpid__doc__[] = |
| 1784 | "getpid() -> pid\n\ |
| 1785 | Return the current process id"; |
| 1786 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1788 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1789 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1790 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1791 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1792 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1795 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1796 | #ifdef HAVE_GETGROUPS |
| 1797 | static char posix_getgroups__doc__[] = "\ |
| 1798 | getgroups() -> list of group IDs\n\ |
| 1799 | Return list of supplemental group IDs for the process."; |
| 1800 | |
| 1801 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1802 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1803 | { |
| 1804 | PyObject *result = NULL; |
| 1805 | |
| 1806 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 1807 | #ifdef NGROUPS_MAX |
| 1808 | #define MAX_GROUPS NGROUPS_MAX |
| 1809 | #else |
| 1810 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 1811 | #define MAX_GROUPS 64 |
| 1812 | #endif |
| 1813 | gid_t grouplist[MAX_GROUPS]; |
| 1814 | int n; |
| 1815 | |
| 1816 | n = getgroups(MAX_GROUPS, grouplist); |
| 1817 | if (n < 0) |
| 1818 | posix_error(); |
| 1819 | else { |
| 1820 | result = PyList_New(n); |
| 1821 | if (result != NULL) { |
| 1822 | PyObject *o; |
| 1823 | int i; |
| 1824 | for (i = 0; i < n; ++i) { |
| 1825 | o = PyInt_FromLong((long)grouplist[i]); |
| 1826 | if (o == NULL) { |
| 1827 | Py_DECREF(result); |
| 1828 | result = NULL; |
| 1829 | break; |
| 1830 | } |
| 1831 | PyList_SET_ITEM(result, i, o); |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | return result; |
| 1837 | } |
| 1838 | #endif |
| 1839 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1840 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1841 | static char posix_getpgrp__doc__[] = |
| 1842 | "getpgrp() -> pgrp\n\ |
| 1843 | Return the current process group id."; |
| 1844 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1845 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1846 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1847 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1848 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1849 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1850 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1851 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1852 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1853 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1854 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1855 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1856 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1857 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1858 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1859 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1860 | static char posix_setpgrp__doc__[] = |
| 1861 | "setpgrp() -> None\n\ |
| 1862 | Make this process a session leader."; |
| 1863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1865 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1866 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1867 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1868 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1869 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1870 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1871 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1872 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1873 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1874 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1875 | Py_INCREF(Py_None); |
| 1876 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1879 | #endif /* HAVE_SETPGRP */ |
| 1880 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1881 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1882 | static char posix_getppid__doc__[] = |
| 1883 | "getppid() -> ppid\n\ |
| 1884 | Return the parent's process id."; |
| 1885 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1886 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1887 | posix_getppid(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, ":getppid")) |
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)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1892 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1893 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1894 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1895 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1896 | #ifdef HAVE_GETLOGIN |
| 1897 | static char posix_getlogin__doc__[] = "\ |
| 1898 | getlogin() -> string\n\ |
| 1899 | Return the actual login name."; |
| 1900 | |
| 1901 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1902 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1903 | { |
| 1904 | PyObject *result = NULL; |
| 1905 | |
| 1906 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 1907 | char *name; |
| 1908 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1909 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 1910 | errno = 0; |
| 1911 | name = getlogin(); |
| 1912 | if (name == NULL) { |
| 1913 | if (errno) |
| 1914 | posix_error(); |
| 1915 | else |
| 1916 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 1917 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 1918 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1919 | else |
| 1920 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 1921 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1922 | } |
| 1923 | return result; |
| 1924 | } |
| 1925 | #endif |
| 1926 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1927 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1928 | static char posix_getuid__doc__[] = |
| 1929 | "getuid() -> uid\n\ |
| 1930 | Return the current process's user id."; |
| 1931 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1932 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1933 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1934 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1935 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1936 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1937 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1938 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1939 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1940 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1941 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1942 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1943 | static char posix_kill__doc__[] = |
| 1944 | "kill(pid, sig) -> None\n\ |
| 1945 | Kill a process with a signal."; |
| 1946 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1947 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1948 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1949 | { |
| 1950 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1951 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1952 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1953 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1954 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 1955 | APIRET rc; |
| 1956 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1957 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1958 | |
| 1959 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 1960 | APIRET rc; |
| 1961 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1962 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1963 | |
| 1964 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1965 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1966 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1967 | if (kill(pid, sig) == -1) |
| 1968 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1969 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1970 | Py_INCREF(Py_None); |
| 1971 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1972 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1973 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1974 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1975 | #ifdef HAVE_PLOCK |
| 1976 | |
| 1977 | #ifdef HAVE_SYS_LOCK_H |
| 1978 | #include <sys/lock.h> |
| 1979 | #endif |
| 1980 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | static char posix_plock__doc__[] = |
| 1982 | "plock(op) -> None\n\ |
| 1983 | Lock program segments into memory."; |
| 1984 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1985 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1986 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1987 | { |
| 1988 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1989 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1990 | return NULL; |
| 1991 | if (plock(op) == -1) |
| 1992 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1993 | Py_INCREF(Py_None); |
| 1994 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1995 | } |
| 1996 | #endif |
| 1997 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1998 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1999 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2000 | static char posix_popen__doc__[] = |
| 2001 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2002 | Open a pipe to/from a command returning a file object."; |
| 2003 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2004 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2005 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2006 | async_system(const char *command) |
| 2007 | { |
| 2008 | char *p, errormsg[256], args[1024]; |
| 2009 | RESULTCODES rcodes; |
| 2010 | APIRET rc; |
| 2011 | char *shell = getenv("COMSPEC"); |
| 2012 | if (!shell) |
| 2013 | shell = "cmd"; |
| 2014 | |
| 2015 | strcpy(args, shell); |
| 2016 | p = &args[ strlen(args)+1 ]; |
| 2017 | strcpy(p, "/c "); |
| 2018 | strcat(p, command); |
| 2019 | p += strlen(p) + 1; |
| 2020 | *p = '\0'; |
| 2021 | |
| 2022 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2023 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2024 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2025 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2026 | &rcodes, shell); |
| 2027 | return rc; |
| 2028 | } |
| 2029 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2030 | static FILE * |
| 2031 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2032 | { |
| 2033 | HFILE rhan, whan; |
| 2034 | FILE *retfd = NULL; |
| 2035 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2036 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2037 | if (rc != NO_ERROR) { |
| 2038 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2039 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2040 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2041 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2042 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2043 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2045 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2046 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2047 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2048 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2049 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2052 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2055 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2056 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2058 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2059 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2060 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2061 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2062 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2063 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2064 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2065 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2066 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2067 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2068 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2071 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2074 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2075 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2076 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2077 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2078 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2079 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2080 | } else { |
| 2081 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2082 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2083 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2087 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2088 | { |
| 2089 | char *name; |
| 2090 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2091 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2092 | FILE *fp; |
| 2093 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2094 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2095 | return NULL; |
| 2096 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2097 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2098 | Py_END_ALLOW_THREADS |
| 2099 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2100 | return os2_error(err); |
| 2101 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2102 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2103 | if (f != NULL) |
| 2104 | PyFile_SetBufSize(f, bufsize); |
| 2105 | return f; |
| 2106 | } |
| 2107 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2108 | #elif defined(MS_WIN32) |
| 2109 | |
| 2110 | /* |
| 2111 | * Portable 'popen' replacement for Win32. |
| 2112 | * |
| 2113 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2114 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2115 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2116 | */ |
| 2117 | |
| 2118 | #include <malloc.h> |
| 2119 | #include <io.h> |
| 2120 | #include <fcntl.h> |
| 2121 | |
| 2122 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2123 | #define POPEN_1 1 |
| 2124 | #define POPEN_2 2 |
| 2125 | #define POPEN_3 3 |
| 2126 | #define POPEN_4 4 |
| 2127 | |
| 2128 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2129 | static int _PyPclose(FILE *file); |
| 2130 | |
| 2131 | /* |
| 2132 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2133 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2134 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2135 | */ |
| 2136 | static PyObject *_PyPopenProcs = NULL; |
| 2137 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2138 | |
| 2139 | /* popen that works from a GUI. |
| 2140 | * |
| 2141 | * The result of this function is a pipe (file) connected to the |
| 2142 | * processes stdin or stdout, depending on the requested mode. |
| 2143 | */ |
| 2144 | |
| 2145 | static PyObject * |
| 2146 | posix_popen(PyObject *self, PyObject *args) |
| 2147 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2148 | PyObject *f, *s; |
| 2149 | int tm = 0; |
| 2150 | |
| 2151 | char *cmdstring; |
| 2152 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2153 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2154 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2155 | return NULL; |
| 2156 | |
| 2157 | s = PyTuple_New(0); |
| 2158 | |
| 2159 | if (*mode == 'r') |
| 2160 | tm = _O_RDONLY; |
| 2161 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2162 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2163 | return NULL; |
| 2164 | } else |
| 2165 | tm = _O_WRONLY; |
| 2166 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2167 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2168 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2169 | return NULL; |
| 2170 | } |
| 2171 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2172 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2173 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2174 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2175 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2176 | else |
| 2177 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2178 | |
| 2179 | return f; |
| 2180 | } |
| 2181 | |
| 2182 | /* Variation on win32pipe.popen |
| 2183 | * |
| 2184 | * The result of this function is a pipe (file) connected to the |
| 2185 | * process's stdin, and a pipe connected to the process's stdout. |
| 2186 | */ |
| 2187 | |
| 2188 | static PyObject * |
| 2189 | win32_popen2(PyObject *self, PyObject *args) |
| 2190 | { |
| 2191 | PyObject *f; |
| 2192 | int tm=0; |
| 2193 | |
| 2194 | char *cmdstring; |
| 2195 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2196 | int bufsize = -1; |
| 2197 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2198 | return NULL; |
| 2199 | |
| 2200 | if (*mode == 't') |
| 2201 | tm = _O_TEXT; |
| 2202 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2203 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2204 | return NULL; |
| 2205 | } else |
| 2206 | tm = _O_BINARY; |
| 2207 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2208 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2209 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2210 | return NULL; |
| 2211 | } |
| 2212 | |
| 2213 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2214 | |
| 2215 | return f; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2220 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2221 | * The result of this function is 3 pipes - the process's stdin, |
| 2222 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2223 | */ |
| 2224 | |
| 2225 | static PyObject * |
| 2226 | win32_popen3(PyObject *self, PyObject *args) |
| 2227 | { |
| 2228 | PyObject *f; |
| 2229 | int tm = 0; |
| 2230 | |
| 2231 | char *cmdstring; |
| 2232 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2233 | int bufsize = -1; |
| 2234 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2235 | return NULL; |
| 2236 | |
| 2237 | if (*mode == 't') |
| 2238 | tm = _O_TEXT; |
| 2239 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2240 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2241 | return NULL; |
| 2242 | } else |
| 2243 | tm = _O_BINARY; |
| 2244 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2245 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2246 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2247 | return NULL; |
| 2248 | } |
| 2249 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2250 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2251 | |
| 2252 | return f; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * Variation on win32pipe.popen |
| 2257 | * |
| 2258 | * The result of this function is 2 pipes - the processes stdin, |
| 2259 | * and stdout+stderr combined as a single pipe. |
| 2260 | */ |
| 2261 | |
| 2262 | static PyObject * |
| 2263 | win32_popen4(PyObject *self, PyObject *args) |
| 2264 | { |
| 2265 | PyObject *f; |
| 2266 | int tm = 0; |
| 2267 | |
| 2268 | char *cmdstring; |
| 2269 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2270 | int bufsize = -1; |
| 2271 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2272 | return NULL; |
| 2273 | |
| 2274 | if (*mode == 't') |
| 2275 | tm = _O_TEXT; |
| 2276 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2277 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2278 | return NULL; |
| 2279 | } else |
| 2280 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2281 | |
| 2282 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2283 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2284 | return NULL; |
| 2285 | } |
| 2286 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2287 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2288 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2289 | return f; |
| 2290 | } |
| 2291 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2292 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2293 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2294 | HANDLE hStdin, |
| 2295 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2296 | HANDLE hStderr, |
| 2297 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2298 | { |
| 2299 | PROCESS_INFORMATION piProcInfo; |
| 2300 | STARTUPINFO siStartInfo; |
| 2301 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2302 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2303 | int i; |
| 2304 | int x; |
| 2305 | |
| 2306 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 2307 | s1 = (char *)_alloca(i); |
| 2308 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2309 | return x; |
| 2310 | if (GetVersion() < 0x80000000) { |
| 2311 | /* |
| 2312 | * NT/2000 |
| 2313 | */ |
| 2314 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2315 | s2 = (char *)_alloca(x); |
| 2316 | ZeroMemory(s2, x); |
| 2317 | sprintf(s2, "%s%s%s", s1, s3, cmdstring); |
| 2318 | } |
| 2319 | else { |
| 2320 | /* |
| 2321 | * Oh gag, we're on Win9x. Use the workaround listed in |
| 2322 | * KB: Q150956 |
| 2323 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2324 | char modulepath[_MAX_PATH]; |
| 2325 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2326 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2327 | for (i = x = 0; modulepath[i]; i++) |
| 2328 | if (modulepath[i] == '\\') |
| 2329 | x = i+1; |
| 2330 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2331 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 2332 | strncat(modulepath, |
| 2333 | szConsoleSpawn, |
| 2334 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2335 | -strlen(modulepath)); |
| 2336 | if (stat(modulepath, &statinfo) != 0) { |
| 2337 | /* Eeek - file-not-found - possibly an embedding |
| 2338 | situation - see if we can locate it in sys.prefix |
| 2339 | */ |
| 2340 | strncpy(modulepath, |
| 2341 | Py_GetExecPrefix(), |
| 2342 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 2343 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 2344 | strcat(modulepath, "\\"); |
| 2345 | strncat(modulepath, |
| 2346 | szConsoleSpawn, |
| 2347 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2348 | -strlen(modulepath)); |
| 2349 | /* No where else to look - raise an easily identifiable |
| 2350 | error, rather than leaving Windows to report |
| 2351 | "file not found" - as the user is probably blissfully |
| 2352 | unaware this shim EXE is used, and it will confuse them. |
| 2353 | (well, it confused me for a while ;-) |
| 2354 | */ |
| 2355 | if (stat(modulepath, &statinfo) != 0) { |
| 2356 | PyErr_Format(PyExc_RuntimeError, |
| 2357 | "Can not locate '%s' which is needed " |
| 2358 | "for popen to work on this platform.", |
| 2359 | szConsoleSpawn); |
| 2360 | return FALSE; |
| 2361 | } |
| 2362 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2363 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2364 | strlen(modulepath) + |
| 2365 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2366 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2367 | s2 = (char *)_alloca(x); |
| 2368 | ZeroMemory(s2, x); |
| 2369 | sprintf( |
| 2370 | s2, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2371 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2372 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2373 | s1, |
| 2374 | s3, |
| 2375 | cmdstring); |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2380 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2381 | else { |
| 2382 | PyErr_SetString(PyExc_RuntimeError, "Can not locate a COMSPEC environment variable to use as the shell"); |
| 2383 | return FALSE; |
| 2384 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2385 | |
| 2386 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2387 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2388 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2389 | siStartInfo.hStdInput = hStdin; |
| 2390 | siStartInfo.hStdOutput = hStdout; |
| 2391 | siStartInfo.hStdError = hStderr; |
| 2392 | siStartInfo.wShowWindow = SW_HIDE; |
| 2393 | |
| 2394 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2395 | s2, |
| 2396 | NULL, |
| 2397 | NULL, |
| 2398 | TRUE, |
| 2399 | CREATE_NEW_CONSOLE, |
| 2400 | NULL, |
| 2401 | NULL, |
| 2402 | &siStartInfo, |
| 2403 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2404 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2405 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2406 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2407 | /* Return process handle */ |
| 2408 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2409 | return TRUE; |
| 2410 | } |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2411 | win32_error("CreateProcess", NULL); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2412 | return FALSE; |
| 2413 | } |
| 2414 | |
| 2415 | /* The following code is based off of KB: Q190351 */ |
| 2416 | |
| 2417 | static PyObject * |
| 2418 | _PyPopen(char *cmdstring, int mode, int n) |
| 2419 | { |
| 2420 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2421 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2422 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2423 | |
| 2424 | SECURITY_ATTRIBUTES saAttr; |
| 2425 | BOOL fSuccess; |
| 2426 | int fd1, fd2, fd3; |
| 2427 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2428 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2429 | PyObject *f; |
| 2430 | |
| 2431 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2432 | saAttr.bInheritHandle = TRUE; |
| 2433 | saAttr.lpSecurityDescriptor = NULL; |
| 2434 | |
| 2435 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2436 | return win32_error("CreatePipe", NULL); |
| 2437 | |
| 2438 | /* Create new output read handle and the input write handle. Set |
| 2439 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2440 | * the these handles; resulting in non-closeable handles to the pipes |
| 2441 | * being created. */ |
| 2442 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2443 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2444 | FALSE, |
| 2445 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2446 | if (!fSuccess) |
| 2447 | return win32_error("DuplicateHandle", NULL); |
| 2448 | |
| 2449 | /* Close the inheritable version of ChildStdin |
| 2450 | that we're using. */ |
| 2451 | CloseHandle(hChildStdinWr); |
| 2452 | |
| 2453 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2454 | return win32_error("CreatePipe", NULL); |
| 2455 | |
| 2456 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2457 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2458 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2459 | if (!fSuccess) |
| 2460 | return win32_error("DuplicateHandle", NULL); |
| 2461 | |
| 2462 | /* Close the inheritable version of ChildStdout |
| 2463 | that we're using. */ |
| 2464 | CloseHandle(hChildStdoutRd); |
| 2465 | |
| 2466 | if (n != POPEN_4) { |
| 2467 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2468 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2469 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2470 | hChildStderrRd, |
| 2471 | GetCurrentProcess(), |
| 2472 | &hChildStderrRdDup, 0, |
| 2473 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2474 | if (!fSuccess) |
| 2475 | return win32_error("DuplicateHandle", NULL); |
| 2476 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2477 | CloseHandle(hChildStderrRd); |
| 2478 | } |
| 2479 | |
| 2480 | switch (n) { |
| 2481 | case POPEN_1: |
| 2482 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2483 | case _O_WRONLY | _O_TEXT: |
| 2484 | /* Case for writing to child Stdin in text mode. */ |
| 2485 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2486 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2487 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2488 | PyFile_SetBufSize(f, 0); |
| 2489 | /* We don't care about these pipes anymore, so close them. */ |
| 2490 | CloseHandle(hChildStdoutRdDup); |
| 2491 | CloseHandle(hChildStderrRdDup); |
| 2492 | break; |
| 2493 | |
| 2494 | case _O_RDONLY | _O_TEXT: |
| 2495 | /* Case for reading from child Stdout in text mode. */ |
| 2496 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2497 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2498 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2499 | PyFile_SetBufSize(f, 0); |
| 2500 | /* We don't care about these pipes anymore, so close them. */ |
| 2501 | CloseHandle(hChildStdinWrDup); |
| 2502 | CloseHandle(hChildStderrRdDup); |
| 2503 | break; |
| 2504 | |
| 2505 | case _O_RDONLY | _O_BINARY: |
| 2506 | /* Case for readinig from child Stdout in binary mode. */ |
| 2507 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2508 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2509 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2510 | PyFile_SetBufSize(f, 0); |
| 2511 | /* We don't care about these pipes anymore, so close them. */ |
| 2512 | CloseHandle(hChildStdinWrDup); |
| 2513 | CloseHandle(hChildStderrRdDup); |
| 2514 | break; |
| 2515 | |
| 2516 | case _O_WRONLY | _O_BINARY: |
| 2517 | /* Case for writing to child Stdin in binary mode. */ |
| 2518 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2519 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2520 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2521 | PyFile_SetBufSize(f, 0); |
| 2522 | /* We don't care about these pipes anymore, so close them. */ |
| 2523 | CloseHandle(hChildStdoutRdDup); |
| 2524 | CloseHandle(hChildStderrRdDup); |
| 2525 | break; |
| 2526 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2527 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | |
| 2530 | case POPEN_2: |
| 2531 | case POPEN_4: |
| 2532 | { |
| 2533 | char *m1, *m2; |
| 2534 | PyObject *p1, *p2; |
| 2535 | |
| 2536 | if (mode && _O_TEXT) { |
| 2537 | m1 = "r"; |
| 2538 | m2 = "w"; |
| 2539 | } else { |
| 2540 | m1 = "rb"; |
| 2541 | m2 = "wb"; |
| 2542 | } |
| 2543 | |
| 2544 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2545 | f1 = _fdopen(fd1, m2); |
| 2546 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2547 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2548 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2549 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2550 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2551 | PyFile_SetBufSize(p2, 0); |
| 2552 | |
| 2553 | if (n != 4) |
| 2554 | CloseHandle(hChildStderrRdDup); |
| 2555 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2556 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2557 | Py_XDECREF(p1); |
| 2558 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2559 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2560 | break; |
| 2561 | } |
| 2562 | |
| 2563 | case POPEN_3: |
| 2564 | { |
| 2565 | char *m1, *m2; |
| 2566 | PyObject *p1, *p2, *p3; |
| 2567 | |
| 2568 | if (mode && _O_TEXT) { |
| 2569 | m1 = "r"; |
| 2570 | m2 = "w"; |
| 2571 | } else { |
| 2572 | m1 = "rb"; |
| 2573 | m2 = "wb"; |
| 2574 | } |
| 2575 | |
| 2576 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2577 | f1 = _fdopen(fd1, m2); |
| 2578 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2579 | f2 = _fdopen(fd2, m1); |
| 2580 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2581 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2582 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2583 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2584 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2585 | PyFile_SetBufSize(p1, 0); |
| 2586 | PyFile_SetBufSize(p2, 0); |
| 2587 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2588 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2589 | Py_XDECREF(p1); |
| 2590 | Py_XDECREF(p2); |
| 2591 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2592 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2593 | break; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | if (n == POPEN_4) { |
| 2598 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2599 | hChildStdinRd, |
| 2600 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2601 | hChildStdoutWr, |
| 2602 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2603 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2604 | } |
| 2605 | else { |
| 2606 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2607 | hChildStdinRd, |
| 2608 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2609 | hChildStderrWr, |
| 2610 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2611 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2614 | /* |
| 2615 | * Insert the files we've created into the process dictionary |
| 2616 | * all referencing the list with the process handle and the |
| 2617 | * initial number of files (see description below in _PyPclose). |
| 2618 | * Since if _PyPclose later tried to wait on a process when all |
| 2619 | * handles weren't closed, it could create a deadlock with the |
| 2620 | * child, we spend some energy here to try to ensure that we |
| 2621 | * either insert all file handles into the dictionary or none |
| 2622 | * at all. It's a little clumsy with the various popen modes |
| 2623 | * and variable number of files involved. |
| 2624 | */ |
| 2625 | if (!_PyPopenProcs) { |
| 2626 | _PyPopenProcs = PyDict_New(); |
| 2627 | } |
| 2628 | |
| 2629 | if (_PyPopenProcs) { |
| 2630 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2631 | int ins_rc[3]; |
| 2632 | |
| 2633 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2634 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2635 | |
| 2636 | procObj = PyList_New(2); |
| 2637 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2638 | intObj = PyInt_FromLong(file_count); |
| 2639 | |
| 2640 | if (procObj && hProcessObj && intObj) { |
| 2641 | PyList_SetItem(procObj,0,hProcessObj); |
| 2642 | PyList_SetItem(procObj,1,intObj); |
| 2643 | |
| 2644 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2645 | if (fileObj[0]) { |
| 2646 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2647 | fileObj[0], |
| 2648 | procObj); |
| 2649 | } |
| 2650 | if (file_count >= 2) { |
| 2651 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2652 | if (fileObj[1]) { |
| 2653 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2654 | fileObj[1], |
| 2655 | procObj); |
| 2656 | } |
| 2657 | } |
| 2658 | if (file_count >= 3) { |
| 2659 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2660 | if (fileObj[2]) { |
| 2661 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2662 | fileObj[2], |
| 2663 | procObj); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2668 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2669 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2670 | /* Something failed - remove any dictionary |
| 2671 | * entries that did make it. |
| 2672 | */ |
| 2673 | if (!ins_rc[0] && fileObj[0]) { |
| 2674 | PyDict_DelItem(_PyPopenProcs, |
| 2675 | fileObj[0]); |
| 2676 | } |
| 2677 | if (!ins_rc[1] && fileObj[1]) { |
| 2678 | PyDict_DelItem(_PyPopenProcs, |
| 2679 | fileObj[1]); |
| 2680 | } |
| 2681 | if (!ins_rc[2] && fileObj[2]) { |
| 2682 | PyDict_DelItem(_PyPopenProcs, |
| 2683 | fileObj[2]); |
| 2684 | } |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * Clean up our localized references for the dictionary keys |
| 2690 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2691 | * that got placed in the dictionary. |
| 2692 | */ |
| 2693 | Py_XDECREF(procObj); |
| 2694 | Py_XDECREF(fileObj[0]); |
| 2695 | Py_XDECREF(fileObj[1]); |
| 2696 | Py_XDECREF(fileObj[2]); |
| 2697 | } |
| 2698 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2699 | /* Child is launched. Close the parents copy of those pipe |
| 2700 | * handles that only the child should have open. You need to |
| 2701 | * make sure that no handles to the write end of the output pipe |
| 2702 | * are maintained in this process or else the pipe will not close |
| 2703 | * when the child process exits and the ReadFile will hang. */ |
| 2704 | |
| 2705 | if (!CloseHandle(hChildStdinRd)) |
| 2706 | return win32_error("CloseHandle", NULL); |
| 2707 | |
| 2708 | if (!CloseHandle(hChildStdoutWr)) |
| 2709 | return win32_error("CloseHandle", NULL); |
| 2710 | |
| 2711 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2712 | return win32_error("CloseHandle", NULL); |
| 2713 | |
| 2714 | return f; |
| 2715 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2716 | |
| 2717 | /* |
| 2718 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2719 | * 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] | 2720 | * |
| 2721 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2722 | * input file pointer to information about the process that was |
| 2723 | * originally created by the popen* call that created the file pointer. |
| 2724 | * The dictionary uses the file pointer as a key (with one entry |
| 2725 | * inserted for each file returned by the original popen* call) and a |
| 2726 | * single list object as the value for all files from a single call. |
| 2727 | * The list object contains the Win32 process handle at [0], and a file |
| 2728 | * count at [1], which is initialized to the total number of file |
| 2729 | * handles using that list. |
| 2730 | * |
| 2731 | * This function closes whichever handle it is passed, and decrements |
| 2732 | * the file count in the dictionary for the process handle pointed to |
| 2733 | * by this file. On the last close (when the file count reaches zero), |
| 2734 | * this function will wait for the child process and then return its |
| 2735 | * exit code as the result of the close() operation. This permits the |
| 2736 | * files to be closed in any order - it is always the close() of the |
| 2737 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2738 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2739 | |
| 2740 | /* RED_FLAG 31-Aug-2000 Tim |
| 2741 | * This is always called (today!) between a pair of |
| 2742 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2743 | * macros. So the thread running this has no valid thread state, as |
| 2744 | * far as Python is concerned. However, this calls some Python API |
| 2745 | * functions that cannot be called safely without a valid thread |
| 2746 | * state, in particular PyDict_GetItem. |
| 2747 | * As a temporary hack (although it may last for years ...), we |
| 2748 | * *rely* on not having a valid thread state in this function, in |
| 2749 | * order to create our own "from scratch". |
| 2750 | * This will deadlock if _PyPclose is ever called by a thread |
| 2751 | * holding the global lock. |
| 2752 | */ |
| 2753 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2754 | static int _PyPclose(FILE *file) |
| 2755 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2756 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2757 | DWORD exit_code; |
| 2758 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2759 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 2760 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2761 | #ifdef WITH_THREAD |
| 2762 | PyInterpreterState* pInterpreterState; |
| 2763 | PyThreadState* pThreadState; |
| 2764 | #endif |
| 2765 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2766 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2767 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2768 | */ |
| 2769 | result = fclose(file); |
| 2770 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2771 | #ifdef WITH_THREAD |
| 2772 | /* Bootstrap a valid thread state into existence. */ |
| 2773 | pInterpreterState = PyInterpreterState_New(); |
| 2774 | if (!pInterpreterState) { |
| 2775 | /* Well, we're hosed now! We don't have a thread |
| 2776 | * state, so can't call a nice error routine, or raise |
| 2777 | * an exception. Just die. |
| 2778 | */ |
| 2779 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2780 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2781 | return -1; /* unreachable */ |
| 2782 | } |
| 2783 | pThreadState = PyThreadState_New(pInterpreterState); |
| 2784 | if (!pThreadState) { |
| 2785 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2786 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2787 | return -1; /* unreachable */ |
| 2788 | } |
| 2789 | /* Grab the global lock. Note that this will deadlock if the |
| 2790 | * current thread already has the lock! (see RED_FLAG comments |
| 2791 | * before this function) |
| 2792 | */ |
| 2793 | PyEval_RestoreThread(pThreadState); |
| 2794 | #endif |
| 2795 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2796 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2797 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2798 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2799 | fileObj)) != NULL && |
| 2800 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 2801 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 2802 | |
| 2803 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 2804 | file_count = PyInt_AsLong(intObj); |
| 2805 | |
| 2806 | if (file_count > 1) { |
| 2807 | /* Still other files referencing process */ |
| 2808 | file_count--; |
| 2809 | PyList_SetItem(procObj,1, |
| 2810 | PyInt_FromLong(file_count)); |
| 2811 | } else { |
| 2812 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2813 | if (result != EOF && |
| 2814 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 2815 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2816 | /* Possible truncation here in 16-bit environments, but |
| 2817 | * real exit codes are just the lower byte in any event. |
| 2818 | */ |
| 2819 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2820 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2821 | /* Indicate failure - this will cause the file object |
| 2822 | * to raise an I/O error and translate the last Win32 |
| 2823 | * error code from errno. We do have a problem with |
| 2824 | * last errors that overlap the normal errno table, |
| 2825 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2826 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2827 | if (result != EOF) { |
| 2828 | /* If the error wasn't from the fclose(), then |
| 2829 | * set errno for the file object error handling. |
| 2830 | */ |
| 2831 | errno = GetLastError(); |
| 2832 | } |
| 2833 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | /* Free up the native handle at this point */ |
| 2837 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2838 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2839 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2840 | /* Remove this file pointer from dictionary */ |
| 2841 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2842 | |
| 2843 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 2844 | Py_DECREF(_PyPopenProcs); |
| 2845 | _PyPopenProcs = NULL; |
| 2846 | } |
| 2847 | |
| 2848 | } /* if object retrieval ok */ |
| 2849 | |
| 2850 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2851 | } /* if _PyPopenProcs */ |
| 2852 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2853 | #ifdef WITH_THREAD |
| 2854 | /* Tear down the thread & interpreter states. |
| 2855 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2856 | * call the thread clear & delete functions, and indeed insist on |
| 2857 | * doing that themselves. The lock must be held during the clear, but |
| 2858 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2859 | */ |
| 2860 | PyInterpreterState_Clear(pInterpreterState); |
| 2861 | PyEval_ReleaseThread(pThreadState); |
| 2862 | PyInterpreterState_Delete(pInterpreterState); |
| 2863 | #endif |
| 2864 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2865 | return result; |
| 2866 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2867 | |
| 2868 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2869 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2870 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2871 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2872 | char *name; |
| 2873 | char *mode = "r"; |
| 2874 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2875 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2876 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2877 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2878 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2879 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2880 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2881 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2882 | if (fp == NULL) |
| 2883 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2884 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2885 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2886 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2887 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2888 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2889 | #endif |
| 2890 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2891 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2892 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2893 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2894 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2895 | static char posix_setuid__doc__[] = |
| 2896 | "setuid(uid) -> None\n\ |
| 2897 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2898 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2899 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2900 | { |
| 2901 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2902 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2903 | return NULL; |
| 2904 | if (setuid(uid) < 0) |
| 2905 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2906 | Py_INCREF(Py_None); |
| 2907 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2908 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2909 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2910 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2911 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 2912 | #ifdef HAVE_SETEUID |
| 2913 | static char posix_seteuid__doc__[] = |
| 2914 | "seteuid(uid) -> None\n\ |
| 2915 | Set the current process's effective user id."; |
| 2916 | static PyObject * |
| 2917 | posix_seteuid (PyObject *self, PyObject *args) |
| 2918 | { |
| 2919 | int euid; |
| 2920 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 2921 | return NULL; |
| 2922 | } else if (seteuid(euid) < 0) { |
| 2923 | return posix_error(); |
| 2924 | } else { |
| 2925 | Py_INCREF(Py_None); |
| 2926 | return Py_None; |
| 2927 | } |
| 2928 | } |
| 2929 | #endif /* HAVE_SETEUID */ |
| 2930 | |
| 2931 | #ifdef HAVE_SETEGID |
| 2932 | static char posix_setegid__doc__[] = |
| 2933 | "setegid(gid) -> None\n\ |
| 2934 | Set the current process's effective group id."; |
| 2935 | static PyObject * |
| 2936 | posix_setegid (PyObject *self, PyObject *args) |
| 2937 | { |
| 2938 | int egid; |
| 2939 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 2940 | return NULL; |
| 2941 | } else if (setegid(egid) < 0) { |
| 2942 | return posix_error(); |
| 2943 | } else { |
| 2944 | Py_INCREF(Py_None); |
| 2945 | return Py_None; |
| 2946 | } |
| 2947 | } |
| 2948 | #endif /* HAVE_SETEGID */ |
| 2949 | |
| 2950 | #ifdef HAVE_SETREUID |
| 2951 | static char posix_setreuid__doc__[] = |
| 2952 | "seteuid(ruid, euid) -> None\n\ |
| 2953 | Set the current process's real and effective user ids."; |
| 2954 | static PyObject * |
| 2955 | posix_setreuid (PyObject *self, PyObject *args) |
| 2956 | { |
| 2957 | int ruid, euid; |
| 2958 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 2959 | return NULL; |
| 2960 | } else if (setreuid(ruid, euid) < 0) { |
| 2961 | return posix_error(); |
| 2962 | } else { |
| 2963 | Py_INCREF(Py_None); |
| 2964 | return Py_None; |
| 2965 | } |
| 2966 | } |
| 2967 | #endif /* HAVE_SETREUID */ |
| 2968 | |
| 2969 | #ifdef HAVE_SETREGID |
| 2970 | static char posix_setregid__doc__[] = |
| 2971 | "setegid(rgid, egid) -> None\n\ |
| 2972 | Set the current process's real and effective group ids."; |
| 2973 | static PyObject * |
| 2974 | posix_setregid (PyObject *self, PyObject *args) |
| 2975 | { |
| 2976 | int rgid, egid; |
| 2977 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 2978 | return NULL; |
| 2979 | } else if (setregid(rgid, egid) < 0) { |
| 2980 | return posix_error(); |
| 2981 | } else { |
| 2982 | Py_INCREF(Py_None); |
| 2983 | return Py_None; |
| 2984 | } |
| 2985 | } |
| 2986 | #endif /* HAVE_SETREGID */ |
| 2987 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2988 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2989 | static char posix_setgid__doc__[] = |
| 2990 | "setgid(gid) -> None\n\ |
| 2991 | Set the current process's group id."; |
| 2992 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2994 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2995 | { |
| 2996 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2997 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2998 | return NULL; |
| 2999 | if (setgid(gid) < 0) |
| 3000 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3001 | Py_INCREF(Py_None); |
| 3002 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3003 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3004 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3005 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3006 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3007 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3008 | static char posix_waitpid__doc__[] = |
| 3009 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3010 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3011 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3012 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3013 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3014 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3015 | int pid, options; |
| 3016 | #ifdef UNION_WAIT |
| 3017 | union wait status; |
| 3018 | #define status_i (status.w_status) |
| 3019 | #else |
| 3020 | int status; |
| 3021 | #define status_i status |
| 3022 | #endif |
| 3023 | status_i = 0; |
| 3024 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3025 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3026 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3027 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3028 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3029 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3030 | #else |
| 3031 | pid = waitpid(pid, &status, options); |
| 3032 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3033 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3034 | if (pid == -1) |
| 3035 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3036 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3037 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3038 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3039 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3040 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3041 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3042 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3043 | static char posix_wait__doc__[] = |
| 3044 | "wait() -> (pid, status)\n\ |
| 3045 | Wait for completion of a child process."; |
| 3046 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3047 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3048 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3049 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3050 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3051 | #ifdef UNION_WAIT |
| 3052 | union wait status; |
| 3053 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3054 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3055 | int status; |
| 3056 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3057 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3058 | if (!PyArg_ParseTuple(args, ":wait")) |
| 3059 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3060 | status_i = 0; |
| 3061 | Py_BEGIN_ALLOW_THREADS |
| 3062 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3063 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3064 | if (pid == -1) |
| 3065 | return posix_error(); |
| 3066 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3067 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3068 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3069 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3070 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3071 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3072 | |
| 3073 | static char posix_lstat__doc__[] = |
| 3074 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3075 | Like stat(path), but do not follow symbolic links."; |
| 3076 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3077 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3078 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3079 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3080 | #ifdef HAVE_LSTAT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3081 | return posix_do_stat(self, args, "s:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3082 | #else /* !HAVE_LSTAT */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3083 | return posix_do_stat(self, args, "s:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3084 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3087 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3088 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3089 | static char posix_readlink__doc__[] = |
| 3090 | "readlink(path) -> path\n\ |
| 3091 | Return a string representing the path to which the symbolic link points."; |
| 3092 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3093 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3094 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3095 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3096 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3097 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3098 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3099 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3100 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3101 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3102 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3103 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3104 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3105 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3106 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3107 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3108 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3109 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3111 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3112 | static char posix_symlink__doc__[] = |
| 3113 | "symlink(src, dst) -> None\n\ |
| 3114 | Create a symbolic link."; |
| 3115 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3116 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3117 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3118 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3119 | return posix_2str(args, "ss:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3120 | } |
| 3121 | #endif /* HAVE_SYMLINK */ |
| 3122 | |
| 3123 | |
| 3124 | #ifdef HAVE_TIMES |
| 3125 | #ifndef HZ |
| 3126 | #define HZ 60 /* Universal constant :-) */ |
| 3127 | #endif /* HZ */ |
| 3128 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3129 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3130 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3131 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3132 | { |
| 3133 | ULONG value = 0; |
| 3134 | |
| 3135 | Py_BEGIN_ALLOW_THREADS |
| 3136 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3137 | Py_END_ALLOW_THREADS |
| 3138 | |
| 3139 | return value; |
| 3140 | } |
| 3141 | |
| 3142 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3143 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3144 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3145 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3146 | return NULL; |
| 3147 | |
| 3148 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3149 | return Py_BuildValue("ddddd", |
| 3150 | (double)0 /* t.tms_utime / HZ */, |
| 3151 | (double)0 /* t.tms_stime / HZ */, |
| 3152 | (double)0 /* t.tms_cutime / HZ */, |
| 3153 | (double)0 /* t.tms_cstime / HZ */, |
| 3154 | (double)system_uptime() / 1000); |
| 3155 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3156 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3158 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3159 | { |
| 3160 | struct tms t; |
| 3161 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3162 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3163 | return NULL; |
| 3164 | errno = 0; |
| 3165 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3166 | if (c == (clock_t) -1) |
| 3167 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3168 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3169 | (double)t.tms_utime / HZ, |
| 3170 | (double)t.tms_stime / HZ, |
| 3171 | (double)t.tms_cutime / HZ, |
| 3172 | (double)t.tms_cstime / HZ, |
| 3173 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3174 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3175 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3176 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3177 | |
| 3178 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3179 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3180 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3181 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3182 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3183 | { |
| 3184 | FILETIME create, exit, kernel, user; |
| 3185 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3186 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3189 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3190 | /* The fields of a FILETIME structure are the hi and lo part |
| 3191 | of a 64-bit value expressed in 100 nanosecond units. |
| 3192 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3193 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3194 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3195 | return Py_BuildValue( |
| 3196 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3197 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3198 | kernel.dwLowDateTime*1e-7), |
| 3199 | (double)(user.dwHighDateTime*429.4967296 + |
| 3200 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3201 | (double)0, |
| 3202 | (double)0, |
| 3203 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3204 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3205 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3206 | |
| 3207 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3208 | static char posix_times__doc__[] = |
| 3209 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3210 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3211 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3212 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3213 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3214 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3215 | static char posix_setsid__doc__[] = |
| 3216 | "setsid() -> None\n\ |
| 3217 | Call the system call setsid()."; |
| 3218 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3219 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3220 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3221 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3222 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3223 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3224 | if (setsid() < 0) |
| 3225 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3226 | Py_INCREF(Py_None); |
| 3227 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3228 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3229 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3230 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3231 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3232 | static char posix_setpgid__doc__[] = |
| 3233 | "setpgid(pid, pgrp) -> None\n\ |
| 3234 | Call the system call setpgid()."; |
| 3235 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3236 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3237 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3238 | { |
| 3239 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3240 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3241 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3242 | if (setpgid(pid, pgrp) < 0) |
| 3243 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3244 | Py_INCREF(Py_None); |
| 3245 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3246 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3247 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3248 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3249 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3250 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 | static char posix_tcgetpgrp__doc__[] = |
| 3252 | "tcgetpgrp(fd) -> pgid\n\ |
| 3253 | Return the process group associated with the terminal given by a fd."; |
| 3254 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3256 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3257 | { |
| 3258 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3259 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3260 | return NULL; |
| 3261 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3262 | if (pgid < 0) |
| 3263 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3264 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3265 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3266 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3267 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3268 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3269 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3270 | static char posix_tcsetpgrp__doc__[] = |
| 3271 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3272 | Set the process group associated with the terminal given by a fd."; |
| 3273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3274 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3275 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3276 | { |
| 3277 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3278 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3279 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3280 | if (tcsetpgrp(fd, pgid) < 0) |
| 3281 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3282 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3283 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3284 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3285 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3286 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3287 | /* Functions acting on file descriptors */ |
| 3288 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3289 | static char posix_open__doc__[] = |
| 3290 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3291 | Open a file (for low level IO)."; |
| 3292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3293 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3294 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3295 | { |
| 3296 | char *file; |
| 3297 | int flag; |
| 3298 | int mode = 0777; |
| 3299 | int fd; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3300 | if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode)) |
| 3301 | return NULL; |
| 3302 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3303 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3304 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3305 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3306 | if (fd < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3307 | return posix_error_with_filename(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3308 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3311 | |
| 3312 | static char posix_close__doc__[] = |
| 3313 | "close(fd) -> None\n\ |
| 3314 | Close a file descriptor (for low level IO)."; |
| 3315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3316 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3317 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3318 | { |
| 3319 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3320 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3321 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3322 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3323 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3324 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3325 | if (res < 0) |
| 3326 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3327 | Py_INCREF(Py_None); |
| 3328 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3331 | |
| 3332 | static char posix_dup__doc__[] = |
| 3333 | "dup(fd) -> fd2\n\ |
| 3334 | Return a duplicate of a file descriptor."; |
| 3335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3337 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3338 | { |
| 3339 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3340 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3341 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3342 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3343 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3344 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3345 | if (fd < 0) |
| 3346 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3347 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3350 | |
| 3351 | static char posix_dup2__doc__[] = |
| 3352 | "dup2(fd, fd2) -> None\n\ |
| 3353 | Duplicate file descriptor."; |
| 3354 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3355 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3356 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3357 | { |
| 3358 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3359 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3360 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3361 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3362 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3363 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3364 | if (res < 0) |
| 3365 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3366 | Py_INCREF(Py_None); |
| 3367 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3370 | |
| 3371 | static char posix_lseek__doc__[] = |
| 3372 | "lseek(fd, pos, how) -> newpos\n\ |
| 3373 | Set the current position of a file descriptor."; |
| 3374 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3375 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3376 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3377 | { |
| 3378 | int fd, how; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3379 | #ifdef MS_WIN64 |
| 3380 | LONG_LONG pos, res; |
| 3381 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3382 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3383 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3384 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3385 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3386 | return NULL; |
| 3387 | #ifdef SEEK_SET |
| 3388 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3389 | switch (how) { |
| 3390 | case 0: how = SEEK_SET; break; |
| 3391 | case 1: how = SEEK_CUR; break; |
| 3392 | case 2: how = SEEK_END; break; |
| 3393 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3394 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3395 | |
| 3396 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3397 | pos = PyInt_AsLong(posobj); |
| 3398 | #else |
| 3399 | pos = PyLong_Check(posobj) ? |
| 3400 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3401 | #endif |
| 3402 | if (PyErr_Occurred()) |
| 3403 | return NULL; |
| 3404 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3405 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3406 | #ifdef MS_WIN64 |
| 3407 | res = _lseeki64(fd, pos, how); |
| 3408 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3409 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3410 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3411 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3412 | if (res < 0) |
| 3413 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3414 | |
| 3415 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3416 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3417 | #else |
| 3418 | return PyLong_FromLongLong(res); |
| 3419 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3422 | |
| 3423 | static char posix_read__doc__[] = |
| 3424 | "read(fd, buffersize) -> string\n\ |
| 3425 | Read a file descriptor."; |
| 3426 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3427 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3428 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3429 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3430 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3431 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3432 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3433 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3434 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3435 | if (buffer == NULL) |
| 3436 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3437 | Py_BEGIN_ALLOW_THREADS |
| 3438 | n = read(fd, PyString_AsString(buffer), size); |
| 3439 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3440 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3441 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3442 | return posix_error(); |
| 3443 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3444 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3445 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3446 | return buffer; |
| 3447 | } |
| 3448 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3449 | |
| 3450 | static char posix_write__doc__[] = |
| 3451 | "write(fd, string) -> byteswritten\n\ |
| 3452 | Write a string to a file descriptor."; |
| 3453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3455 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3456 | { |
| 3457 | int fd, size; |
| 3458 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3459 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3460 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3461 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3462 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3463 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3464 | if (size < 0) |
| 3465 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3466 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3469 | |
| 3470 | static char posix_fstat__doc__[]= |
| 3471 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3472 | Like stat(), but for an open file descriptor."; |
| 3473 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3475 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3476 | { |
| 3477 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3478 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3479 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3480 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3481 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3482 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3483 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3484 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3485 | if (res != 0) |
| 3486 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3487 | |
| 3488 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3491 | |
| 3492 | static char posix_fdopen__doc__[] = |
| 3493 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3494 | Return an open file object connected to a file descriptor."; |
| 3495 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3496 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3497 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3498 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3499 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3500 | char *mode = "r"; |
| 3501 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3502 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3503 | PyObject *f; |
| 3504 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3505 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3507 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3508 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3509 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3510 | if (fp == NULL) |
| 3511 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3512 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3513 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3514 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3515 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3518 | static char posix_isatty__doc__[] = |
| 3519 | "isatty(fd) -> Boolean\n\ |
| 3520 | 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] | 3521 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3522 | |
| 3523 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3524 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3525 | { |
| 3526 | int fd; |
| 3527 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3528 | return NULL; |
| 3529 | return Py_BuildValue("i", isatty(fd)); |
| 3530 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3531 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3532 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3533 | static char posix_pipe__doc__[] = |
| 3534 | "pipe() -> (read_end, write_end)\n\ |
| 3535 | Create a pipe."; |
| 3536 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3537 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3538 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3539 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3540 | #if defined(PYOS_OS2) |
| 3541 | HFILE read, write; |
| 3542 | APIRET rc; |
| 3543 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3544 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3545 | return NULL; |
| 3546 | |
| 3547 | Py_BEGIN_ALLOW_THREADS |
| 3548 | rc = DosCreatePipe( &read, &write, 4096); |
| 3549 | Py_END_ALLOW_THREADS |
| 3550 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3551 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3552 | |
| 3553 | return Py_BuildValue("(ii)", read, write); |
| 3554 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3555 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3556 | int fds[2]; |
| 3557 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3558 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3559 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3560 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3561 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3562 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3563 | if (res != 0) |
| 3564 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3565 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3566 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3567 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3568 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3569 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3570 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3571 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3572 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3573 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3574 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3575 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3576 | return win32_error("CreatePipe", NULL); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3577 | read_fd = _open_osfhandle((intptr_t)read, 0); |
| 3578 | write_fd = _open_osfhandle((intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3579 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3580 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3581 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3582 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3583 | #endif /* HAVE_PIPE */ |
| 3584 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3585 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3586 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3587 | static char posix_mkfifo__doc__[] = |
| 3588 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3589 | Create a FIFO (a POSIX named pipe)."; |
| 3590 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3591 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3592 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3593 | { |
| 3594 | char *file; |
| 3595 | int mode = 0666; |
| 3596 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3597 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3598 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3599 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3600 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3601 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3602 | if (res < 0) |
| 3603 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3604 | Py_INCREF(Py_None); |
| 3605 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3606 | } |
| 3607 | #endif |
| 3608 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3609 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3610 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3611 | static char posix_ftruncate__doc__[] = |
| 3612 | "ftruncate(fd, length) -> None\n\ |
| 3613 | Truncate a file to a specified length."; |
| 3614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3615 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3616 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3617 | { |
| 3618 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3619 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3620 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3621 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3622 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3623 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3624 | return NULL; |
| 3625 | |
| 3626 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3627 | length = PyInt_AsLong(lenobj); |
| 3628 | #else |
| 3629 | length = PyLong_Check(lenobj) ? |
| 3630 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3631 | #endif |
| 3632 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3633 | return NULL; |
| 3634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3635 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3636 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3637 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3638 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3639 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3640 | return NULL; |
| 3641 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3642 | Py_INCREF(Py_None); |
| 3643 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3644 | } |
| 3645 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3646 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3647 | #ifdef NeXT |
| 3648 | #define HAVE_PUTENV |
| 3649 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3650 | static int |
| 3651 | putenv(char *newval) |
| 3652 | { |
| 3653 | extern char **environ; |
| 3654 | |
| 3655 | static int firstTime = 1; |
| 3656 | char **ep; |
| 3657 | char *cp; |
| 3658 | int esiz; |
| 3659 | char *np; |
| 3660 | |
| 3661 | if (!(np = strchr(newval, '='))) |
| 3662 | return 1; |
| 3663 | *np = '\0'; |
| 3664 | |
| 3665 | /* look it up */ |
| 3666 | for (ep=environ ; *ep ; ep++) |
| 3667 | { |
| 3668 | /* this should always be true... */ |
| 3669 | if (cp = strchr(*ep, '=')) |
| 3670 | { |
| 3671 | *cp = '\0'; |
| 3672 | if (!strcmp(*ep, newval)) |
| 3673 | { |
| 3674 | /* got it! */ |
| 3675 | *cp = '='; |
| 3676 | break; |
| 3677 | } |
| 3678 | *cp = '='; |
| 3679 | } |
| 3680 | else |
| 3681 | { |
| 3682 | *np = '='; |
| 3683 | return 1; |
| 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | *np = '='; |
| 3688 | if (*ep) |
| 3689 | { |
| 3690 | /* the string was already there: |
| 3691 | just replace it with the new one */ |
| 3692 | *ep = newval; |
| 3693 | return 0; |
| 3694 | } |
| 3695 | |
| 3696 | /* expand environ by one */ |
| 3697 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3698 | esiz++; |
| 3699 | if (firstTime) |
| 3700 | { |
| 3701 | char **epp; |
| 3702 | char **newenv; |
| 3703 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 3704 | return 1; |
| 3705 | |
| 3706 | for (ep=environ, epp=newenv ; *ep ;) |
| 3707 | *epp++ = *ep++; |
| 3708 | *epp++ = newval; |
| 3709 | *epp = (char *) 0; |
| 3710 | environ = newenv; |
| 3711 | } |
| 3712 | else |
| 3713 | { |
| 3714 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 3715 | return 1; |
| 3716 | environ[esiz - 2] = newval; |
| 3717 | environ[esiz - 1] = (char *) 0; |
| 3718 | firstTime = 0; |
| 3719 | } |
| 3720 | |
| 3721 | return 0; |
| 3722 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 3723 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3725 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3726 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3727 | static char posix_putenv__doc__[] = |
| 3728 | "putenv(key, value) -> None\n\ |
| 3729 | Change or add an environment variable."; |
| 3730 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3731 | /* Save putenv() parameters as values here, so we can collect them when they |
| 3732 | * get re-set with another call for the same key. */ |
| 3733 | static PyObject *posix_putenv_garbage; |
| 3734 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3735 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3736 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3737 | { |
| 3738 | char *s1, *s2; |
| 3739 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3740 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3741 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3742 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3743 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3744 | |
| 3745 | #if defined(PYOS_OS2) |
| 3746 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 3747 | APIRET rc; |
| 3748 | |
| 3749 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3750 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3751 | |
| 3752 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 3753 | if (rc != NO_ERROR) |
| 3754 | return os2_error(rc); |
| 3755 | |
| 3756 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 3757 | APIRET rc; |
| 3758 | |
| 3759 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3760 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3761 | |
| 3762 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 3763 | if (rc != NO_ERROR) |
| 3764 | return os2_error(rc); |
| 3765 | } else { |
| 3766 | #endif |
| 3767 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3768 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 3769 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 3770 | if (newstr == NULL) |
| 3771 | return PyErr_NoMemory(); |
| 3772 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3773 | (void) sprintf(new, "%s=%s", s1, s2); |
| 3774 | if (putenv(new)) { |
| 3775 | posix_error(); |
| 3776 | return NULL; |
| 3777 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3778 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 3779 | * this will cause previous value to be collected. This has to |
| 3780 | * happen after the real putenv() call because the old value |
| 3781 | * was still accessible until then. */ |
| 3782 | if (PyDict_SetItem(posix_putenv_garbage, |
| 3783 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 3784 | /* really not much we can do; just leak */ |
| 3785 | PyErr_Clear(); |
| 3786 | } |
| 3787 | else { |
| 3788 | Py_DECREF(newstr); |
| 3789 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3790 | |
| 3791 | #if defined(PYOS_OS2) |
| 3792 | } |
| 3793 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3794 | Py_INCREF(Py_None); |
| 3795 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3796 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3797 | #endif /* putenv */ |
| 3798 | |
| 3799 | #ifdef HAVE_STRERROR |
| 3800 | static char posix_strerror__doc__[] = |
| 3801 | "strerror(code) -> string\n\ |
| 3802 | Translate an error code to a message string."; |
| 3803 | |
| 3804 | PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3805 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3806 | { |
| 3807 | int code; |
| 3808 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3809 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3810 | return NULL; |
| 3811 | message = strerror(code); |
| 3812 | if (message == NULL) { |
| 3813 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3814 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3815 | return NULL; |
| 3816 | } |
| 3817 | return PyString_FromString(message); |
| 3818 | } |
| 3819 | #endif /* strerror */ |
| 3820 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3821 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3822 | #ifdef HAVE_SYS_WAIT_H |
| 3823 | |
| 3824 | #ifdef WIFSTOPPED |
| 3825 | static char posix_WIFSTOPPED__doc__[] = |
| 3826 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3827 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3828 | |
| 3829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3830 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3831 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3832 | #ifdef UNION_WAIT |
| 3833 | union wait status; |
| 3834 | #define status_i (status.w_status) |
| 3835 | #else |
| 3836 | int status; |
| 3837 | #define status_i status |
| 3838 | #endif |
| 3839 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3840 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3841 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3842 | { |
| 3843 | return NULL; |
| 3844 | } |
| 3845 | |
| 3846 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3847 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3848 | } |
| 3849 | #endif /* WIFSTOPPED */ |
| 3850 | |
| 3851 | #ifdef WIFSIGNALED |
| 3852 | static char posix_WIFSIGNALED__doc__[] = |
| 3853 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 3854 | 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] | 3855 | |
| 3856 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3857 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3858 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3859 | #ifdef UNION_WAIT |
| 3860 | union wait status; |
| 3861 | #define status_i (status.w_status) |
| 3862 | #else |
| 3863 | int status; |
| 3864 | #define status_i status |
| 3865 | #endif |
| 3866 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3867 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3868 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3869 | { |
| 3870 | return NULL; |
| 3871 | } |
| 3872 | |
| 3873 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3874 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3875 | } |
| 3876 | #endif /* WIFSIGNALED */ |
| 3877 | |
| 3878 | #ifdef WIFEXITED |
| 3879 | static char posix_WIFEXITED__doc__[] = |
| 3880 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3881 | Return true if the process returning 'status' exited using the exit()\n\ |
| 3882 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3883 | |
| 3884 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3885 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3886 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3887 | #ifdef UNION_WAIT |
| 3888 | union wait status; |
| 3889 | #define status_i (status.w_status) |
| 3890 | #else |
| 3891 | int status; |
| 3892 | #define status_i status |
| 3893 | #endif |
| 3894 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3895 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3896 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3897 | { |
| 3898 | return NULL; |
| 3899 | } |
| 3900 | |
| 3901 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3902 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3903 | } |
| 3904 | #endif /* WIFEXITED */ |
| 3905 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3906 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3907 | static char posix_WEXITSTATUS__doc__[] = |
| 3908 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3909 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3910 | |
| 3911 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3912 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3913 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3914 | #ifdef UNION_WAIT |
| 3915 | union wait status; |
| 3916 | #define status_i (status.w_status) |
| 3917 | #else |
| 3918 | int status; |
| 3919 | #define status_i status |
| 3920 | #endif |
| 3921 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3922 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3923 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3924 | { |
| 3925 | return NULL; |
| 3926 | } |
| 3927 | |
| 3928 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3929 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3930 | } |
| 3931 | #endif /* WEXITSTATUS */ |
| 3932 | |
| 3933 | #ifdef WTERMSIG |
| 3934 | static char posix_WTERMSIG__doc__[] = |
| 3935 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3936 | Return the signal that terminated the process that provided the 'status'\n\ |
| 3937 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3938 | |
| 3939 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3940 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3941 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3942 | #ifdef UNION_WAIT |
| 3943 | union wait status; |
| 3944 | #define status_i (status.w_status) |
| 3945 | #else |
| 3946 | int status; |
| 3947 | #define status_i status |
| 3948 | #endif |
| 3949 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3950 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3951 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3952 | { |
| 3953 | return NULL; |
| 3954 | } |
| 3955 | |
| 3956 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3957 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3958 | } |
| 3959 | #endif /* WTERMSIG */ |
| 3960 | |
| 3961 | #ifdef WSTOPSIG |
| 3962 | static char posix_WSTOPSIG__doc__[] = |
| 3963 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3964 | 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] | 3965 | |
| 3966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3967 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3968 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3969 | #ifdef UNION_WAIT |
| 3970 | union wait status; |
| 3971 | #define status_i (status.w_status) |
| 3972 | #else |
| 3973 | int status; |
| 3974 | #define status_i status |
| 3975 | #endif |
| 3976 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3977 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3978 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3979 | { |
| 3980 | return NULL; |
| 3981 | } |
| 3982 | |
| 3983 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3984 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3985 | } |
| 3986 | #endif /* WSTOPSIG */ |
| 3987 | |
| 3988 | #endif /* HAVE_SYS_WAIT_H */ |
| 3989 | |
| 3990 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3991 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 3992 | #ifdef _SCO_DS |
| 3993 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 3994 | needed definitions in sys/statvfs.h */ |
| 3995 | #define _SVID3 |
| 3996 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3997 | #include <sys/statvfs.h> |
| 3998 | |
| 3999 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4000 | "fstatvfs(fd) -> \n\ |
| 4001 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4002 | Perform an fstatvfs system call on the given fd."; |
| 4003 | |
| 4004 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4005 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4006 | { |
| 4007 | int fd, res; |
| 4008 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4009 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4010 | return NULL; |
| 4011 | Py_BEGIN_ALLOW_THREADS |
| 4012 | res = fstatvfs(fd, &st); |
| 4013 | Py_END_ALLOW_THREADS |
| 4014 | if (res != 0) |
| 4015 | return posix_error(); |
| 4016 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4017 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4018 | (long) st.f_bsize, |
| 4019 | (long) st.f_frsize, |
| 4020 | (long) st.f_blocks, |
| 4021 | (long) st.f_bfree, |
| 4022 | (long) st.f_bavail, |
| 4023 | (long) st.f_files, |
| 4024 | (long) st.f_ffree, |
| 4025 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4026 | (long) st.f_flag, |
| 4027 | (long) st.f_namemax); |
| 4028 | #else |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4029 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4030 | (long) st.f_bsize, |
| 4031 | (long) st.f_frsize, |
| 4032 | (LONG_LONG) st.f_blocks, |
| 4033 | (LONG_LONG) st.f_bfree, |
| 4034 | (LONG_LONG) st.f_bavail, |
| 4035 | (LONG_LONG) st.f_files, |
| 4036 | (LONG_LONG) st.f_ffree, |
| 4037 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4038 | (long) st.f_flag, |
| 4039 | (long) st.f_namemax); |
| 4040 | #endif |
| 4041 | } |
| 4042 | #endif /* HAVE_FSTATVFS */ |
| 4043 | |
| 4044 | |
| 4045 | #if defined(HAVE_STATVFS) |
| 4046 | #include <sys/statvfs.h> |
| 4047 | |
| 4048 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4049 | "statvfs(path) -> \n\ |
| 4050 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4051 | Perform a statvfs system call on the given path."; |
| 4052 | |
| 4053 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4054 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4055 | { |
| 4056 | char *path; |
| 4057 | int res; |
| 4058 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4059 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4060 | return NULL; |
| 4061 | Py_BEGIN_ALLOW_THREADS |
| 4062 | res = statvfs(path, &st); |
| 4063 | Py_END_ALLOW_THREADS |
| 4064 | if (res != 0) |
| 4065 | return posix_error_with_filename(path); |
| 4066 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4067 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4068 | (long) st.f_bsize, |
| 4069 | (long) st.f_frsize, |
| 4070 | (long) st.f_blocks, |
| 4071 | (long) st.f_bfree, |
| 4072 | (long) st.f_bavail, |
| 4073 | (long) st.f_files, |
| 4074 | (long) st.f_ffree, |
| 4075 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4076 | (long) st.f_flag, |
| 4077 | (long) st.f_namemax); |
| 4078 | #else /* HAVE_LARGEFILE_SUPPORT */ |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4079 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4080 | (long) st.f_bsize, |
| 4081 | (long) st.f_frsize, |
| 4082 | (LONG_LONG) st.f_blocks, |
| 4083 | (LONG_LONG) st.f_bfree, |
| 4084 | (LONG_LONG) st.f_bavail, |
| 4085 | (LONG_LONG) st.f_files, |
| 4086 | (LONG_LONG) st.f_ffree, |
| 4087 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4088 | (long) st.f_flag, |
| 4089 | (long) st.f_namemax); |
| 4090 | #endif |
| 4091 | } |
| 4092 | #endif /* HAVE_STATVFS */ |
| 4093 | |
| 4094 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4095 | #ifdef HAVE_TEMPNAM |
| 4096 | static char posix_tempnam__doc__[] = "\ |
| 4097 | tempnam([dir[, prefix]]) -> string\n\ |
| 4098 | Return a unique name for a temporary file.\n\ |
| 4099 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4100 | or None if not needed."; |
| 4101 | |
| 4102 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4103 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4104 | { |
| 4105 | PyObject *result = NULL; |
| 4106 | char *dir = NULL; |
| 4107 | char *pfx = NULL; |
| 4108 | char *name; |
| 4109 | |
| 4110 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4111 | return NULL; |
| 4112 | name = tempnam(dir, pfx); |
| 4113 | if (name == NULL) |
| 4114 | return PyErr_NoMemory(); |
| 4115 | result = PyString_FromString(name); |
| 4116 | free(name); |
| 4117 | return result; |
| 4118 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4119 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4120 | |
| 4121 | |
| 4122 | #ifdef HAVE_TMPFILE |
| 4123 | static char posix_tmpfile__doc__[] = "\ |
| 4124 | tmpfile() -> file object\n\ |
| 4125 | Create a temporary file with no directory entries."; |
| 4126 | |
| 4127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4128 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4129 | { |
| 4130 | FILE *fp; |
| 4131 | |
| 4132 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4133 | return NULL; |
| 4134 | fp = tmpfile(); |
| 4135 | if (fp == NULL) |
| 4136 | return posix_error(); |
| 4137 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4138 | } |
| 4139 | #endif |
| 4140 | |
| 4141 | |
| 4142 | #ifdef HAVE_TMPNAM |
| 4143 | static char posix_tmpnam__doc__[] = "\ |
| 4144 | tmpnam() -> string\n\ |
| 4145 | Return a unique name for a temporary file."; |
| 4146 | |
| 4147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4148 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4149 | { |
| 4150 | char buffer[L_tmpnam]; |
| 4151 | char *name; |
| 4152 | |
| 4153 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4154 | return NULL; |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4155 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4156 | name = tmpnam_r(buffer); |
| 4157 | #else |
| 4158 | name = tmpnam(buffer); |
| 4159 | #endif |
| 4160 | if (name == NULL) { |
| 4161 | PyErr_SetObject(PyExc_OSError, |
| 4162 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4163 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4164 | "unexpected NULL from tmpnam_r" |
| 4165 | #else |
| 4166 | "unexpected NULL from tmpnam" |
| 4167 | #endif |
| 4168 | )); |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | return PyString_FromString(buffer); |
| 4172 | } |
| 4173 | #endif |
| 4174 | |
| 4175 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4176 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4177 | * It maps strings representing configuration variable names to |
| 4178 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4179 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4180 | * rarely-used constants. There are three separate tables that use |
| 4181 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4182 | * |
| 4183 | * This code is always included, even if none of the interfaces that |
| 4184 | * need it are included. The #if hackery needed to avoid it would be |
| 4185 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4186 | */ |
| 4187 | struct constdef { |
| 4188 | char *name; |
| 4189 | long value; |
| 4190 | }; |
| 4191 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4192 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4193 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4194 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4195 | { |
| 4196 | if (PyInt_Check(arg)) { |
| 4197 | *valuep = PyInt_AS_LONG(arg); |
| 4198 | return 1; |
| 4199 | } |
| 4200 | if (PyString_Check(arg)) { |
| 4201 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4202 | size_t lo = 0; |
| 4203 | size_t mid; |
| 4204 | size_t hi = tablesize; |
| 4205 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4206 | char *confname = PyString_AS_STRING(arg); |
| 4207 | while (lo < hi) { |
| 4208 | mid = (lo + hi) / 2; |
| 4209 | cmp = strcmp(confname, table[mid].name); |
| 4210 | if (cmp < 0) |
| 4211 | hi = mid; |
| 4212 | else if (cmp > 0) |
| 4213 | lo = mid + 1; |
| 4214 | else { |
| 4215 | *valuep = table[mid].value; |
| 4216 | return 1; |
| 4217 | } |
| 4218 | } |
| 4219 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4220 | } |
| 4221 | else |
| 4222 | PyErr_SetString(PyExc_TypeError, |
| 4223 | "configuration names must be strings or integers"); |
| 4224 | return 0; |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4229 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4230 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4231 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4232 | #endif |
| 4233 | #ifdef _PC_ABI_ASYNC_IO |
| 4234 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4235 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4236 | #ifdef _PC_ASYNC_IO |
| 4237 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4238 | #endif |
| 4239 | #ifdef _PC_CHOWN_RESTRICTED |
| 4240 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4241 | #endif |
| 4242 | #ifdef _PC_FILESIZEBITS |
| 4243 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4244 | #endif |
| 4245 | #ifdef _PC_LAST |
| 4246 | {"PC_LAST", _PC_LAST}, |
| 4247 | #endif |
| 4248 | #ifdef _PC_LINK_MAX |
| 4249 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4250 | #endif |
| 4251 | #ifdef _PC_MAX_CANON |
| 4252 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4253 | #endif |
| 4254 | #ifdef _PC_MAX_INPUT |
| 4255 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4256 | #endif |
| 4257 | #ifdef _PC_NAME_MAX |
| 4258 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4259 | #endif |
| 4260 | #ifdef _PC_NO_TRUNC |
| 4261 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4262 | #endif |
| 4263 | #ifdef _PC_PATH_MAX |
| 4264 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4265 | #endif |
| 4266 | #ifdef _PC_PIPE_BUF |
| 4267 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4268 | #endif |
| 4269 | #ifdef _PC_PRIO_IO |
| 4270 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4271 | #endif |
| 4272 | #ifdef _PC_SOCK_MAXBUF |
| 4273 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4274 | #endif |
| 4275 | #ifdef _PC_SYNC_IO |
| 4276 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4277 | #endif |
| 4278 | #ifdef _PC_VDISABLE |
| 4279 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4280 | #endif |
| 4281 | }; |
| 4282 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4283 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4284 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4285 | { |
| 4286 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4287 | sizeof(posix_constants_pathconf) |
| 4288 | / sizeof(struct constdef)); |
| 4289 | } |
| 4290 | #endif |
| 4291 | |
| 4292 | #ifdef HAVE_FPATHCONF |
| 4293 | static char posix_fpathconf__doc__[] = "\ |
| 4294 | fpathconf(fd, name) -> integer\n\ |
| 4295 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4296 | If there is no limit, return -1."; |
| 4297 | |
| 4298 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4299 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4300 | { |
| 4301 | PyObject *result = NULL; |
| 4302 | int name, fd; |
| 4303 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4304 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4305 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4306 | long limit; |
| 4307 | |
| 4308 | errno = 0; |
| 4309 | limit = fpathconf(fd, name); |
| 4310 | if (limit == -1 && errno != 0) |
| 4311 | posix_error(); |
| 4312 | else |
| 4313 | result = PyInt_FromLong(limit); |
| 4314 | } |
| 4315 | return result; |
| 4316 | } |
| 4317 | #endif |
| 4318 | |
| 4319 | |
| 4320 | #ifdef HAVE_PATHCONF |
| 4321 | static char posix_pathconf__doc__[] = "\ |
| 4322 | pathconf(path, name) -> integer\n\ |
| 4323 | Return the configuration limit name for the file or directory path.\n\ |
| 4324 | If there is no limit, return -1."; |
| 4325 | |
| 4326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4327 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4328 | { |
| 4329 | PyObject *result = NULL; |
| 4330 | int name; |
| 4331 | char *path; |
| 4332 | |
| 4333 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4334 | conv_path_confname, &name)) { |
| 4335 | long limit; |
| 4336 | |
| 4337 | errno = 0; |
| 4338 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4339 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4340 | if (errno == EINVAL) |
| 4341 | /* could be a path or name problem */ |
| 4342 | posix_error(); |
| 4343 | else |
| 4344 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4345 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4346 | else |
| 4347 | result = PyInt_FromLong(limit); |
| 4348 | } |
| 4349 | return result; |
| 4350 | } |
| 4351 | #endif |
| 4352 | |
| 4353 | #ifdef HAVE_CONFSTR |
| 4354 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4355 | #ifdef _CS_ARCHITECTURE |
| 4356 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4357 | #endif |
| 4358 | #ifdef _CS_HOSTNAME |
| 4359 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4360 | #endif |
| 4361 | #ifdef _CS_HW_PROVIDER |
| 4362 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4363 | #endif |
| 4364 | #ifdef _CS_HW_SERIAL |
| 4365 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4366 | #endif |
| 4367 | #ifdef _CS_INITTAB_NAME |
| 4368 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4369 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4370 | #ifdef _CS_LFS64_CFLAGS |
| 4371 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4372 | #endif |
| 4373 | #ifdef _CS_LFS64_LDFLAGS |
| 4374 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4375 | #endif |
| 4376 | #ifdef _CS_LFS64_LIBS |
| 4377 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4378 | #endif |
| 4379 | #ifdef _CS_LFS64_LINTFLAGS |
| 4380 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4381 | #endif |
| 4382 | #ifdef _CS_LFS_CFLAGS |
| 4383 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4384 | #endif |
| 4385 | #ifdef _CS_LFS_LDFLAGS |
| 4386 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4387 | #endif |
| 4388 | #ifdef _CS_LFS_LIBS |
| 4389 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4390 | #endif |
| 4391 | #ifdef _CS_LFS_LINTFLAGS |
| 4392 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4393 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4394 | #ifdef _CS_MACHINE |
| 4395 | {"CS_MACHINE", _CS_MACHINE}, |
| 4396 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4397 | #ifdef _CS_PATH |
| 4398 | {"CS_PATH", _CS_PATH}, |
| 4399 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4400 | #ifdef _CS_RELEASE |
| 4401 | {"CS_RELEASE", _CS_RELEASE}, |
| 4402 | #endif |
| 4403 | #ifdef _CS_SRPC_DOMAIN |
| 4404 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4405 | #endif |
| 4406 | #ifdef _CS_SYSNAME |
| 4407 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4408 | #endif |
| 4409 | #ifdef _CS_VERSION |
| 4410 | {"CS_VERSION", _CS_VERSION}, |
| 4411 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4412 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4413 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4414 | #endif |
| 4415 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4416 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4417 | #endif |
| 4418 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4419 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4420 | #endif |
| 4421 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4422 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4423 | #endif |
| 4424 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4425 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4426 | #endif |
| 4427 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4428 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4429 | #endif |
| 4430 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4431 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4432 | #endif |
| 4433 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4434 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4435 | #endif |
| 4436 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4437 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4438 | #endif |
| 4439 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4440 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4441 | #endif |
| 4442 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4443 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4444 | #endif |
| 4445 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4446 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4447 | #endif |
| 4448 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4449 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4450 | #endif |
| 4451 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4452 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4453 | #endif |
| 4454 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4455 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4456 | #endif |
| 4457 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4458 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4459 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4460 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4461 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4462 | #endif |
| 4463 | #ifdef _MIPS_CS_BASE |
| 4464 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4465 | #endif |
| 4466 | #ifdef _MIPS_CS_HOSTID |
| 4467 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4468 | #endif |
| 4469 | #ifdef _MIPS_CS_HW_NAME |
| 4470 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4471 | #endif |
| 4472 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4473 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4474 | #endif |
| 4475 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4476 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4477 | #endif |
| 4478 | #ifdef _MIPS_CS_OSREL_MIN |
| 4479 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4480 | #endif |
| 4481 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4482 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4483 | #endif |
| 4484 | #ifdef _MIPS_CS_OS_NAME |
| 4485 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4486 | #endif |
| 4487 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4488 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4489 | #endif |
| 4490 | #ifdef _MIPS_CS_PROCESSORS |
| 4491 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4492 | #endif |
| 4493 | #ifdef _MIPS_CS_SERIAL |
| 4494 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4495 | #endif |
| 4496 | #ifdef _MIPS_CS_VENDOR |
| 4497 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4498 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4499 | }; |
| 4500 | |
| 4501 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4502 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4503 | { |
| 4504 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4505 | sizeof(posix_constants_confstr) |
| 4506 | / sizeof(struct constdef)); |
| 4507 | } |
| 4508 | |
| 4509 | static char posix_confstr__doc__[] = "\ |
| 4510 | confstr(name) -> string\n\ |
| 4511 | Return a string-valued system configuration variable."; |
| 4512 | |
| 4513 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4514 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4515 | { |
| 4516 | PyObject *result = NULL; |
| 4517 | int name; |
| 4518 | char buffer[64]; |
| 4519 | |
| 4520 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4521 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4522 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4523 | errno = 0; |
| 4524 | if (len == 0) { |
| 4525 | if (errno != 0) |
| 4526 | posix_error(); |
| 4527 | else |
| 4528 | result = PyString_FromString(""); |
| 4529 | } |
| 4530 | else { |
| 4531 | if (len >= sizeof(buffer)) { |
| 4532 | result = PyString_FromStringAndSize(NULL, len); |
| 4533 | if (result != NULL) |
| 4534 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4535 | } |
| 4536 | else |
| 4537 | result = PyString_FromString(buffer); |
| 4538 | } |
| 4539 | } |
| 4540 | return result; |
| 4541 | } |
| 4542 | #endif |
| 4543 | |
| 4544 | |
| 4545 | #ifdef HAVE_SYSCONF |
| 4546 | static struct constdef posix_constants_sysconf[] = { |
| 4547 | #ifdef _SC_2_CHAR_TERM |
| 4548 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4549 | #endif |
| 4550 | #ifdef _SC_2_C_BIND |
| 4551 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4552 | #endif |
| 4553 | #ifdef _SC_2_C_DEV |
| 4554 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4555 | #endif |
| 4556 | #ifdef _SC_2_C_VERSION |
| 4557 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4558 | #endif |
| 4559 | #ifdef _SC_2_FORT_DEV |
| 4560 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4561 | #endif |
| 4562 | #ifdef _SC_2_FORT_RUN |
| 4563 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4564 | #endif |
| 4565 | #ifdef _SC_2_LOCALEDEF |
| 4566 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4567 | #endif |
| 4568 | #ifdef _SC_2_SW_DEV |
| 4569 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4570 | #endif |
| 4571 | #ifdef _SC_2_UPE |
| 4572 | {"SC_2_UPE", _SC_2_UPE}, |
| 4573 | #endif |
| 4574 | #ifdef _SC_2_VERSION |
| 4575 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4576 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4577 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4578 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4579 | #endif |
| 4580 | #ifdef _SC_ACL |
| 4581 | {"SC_ACL", _SC_ACL}, |
| 4582 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4583 | #ifdef _SC_AIO_LISTIO_MAX |
| 4584 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4585 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4586 | #ifdef _SC_AIO_MAX |
| 4587 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4588 | #endif |
| 4589 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4590 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4591 | #endif |
| 4592 | #ifdef _SC_ARG_MAX |
| 4593 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4594 | #endif |
| 4595 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4596 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4597 | #endif |
| 4598 | #ifdef _SC_ATEXIT_MAX |
| 4599 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4600 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4601 | #ifdef _SC_AUDIT |
| 4602 | {"SC_AUDIT", _SC_AUDIT}, |
| 4603 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4604 | #ifdef _SC_AVPHYS_PAGES |
| 4605 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4606 | #endif |
| 4607 | #ifdef _SC_BC_BASE_MAX |
| 4608 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4609 | #endif |
| 4610 | #ifdef _SC_BC_DIM_MAX |
| 4611 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4612 | #endif |
| 4613 | #ifdef _SC_BC_SCALE_MAX |
| 4614 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4615 | #endif |
| 4616 | #ifdef _SC_BC_STRING_MAX |
| 4617 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4618 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4619 | #ifdef _SC_CAP |
| 4620 | {"SC_CAP", _SC_CAP}, |
| 4621 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4622 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4623 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4624 | #endif |
| 4625 | #ifdef _SC_CHAR_BIT |
| 4626 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4627 | #endif |
| 4628 | #ifdef _SC_CHAR_MAX |
| 4629 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4630 | #endif |
| 4631 | #ifdef _SC_CHAR_MIN |
| 4632 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4633 | #endif |
| 4634 | #ifdef _SC_CHILD_MAX |
| 4635 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4636 | #endif |
| 4637 | #ifdef _SC_CLK_TCK |
| 4638 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4639 | #endif |
| 4640 | #ifdef _SC_COHER_BLKSZ |
| 4641 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4642 | #endif |
| 4643 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4644 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4645 | #endif |
| 4646 | #ifdef _SC_DCACHE_ASSOC |
| 4647 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4648 | #endif |
| 4649 | #ifdef _SC_DCACHE_BLKSZ |
| 4650 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4651 | #endif |
| 4652 | #ifdef _SC_DCACHE_LINESZ |
| 4653 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4654 | #endif |
| 4655 | #ifdef _SC_DCACHE_SZ |
| 4656 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 4657 | #endif |
| 4658 | #ifdef _SC_DCACHE_TBLKSZ |
| 4659 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 4660 | #endif |
| 4661 | #ifdef _SC_DELAYTIMER_MAX |
| 4662 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 4663 | #endif |
| 4664 | #ifdef _SC_EQUIV_CLASS_MAX |
| 4665 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 4666 | #endif |
| 4667 | #ifdef _SC_EXPR_NEST_MAX |
| 4668 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 4669 | #endif |
| 4670 | #ifdef _SC_FSYNC |
| 4671 | {"SC_FSYNC", _SC_FSYNC}, |
| 4672 | #endif |
| 4673 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 4674 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 4675 | #endif |
| 4676 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 4677 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 4678 | #endif |
| 4679 | #ifdef _SC_ICACHE_ASSOC |
| 4680 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 4681 | #endif |
| 4682 | #ifdef _SC_ICACHE_BLKSZ |
| 4683 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 4684 | #endif |
| 4685 | #ifdef _SC_ICACHE_LINESZ |
| 4686 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 4687 | #endif |
| 4688 | #ifdef _SC_ICACHE_SZ |
| 4689 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 4690 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4691 | #ifdef _SC_INF |
| 4692 | {"SC_INF", _SC_INF}, |
| 4693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4694 | #ifdef _SC_INT_MAX |
| 4695 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 4696 | #endif |
| 4697 | #ifdef _SC_INT_MIN |
| 4698 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 4699 | #endif |
| 4700 | #ifdef _SC_IOV_MAX |
| 4701 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 4702 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4703 | #ifdef _SC_IP_SECOPTS |
| 4704 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 4705 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4706 | #ifdef _SC_JOB_CONTROL |
| 4707 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 4708 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4709 | #ifdef _SC_KERN_POINTERS |
| 4710 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 4711 | #endif |
| 4712 | #ifdef _SC_KERN_SIM |
| 4713 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 4714 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4715 | #ifdef _SC_LINE_MAX |
| 4716 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 4717 | #endif |
| 4718 | #ifdef _SC_LOGIN_NAME_MAX |
| 4719 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 4720 | #endif |
| 4721 | #ifdef _SC_LOGNAME_MAX |
| 4722 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 4723 | #endif |
| 4724 | #ifdef _SC_LONG_BIT |
| 4725 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 4726 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4727 | #ifdef _SC_MAC |
| 4728 | {"SC_MAC", _SC_MAC}, |
| 4729 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4730 | #ifdef _SC_MAPPED_FILES |
| 4731 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 4732 | #endif |
| 4733 | #ifdef _SC_MAXPID |
| 4734 | {"SC_MAXPID", _SC_MAXPID}, |
| 4735 | #endif |
| 4736 | #ifdef _SC_MB_LEN_MAX |
| 4737 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 4738 | #endif |
| 4739 | #ifdef _SC_MEMLOCK |
| 4740 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 4741 | #endif |
| 4742 | #ifdef _SC_MEMLOCK_RANGE |
| 4743 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 4744 | #endif |
| 4745 | #ifdef _SC_MEMORY_PROTECTION |
| 4746 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 4747 | #endif |
| 4748 | #ifdef _SC_MESSAGE_PASSING |
| 4749 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 4750 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4751 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 4752 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 4753 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4754 | #ifdef _SC_MQ_OPEN_MAX |
| 4755 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 4756 | #endif |
| 4757 | #ifdef _SC_MQ_PRIO_MAX |
| 4758 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 4759 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4760 | #ifdef _SC_NACLS_MAX |
| 4761 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 4762 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4763 | #ifdef _SC_NGROUPS_MAX |
| 4764 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 4765 | #endif |
| 4766 | #ifdef _SC_NL_ARGMAX |
| 4767 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 4768 | #endif |
| 4769 | #ifdef _SC_NL_LANGMAX |
| 4770 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 4771 | #endif |
| 4772 | #ifdef _SC_NL_MSGMAX |
| 4773 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 4774 | #endif |
| 4775 | #ifdef _SC_NL_NMAX |
| 4776 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 4777 | #endif |
| 4778 | #ifdef _SC_NL_SETMAX |
| 4779 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 4780 | #endif |
| 4781 | #ifdef _SC_NL_TEXTMAX |
| 4782 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 4783 | #endif |
| 4784 | #ifdef _SC_NPROCESSORS_CONF |
| 4785 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 4786 | #endif |
| 4787 | #ifdef _SC_NPROCESSORS_ONLN |
| 4788 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 4789 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4790 | #ifdef _SC_NPROC_CONF |
| 4791 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 4792 | #endif |
| 4793 | #ifdef _SC_NPROC_ONLN |
| 4794 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 4795 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4796 | #ifdef _SC_NZERO |
| 4797 | {"SC_NZERO", _SC_NZERO}, |
| 4798 | #endif |
| 4799 | #ifdef _SC_OPEN_MAX |
| 4800 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 4801 | #endif |
| 4802 | #ifdef _SC_PAGESIZE |
| 4803 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 4804 | #endif |
| 4805 | #ifdef _SC_PAGE_SIZE |
| 4806 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 4807 | #endif |
| 4808 | #ifdef _SC_PASS_MAX |
| 4809 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 4810 | #endif |
| 4811 | #ifdef _SC_PHYS_PAGES |
| 4812 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 4813 | #endif |
| 4814 | #ifdef _SC_PII |
| 4815 | {"SC_PII", _SC_PII}, |
| 4816 | #endif |
| 4817 | #ifdef _SC_PII_INTERNET |
| 4818 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 4819 | #endif |
| 4820 | #ifdef _SC_PII_INTERNET_DGRAM |
| 4821 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 4822 | #endif |
| 4823 | #ifdef _SC_PII_INTERNET_STREAM |
| 4824 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 4825 | #endif |
| 4826 | #ifdef _SC_PII_OSI |
| 4827 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 4828 | #endif |
| 4829 | #ifdef _SC_PII_OSI_CLTS |
| 4830 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 4831 | #endif |
| 4832 | #ifdef _SC_PII_OSI_COTS |
| 4833 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 4834 | #endif |
| 4835 | #ifdef _SC_PII_OSI_M |
| 4836 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 4837 | #endif |
| 4838 | #ifdef _SC_PII_SOCKET |
| 4839 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 4840 | #endif |
| 4841 | #ifdef _SC_PII_XTI |
| 4842 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 4843 | #endif |
| 4844 | #ifdef _SC_POLL |
| 4845 | {"SC_POLL", _SC_POLL}, |
| 4846 | #endif |
| 4847 | #ifdef _SC_PRIORITIZED_IO |
| 4848 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 4849 | #endif |
| 4850 | #ifdef _SC_PRIORITY_SCHEDULING |
| 4851 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 4852 | #endif |
| 4853 | #ifdef _SC_REALTIME_SIGNALS |
| 4854 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 4855 | #endif |
| 4856 | #ifdef _SC_RE_DUP_MAX |
| 4857 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 4858 | #endif |
| 4859 | #ifdef _SC_RTSIG_MAX |
| 4860 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 4861 | #endif |
| 4862 | #ifdef _SC_SAVED_IDS |
| 4863 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 4864 | #endif |
| 4865 | #ifdef _SC_SCHAR_MAX |
| 4866 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 4867 | #endif |
| 4868 | #ifdef _SC_SCHAR_MIN |
| 4869 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 4870 | #endif |
| 4871 | #ifdef _SC_SELECT |
| 4872 | {"SC_SELECT", _SC_SELECT}, |
| 4873 | #endif |
| 4874 | #ifdef _SC_SEMAPHORES |
| 4875 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 4876 | #endif |
| 4877 | #ifdef _SC_SEM_NSEMS_MAX |
| 4878 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 4879 | #endif |
| 4880 | #ifdef _SC_SEM_VALUE_MAX |
| 4881 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 4882 | #endif |
| 4883 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 4884 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 4885 | #endif |
| 4886 | #ifdef _SC_SHRT_MAX |
| 4887 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 4888 | #endif |
| 4889 | #ifdef _SC_SHRT_MIN |
| 4890 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 4891 | #endif |
| 4892 | #ifdef _SC_SIGQUEUE_MAX |
| 4893 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 4894 | #endif |
| 4895 | #ifdef _SC_SIGRT_MAX |
| 4896 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 4897 | #endif |
| 4898 | #ifdef _SC_SIGRT_MIN |
| 4899 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 4900 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4901 | #ifdef _SC_SOFTPOWER |
| 4902 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 4903 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4904 | #ifdef _SC_SPLIT_CACHE |
| 4905 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 4906 | #endif |
| 4907 | #ifdef _SC_SSIZE_MAX |
| 4908 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 4909 | #endif |
| 4910 | #ifdef _SC_STACK_PROT |
| 4911 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 4912 | #endif |
| 4913 | #ifdef _SC_STREAM_MAX |
| 4914 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 4915 | #endif |
| 4916 | #ifdef _SC_SYNCHRONIZED_IO |
| 4917 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 4918 | #endif |
| 4919 | #ifdef _SC_THREADS |
| 4920 | {"SC_THREADS", _SC_THREADS}, |
| 4921 | #endif |
| 4922 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 4923 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 4924 | #endif |
| 4925 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 4926 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 4927 | #endif |
| 4928 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 4929 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 4930 | #endif |
| 4931 | #ifdef _SC_THREAD_KEYS_MAX |
| 4932 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 4933 | #endif |
| 4934 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 4935 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 4936 | #endif |
| 4937 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 4938 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 4939 | #endif |
| 4940 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 4941 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 4942 | #endif |
| 4943 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 4944 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 4945 | #endif |
| 4946 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 4947 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 4948 | #endif |
| 4949 | #ifdef _SC_THREAD_STACK_MIN |
| 4950 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 4951 | #endif |
| 4952 | #ifdef _SC_THREAD_THREADS_MAX |
| 4953 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 4954 | #endif |
| 4955 | #ifdef _SC_TIMERS |
| 4956 | {"SC_TIMERS", _SC_TIMERS}, |
| 4957 | #endif |
| 4958 | #ifdef _SC_TIMER_MAX |
| 4959 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 4960 | #endif |
| 4961 | #ifdef _SC_TTY_NAME_MAX |
| 4962 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 4963 | #endif |
| 4964 | #ifdef _SC_TZNAME_MAX |
| 4965 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 4966 | #endif |
| 4967 | #ifdef _SC_T_IOV_MAX |
| 4968 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 4969 | #endif |
| 4970 | #ifdef _SC_UCHAR_MAX |
| 4971 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 4972 | #endif |
| 4973 | #ifdef _SC_UINT_MAX |
| 4974 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 4975 | #endif |
| 4976 | #ifdef _SC_UIO_MAXIOV |
| 4977 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 4978 | #endif |
| 4979 | #ifdef _SC_ULONG_MAX |
| 4980 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 4981 | #endif |
| 4982 | #ifdef _SC_USHRT_MAX |
| 4983 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 4984 | #endif |
| 4985 | #ifdef _SC_VERSION |
| 4986 | {"SC_VERSION", _SC_VERSION}, |
| 4987 | #endif |
| 4988 | #ifdef _SC_WORD_BIT |
| 4989 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 4990 | #endif |
| 4991 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 4992 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 4993 | #endif |
| 4994 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 4995 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 4996 | #endif |
| 4997 | #ifdef _SC_XBS5_LP64_OFF64 |
| 4998 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 4999 | #endif |
| 5000 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5001 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5002 | #endif |
| 5003 | #ifdef _SC_XOPEN_CRYPT |
| 5004 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5005 | #endif |
| 5006 | #ifdef _SC_XOPEN_ENH_I18N |
| 5007 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5008 | #endif |
| 5009 | #ifdef _SC_XOPEN_LEGACY |
| 5010 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5011 | #endif |
| 5012 | #ifdef _SC_XOPEN_REALTIME |
| 5013 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5014 | #endif |
| 5015 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5016 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5017 | #endif |
| 5018 | #ifdef _SC_XOPEN_SHM |
| 5019 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5020 | #endif |
| 5021 | #ifdef _SC_XOPEN_UNIX |
| 5022 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5023 | #endif |
| 5024 | #ifdef _SC_XOPEN_VERSION |
| 5025 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5026 | #endif |
| 5027 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5028 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5029 | #endif |
| 5030 | #ifdef _SC_XOPEN_XPG2 |
| 5031 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5032 | #endif |
| 5033 | #ifdef _SC_XOPEN_XPG3 |
| 5034 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5035 | #endif |
| 5036 | #ifdef _SC_XOPEN_XPG4 |
| 5037 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5038 | #endif |
| 5039 | }; |
| 5040 | |
| 5041 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5042 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5043 | { |
| 5044 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5045 | sizeof(posix_constants_sysconf) |
| 5046 | / sizeof(struct constdef)); |
| 5047 | } |
| 5048 | |
| 5049 | static char posix_sysconf__doc__[] = "\ |
| 5050 | sysconf(name) -> integer\n\ |
| 5051 | Return an integer-valued system configuration variable."; |
| 5052 | |
| 5053 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5054 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5055 | { |
| 5056 | PyObject *result = NULL; |
| 5057 | int name; |
| 5058 | |
| 5059 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5060 | int value; |
| 5061 | |
| 5062 | errno = 0; |
| 5063 | value = sysconf(name); |
| 5064 | if (value == -1 && errno != 0) |
| 5065 | posix_error(); |
| 5066 | else |
| 5067 | result = PyInt_FromLong(value); |
| 5068 | } |
| 5069 | return result; |
| 5070 | } |
| 5071 | #endif |
| 5072 | |
| 5073 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5074 | /* This code is used to ensure that the tables of configuration value names |
| 5075 | * are in sorted order as required by conv_confname(), and also to build the |
| 5076 | * the exported dictionaries that are used to publish information about the |
| 5077 | * names available on the host platform. |
| 5078 | * |
| 5079 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5080 | * when used, even for platforms we're not able to test on. It also makes |
| 5081 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5082 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5083 | |
| 5084 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5085 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5086 | { |
| 5087 | const struct constdef *c1 = |
| 5088 | (const struct constdef *) v1; |
| 5089 | const struct constdef *c2 = |
| 5090 | (const struct constdef *) v2; |
| 5091 | |
| 5092 | return strcmp(c1->name, c2->name); |
| 5093 | } |
| 5094 | |
| 5095 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5096 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5097 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5098 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5099 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5100 | size_t i; |
| 5101 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5102 | |
| 5103 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5104 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5105 | if (d == NULL) |
| 5106 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5107 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5108 | for (i=0; i < tablesize; ++i) { |
| 5109 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5110 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5111 | Py_XDECREF(o); |
| 5112 | Py_DECREF(d); |
| 5113 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5114 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5115 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5116 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5117 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5118 | Py_DECREF(d); |
| 5119 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5122 | /* Return -1 on failure, 0 on success. */ |
| 5123 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5124 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5125 | { |
| 5126 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5127 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5128 | sizeof(posix_constants_pathconf) |
| 5129 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5130 | "pathconf_names", moddict)) |
| 5131 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5132 | #endif |
| 5133 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5134 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5135 | sizeof(posix_constants_confstr) |
| 5136 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5137 | "confstr_names", moddict)) |
| 5138 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5139 | #endif |
| 5140 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5141 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5142 | sizeof(posix_constants_sysconf) |
| 5143 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5144 | "sysconf_names", moddict)) |
| 5145 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5146 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5147 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5148 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5149 | |
| 5150 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5151 | static char posix_abort__doc__[] = "\ |
| 5152 | abort() -> does not return!\n\ |
| 5153 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5154 | in the hardest way possible on the hosting operating system."; |
| 5155 | |
| 5156 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5157 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5158 | { |
| 5159 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5160 | return NULL; |
| 5161 | abort(); |
| 5162 | /*NOTREACHED*/ |
| 5163 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5164 | return NULL; |
| 5165 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5166 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5167 | #ifdef MS_WIN32 |
| 5168 | static char win32_startfile__doc__[] = "\ |
| 5169 | startfile(filepath) - Start a file with its associated application.\n\ |
| 5170 | \n\ |
| 5171 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 5172 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 5173 | with whatever application (if any) its extension is associated.\n\ |
| 5174 | \n\ |
| 5175 | startfile returns as soon as the associated application is launched.\n\ |
| 5176 | There is no option to wait for the application to close, and no way\n\ |
| 5177 | to retrieve the application's exit status.\n\ |
| 5178 | \n\ |
| 5179 | The filepath is relative to the current directory. If you want to use\n\ |
| 5180 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 5181 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 5182 | |
| 5183 | static PyObject * |
| 5184 | win32_startfile(PyObject *self, PyObject *args) |
| 5185 | { |
| 5186 | char *filepath; |
| 5187 | HINSTANCE rc; |
| 5188 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 5189 | return NULL; |
| 5190 | Py_BEGIN_ALLOW_THREADS |
| 5191 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 5192 | Py_END_ALLOW_THREADS |
| 5193 | if (rc <= (HINSTANCE)32) |
| 5194 | return win32_error("startfile", filepath); |
| 5195 | Py_INCREF(Py_None); |
| 5196 | return Py_None; |
| 5197 | } |
| 5198 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5199 | |
| 5200 | static PyMethodDef posix_methods[] = { |
| 5201 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5202 | #ifdef HAVE_TTYNAME |
| 5203 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5204 | #endif |
| 5205 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5206 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5207 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5208 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5209 | #endif /* HAVE_CHOWN */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5210 | #ifdef HAVE_CTERMID |
| 5211 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5212 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5213 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5214 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5215 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5216 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5217 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5218 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5219 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5220 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5221 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5222 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5223 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5224 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5225 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5226 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5227 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5228 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5229 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5230 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5231 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5232 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5233 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5234 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5235 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5236 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5237 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5238 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5239 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5240 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5241 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5242 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5243 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5244 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5245 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5246 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5247 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5248 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5249 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5250 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5251 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5252 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5253 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5254 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5255 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5256 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5257 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5258 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5259 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5260 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5261 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5262 | #ifdef HAVE_FORKPTY |
| 5263 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5264 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5265 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5266 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5267 | #endif /* HAVE_GETEGID */ |
| 5268 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5269 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5270 | #endif /* HAVE_GETEUID */ |
| 5271 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5272 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5273 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5274 | #ifdef HAVE_GETGROUPS |
| 5275 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5276 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5277 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5278 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5279 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5280 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5281 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5282 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5283 | #endif /* HAVE_GETPPID */ |
| 5284 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5285 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5286 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5287 | #ifdef HAVE_GETLOGIN |
| 5288 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5289 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5290 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5291 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5292 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5293 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5294 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5295 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5296 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5297 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5298 | #ifdef MS_WIN32 |
| 5299 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5300 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5301 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5302 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5303 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5304 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5305 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5306 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5307 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5308 | #ifdef HAVE_SETEUID |
| 5309 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5310 | #endif /* HAVE_SETEUID */ |
| 5311 | #ifdef HAVE_SETEGID |
| 5312 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5313 | #endif /* HAVE_SETEGID */ |
| 5314 | #ifdef HAVE_SETREUID |
| 5315 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5316 | #endif /* HAVE_SETREUID */ |
| 5317 | #ifdef HAVE_SETREGID |
| 5318 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5319 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5320 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5321 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5322 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5323 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5324 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5325 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5326 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5327 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5328 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5329 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5330 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5331 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5332 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5333 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5334 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5335 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5336 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5337 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5338 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5339 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5340 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5341 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5342 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5343 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5344 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5345 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5346 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5347 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5348 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5349 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5350 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5351 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5352 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5353 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5354 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5355 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5356 | #endif |
| 5357 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5358 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5359 | #endif |
| 5360 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5361 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5362 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5363 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5364 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5365 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5366 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5367 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5368 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5369 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5370 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5371 | #endif |
| 5372 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5373 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5374 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5375 | #ifdef HAVE_SYS_WAIT_H |
| 5376 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5377 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5378 | #endif /* WIFSTOPPED */ |
| 5379 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5380 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5381 | #endif /* WIFSIGNALED */ |
| 5382 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5383 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5384 | #endif /* WIFEXITED */ |
| 5385 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5386 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5387 | #endif /* WEXITSTATUS */ |
| 5388 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5389 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5390 | #endif /* WTERMSIG */ |
| 5391 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5392 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5393 | #endif /* WSTOPSIG */ |
| 5394 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5395 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5396 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5397 | #endif |
| 5398 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5399 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5400 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 5401 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5402 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5403 | #endif |
| 5404 | #ifdef HAVE_TEMPNAM |
| 5405 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5406 | #endif |
| 5407 | #ifdef HAVE_TMPNAM |
| 5408 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5409 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5410 | #ifdef HAVE_CONFSTR |
| 5411 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5412 | #endif |
| 5413 | #ifdef HAVE_SYSCONF |
| 5414 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5415 | #endif |
| 5416 | #ifdef HAVE_FPATHCONF |
| 5417 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5418 | #endif |
| 5419 | #ifdef HAVE_PATHCONF |
| 5420 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5421 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5422 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5423 | {NULL, NULL} /* Sentinel */ |
| 5424 | }; |
| 5425 | |
| 5426 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5427 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5428 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5429 | { |
| 5430 | PyObject* v = PyInt_FromLong(value); |
| 5431 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5432 | return -1; /* triggers fatal error */ |
| 5433 | |
| 5434 | Py_DECREF(v); |
| 5435 | return 0; |
| 5436 | } |
| 5437 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5438 | #if defined(PYOS_OS2) |
| 5439 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5440 | static int insertvalues(PyObject *d) |
| 5441 | { |
| 5442 | APIRET rc; |
| 5443 | ULONG values[QSV_MAX+1]; |
| 5444 | PyObject *v; |
| 5445 | char *ver, tmp[10]; |
| 5446 | |
| 5447 | Py_BEGIN_ALLOW_THREADS |
| 5448 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5449 | Py_END_ALLOW_THREADS |
| 5450 | |
| 5451 | if (rc != NO_ERROR) { |
| 5452 | os2_error(rc); |
| 5453 | return -1; |
| 5454 | } |
| 5455 | |
| 5456 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5457 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5458 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5459 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5460 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5461 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5462 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5463 | |
| 5464 | switch (values[QSV_VERSION_MINOR]) { |
| 5465 | case 0: ver = "2.00"; break; |
| 5466 | case 10: ver = "2.10"; break; |
| 5467 | case 11: ver = "2.11"; break; |
| 5468 | case 30: ver = "3.00"; break; |
| 5469 | case 40: ver = "4.00"; break; |
| 5470 | case 50: ver = "5.00"; break; |
| 5471 | default: |
| 5472 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 5473 | values[QSV_VERSION_MINOR]); |
| 5474 | ver = &tmp[0]; |
| 5475 | } |
| 5476 | |
| 5477 | /* Add Indicator of the Version of the Operating System */ |
| 5478 | v = PyString_FromString(ver); |
| 5479 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5480 | return -1; |
| 5481 | Py_DECREF(v); |
| 5482 | |
| 5483 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5484 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5485 | tmp[1] = ':'; |
| 5486 | tmp[2] = '\0'; |
| 5487 | |
| 5488 | v = PyString_FromString(tmp); |
| 5489 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5490 | return -1; |
| 5491 | Py_DECREF(v); |
| 5492 | |
| 5493 | return 0; |
| 5494 | } |
| 5495 | #endif |
| 5496 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5497 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5498 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5499 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5500 | #ifdef F_OK |
| 5501 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5502 | #endif |
| 5503 | #ifdef R_OK |
| 5504 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5505 | #endif |
| 5506 | #ifdef W_OK |
| 5507 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5508 | #endif |
| 5509 | #ifdef X_OK |
| 5510 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5511 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5512 | #ifdef NGROUPS_MAX |
| 5513 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5514 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5515 | #ifdef TMP_MAX |
| 5516 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5517 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5518 | #ifdef WNOHANG |
| 5519 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5520 | #endif |
| 5521 | #ifdef O_RDONLY |
| 5522 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5523 | #endif |
| 5524 | #ifdef O_WRONLY |
| 5525 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5526 | #endif |
| 5527 | #ifdef O_RDWR |
| 5528 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5529 | #endif |
| 5530 | #ifdef O_NDELAY |
| 5531 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5532 | #endif |
| 5533 | #ifdef O_NONBLOCK |
| 5534 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5535 | #endif |
| 5536 | #ifdef O_APPEND |
| 5537 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5538 | #endif |
| 5539 | #ifdef O_DSYNC |
| 5540 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5541 | #endif |
| 5542 | #ifdef O_RSYNC |
| 5543 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5544 | #endif |
| 5545 | #ifdef O_SYNC |
| 5546 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5547 | #endif |
| 5548 | #ifdef O_NOCTTY |
| 5549 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5550 | #endif |
| 5551 | #ifdef O_CREAT |
| 5552 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5553 | #endif |
| 5554 | #ifdef O_EXCL |
| 5555 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5556 | #endif |
| 5557 | #ifdef O_TRUNC |
| 5558 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5559 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5560 | #ifdef O_BINARY |
| 5561 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5562 | #endif |
| 5563 | #ifdef O_TEXT |
| 5564 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5565 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5566 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5567 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5568 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5569 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5570 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5571 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5572 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5573 | #endif |
| 5574 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5575 | #if defined(PYOS_OS2) |
| 5576 | if (insertvalues(d)) return -1; |
| 5577 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5578 | return 0; |
| 5579 | } |
| 5580 | |
| 5581 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5582 | #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5583 | #define INITFUNC initnt |
| 5584 | #define MODNAME "nt" |
| 5585 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5586 | #if defined(PYOS_OS2) |
| 5587 | #define INITFUNC initos2 |
| 5588 | #define MODNAME "os2" |
| 5589 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5590 | #define INITFUNC initposix |
| 5591 | #define MODNAME "posix" |
| 5592 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5593 | #endif |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5594 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5595 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5596 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5597 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5598 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5599 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5600 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5601 | posix_methods, |
| 5602 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5603 | (PyObject *)NULL, |
| 5604 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5605 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5606 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5607 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5608 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5609 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5610 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5611 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5612 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5613 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5614 | return; |
| 5615 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5616 | if (setup_confname_tables(d)) |
| 5617 | return; |
| 5618 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5619 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5620 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5621 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 5622 | if (posix_putenv_garbage == NULL) |
| 5623 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5624 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5625 | } |