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 | |
| 111 | #ifdef NeXT |
| 112 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 113 | #undef HAVE_UNISTD_H |
| 114 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 115 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 116 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 117 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 120 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 121 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 122 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 123 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 124 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 125 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 127 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 129 | #endif |
| 130 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 131 | extern int chdir(char *); |
| 132 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 133 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 134 | extern int chdir(const char *); |
| 135 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 136 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 137 | extern int chmod(const char *, mode_t); |
| 138 | extern int chown(const char *, uid_t, gid_t); |
| 139 | extern char *getcwd(char *, int); |
| 140 | extern char *strerror(int); |
| 141 | extern int link(const char *, const char *); |
| 142 | extern int rename(const char *, const char *); |
| 143 | extern int stat(const char *, struct stat *); |
| 144 | extern int unlink(const char *); |
| 145 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 146 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 147 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 148 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 149 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 150 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 151 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 152 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 156 | #ifdef HAVE_UTIME_H |
| 157 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 158 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 160 | #ifdef HAVE_SYS_UTIME_H |
| 161 | #include <sys/utime.h> |
| 162 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 163 | #endif /* HAVE_SYS_UTIME_H */ |
| 164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #ifdef HAVE_SYS_TIMES_H |
| 166 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 167 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | |
| 169 | #ifdef HAVE_SYS_PARAM_H |
| 170 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 171 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | |
| 173 | #ifdef HAVE_SYS_UTSNAME_H |
| 174 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 175 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 176 | |
| 177 | #ifndef MAXPATHLEN |
| 178 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 179 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 181 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 182 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 183 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 184 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 185 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 186 | #include <direct.h> |
| 187 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 188 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 190 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 192 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 193 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 194 | #endif |
| 195 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 197 | #endif |
| 198 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 200 | #endif |
| 201 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 203 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #include <direct.h> |
| 205 | #include <io.h> |
| 206 | #include <process.h> |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 207 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 209 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 211 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 212 | #else /* 16-bit Windows */ |
| 213 | #include <dos.h> |
| 214 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 215 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 216 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 218 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 220 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 222 | #ifdef UNION_WAIT |
| 223 | /* Emulate some macros on systems that have a union instead of macros */ |
| 224 | |
| 225 | #ifndef WIFEXITED |
| 226 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 227 | #endif |
| 228 | |
| 229 | #ifndef WEXITSTATUS |
| 230 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 231 | #endif |
| 232 | |
| 233 | #ifndef WTERMSIG |
| 234 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 235 | #endif |
| 236 | |
| 237 | #endif /* UNION_WAIT */ |
| 238 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 239 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 240 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 241 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 242 | #define USE_CTERMID_R |
| 243 | #endif |
| 244 | |
| 245 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 246 | #define USE_TMPNAM_R |
| 247 | #endif |
| 248 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 249 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 250 | #undef STAT |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 251 | #ifdef MS_WIN64 |
| 252 | # define STAT _stati64 |
| 253 | # define FSTAT _fstati64 |
| 254 | # define STRUCT_STAT struct _stati64 |
| 255 | #else |
| 256 | # define STAT stat |
| 257 | # define FSTAT fstat |
| 258 | # define STRUCT_STAT struct stat |
| 259 | #endif |
| 260 | |
| 261 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 263 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 264 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 266 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 268 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 269 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 271 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 272 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 273 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | if (d == NULL) |
| 275 | return NULL; |
| 276 | if (environ == NULL) |
| 277 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 278 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 279 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 280 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 281 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | char *p = strchr(*e, '='); |
| 283 | if (p == NULL) |
| 284 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 285 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 286 | if (k == NULL) { |
| 287 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 289 | } |
| 290 | v = PyString_FromString(p+1); |
| 291 | if (v == NULL) { |
| 292 | PyErr_Clear(); |
| 293 | Py_DECREF(k); |
| 294 | continue; |
| 295 | } |
| 296 | if (PyDict_GetItem(d, k) == NULL) { |
| 297 | if (PyDict_SetItem(d, k, v) != 0) |
| 298 | PyErr_Clear(); |
| 299 | } |
| 300 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 301 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 303 | #if defined(PYOS_OS2) |
| 304 | { |
| 305 | APIRET rc; |
| 306 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 307 | |
| 308 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 309 | 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] | 310 | PyObject *v = PyString_FromString(buffer); |
| 311 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 312 | Py_DECREF(v); |
| 313 | } |
| 314 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 315 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 316 | PyObject *v = PyString_FromString(buffer); |
| 317 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 318 | Py_DECREF(v); |
| 319 | } |
| 320 | } |
| 321 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | return d; |
| 323 | } |
| 324 | |
| 325 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 327 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 328 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 329 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 330 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 331 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 332 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 333 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 334 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 335 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 336 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 339 | #ifdef MS_WIN32 |
| 340 | static PyObject * |
| 341 | win32_error(char* function, char* filename) |
| 342 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 343 | /* XXX We should pass the function name along in the future. |
| 344 | (_winreg.c also wants to pass the function name.) |
| 345 | This would however require an additional param to the |
| 346 | Windows error object, which is non-trivial. |
| 347 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 348 | errno = GetLastError(); |
| 349 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 350 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 351 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 352 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 353 | } |
| 354 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 356 | #if defined(PYOS_OS2) |
| 357 | /********************************************************************** |
| 358 | * Helper Function to Trim and Format OS/2 Messages |
| 359 | **********************************************************************/ |
| 360 | static void |
| 361 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 362 | { |
| 363 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 364 | |
| 365 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 366 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 367 | |
| 368 | while (lastc > msgbuf && isspace(*lastc)) |
| 369 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 370 | } |
| 371 | |
| 372 | /* Add Optional Reason Text */ |
| 373 | if (reason) { |
| 374 | strcat(msgbuf, " : "); |
| 375 | strcat(msgbuf, reason); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /********************************************************************** |
| 380 | * Decode an OS/2 Operating System Error Code |
| 381 | * |
| 382 | * A convenience function to lookup an OS/2 error code and return a |
| 383 | * text message we can use to raise a Python exception. |
| 384 | * |
| 385 | * Notes: |
| 386 | * The messages for errors returned from the OS/2 kernel reside in |
| 387 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 388 | * |
| 389 | **********************************************************************/ |
| 390 | static char * |
| 391 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 392 | { |
| 393 | APIRET rc; |
| 394 | ULONG msglen; |
| 395 | |
| 396 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 397 | Py_BEGIN_ALLOW_THREADS |
| 398 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 399 | errorcode, "oso001.msg", &msglen); |
| 400 | Py_END_ALLOW_THREADS |
| 401 | |
| 402 | if (rc == NO_ERROR) |
| 403 | os2_formatmsg(msgbuf, msglen, reason); |
| 404 | else |
| 405 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 406 | |
| 407 | return msgbuf; |
| 408 | } |
| 409 | |
| 410 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 411 | errors are not in a global variable e.g. 'errno' nor are |
| 412 | they congruent with posix error numbers. */ |
| 413 | |
| 414 | static PyObject * os2_error(int code) |
| 415 | { |
| 416 | char text[1024]; |
| 417 | PyObject *v; |
| 418 | |
| 419 | os2_strerror(text, sizeof(text), code, ""); |
| 420 | |
| 421 | v = Py_BuildValue("(is)", code, text); |
| 422 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 423 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 424 | Py_DECREF(v); |
| 425 | } |
| 426 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 427 | } |
| 428 | |
| 429 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | |
| 431 | /* POSIX generic methods */ |
| 432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 433 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 434 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 435 | { |
| 436 | int fd; |
| 437 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 438 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | Py_BEGIN_ALLOW_THREADS |
| 441 | res = (*func)(fd); |
| 442 | Py_END_ALLOW_THREADS |
| 443 | if (res < 0) |
| 444 | return posix_error(); |
| 445 | Py_INCREF(Py_None); |
| 446 | return Py_None; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 451 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 453 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 454 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 455 | if (!PyArg_ParseTuple(args, format, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 456 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 457 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 458 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 459 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 460 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 461 | return posix_error_with_filename(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 462 | Py_INCREF(Py_None); |
| 463 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 466 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 467 | posix_2str(PyObject *args, char *format, |
| 468 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 469 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 470 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 471 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 472 | if (!PyArg_ParseTuple(args, format, &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 473 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 474 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 475 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 476 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 477 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 478 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 480 | Py_INCREF(Py_None); |
| 481 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 484 | /* pack a system stat C structure into the Python stat tuple |
| 485 | (used by posix_stat() and posix_fstat()) */ |
| 486 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 487 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 488 | { |
| 489 | PyObject *v = PyTuple_New(10); |
| 490 | if (v == NULL) |
| 491 | return NULL; |
| 492 | |
| 493 | PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode)); |
| 494 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 495 | PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
| 496 | #else |
| 497 | PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino)); |
| 498 | #endif |
| 499 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
| 500 | PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
| 501 | #else |
| 502 | PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev)); |
| 503 | #endif |
| 504 | PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 505 | PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 506 | PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid)); |
| 507 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 508 | PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size)); |
| 509 | #else |
| 510 | PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size)); |
| 511 | #endif |
| 512 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 513 | PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 514 | PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 515 | PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
| 516 | #else |
| 517 | PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 518 | PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 519 | PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 520 | #endif |
| 521 | |
| 522 | if (PyErr_Occurred()) { |
| 523 | Py_DECREF(v); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | return v; |
| 528 | } |
| 529 | |
| 530 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 531 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 532 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 533 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 534 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 535 | STRUCT_STAT st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 536 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 537 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 538 | |
| 539 | #ifdef MS_WIN32 |
| 540 | int pathlen; |
| 541 | char pathcopy[MAX_PATH]; |
| 542 | #endif /* MS_WIN32 */ |
| 543 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 544 | if (!PyArg_ParseTuple(args, format, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 545 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 546 | |
| 547 | #ifdef MS_WIN32 |
| 548 | pathlen = strlen(path); |
| 549 | /* the library call can blow up if the file name is too long! */ |
| 550 | if (pathlen > MAX_PATH) { |
| 551 | errno = ENAMETOOLONG; |
| 552 | return posix_error(); |
| 553 | } |
| 554 | |
| 555 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 556 | /* exception for specific or current drive root */ |
| 557 | if (!((pathlen == 1) || |
| 558 | ((pathlen == 3) && |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 559 | (path[1] == ':') && |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 560 | (path[2] == '\\' || path[2] == '/')))) |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 561 | { |
| 562 | strncpy(pathcopy, path, pathlen); |
| 563 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 564 | path = pathcopy; |
| 565 | } |
| 566 | } |
| 567 | #endif /* MS_WIN32 */ |
| 568 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 569 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 570 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 571 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 572 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 573 | return posix_error_with_filename(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 574 | |
| 575 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| 579 | /* POSIX methods */ |
| 580 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 581 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 582 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 583 | Test for access to a file."; |
| 584 | |
| 585 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 586 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 587 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 588 | char *path; |
| 589 | int mode; |
| 590 | int res; |
| 591 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 592 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 593 | return NULL; |
| 594 | Py_BEGIN_ALLOW_THREADS |
| 595 | res = access(path, mode); |
| 596 | Py_END_ALLOW_THREADS |
| 597 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 600 | #ifndef F_OK |
| 601 | #define F_OK 0 |
| 602 | #endif |
| 603 | #ifndef R_OK |
| 604 | #define R_OK 4 |
| 605 | #endif |
| 606 | #ifndef W_OK |
| 607 | #define W_OK 2 |
| 608 | #endif |
| 609 | #ifndef X_OK |
| 610 | #define X_OK 1 |
| 611 | #endif |
| 612 | |
| 613 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 614 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 615 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 616 | Return the name of the terminal device connected to 'fd'."; |
| 617 | |
| 618 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 619 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 620 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 621 | int id; |
| 622 | char *ret; |
| 623 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 624 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 625 | return NULL; |
| 626 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 627 | ret = ttyname(id); |
| 628 | if (ret == NULL) |
| 629 | return(posix_error()); |
| 630 | return(PyString_FromString(ret)); |
| 631 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 632 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 633 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 634 | #ifdef HAVE_CTERMID |
| 635 | static char posix_ctermid__doc__[] = |
| 636 | "ctermid() -> String\n\ |
| 637 | Return the name of the controlling terminal for this process."; |
| 638 | |
| 639 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 640 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 641 | { |
| 642 | char *ret; |
| 643 | char buffer[L_ctermid]; |
| 644 | |
| 645 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 646 | return NULL; |
| 647 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 648 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 649 | ret = ctermid_r(buffer); |
| 650 | #else |
| 651 | ret = ctermid(buffer); |
| 652 | #endif |
| 653 | if (ret == NULL) |
| 654 | return(posix_error()); |
| 655 | return(PyString_FromString(buffer)); |
| 656 | } |
| 657 | #endif |
| 658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 659 | static char posix_chdir__doc__[] = |
| 660 | "chdir(path) -> None\n\ |
| 661 | Change the current working directory to the specified path."; |
| 662 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 664 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 665 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 666 | return posix_1str(args, "s:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 669 | |
| 670 | static char posix_chmod__doc__[] = |
| 671 | "chmod(path, mode) -> None\n\ |
| 672 | Change the access permissions of a file."; |
| 673 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 674 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 675 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 676 | { |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 677 | char *path; |
| 678 | int i; |
| 679 | int res; |
Guido van Rossum | 49679b4 | 2000-03-31 00:48:21 +0000 | [diff] [blame] | 680 | if (!PyArg_ParseTuple(args, "si", &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 681 | return NULL; |
| 682 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 683 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 684 | Py_END_ALLOW_THREADS |
| 685 | if (res < 0) |
| 686 | return posix_error_with_filename(path); |
| 687 | Py_INCREF(Py_None); |
| 688 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 691 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 692 | #ifdef HAVE_FSYNC |
| 693 | static char posix_fsync__doc__[] = |
| 694 | "fsync(fildes) -> None\n\ |
| 695 | force write of file with filedescriptor to disk."; |
| 696 | |
| 697 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 698 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 699 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 700 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 701 | } |
| 702 | #endif /* HAVE_FSYNC */ |
| 703 | |
| 704 | #ifdef HAVE_FDATASYNC |
| 705 | static char posix_fdatasync__doc__[] = |
| 706 | "fdatasync(fildes) -> None\n\ |
| 707 | force write of file with filedescriptor to disk.\n\ |
| 708 | does not force update of metadata."; |
| 709 | |
| 710 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 711 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 712 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 713 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 714 | } |
| 715 | #endif /* HAVE_FDATASYNC */ |
| 716 | |
| 717 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 718 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 719 | static char posix_chown__doc__[] = |
| 720 | "chown(path, uid, gid) -> None\n\ |
| 721 | Change the owner and group id of path to the numeric uid and gid."; |
| 722 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 723 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 724 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 725 | { |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 726 | char *path; |
| 727 | int uid, gid; |
| 728 | int res; |
| 729 | if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid)) |
| 730 | return NULL; |
| 731 | Py_BEGIN_ALLOW_THREADS |
| 732 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 733 | Py_END_ALLOW_THREADS |
| 734 | if (res < 0) |
| 735 | return posix_error_with_filename(path); |
| 736 | Py_INCREF(Py_None); |
| 737 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 738 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 739 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 742 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 743 | static char posix_getcwd__doc__[] = |
| 744 | "getcwd() -> path\n\ |
| 745 | Return a string representing the current working directory."; |
| 746 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 747 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 748 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 749 | { |
| 750 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 751 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 752 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 753 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 754 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 755 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 756 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 757 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 758 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 759 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 760 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 761 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 764 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 765 | static char posix_link__doc__[] = |
| 766 | "link(src, dst) -> None\n\ |
| 767 | Create a hard link to a file."; |
| 768 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 769 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 770 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 771 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 772 | return posix_2str(args, "ss:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 773 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 774 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 776 | |
| 777 | static char posix_listdir__doc__[] = |
| 778 | "listdir(path) -> list_of_strings\n\ |
| 779 | Return a list containing the names of the entries in the directory.\n\ |
| 780 | \n\ |
| 781 | path: path of directory to list\n\ |
| 782 | \n\ |
| 783 | The list is in arbitrary order. It does not include the special\n\ |
| 784 | entries '.' and '..' even if they are present in the directory."; |
| 785 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 786 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 787 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 788 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 789 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 790 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 791 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 792 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 793 | char *name; |
| 794 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 795 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 796 | HANDLE hFindFile; |
| 797 | WIN32_FIND_DATA FileData; |
| 798 | char namebuf[MAX_PATH+5]; |
| 799 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 800 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 803 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 804 | return NULL; |
| 805 | } |
| 806 | strcpy(namebuf, name); |
| 807 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 808 | namebuf[len++] = '/'; |
| 809 | strcpy(namebuf + len, "*.*"); |
| 810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 811 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 812 | return NULL; |
| 813 | |
| 814 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 815 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 816 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 817 | if (errno == ERROR_FILE_NOT_FOUND) |
| 818 | return PyList_New(0); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 819 | return win32_error("FindFirstFile", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 820 | } |
| 821 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 822 | if (FileData.cFileName[0] == '.' && |
| 823 | (FileData.cFileName[1] == '\0' || |
| 824 | FileData.cFileName[1] == '.' && |
| 825 | FileData.cFileName[2] == '\0')) |
| 826 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 827 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 828 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 829 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 830 | d = NULL; |
| 831 | break; |
| 832 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 833 | if (PyList_Append(d, v) != 0) { |
| 834 | Py_DECREF(v); |
| 835 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 836 | d = NULL; |
| 837 | break; |
| 838 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 839 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 840 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 841 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 842 | if (FindClose(hFindFile) == FALSE) |
| 843 | return win32_error("FindClose", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 844 | |
| 845 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 847 | #else /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 848 | #ifdef _MSC_VER /* 16-bit Windows */ |
| 849 | |
| 850 | #ifndef MAX_PATH |
| 851 | #define MAX_PATH 250 |
| 852 | #endif |
| 853 | char *name, *pt; |
| 854 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 855 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 856 | char namebuf[MAX_PATH+5]; |
| 857 | struct _find_t ep; |
| 858 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 859 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 860 | return NULL; |
| 861 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 862 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 863 | return NULL; |
| 864 | } |
| 865 | strcpy(namebuf, name); |
| 866 | for (pt = namebuf; *pt; pt++) |
| 867 | if (*pt == '/') |
| 868 | *pt = '\\'; |
| 869 | if (namebuf[len-1] != '\\') |
| 870 | namebuf[len++] = '\\'; |
| 871 | strcpy(namebuf + len, "*.*"); |
| 872 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 873 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 874 | return NULL; |
| 875 | |
| 876 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 877 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 878 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 879 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 880 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 881 | } |
| 882 | do { |
| 883 | if (ep.name[0] == '.' && |
| 884 | (ep.name[1] == '\0' || |
| 885 | ep.name[1] == '.' && |
| 886 | ep.name[2] == '\0')) |
| 887 | continue; |
| 888 | strcpy(namebuf, ep.name); |
| 889 | for (pt = namebuf; *pt; pt++) |
| 890 | if (isupper(*pt)) |
| 891 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 892 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 893 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 894 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 895 | d = NULL; |
| 896 | break; |
| 897 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 898 | if (PyList_Append(d, v) != 0) { |
| 899 | Py_DECREF(v); |
| 900 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 901 | d = NULL; |
| 902 | break; |
| 903 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 904 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 905 | } while (_dos_findnext(&ep) == 0); |
| 906 | |
| 907 | return d; |
| 908 | |
| 909 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 910 | #if defined(PYOS_OS2) |
| 911 | |
| 912 | #ifndef MAX_PATH |
| 913 | #define MAX_PATH CCHMAXPATH |
| 914 | #endif |
| 915 | char *name, *pt; |
| 916 | int len; |
| 917 | PyObject *d, *v; |
| 918 | char namebuf[MAX_PATH+5]; |
| 919 | HDIR hdir = 1; |
| 920 | ULONG srchcnt = 1; |
| 921 | FILEFINDBUF3 ep; |
| 922 | APIRET rc; |
| 923 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 924 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 925 | return NULL; |
| 926 | if (len >= MAX_PATH) { |
| 927 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 928 | return NULL; |
| 929 | } |
| 930 | strcpy(namebuf, name); |
| 931 | for (pt = namebuf; *pt; pt++) |
| 932 | if (*pt == '/') |
| 933 | *pt = '\\'; |
| 934 | if (namebuf[len-1] != '\\') |
| 935 | namebuf[len++] = '\\'; |
| 936 | strcpy(namebuf + len, "*.*"); |
| 937 | |
| 938 | if ((d = PyList_New(0)) == NULL) |
| 939 | return NULL; |
| 940 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 941 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 942 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 943 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 944 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 945 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 946 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 947 | |
| 948 | if (rc != NO_ERROR) { |
| 949 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 950 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 953 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 954 | do { |
| 955 | if (ep.achName[0] == '.' |
| 956 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 957 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 958 | |
| 959 | strcpy(namebuf, ep.achName); |
| 960 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 961 | /* Leave Case of Name Alone -- In Native Form */ |
| 962 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 963 | |
| 964 | v = PyString_FromString(namebuf); |
| 965 | if (v == NULL) { |
| 966 | Py_DECREF(d); |
| 967 | d = NULL; |
| 968 | break; |
| 969 | } |
| 970 | if (PyList_Append(d, v) != 0) { |
| 971 | Py_DECREF(v); |
| 972 | Py_DECREF(d); |
| 973 | d = NULL; |
| 974 | break; |
| 975 | } |
| 976 | Py_DECREF(v); |
| 977 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 978 | } |
| 979 | |
| 980 | return d; |
| 981 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 983 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 984 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 985 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 986 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 987 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 988 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 989 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 990 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 991 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 992 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 993 | closedir(dirp); |
| 994 | return NULL; |
| 995 | } |
| 996 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 997 | if (ep->d_name[0] == '.' && |
| 998 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 999 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1000 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1001 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1002 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1003 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1004 | d = NULL; |
| 1005 | break; |
| 1006 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1007 | if (PyList_Append(d, v) != 0) { |
| 1008 | Py_DECREF(v); |
| 1009 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1010 | d = NULL; |
| 1011 | break; |
| 1012 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1013 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1014 | } |
| 1015 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1016 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1017 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1018 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1019 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1020 | #endif /* !_MSC_VER */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1021 | #endif /* !MS_WIN32 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1024 | static char posix_mkdir__doc__[] = |
| 1025 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1026 | Create a directory."; |
| 1027 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1028 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1029 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1030 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1031 | int res; |
| 1032 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1033 | int mode = 0777; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1034 | if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1035 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1036 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1037 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1038 | res = mkdir(path); |
| 1039 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1040 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1041 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1042 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1043 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1044 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1045 | Py_INCREF(Py_None); |
| 1046 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1049 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1050 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1051 | static char posix_nice__doc__[] = |
| 1052 | "nice(inc) -> new_priority\n\ |
| 1053 | Decrease the priority of process and return new priority."; |
| 1054 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1055 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1056 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1057 | { |
| 1058 | int increment, value; |
| 1059 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1060 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1061 | return NULL; |
| 1062 | value = nice(increment); |
| 1063 | if (value == -1) |
| 1064 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1065 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1066 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1067 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1069 | |
| 1070 | static char posix_rename__doc__[] = |
| 1071 | "rename(old, new) -> None\n\ |
| 1072 | Rename a file or directory."; |
| 1073 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1075 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1076 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1077 | return posix_2str(args, "ss:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1080 | |
| 1081 | static char posix_rmdir__doc__[] = |
| 1082 | "rmdir(path) -> None\n\ |
| 1083 | Remove a directory."; |
| 1084 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1085 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1086 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1087 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1088 | return posix_1str(args, "s:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1091 | |
| 1092 | static char posix_stat__doc__[] = |
| 1093 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1094 | Perform a stat system call on the given path."; |
| 1095 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1096 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1097 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1098 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1099 | return posix_do_stat(self, args, "s:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1103 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1104 | static char posix_system__doc__[] = |
| 1105 | "system(command) -> exit_status\n\ |
| 1106 | Execute the command (a string) in a subshell."; |
| 1107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1109 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1110 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1111 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1112 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1113 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1114 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1115 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1116 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1117 | Py_END_ALLOW_THREADS |
| 1118 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1119 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1120 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1122 | |
| 1123 | static char posix_umask__doc__[] = |
| 1124 | "umask(new_mask) -> old_mask\n\ |
| 1125 | Set the current numeric umask and return the previous umask."; |
| 1126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1128 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1129 | { |
| 1130 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1131 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1132 | return NULL; |
| 1133 | i = umask(i); |
| 1134 | if (i < 0) |
| 1135 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1136 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1139 | |
| 1140 | static char posix_unlink__doc__[] = |
| 1141 | "unlink(path) -> None\n\ |
| 1142 | Remove a file (same as remove(path))."; |
| 1143 | |
| 1144 | static char posix_remove__doc__[] = |
| 1145 | "remove(path) -> None\n\ |
| 1146 | Remove a file (same as unlink(path))."; |
| 1147 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1148 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1149 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1150 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1151 | return posix_1str(args, "s:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1154 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1155 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1156 | static char posix_uname__doc__[] = |
| 1157 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1158 | Return a tuple identifying the current operating system."; |
| 1159 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1160 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1161 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1162 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1163 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1164 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1165 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1166 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1167 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1168 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1169 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1170 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1171 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1172 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1173 | u.sysname, |
| 1174 | u.nodename, |
| 1175 | u.release, |
| 1176 | u.version, |
| 1177 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1178 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1179 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1180 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1181 | |
| 1182 | static char posix_utime__doc__[] = |
| 1183 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1184 | utime(path, None) -> None\n\ |
| 1185 | Set the access and modified time of the file to the given values. If the\n\ |
| 1186 | 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] | 1187 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1188 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1189 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1190 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1191 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1192 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1193 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1194 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1195 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1196 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1197 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1198 | struct utimbuf buf; |
| 1199 | #define ATIME buf.actime |
| 1200 | #define MTIME buf.modtime |
| 1201 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1202 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1203 | time_t buf[2]; |
| 1204 | #define ATIME buf[0] |
| 1205 | #define MTIME buf[1] |
| 1206 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1207 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1208 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1209 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1210 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1211 | if (arg == Py_None) { |
| 1212 | /* optional time values not given */ |
| 1213 | Py_BEGIN_ALLOW_THREADS |
| 1214 | res = utime(path, NULL); |
| 1215 | Py_END_ALLOW_THREADS |
| 1216 | } |
| 1217 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1218 | PyErr_SetString(PyExc_TypeError, |
| 1219 | "Second argument must be a 2-tuple of numbers."); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | else { |
| 1223 | ATIME = atime; |
| 1224 | MTIME = mtime; |
| 1225 | Py_BEGIN_ALLOW_THREADS |
| 1226 | res = utime(path, UTIME_ARG); |
| 1227 | Py_END_ALLOW_THREADS |
| 1228 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1229 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1230 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1231 | Py_INCREF(Py_None); |
| 1232 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1233 | #undef UTIME_ARG |
| 1234 | #undef ATIME |
| 1235 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1238 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1239 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1240 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1241 | static char posix__exit__doc__[] = |
| 1242 | "_exit(status)\n\ |
| 1243 | Exit to the system with specified status, without normal exit processing."; |
| 1244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1246 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1247 | { |
| 1248 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1249 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1250 | return NULL; |
| 1251 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1252 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1255 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1256 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1257 | static char posix_execv__doc__[] = |
| 1258 | "execv(path, args)\n\ |
| 1259 | Execute an executable path with arguments, replacing current process.\n\ |
| 1260 | \n\ |
| 1261 | path: path of executable file\n\ |
| 1262 | args: tuple or list of strings"; |
| 1263 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1264 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1265 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1266 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1267 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1268 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1269 | char **argvlist; |
| 1270 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1271 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1273 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1274 | argv is a list or tuple of strings. */ |
| 1275 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1276 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1277 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1278 | if (PyList_Check(argv)) { |
| 1279 | argc = PyList_Size(argv); |
| 1280 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1281 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1282 | else if (PyTuple_Check(argv)) { |
| 1283 | argc = PyTuple_Size(argv); |
| 1284 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1285 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1286 | else { |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1287 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | |
| 1291 | if (argc == 0) { |
| 1292 | PyErr_SetString(PyExc_ValueError, "empty argument list"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1293 | return NULL; |
| 1294 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1295 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1296 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1297 | if (argvlist == NULL) |
| 1298 | return NULL; |
| 1299 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1300 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1301 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1302 | PyErr_SetString(PyExc_TypeError, |
| 1303 | "all arguments must be strings"); |
| 1304 | return NULL; |
| 1305 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1306 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1307 | } |
| 1308 | argvlist[argc] = NULL; |
| 1309 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1310 | #ifdef BAD_EXEC_PROTOTYPES |
| 1311 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1312 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1313 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1314 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1316 | /* If we get here it's definitely an error */ |
| 1317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1318 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1319 | return posix_error(); |
| 1320 | } |
| 1321 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1322 | |
| 1323 | static char posix_execve__doc__[] = |
| 1324 | "execve(path, args, env)\n\ |
| 1325 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1326 | \n\ |
| 1327 | path: path of executable file\n\ |
| 1328 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1329 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1330 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1331 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1332 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1333 | { |
| 1334 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1335 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1336 | char **argvlist; |
| 1337 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1338 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1339 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1340 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1341 | |
| 1342 | /* execve has three arguments: (path, argv, env), where |
| 1343 | argv is a list or tuple of strings and env is a dictionary |
| 1344 | like posix.environ. */ |
| 1345 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1346 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1347 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1348 | if (PyList_Check(argv)) { |
| 1349 | argc = PyList_Size(argv); |
| 1350 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1351 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1352 | else if (PyTuple_Check(argv)) { |
| 1353 | argc = PyTuple_Size(argv); |
| 1354 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1355 | } |
| 1356 | else { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1357 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1358 | return NULL; |
| 1359 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1360 | if (!PyMapping_Check(env)) { |
| 1361 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1362 | return NULL; |
| 1363 | } |
| 1364 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1365 | if (argc == 0) { |
| 1366 | PyErr_SetString(PyExc_ValueError, |
| 1367 | "empty argument list"); |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1371 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1372 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1373 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1374 | return NULL; |
| 1375 | } |
| 1376 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1377 | if (!PyArg_Parse((*getitem)(argv, i), |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1378 | "s;argv must be list of strings", |
| 1379 | &argvlist[i])) |
| 1380 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1381 | goto fail_1; |
| 1382 | } |
| 1383 | } |
| 1384 | argvlist[argc] = NULL; |
| 1385 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1386 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1387 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1388 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1389 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1390 | goto fail_1; |
| 1391 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1392 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1393 | keys = PyMapping_Keys(env); |
| 1394 | vals = PyMapping_Values(env); |
| 1395 | if (!keys || !vals) |
| 1396 | goto fail_2; |
| 1397 | |
| 1398 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1399 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1400 | |
| 1401 | key = PyList_GetItem(keys, pos); |
| 1402 | val = PyList_GetItem(vals, pos); |
| 1403 | if (!key || !val) |
| 1404 | goto fail_2; |
| 1405 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1406 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1407 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1408 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1409 | goto fail_2; |
| 1410 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1411 | |
| 1412 | #if defined(PYOS_OS2) |
| 1413 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1414 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1415 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1416 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1417 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1418 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1419 | goto fail_2; |
| 1420 | } |
| 1421 | sprintf(p, "%s=%s", k, v); |
| 1422 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1423 | #if defined(PYOS_OS2) |
| 1424 | } |
| 1425 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1426 | } |
| 1427 | envlist[envc] = 0; |
| 1428 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1429 | |
| 1430 | #ifdef BAD_EXEC_PROTOTYPES |
| 1431 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1432 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1433 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1434 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* If we get here it's definitely an error */ |
| 1437 | |
| 1438 | (void) posix_error(); |
| 1439 | |
| 1440 | fail_2: |
| 1441 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1442 | PyMem_DEL(envlist[envc]); |
| 1443 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1444 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1445 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1446 | Py_XDECREF(vals); |
| 1447 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1448 | return NULL; |
| 1449 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1450 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1451 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1452 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1453 | #ifdef HAVE_SPAWNV |
| 1454 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1455 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1456 | Execute an executable path with arguments, replacing current process.\n\ |
| 1457 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1458 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1459 | path: path of executable file\n\ |
| 1460 | args: tuple or list of strings"; |
| 1461 | |
| 1462 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1463 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1464 | { |
| 1465 | char *path; |
| 1466 | PyObject *argv; |
| 1467 | char **argvlist; |
| 1468 | int mode, i, argc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1469 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1470 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1471 | |
| 1472 | /* spawnv has three arguments: (mode, path, argv), where |
| 1473 | argv is a list or tuple of strings. */ |
| 1474 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1475 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1476 | return NULL; |
| 1477 | if (PyList_Check(argv)) { |
| 1478 | argc = PyList_Size(argv); |
| 1479 | getitem = PyList_GetItem; |
| 1480 | } |
| 1481 | else if (PyTuple_Check(argv)) { |
| 1482 | argc = PyTuple_Size(argv); |
| 1483 | getitem = PyTuple_GetItem; |
| 1484 | } |
| 1485 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1486 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1487 | return NULL; |
| 1488 | } |
| 1489 | |
| 1490 | argvlist = PyMem_NEW(char *, argc+1); |
| 1491 | if (argvlist == NULL) |
| 1492 | return NULL; |
| 1493 | for (i = 0; i < argc; i++) { |
| 1494 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1495 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1496 | PyErr_SetString(PyExc_TypeError, |
| 1497 | "all arguments must be strings"); |
| 1498 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | argvlist[argc] = NULL; |
| 1502 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1503 | if (mode == _OLD_P_OVERLAY) |
| 1504 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1505 | spawnval = _spawnv(mode, path, argvlist); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1506 | |
| 1507 | PyMem_DEL(argvlist); |
| 1508 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1509 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1510 | return posix_error(); |
| 1511 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1512 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1513 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1514 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1515 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1516 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1521 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1522 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1523 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1524 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1525 | path: path of executable file\n\ |
| 1526 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1527 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1528 | |
| 1529 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1530 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1531 | { |
| 1532 | char *path; |
| 1533 | PyObject *argv, *env; |
| 1534 | char **argvlist; |
| 1535 | char **envlist; |
| 1536 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1537 | int mode, i, pos, argc, envc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1538 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1539 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1540 | |
| 1541 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1542 | argv is a list or tuple of strings and env is a dictionary |
| 1543 | like posix.environ. */ |
| 1544 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1545 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1546 | return NULL; |
| 1547 | if (PyList_Check(argv)) { |
| 1548 | argc = PyList_Size(argv); |
| 1549 | getitem = PyList_GetItem; |
| 1550 | } |
| 1551 | else if (PyTuple_Check(argv)) { |
| 1552 | argc = PyTuple_Size(argv); |
| 1553 | getitem = PyTuple_GetItem; |
| 1554 | } |
| 1555 | else { |
| 1556 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | if (!PyMapping_Check(env)) { |
| 1560 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | argvlist = PyMem_NEW(char *, argc+1); |
| 1565 | if (argvlist == NULL) { |
| 1566 | PyErr_NoMemory(); |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | for (i = 0; i < argc; i++) { |
| 1570 | if (!PyArg_Parse((*getitem)(argv, i), |
| 1571 | "s;argv must be list of strings", |
| 1572 | &argvlist[i])) |
| 1573 | { |
| 1574 | goto fail_1; |
| 1575 | } |
| 1576 | } |
| 1577 | argvlist[argc] = NULL; |
| 1578 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1579 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1580 | envlist = PyMem_NEW(char *, i + 1); |
| 1581 | if (envlist == NULL) { |
| 1582 | PyErr_NoMemory(); |
| 1583 | goto fail_1; |
| 1584 | } |
| 1585 | envc = 0; |
| 1586 | keys = PyMapping_Keys(env); |
| 1587 | vals = PyMapping_Values(env); |
| 1588 | if (!keys || !vals) |
| 1589 | goto fail_2; |
| 1590 | |
| 1591 | for (pos = 0; pos < i; pos++) { |
| 1592 | char *p, *k, *v; |
| 1593 | |
| 1594 | key = PyList_GetItem(keys, pos); |
| 1595 | val = PyList_GetItem(vals, pos); |
| 1596 | if (!key || !val) |
| 1597 | goto fail_2; |
| 1598 | |
| 1599 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
| 1600 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1601 | { |
| 1602 | goto fail_2; |
| 1603 | } |
| 1604 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1605 | if (p == NULL) { |
| 1606 | PyErr_NoMemory(); |
| 1607 | goto fail_2; |
| 1608 | } |
| 1609 | sprintf(p, "%s=%s", k, v); |
| 1610 | envlist[envc++] = p; |
| 1611 | } |
| 1612 | envlist[envc] = 0; |
| 1613 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1614 | if (mode == _OLD_P_OVERLAY) |
| 1615 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1616 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 1617 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1618 | (void) posix_error(); |
| 1619 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1620 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1621 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1622 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1623 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1624 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1625 | |
| 1626 | fail_2: |
| 1627 | while (--envc >= 0) |
| 1628 | PyMem_DEL(envlist[envc]); |
| 1629 | PyMem_DEL(envlist); |
| 1630 | fail_1: |
| 1631 | PyMem_DEL(argvlist); |
| 1632 | Py_XDECREF(vals); |
| 1633 | Py_XDECREF(keys); |
| 1634 | return res; |
| 1635 | } |
| 1636 | #endif /* HAVE_SPAWNV */ |
| 1637 | |
| 1638 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1639 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1640 | static char posix_fork__doc__[] = |
| 1641 | "fork() -> pid\n\ |
| 1642 | Fork a child process.\n\ |
| 1643 | \n\ |
| 1644 | Return 0 to child process and PID of child to parent process."; |
| 1645 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1646 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1647 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1648 | { |
| 1649 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1650 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1651 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1652 | pid = fork(); |
| 1653 | if (pid == -1) |
| 1654 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1655 | if (pid == 0) |
| 1656 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1657 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1658 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1659 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1660 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1661 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1662 | #ifdef HAVE_PTY_H |
| 1663 | #include <pty.h> |
| 1664 | #else |
| 1665 | #ifdef HAVE_LIBUTIL_H |
| 1666 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1667 | #endif /* HAVE_LIBUTIL_H */ |
| 1668 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1669 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1670 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1671 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1672 | static char posix_openpty__doc__[] = |
| 1673 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1674 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1675 | |
| 1676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1677 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1678 | { |
| 1679 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1680 | #ifndef HAVE_OPENPTY |
| 1681 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1682 | #endif |
| 1683 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1684 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1685 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1686 | |
| 1687 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1688 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1689 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1690 | #else |
| 1691 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1692 | if (slave_name == NULL) |
| 1693 | return posix_error(); |
| 1694 | |
| 1695 | slave_fd = open(slave_name, O_RDWR); |
| 1696 | if (slave_fd < 0) |
| 1697 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1698 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1699 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1700 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1701 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1702 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1703 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1704 | |
| 1705 | #ifdef HAVE_FORKPTY |
| 1706 | static char posix_forkpty__doc__[] = |
| 1707 | "forkpty() -> (pid, master_fd)\n\ |
| 1708 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1709 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1710 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1711 | |
| 1712 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1713 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1714 | { |
| 1715 | int master_fd, pid; |
| 1716 | |
| 1717 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1718 | return NULL; |
| 1719 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1720 | if (pid == -1) |
| 1721 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1722 | if (pid == 0) |
| 1723 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1724 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1725 | } |
| 1726 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1727 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1728 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1729 | static char posix_getegid__doc__[] = |
| 1730 | "getegid() -> egid\n\ |
| 1731 | Return the current process's effective group id."; |
| 1732 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1733 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1734 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1735 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1736 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1737 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1738 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1739 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1740 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1741 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1742 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1743 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1744 | static char posix_geteuid__doc__[] = |
| 1745 | "geteuid() -> euid\n\ |
| 1746 | Return the current process's effective user id."; |
| 1747 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1748 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1749 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1750 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1751 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1752 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1753 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1754 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1755 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1756 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1757 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1758 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1759 | static char posix_getgid__doc__[] = |
| 1760 | "getgid() -> gid\n\ |
| 1761 | Return the current process's group id."; |
| 1762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1764 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1765 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1766 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1767 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1768 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1769 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1770 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1771 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1772 | |
| 1773 | static char posix_getpid__doc__[] = |
| 1774 | "getpid() -> pid\n\ |
| 1775 | Return the current process id"; |
| 1776 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1778 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1779 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1780 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1781 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1782 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1785 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1786 | #ifdef HAVE_GETGROUPS |
| 1787 | static char posix_getgroups__doc__[] = "\ |
| 1788 | getgroups() -> list of group IDs\n\ |
| 1789 | Return list of supplemental group IDs for the process."; |
| 1790 | |
| 1791 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1792 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1793 | { |
| 1794 | PyObject *result = NULL; |
| 1795 | |
| 1796 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 1797 | #ifdef NGROUPS_MAX |
| 1798 | #define MAX_GROUPS NGROUPS_MAX |
| 1799 | #else |
| 1800 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 1801 | #define MAX_GROUPS 64 |
| 1802 | #endif |
| 1803 | gid_t grouplist[MAX_GROUPS]; |
| 1804 | int n; |
| 1805 | |
| 1806 | n = getgroups(MAX_GROUPS, grouplist); |
| 1807 | if (n < 0) |
| 1808 | posix_error(); |
| 1809 | else { |
| 1810 | result = PyList_New(n); |
| 1811 | if (result != NULL) { |
| 1812 | PyObject *o; |
| 1813 | int i; |
| 1814 | for (i = 0; i < n; ++i) { |
| 1815 | o = PyInt_FromLong((long)grouplist[i]); |
| 1816 | if (o == NULL) { |
| 1817 | Py_DECREF(result); |
| 1818 | result = NULL; |
| 1819 | break; |
| 1820 | } |
| 1821 | PyList_SET_ITEM(result, i, o); |
| 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | return result; |
| 1827 | } |
| 1828 | #endif |
| 1829 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1830 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1831 | static char posix_getpgrp__doc__[] = |
| 1832 | "getpgrp() -> pgrp\n\ |
| 1833 | Return the current process group id."; |
| 1834 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1835 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1836 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1837 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1838 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1839 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1840 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1841 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1842 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1843 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1844 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1845 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1846 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1847 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1848 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1849 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1850 | static char posix_setpgrp__doc__[] = |
| 1851 | "setpgrp() -> None\n\ |
| 1852 | Make this process a session leader."; |
| 1853 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1854 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1855 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1856 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1857 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1858 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1859 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1860 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1861 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1862 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1863 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1864 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1865 | Py_INCREF(Py_None); |
| 1866 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1869 | #endif /* HAVE_SETPGRP */ |
| 1870 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1871 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1872 | static char posix_getppid__doc__[] = |
| 1873 | "getppid() -> ppid\n\ |
| 1874 | Return the parent's process id."; |
| 1875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1876 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1877 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1878 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1879 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1880 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1881 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1882 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1883 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1884 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1885 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1886 | #ifdef HAVE_GETLOGIN |
| 1887 | static char posix_getlogin__doc__[] = "\ |
| 1888 | getlogin() -> string\n\ |
| 1889 | Return the actual login name."; |
| 1890 | |
| 1891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1892 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1893 | { |
| 1894 | PyObject *result = NULL; |
| 1895 | |
| 1896 | if (PyArg_ParseTuple(args, ":getlogin")) { |
| 1897 | char *name = getlogin(); |
| 1898 | |
| 1899 | if (name == NULL) |
| 1900 | posix_error(); |
| 1901 | else |
| 1902 | result = PyString_FromString(name); |
| 1903 | } |
| 1904 | return result; |
| 1905 | } |
| 1906 | #endif |
| 1907 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1908 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1909 | static char posix_getuid__doc__[] = |
| 1910 | "getuid() -> uid\n\ |
| 1911 | Return the current process's user id."; |
| 1912 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1913 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1914 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1915 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1916 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1917 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1918 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1919 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1920 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1921 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1922 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1923 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1924 | static char posix_kill__doc__[] = |
| 1925 | "kill(pid, sig) -> None\n\ |
| 1926 | Kill a process with a signal."; |
| 1927 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1928 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1929 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1930 | { |
| 1931 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1932 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1933 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1934 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1935 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 1936 | APIRET rc; |
| 1937 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1938 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1939 | |
| 1940 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 1941 | APIRET rc; |
| 1942 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1943 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1944 | |
| 1945 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1946 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1947 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1948 | if (kill(pid, sig) == -1) |
| 1949 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1950 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1951 | Py_INCREF(Py_None); |
| 1952 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1953 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1954 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1955 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1956 | #ifdef HAVE_PLOCK |
| 1957 | |
| 1958 | #ifdef HAVE_SYS_LOCK_H |
| 1959 | #include <sys/lock.h> |
| 1960 | #endif |
| 1961 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1962 | static char posix_plock__doc__[] = |
| 1963 | "plock(op) -> None\n\ |
| 1964 | Lock program segments into memory."; |
| 1965 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1967 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1968 | { |
| 1969 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1970 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1971 | return NULL; |
| 1972 | if (plock(op) == -1) |
| 1973 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1974 | Py_INCREF(Py_None); |
| 1975 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1976 | } |
| 1977 | #endif |
| 1978 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1980 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | static char posix_popen__doc__[] = |
| 1982 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 1983 | Open a pipe to/from a command returning a file object."; |
| 1984 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1985 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1986 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1987 | async_system(const char *command) |
| 1988 | { |
| 1989 | char *p, errormsg[256], args[1024]; |
| 1990 | RESULTCODES rcodes; |
| 1991 | APIRET rc; |
| 1992 | char *shell = getenv("COMSPEC"); |
| 1993 | if (!shell) |
| 1994 | shell = "cmd"; |
| 1995 | |
| 1996 | strcpy(args, shell); |
| 1997 | p = &args[ strlen(args)+1 ]; |
| 1998 | strcpy(p, "/c "); |
| 1999 | strcat(p, command); |
| 2000 | p += strlen(p) + 1; |
| 2001 | *p = '\0'; |
| 2002 | |
| 2003 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2004 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2005 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2006 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2007 | &rcodes, shell); |
| 2008 | return rc; |
| 2009 | } |
| 2010 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2011 | static FILE * |
| 2012 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2013 | { |
| 2014 | HFILE rhan, whan; |
| 2015 | FILE *retfd = NULL; |
| 2016 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2017 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2018 | if (rc != NO_ERROR) { |
| 2019 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2020 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2021 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2022 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2023 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2024 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2026 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2027 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2029 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2030 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2031 | |
| 2032 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2033 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2036 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2037 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2039 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2040 | return retfd; /* Return fd of Pipe or NULL if Error */ |
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 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2043 | int oldfd = dup(0); /* Save STDIN 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(0); /* Make STDIN 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(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2049 | DosClose(rhan); /* Close Now-Unused Pipe Read 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(whan, mode); /* And Return Pipe Write 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, 0); /* Reconnect STDIN 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 STDIN 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 | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2061 | } else { |
| 2062 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2063 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2064 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2068 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2069 | { |
| 2070 | char *name; |
| 2071 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2072 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2073 | FILE *fp; |
| 2074 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2075 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2076 | return NULL; |
| 2077 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2078 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2079 | Py_END_ALLOW_THREADS |
| 2080 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2081 | return os2_error(err); |
| 2082 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2083 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2084 | if (f != NULL) |
| 2085 | PyFile_SetBufSize(f, bufsize); |
| 2086 | return f; |
| 2087 | } |
| 2088 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2089 | #elif defined(MS_WIN32) |
| 2090 | |
| 2091 | /* |
| 2092 | * Portable 'popen' replacement for Win32. |
| 2093 | * |
| 2094 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2095 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2096 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2097 | */ |
| 2098 | |
| 2099 | #include <malloc.h> |
| 2100 | #include <io.h> |
| 2101 | #include <fcntl.h> |
| 2102 | |
| 2103 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2104 | #define POPEN_1 1 |
| 2105 | #define POPEN_2 2 |
| 2106 | #define POPEN_3 3 |
| 2107 | #define POPEN_4 4 |
| 2108 | |
| 2109 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2110 | static int _PyPclose(FILE *file); |
| 2111 | |
| 2112 | /* |
| 2113 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2114 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2115 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2116 | */ |
| 2117 | static PyObject *_PyPopenProcs = NULL; |
| 2118 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2119 | |
| 2120 | /* popen that works from a GUI. |
| 2121 | * |
| 2122 | * The result of this function is a pipe (file) connected to the |
| 2123 | * processes stdin or stdout, depending on the requested mode. |
| 2124 | */ |
| 2125 | |
| 2126 | static PyObject * |
| 2127 | posix_popen(PyObject *self, PyObject *args) |
| 2128 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2129 | PyObject *f, *s; |
| 2130 | int tm = 0; |
| 2131 | |
| 2132 | char *cmdstring; |
| 2133 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2134 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2135 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2136 | return NULL; |
| 2137 | |
| 2138 | s = PyTuple_New(0); |
| 2139 | |
| 2140 | if (*mode == 'r') |
| 2141 | tm = _O_RDONLY; |
| 2142 | else if (*mode != 'w') { |
| 2143 | PyErr_SetString(PyExc_ValueError, "mode must be 'r' or 'w'"); |
| 2144 | return NULL; |
| 2145 | } else |
| 2146 | tm = _O_WRONLY; |
| 2147 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2148 | if (bufsize != -1) { |
| 2149 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2153 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2154 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2155 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2156 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2157 | else |
| 2158 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2159 | |
| 2160 | return f; |
| 2161 | } |
| 2162 | |
| 2163 | /* Variation on win32pipe.popen |
| 2164 | * |
| 2165 | * The result of this function is a pipe (file) connected to the |
| 2166 | * process's stdin, and a pipe connected to the process's stdout. |
| 2167 | */ |
| 2168 | |
| 2169 | static PyObject * |
| 2170 | win32_popen2(PyObject *self, PyObject *args) |
| 2171 | { |
| 2172 | PyObject *f; |
| 2173 | int tm=0; |
| 2174 | |
| 2175 | char *cmdstring; |
| 2176 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2177 | int bufsize = -1; |
| 2178 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2179 | return NULL; |
| 2180 | |
| 2181 | if (*mode == 't') |
| 2182 | tm = _O_TEXT; |
| 2183 | else if (*mode != 'b') { |
| 2184 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2185 | return NULL; |
| 2186 | } else |
| 2187 | tm = _O_BINARY; |
| 2188 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2189 | if (bufsize != -1) { |
| 2190 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2191 | return NULL; |
| 2192 | } |
| 2193 | |
| 2194 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2195 | |
| 2196 | return f; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2201 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2202 | * The result of this function is 3 pipes - the process's stdin, |
| 2203 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2204 | */ |
| 2205 | |
| 2206 | static PyObject * |
| 2207 | win32_popen3(PyObject *self, PyObject *args) |
| 2208 | { |
| 2209 | PyObject *f; |
| 2210 | int tm = 0; |
| 2211 | |
| 2212 | char *cmdstring; |
| 2213 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2214 | int bufsize = -1; |
| 2215 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2216 | return NULL; |
| 2217 | |
| 2218 | if (*mode == 't') |
| 2219 | tm = _O_TEXT; |
| 2220 | else if (*mode != 'b') { |
| 2221 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2222 | return NULL; |
| 2223 | } else |
| 2224 | tm = _O_BINARY; |
| 2225 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2226 | if (bufsize != -1) { |
| 2227 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2228 | return NULL; |
| 2229 | } |
| 2230 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2231 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2232 | |
| 2233 | return f; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Variation on win32pipe.popen |
| 2238 | * |
| 2239 | * The result of this function is 2 pipes - the processes stdin, |
| 2240 | * and stdout+stderr combined as a single pipe. |
| 2241 | */ |
| 2242 | |
| 2243 | static PyObject * |
| 2244 | win32_popen4(PyObject *self, PyObject *args) |
| 2245 | { |
| 2246 | PyObject *f; |
| 2247 | int tm = 0; |
| 2248 | |
| 2249 | char *cmdstring; |
| 2250 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2251 | int bufsize = -1; |
| 2252 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2253 | return NULL; |
| 2254 | |
| 2255 | if (*mode == 't') |
| 2256 | tm = _O_TEXT; |
| 2257 | else if (*mode != 'b') { |
| 2258 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2259 | return NULL; |
| 2260 | } else |
| 2261 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2262 | |
| 2263 | if (bufsize != -1) { |
| 2264 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2268 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2269 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2270 | return f; |
| 2271 | } |
| 2272 | |
| 2273 | static int |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2274 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2275 | HANDLE hStdin, |
| 2276 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2277 | HANDLE hStderr, |
| 2278 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2279 | { |
| 2280 | PROCESS_INFORMATION piProcInfo; |
| 2281 | STARTUPINFO siStartInfo; |
| 2282 | char *s1,*s2, *s3 = " /c "; |
| 2283 | const char *szConsoleSpawn = "w9xpopen.exe \""; |
| 2284 | int i; |
| 2285 | int x; |
| 2286 | |
| 2287 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 2288 | s1 = (char *)_alloca(i); |
| 2289 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2290 | return x; |
| 2291 | if (GetVersion() < 0x80000000) { |
| 2292 | /* |
| 2293 | * NT/2000 |
| 2294 | */ |
| 2295 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2296 | s2 = (char *)_alloca(x); |
| 2297 | ZeroMemory(s2, x); |
| 2298 | sprintf(s2, "%s%s%s", s1, s3, cmdstring); |
| 2299 | } |
| 2300 | else { |
| 2301 | /* |
| 2302 | * Oh gag, we're on Win9x. Use the workaround listed in |
| 2303 | * KB: Q150956 |
| 2304 | */ |
| 2305 | char modulepath[256]; |
| 2306 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2307 | for (i = x = 0; modulepath[i]; i++) |
| 2308 | if (modulepath[i] == '\\') |
| 2309 | x = i+1; |
| 2310 | modulepath[x] = '\0'; |
| 2311 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2312 | strlen(modulepath) + |
| 2313 | strlen(szConsoleSpawn) + 1; |
| 2314 | s2 = (char *)_alloca(x); |
| 2315 | ZeroMemory(s2, x); |
| 2316 | sprintf( |
| 2317 | s2, |
| 2318 | "%s%s%s%s%s\"", |
| 2319 | modulepath, |
| 2320 | szConsoleSpawn, |
| 2321 | s1, |
| 2322 | s3, |
| 2323 | cmdstring); |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2328 | Now we'll just error out.. */ |
| 2329 | else |
| 2330 | return -1; |
| 2331 | |
| 2332 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2333 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2334 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2335 | siStartInfo.hStdInput = hStdin; |
| 2336 | siStartInfo.hStdOutput = hStdout; |
| 2337 | siStartInfo.hStdError = hStderr; |
| 2338 | siStartInfo.wShowWindow = SW_HIDE; |
| 2339 | |
| 2340 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2341 | s2, |
| 2342 | NULL, |
| 2343 | NULL, |
| 2344 | TRUE, |
| 2345 | CREATE_NEW_CONSOLE, |
| 2346 | NULL, |
| 2347 | NULL, |
| 2348 | &siStartInfo, |
| 2349 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2350 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2351 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2352 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2353 | /* Return process handle */ |
| 2354 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2355 | return TRUE; |
| 2356 | } |
| 2357 | return FALSE; |
| 2358 | } |
| 2359 | |
| 2360 | /* The following code is based off of KB: Q190351 */ |
| 2361 | |
| 2362 | static PyObject * |
| 2363 | _PyPopen(char *cmdstring, int mode, int n) |
| 2364 | { |
| 2365 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2366 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2367 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2368 | |
| 2369 | SECURITY_ATTRIBUTES saAttr; |
| 2370 | BOOL fSuccess; |
| 2371 | int fd1, fd2, fd3; |
| 2372 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2373 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2374 | PyObject *f; |
| 2375 | |
| 2376 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2377 | saAttr.bInheritHandle = TRUE; |
| 2378 | saAttr.lpSecurityDescriptor = NULL; |
| 2379 | |
| 2380 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2381 | return win32_error("CreatePipe", NULL); |
| 2382 | |
| 2383 | /* Create new output read handle and the input write handle. Set |
| 2384 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2385 | * the these handles; resulting in non-closeable handles to the pipes |
| 2386 | * being created. */ |
| 2387 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2388 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2389 | FALSE, |
| 2390 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2391 | if (!fSuccess) |
| 2392 | return win32_error("DuplicateHandle", NULL); |
| 2393 | |
| 2394 | /* Close the inheritable version of ChildStdin |
| 2395 | that we're using. */ |
| 2396 | CloseHandle(hChildStdinWr); |
| 2397 | |
| 2398 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2399 | return win32_error("CreatePipe", NULL); |
| 2400 | |
| 2401 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2402 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2403 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2404 | if (!fSuccess) |
| 2405 | return win32_error("DuplicateHandle", NULL); |
| 2406 | |
| 2407 | /* Close the inheritable version of ChildStdout |
| 2408 | that we're using. */ |
| 2409 | CloseHandle(hChildStdoutRd); |
| 2410 | |
| 2411 | if (n != POPEN_4) { |
| 2412 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2413 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2414 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2415 | hChildStderrRd, |
| 2416 | GetCurrentProcess(), |
| 2417 | &hChildStderrRdDup, 0, |
| 2418 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2419 | if (!fSuccess) |
| 2420 | return win32_error("DuplicateHandle", NULL); |
| 2421 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2422 | CloseHandle(hChildStderrRd); |
| 2423 | } |
| 2424 | |
| 2425 | switch (n) { |
| 2426 | case POPEN_1: |
| 2427 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2428 | case _O_WRONLY | _O_TEXT: |
| 2429 | /* Case for writing to child Stdin in text mode. */ |
| 2430 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2431 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2432 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2433 | PyFile_SetBufSize(f, 0); |
| 2434 | /* We don't care about these pipes anymore, so close them. */ |
| 2435 | CloseHandle(hChildStdoutRdDup); |
| 2436 | CloseHandle(hChildStderrRdDup); |
| 2437 | break; |
| 2438 | |
| 2439 | case _O_RDONLY | _O_TEXT: |
| 2440 | /* Case for reading from child Stdout in text mode. */ |
| 2441 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2442 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2443 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2444 | PyFile_SetBufSize(f, 0); |
| 2445 | /* We don't care about these pipes anymore, so close them. */ |
| 2446 | CloseHandle(hChildStdinWrDup); |
| 2447 | CloseHandle(hChildStderrRdDup); |
| 2448 | break; |
| 2449 | |
| 2450 | case _O_RDONLY | _O_BINARY: |
| 2451 | /* Case for readinig from child Stdout in binary mode. */ |
| 2452 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2453 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2454 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2455 | PyFile_SetBufSize(f, 0); |
| 2456 | /* We don't care about these pipes anymore, so close them. */ |
| 2457 | CloseHandle(hChildStdinWrDup); |
| 2458 | CloseHandle(hChildStderrRdDup); |
| 2459 | break; |
| 2460 | |
| 2461 | case _O_WRONLY | _O_BINARY: |
| 2462 | /* Case for writing to child Stdin in binary mode. */ |
| 2463 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2464 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2465 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2466 | PyFile_SetBufSize(f, 0); |
| 2467 | /* We don't care about these pipes anymore, so close them. */ |
| 2468 | CloseHandle(hChildStdoutRdDup); |
| 2469 | CloseHandle(hChildStderrRdDup); |
| 2470 | break; |
| 2471 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2472 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2473 | break; |
| 2474 | |
| 2475 | case POPEN_2: |
| 2476 | case POPEN_4: |
| 2477 | { |
| 2478 | char *m1, *m2; |
| 2479 | PyObject *p1, *p2; |
| 2480 | |
| 2481 | if (mode && _O_TEXT) { |
| 2482 | m1 = "r"; |
| 2483 | m2 = "w"; |
| 2484 | } else { |
| 2485 | m1 = "rb"; |
| 2486 | m2 = "wb"; |
| 2487 | } |
| 2488 | |
| 2489 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2490 | f1 = _fdopen(fd1, m2); |
| 2491 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2492 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2493 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2494 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2495 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2496 | PyFile_SetBufSize(p2, 0); |
| 2497 | |
| 2498 | if (n != 4) |
| 2499 | CloseHandle(hChildStderrRdDup); |
| 2500 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2501 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2502 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2503 | break; |
| 2504 | } |
| 2505 | |
| 2506 | case POPEN_3: |
| 2507 | { |
| 2508 | char *m1, *m2; |
| 2509 | PyObject *p1, *p2, *p3; |
| 2510 | |
| 2511 | if (mode && _O_TEXT) { |
| 2512 | m1 = "r"; |
| 2513 | m2 = "w"; |
| 2514 | } else { |
| 2515 | m1 = "rb"; |
| 2516 | m2 = "wb"; |
| 2517 | } |
| 2518 | |
| 2519 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2520 | f1 = _fdopen(fd1, m2); |
| 2521 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2522 | f2 = _fdopen(fd2, m1); |
| 2523 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2524 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2525 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2526 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2527 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2528 | PyFile_SetBufSize(p1, 0); |
| 2529 | PyFile_SetBufSize(p2, 0); |
| 2530 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2531 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2532 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2533 | break; |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | if (n == POPEN_4) { |
| 2538 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2539 | hChildStdinRd, |
| 2540 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2541 | hChildStdoutWr, |
| 2542 | &hProcess)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2543 | return win32_error("CreateProcess", NULL); |
| 2544 | } |
| 2545 | else { |
| 2546 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2547 | hChildStdinRd, |
| 2548 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2549 | hChildStderrWr, |
| 2550 | &hProcess)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2551 | return win32_error("CreateProcess", NULL); |
| 2552 | } |
| 2553 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2554 | /* |
| 2555 | * Insert the files we've created into the process dictionary |
| 2556 | * all referencing the list with the process handle and the |
| 2557 | * initial number of files (see description below in _PyPclose). |
| 2558 | * Since if _PyPclose later tried to wait on a process when all |
| 2559 | * handles weren't closed, it could create a deadlock with the |
| 2560 | * child, we spend some energy here to try to ensure that we |
| 2561 | * either insert all file handles into the dictionary or none |
| 2562 | * at all. It's a little clumsy with the various popen modes |
| 2563 | * and variable number of files involved. |
| 2564 | */ |
| 2565 | if (!_PyPopenProcs) { |
| 2566 | _PyPopenProcs = PyDict_New(); |
| 2567 | } |
| 2568 | |
| 2569 | if (_PyPopenProcs) { |
| 2570 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2571 | int ins_rc[3]; |
| 2572 | |
| 2573 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2574 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2575 | |
| 2576 | procObj = PyList_New(2); |
| 2577 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2578 | intObj = PyInt_FromLong(file_count); |
| 2579 | |
| 2580 | if (procObj && hProcessObj && intObj) { |
| 2581 | PyList_SetItem(procObj,0,hProcessObj); |
| 2582 | PyList_SetItem(procObj,1,intObj); |
| 2583 | |
| 2584 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2585 | if (fileObj[0]) { |
| 2586 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2587 | fileObj[0], |
| 2588 | procObj); |
| 2589 | } |
| 2590 | if (file_count >= 2) { |
| 2591 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2592 | if (fileObj[1]) { |
| 2593 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2594 | fileObj[1], |
| 2595 | procObj); |
| 2596 | } |
| 2597 | } |
| 2598 | if (file_count >= 3) { |
| 2599 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2600 | if (fileObj[2]) { |
| 2601 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2602 | fileObj[2], |
| 2603 | procObj); |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2608 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2609 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2610 | /* Something failed - remove any dictionary |
| 2611 | * entries that did make it. |
| 2612 | */ |
| 2613 | if (!ins_rc[0] && fileObj[0]) { |
| 2614 | PyDict_DelItem(_PyPopenProcs, |
| 2615 | fileObj[0]); |
| 2616 | } |
| 2617 | if (!ins_rc[1] && fileObj[1]) { |
| 2618 | PyDict_DelItem(_PyPopenProcs, |
| 2619 | fileObj[1]); |
| 2620 | } |
| 2621 | if (!ins_rc[2] && fileObj[2]) { |
| 2622 | PyDict_DelItem(_PyPopenProcs, |
| 2623 | fileObj[2]); |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | /* |
| 2629 | * Clean up our localized references for the dictionary keys |
| 2630 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2631 | * that got placed in the dictionary. |
| 2632 | */ |
| 2633 | Py_XDECREF(procObj); |
| 2634 | Py_XDECREF(fileObj[0]); |
| 2635 | Py_XDECREF(fileObj[1]); |
| 2636 | Py_XDECREF(fileObj[2]); |
| 2637 | } |
| 2638 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2639 | /* Child is launched. Close the parents copy of those pipe |
| 2640 | * handles that only the child should have open. You need to |
| 2641 | * make sure that no handles to the write end of the output pipe |
| 2642 | * are maintained in this process or else the pipe will not close |
| 2643 | * when the child process exits and the ReadFile will hang. */ |
| 2644 | |
| 2645 | if (!CloseHandle(hChildStdinRd)) |
| 2646 | return win32_error("CloseHandle", NULL); |
| 2647 | |
| 2648 | if (!CloseHandle(hChildStdoutWr)) |
| 2649 | return win32_error("CloseHandle", NULL); |
| 2650 | |
| 2651 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2652 | return win32_error("CloseHandle", NULL); |
| 2653 | |
| 2654 | return f; |
| 2655 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2656 | |
| 2657 | /* |
| 2658 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2659 | * 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] | 2660 | * |
| 2661 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2662 | * input file pointer to information about the process that was |
| 2663 | * originally created by the popen* call that created the file pointer. |
| 2664 | * The dictionary uses the file pointer as a key (with one entry |
| 2665 | * inserted for each file returned by the original popen* call) and a |
| 2666 | * single list object as the value for all files from a single call. |
| 2667 | * The list object contains the Win32 process handle at [0], and a file |
| 2668 | * count at [1], which is initialized to the total number of file |
| 2669 | * handles using that list. |
| 2670 | * |
| 2671 | * This function closes whichever handle it is passed, and decrements |
| 2672 | * the file count in the dictionary for the process handle pointed to |
| 2673 | * by this file. On the last close (when the file count reaches zero), |
| 2674 | * this function will wait for the child process and then return its |
| 2675 | * exit code as the result of the close() operation. This permits the |
| 2676 | * files to be closed in any order - it is always the close() of the |
| 2677 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2678 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2679 | |
| 2680 | /* RED_FLAG 31-Aug-2000 Tim |
| 2681 | * This is always called (today!) between a pair of |
| 2682 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2683 | * macros. So the thread running this has no valid thread state, as |
| 2684 | * far as Python is concerned. However, this calls some Python API |
| 2685 | * functions that cannot be called safely without a valid thread |
| 2686 | * state, in particular PyDict_GetItem. |
| 2687 | * As a temporary hack (although it may last for years ...), we |
| 2688 | * *rely* on not having a valid thread state in this function, in |
| 2689 | * order to create our own "from scratch". |
| 2690 | * This will deadlock if _PyPclose is ever called by a thread |
| 2691 | * holding the global lock. |
| 2692 | */ |
| 2693 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2694 | static int _PyPclose(FILE *file) |
| 2695 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2696 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2697 | DWORD exit_code; |
| 2698 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2699 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 2700 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2701 | #ifdef WITH_THREAD |
| 2702 | PyInterpreterState* pInterpreterState; |
| 2703 | PyThreadState* pThreadState; |
| 2704 | #endif |
| 2705 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2706 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2707 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2708 | */ |
| 2709 | result = fclose(file); |
| 2710 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2711 | #ifdef WITH_THREAD |
| 2712 | /* Bootstrap a valid thread state into existence. */ |
| 2713 | pInterpreterState = PyInterpreterState_New(); |
| 2714 | if (!pInterpreterState) { |
| 2715 | /* Well, we're hosed now! We don't have a thread |
| 2716 | * state, so can't call a nice error routine, or raise |
| 2717 | * an exception. Just die. |
| 2718 | */ |
| 2719 | Py_FatalError("unable to allocate interpreter state " |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2720 | "when closing popen object."); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2721 | return -1; /* unreachable */ |
| 2722 | } |
| 2723 | pThreadState = PyThreadState_New(pInterpreterState); |
| 2724 | if (!pThreadState) { |
| 2725 | Py_FatalError("unable to allocate thread state " |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2726 | "when closing popen object."); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2727 | return -1; /* unreachable */ |
| 2728 | } |
| 2729 | /* Grab the global lock. Note that this will deadlock if the |
| 2730 | * current thread already has the lock! (see RED_FLAG comments |
| 2731 | * before this function) |
| 2732 | */ |
| 2733 | PyEval_RestoreThread(pThreadState); |
| 2734 | #endif |
| 2735 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2736 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2737 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2738 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2739 | fileObj)) != NULL && |
| 2740 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 2741 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 2742 | |
| 2743 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 2744 | file_count = PyInt_AsLong(intObj); |
| 2745 | |
| 2746 | if (file_count > 1) { |
| 2747 | /* Still other files referencing process */ |
| 2748 | file_count--; |
| 2749 | PyList_SetItem(procObj,1, |
| 2750 | PyInt_FromLong(file_count)); |
| 2751 | } else { |
| 2752 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2753 | if (result != EOF && |
| 2754 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 2755 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2756 | /* Possible truncation here in 16-bit environments, but |
| 2757 | * real exit codes are just the lower byte in any event. |
| 2758 | */ |
| 2759 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2760 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2761 | /* Indicate failure - this will cause the file object |
| 2762 | * to raise an I/O error and translate the last Win32 |
| 2763 | * error code from errno. We do have a problem with |
| 2764 | * last errors that overlap the normal errno table, |
| 2765 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2766 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2767 | if (result != EOF) { |
| 2768 | /* If the error wasn't from the fclose(), then |
| 2769 | * set errno for the file object error handling. |
| 2770 | */ |
| 2771 | errno = GetLastError(); |
| 2772 | } |
| 2773 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /* Free up the native handle at this point */ |
| 2777 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2778 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2779 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2780 | /* Remove this file pointer from dictionary */ |
| 2781 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2782 | |
| 2783 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 2784 | Py_DECREF(_PyPopenProcs); |
| 2785 | _PyPopenProcs = NULL; |
| 2786 | } |
| 2787 | |
| 2788 | } /* if object retrieval ok */ |
| 2789 | |
| 2790 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2791 | } /* if _PyPopenProcs */ |
| 2792 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2793 | #ifdef WITH_THREAD |
| 2794 | /* Tear down the thread & interpreter states. |
| 2795 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2796 | * call the thread clear & delete functions, and indeed insist on |
| 2797 | * doing that themselves. The lock must be held during the clear, but |
| 2798 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2799 | */ |
| 2800 | PyInterpreterState_Clear(pInterpreterState); |
| 2801 | PyEval_ReleaseThread(pThreadState); |
| 2802 | PyInterpreterState_Delete(pInterpreterState); |
| 2803 | #endif |
| 2804 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2805 | return result; |
| 2806 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 2807 | |
| 2808 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2810 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2812 | char *name; |
| 2813 | char *mode = "r"; |
| 2814 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2815 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2816 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2817 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2818 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2819 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2820 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2821 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2822 | if (fp == NULL) |
| 2823 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2824 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2825 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2826 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2827 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2828 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2829 | #endif |
| 2830 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2831 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2832 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2833 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2834 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2835 | static char posix_setuid__doc__[] = |
| 2836 | "setuid(uid) -> None\n\ |
| 2837 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2838 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2839 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2840 | { |
| 2841 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2842 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2843 | return NULL; |
| 2844 | if (setuid(uid) < 0) |
| 2845 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2846 | Py_INCREF(Py_None); |
| 2847 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2848 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2849 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2850 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2851 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 2852 | #ifdef HAVE_SETEUID |
| 2853 | static char posix_seteuid__doc__[] = |
| 2854 | "seteuid(uid) -> None\n\ |
| 2855 | Set the current process's effective user id."; |
| 2856 | static PyObject * |
| 2857 | posix_seteuid (PyObject *self, PyObject *args) |
| 2858 | { |
| 2859 | int euid; |
| 2860 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 2861 | return NULL; |
| 2862 | } else if (seteuid(euid) < 0) { |
| 2863 | return posix_error(); |
| 2864 | } else { |
| 2865 | Py_INCREF(Py_None); |
| 2866 | return Py_None; |
| 2867 | } |
| 2868 | } |
| 2869 | #endif /* HAVE_SETEUID */ |
| 2870 | |
| 2871 | #ifdef HAVE_SETEGID |
| 2872 | static char posix_setegid__doc__[] = |
| 2873 | "setegid(gid) -> None\n\ |
| 2874 | Set the current process's effective group id."; |
| 2875 | static PyObject * |
| 2876 | posix_setegid (PyObject *self, PyObject *args) |
| 2877 | { |
| 2878 | int egid; |
| 2879 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 2880 | return NULL; |
| 2881 | } else if (setegid(egid) < 0) { |
| 2882 | return posix_error(); |
| 2883 | } else { |
| 2884 | Py_INCREF(Py_None); |
| 2885 | return Py_None; |
| 2886 | } |
| 2887 | } |
| 2888 | #endif /* HAVE_SETEGID */ |
| 2889 | |
| 2890 | #ifdef HAVE_SETREUID |
| 2891 | static char posix_setreuid__doc__[] = |
| 2892 | "seteuid(ruid, euid) -> None\n\ |
| 2893 | Set the current process's real and effective user ids."; |
| 2894 | static PyObject * |
| 2895 | posix_setreuid (PyObject *self, PyObject *args) |
| 2896 | { |
| 2897 | int ruid, euid; |
| 2898 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 2899 | return NULL; |
| 2900 | } else if (setreuid(ruid, euid) < 0) { |
| 2901 | return posix_error(); |
| 2902 | } else { |
| 2903 | Py_INCREF(Py_None); |
| 2904 | return Py_None; |
| 2905 | } |
| 2906 | } |
| 2907 | #endif /* HAVE_SETREUID */ |
| 2908 | |
| 2909 | #ifdef HAVE_SETREGID |
| 2910 | static char posix_setregid__doc__[] = |
| 2911 | "setegid(rgid, egid) -> None\n\ |
| 2912 | Set the current process's real and effective group ids."; |
| 2913 | static PyObject * |
| 2914 | posix_setregid (PyObject *self, PyObject *args) |
| 2915 | { |
| 2916 | int rgid, egid; |
| 2917 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 2918 | return NULL; |
| 2919 | } else if (setregid(rgid, egid) < 0) { |
| 2920 | return posix_error(); |
| 2921 | } else { |
| 2922 | Py_INCREF(Py_None); |
| 2923 | return Py_None; |
| 2924 | } |
| 2925 | } |
| 2926 | #endif /* HAVE_SETREGID */ |
| 2927 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2928 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2929 | static char posix_setgid__doc__[] = |
| 2930 | "setgid(gid) -> None\n\ |
| 2931 | Set the current process's group id."; |
| 2932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2934 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2935 | { |
| 2936 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2937 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2938 | return NULL; |
| 2939 | if (setgid(gid) < 0) |
| 2940 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2941 | Py_INCREF(Py_None); |
| 2942 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2943 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2944 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2945 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2946 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2947 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2948 | static char posix_waitpid__doc__[] = |
| 2949 | "waitpid(pid, options) -> (pid, status)\n\ |
| 2950 | Wait for completion of a give child process."; |
| 2951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2953 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2954 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2955 | int pid, options; |
| 2956 | #ifdef UNION_WAIT |
| 2957 | union wait status; |
| 2958 | #define status_i (status.w_status) |
| 2959 | #else |
| 2960 | int status; |
| 2961 | #define status_i status |
| 2962 | #endif |
| 2963 | status_i = 0; |
| 2964 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2965 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2966 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2967 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2968 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2969 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2970 | #else |
| 2971 | pid = waitpid(pid, &status, options); |
| 2972 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2973 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2974 | if (pid == -1) |
| 2975 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2976 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2977 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2978 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2979 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2980 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2981 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2982 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2983 | static char posix_wait__doc__[] = |
| 2984 | "wait() -> (pid, status)\n\ |
| 2985 | Wait for completion of a child process."; |
| 2986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2988 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2989 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2990 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2991 | #ifdef UNION_WAIT |
| 2992 | union wait status; |
| 2993 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2994 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2995 | int status; |
| 2996 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2997 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2998 | if (!PyArg_ParseTuple(args, ":wait")) |
| 2999 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3000 | status_i = 0; |
| 3001 | Py_BEGIN_ALLOW_THREADS |
| 3002 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3003 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3004 | if (pid == -1) |
| 3005 | return posix_error(); |
| 3006 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3007 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3008 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3009 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3010 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3012 | |
| 3013 | static char posix_lstat__doc__[] = |
| 3014 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3015 | Like stat(path), but do not follow symbolic links."; |
| 3016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3018 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3019 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3020 | #ifdef HAVE_LSTAT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3021 | return posix_do_stat(self, args, "s:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3022 | #else /* !HAVE_LSTAT */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3023 | return posix_do_stat(self, args, "s:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3024 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3027 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3028 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3029 | static char posix_readlink__doc__[] = |
| 3030 | "readlink(path) -> path\n\ |
| 3031 | Return a string representing the path to which the symbolic link points."; |
| 3032 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3033 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3034 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3035 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3036 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3037 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3038 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3039 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3040 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3041 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3042 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3043 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3044 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3045 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3046 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3047 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3048 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3049 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3050 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3051 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3052 | static char posix_symlink__doc__[] = |
| 3053 | "symlink(src, dst) -> None\n\ |
| 3054 | Create a symbolic link."; |
| 3055 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3056 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3057 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3058 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3059 | return posix_2str(args, "ss:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3060 | } |
| 3061 | #endif /* HAVE_SYMLINK */ |
| 3062 | |
| 3063 | |
| 3064 | #ifdef HAVE_TIMES |
| 3065 | #ifndef HZ |
| 3066 | #define HZ 60 /* Universal constant :-) */ |
| 3067 | #endif /* HZ */ |
| 3068 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3069 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3070 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3071 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3072 | { |
| 3073 | ULONG value = 0; |
| 3074 | |
| 3075 | Py_BEGIN_ALLOW_THREADS |
| 3076 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3077 | Py_END_ALLOW_THREADS |
| 3078 | |
| 3079 | return value; |
| 3080 | } |
| 3081 | |
| 3082 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3083 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3084 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3085 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3086 | return NULL; |
| 3087 | |
| 3088 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3089 | return Py_BuildValue("ddddd", |
| 3090 | (double)0 /* t.tms_utime / HZ */, |
| 3091 | (double)0 /* t.tms_stime / HZ */, |
| 3092 | (double)0 /* t.tms_cutime / HZ */, |
| 3093 | (double)0 /* t.tms_cstime / HZ */, |
| 3094 | (double)system_uptime() / 1000); |
| 3095 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3096 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3097 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3098 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3099 | { |
| 3100 | struct tms t; |
| 3101 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3102 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3103 | return NULL; |
| 3104 | errno = 0; |
| 3105 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3106 | if (c == (clock_t) -1) |
| 3107 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3108 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3109 | (double)t.tms_utime / HZ, |
| 3110 | (double)t.tms_stime / HZ, |
| 3111 | (double)t.tms_cutime / HZ, |
| 3112 | (double)t.tms_cstime / HZ, |
| 3113 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3114 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3115 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3116 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3117 | |
| 3118 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3119 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3120 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3121 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3122 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3123 | { |
| 3124 | FILETIME create, exit, kernel, user; |
| 3125 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3126 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3127 | return NULL; |
| 3128 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3129 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3130 | /* The fields of a FILETIME structure are the hi and lo part |
| 3131 | of a 64-bit value expressed in 100 nanosecond units. |
| 3132 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3133 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3134 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3135 | return Py_BuildValue( |
| 3136 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3137 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3138 | kernel.dwLowDateTime*1e-7), |
| 3139 | (double)(user.dwHighDateTime*429.4967296 + |
| 3140 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3141 | (double)0, |
| 3142 | (double)0, |
| 3143 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3144 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3145 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3146 | |
| 3147 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3148 | static char posix_times__doc__[] = |
| 3149 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3150 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3151 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3152 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3153 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3154 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3155 | static char posix_setsid__doc__[] = |
| 3156 | "setsid() -> None\n\ |
| 3157 | Call the system call setsid()."; |
| 3158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3160 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3161 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3162 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3163 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3164 | if (setsid() < 0) |
| 3165 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3166 | Py_INCREF(Py_None); |
| 3167 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3168 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3169 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3170 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3171 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3172 | static char posix_setpgid__doc__[] = |
| 3173 | "setpgid(pid, pgrp) -> None\n\ |
| 3174 | Call the system call setpgid()."; |
| 3175 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3176 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3177 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3178 | { |
| 3179 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3180 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3181 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3182 | if (setpgid(pid, pgrp) < 0) |
| 3183 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3184 | Py_INCREF(Py_None); |
| 3185 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3186 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3187 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3188 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3189 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3190 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3191 | static char posix_tcgetpgrp__doc__[] = |
| 3192 | "tcgetpgrp(fd) -> pgid\n\ |
| 3193 | Return the process group associated with the terminal given by a fd."; |
| 3194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3195 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3196 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3197 | { |
| 3198 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3199 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3200 | return NULL; |
| 3201 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3202 | if (pgid < 0) |
| 3203 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3204 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3205 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3206 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3207 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3208 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3209 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3210 | static char posix_tcsetpgrp__doc__[] = |
| 3211 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3212 | Set the process group associated with the terminal given by a fd."; |
| 3213 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3214 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3215 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3216 | { |
| 3217 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3218 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3219 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3220 | if (tcsetpgrp(fd, pgid) < 0) |
| 3221 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3222 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3223 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3224 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3225 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3226 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3227 | /* Functions acting on file descriptors */ |
| 3228 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3229 | static char posix_open__doc__[] = |
| 3230 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3231 | Open a file (for low level IO)."; |
| 3232 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3233 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3234 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3235 | { |
| 3236 | char *file; |
| 3237 | int flag; |
| 3238 | int mode = 0777; |
| 3239 | int fd; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3240 | if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode)) |
| 3241 | return NULL; |
| 3242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3243 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3244 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3245 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3246 | if (fd < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3247 | return posix_error_with_filename(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3248 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3251 | |
| 3252 | static char posix_close__doc__[] = |
| 3253 | "close(fd) -> None\n\ |
| 3254 | Close a file descriptor (for low level IO)."; |
| 3255 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3256 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3257 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3258 | { |
| 3259 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3260 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3261 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3262 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3263 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3264 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3265 | if (res < 0) |
| 3266 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | Py_INCREF(Py_None); |
| 3268 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3271 | |
| 3272 | static char posix_dup__doc__[] = |
| 3273 | "dup(fd) -> fd2\n\ |
| 3274 | Return a duplicate of a file descriptor."; |
| 3275 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3276 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3277 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3278 | { |
| 3279 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3280 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3281 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3282 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3283 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3284 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3285 | if (fd < 0) |
| 3286 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3287 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3290 | |
| 3291 | static char posix_dup2__doc__[] = |
| 3292 | "dup2(fd, fd2) -> None\n\ |
| 3293 | Duplicate file descriptor."; |
| 3294 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3295 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3296 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3297 | { |
| 3298 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3299 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3300 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3301 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3302 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3303 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3304 | if (res < 0) |
| 3305 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3306 | Py_INCREF(Py_None); |
| 3307 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3310 | |
| 3311 | static char posix_lseek__doc__[] = |
| 3312 | "lseek(fd, pos, how) -> newpos\n\ |
| 3313 | Set the current position of a file descriptor."; |
| 3314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3315 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3316 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3317 | { |
| 3318 | int fd, how; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3319 | #ifdef MS_WIN64 |
| 3320 | LONG_LONG pos, res; |
| 3321 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3322 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3323 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3324 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3325 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3326 | return NULL; |
| 3327 | #ifdef SEEK_SET |
| 3328 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3329 | switch (how) { |
| 3330 | case 0: how = SEEK_SET; break; |
| 3331 | case 1: how = SEEK_CUR; break; |
| 3332 | case 2: how = SEEK_END; break; |
| 3333 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3334 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3335 | |
| 3336 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3337 | pos = PyInt_AsLong(posobj); |
| 3338 | #else |
| 3339 | pos = PyLong_Check(posobj) ? |
| 3340 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3341 | #endif |
| 3342 | if (PyErr_Occurred()) |
| 3343 | return NULL; |
| 3344 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3345 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3346 | #ifdef MS_WIN64 |
| 3347 | res = _lseeki64(fd, pos, how); |
| 3348 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3349 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3350 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3351 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3352 | if (res < 0) |
| 3353 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3354 | |
| 3355 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3356 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3357 | #else |
| 3358 | return PyLong_FromLongLong(res); |
| 3359 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3362 | |
| 3363 | static char posix_read__doc__[] = |
| 3364 | "read(fd, buffersize) -> string\n\ |
| 3365 | Read a file descriptor."; |
| 3366 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3367 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3368 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3369 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3370 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3371 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3372 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3373 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3374 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3375 | if (buffer == NULL) |
| 3376 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3377 | Py_BEGIN_ALLOW_THREADS |
| 3378 | n = read(fd, PyString_AsString(buffer), size); |
| 3379 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3380 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3381 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3382 | return posix_error(); |
| 3383 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3384 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3385 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3386 | return buffer; |
| 3387 | } |
| 3388 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3389 | |
| 3390 | static char posix_write__doc__[] = |
| 3391 | "write(fd, string) -> byteswritten\n\ |
| 3392 | Write a string to a file descriptor."; |
| 3393 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3394 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3395 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3396 | { |
| 3397 | int fd, size; |
| 3398 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3399 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3400 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3401 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3402 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3403 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3404 | if (size < 0) |
| 3405 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3406 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3409 | |
| 3410 | static char posix_fstat__doc__[]= |
| 3411 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3412 | Like stat(), but for an open file descriptor."; |
| 3413 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3415 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3416 | { |
| 3417 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3418 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3419 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3420 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3421 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3422 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3423 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3424 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3425 | if (res != 0) |
| 3426 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3427 | |
| 3428 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3431 | |
| 3432 | static char posix_fdopen__doc__[] = |
| 3433 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3434 | Return an open file object connected to a file descriptor."; |
| 3435 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3436 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3437 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3438 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3439 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3440 | char *mode = "r"; |
| 3441 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3442 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3443 | PyObject *f; |
| 3444 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3445 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3446 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3447 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3448 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3449 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3450 | if (fp == NULL) |
| 3451 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3452 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3453 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3455 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3458 | static char posix_isatty__doc__[] = |
| 3459 | "isatty(fd) -> Boolean\n\ |
| 3460 | Return true if the file descriptor 'fd' is an open file descriptor\n\ |
| 3461 | connected to a terminal."; |
| 3462 | |
| 3463 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3464 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3465 | { |
| 3466 | int fd; |
| 3467 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3468 | return NULL; |
| 3469 | return Py_BuildValue("i", isatty(fd)); |
| 3470 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3471 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3472 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3473 | static char posix_pipe__doc__[] = |
| 3474 | "pipe() -> (read_end, write_end)\n\ |
| 3475 | Create a pipe."; |
| 3476 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3477 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3478 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3479 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3480 | #if defined(PYOS_OS2) |
| 3481 | HFILE read, write; |
| 3482 | APIRET rc; |
| 3483 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3484 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3485 | return NULL; |
| 3486 | |
| 3487 | Py_BEGIN_ALLOW_THREADS |
| 3488 | rc = DosCreatePipe( &read, &write, 4096); |
| 3489 | Py_END_ALLOW_THREADS |
| 3490 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3491 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3492 | |
| 3493 | return Py_BuildValue("(ii)", read, write); |
| 3494 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3495 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3496 | int fds[2]; |
| 3497 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3498 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3499 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3500 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3501 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3502 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3503 | if (res != 0) |
| 3504 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3505 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3506 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3507 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3508 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3509 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3510 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3511 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3512 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3513 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3514 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3515 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3516 | return win32_error("CreatePipe", NULL); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3517 | read_fd = _open_osfhandle((intptr_t)read, 0); |
| 3518 | write_fd = _open_osfhandle((intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3519 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3520 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3521 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3522 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3523 | #endif /* HAVE_PIPE */ |
| 3524 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3525 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3526 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3527 | static char posix_mkfifo__doc__[] = |
| 3528 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3529 | Create a FIFO (a POSIX named pipe)."; |
| 3530 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3531 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3532 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3533 | { |
| 3534 | char *file; |
| 3535 | int mode = 0666; |
| 3536 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3537 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3538 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3539 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3540 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3541 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3542 | if (res < 0) |
| 3543 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3544 | Py_INCREF(Py_None); |
| 3545 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3546 | } |
| 3547 | #endif |
| 3548 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3550 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3551 | static char posix_ftruncate__doc__[] = |
| 3552 | "ftruncate(fd, length) -> None\n\ |
| 3553 | Truncate a file to a specified length."; |
| 3554 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3555 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3556 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3557 | { |
| 3558 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3559 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3560 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3561 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3562 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3563 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3564 | return NULL; |
| 3565 | |
| 3566 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3567 | length = PyInt_AsLong(lenobj); |
| 3568 | #else |
| 3569 | length = PyLong_Check(lenobj) ? |
| 3570 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3571 | #endif |
| 3572 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3573 | return NULL; |
| 3574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3575 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3576 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3577 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3578 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3579 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3580 | return NULL; |
| 3581 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3582 | Py_INCREF(Py_None); |
| 3583 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3584 | } |
| 3585 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3586 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3587 | #ifdef NeXT |
| 3588 | #define HAVE_PUTENV |
| 3589 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3590 | static int |
| 3591 | putenv(char *newval) |
| 3592 | { |
| 3593 | extern char **environ; |
| 3594 | |
| 3595 | static int firstTime = 1; |
| 3596 | char **ep; |
| 3597 | char *cp; |
| 3598 | int esiz; |
| 3599 | char *np; |
| 3600 | |
| 3601 | if (!(np = strchr(newval, '='))) |
| 3602 | return 1; |
| 3603 | *np = '\0'; |
| 3604 | |
| 3605 | /* look it up */ |
| 3606 | for (ep=environ ; *ep ; ep++) |
| 3607 | { |
| 3608 | /* this should always be true... */ |
| 3609 | if (cp = strchr(*ep, '=')) |
| 3610 | { |
| 3611 | *cp = '\0'; |
| 3612 | if (!strcmp(*ep, newval)) |
| 3613 | { |
| 3614 | /* got it! */ |
| 3615 | *cp = '='; |
| 3616 | break; |
| 3617 | } |
| 3618 | *cp = '='; |
| 3619 | } |
| 3620 | else |
| 3621 | { |
| 3622 | *np = '='; |
| 3623 | return 1; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | *np = '='; |
| 3628 | if (*ep) |
| 3629 | { |
| 3630 | /* the string was already there: |
| 3631 | just replace it with the new one */ |
| 3632 | *ep = newval; |
| 3633 | return 0; |
| 3634 | } |
| 3635 | |
| 3636 | /* expand environ by one */ |
| 3637 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3638 | esiz++; |
| 3639 | if (firstTime) |
| 3640 | { |
| 3641 | char **epp; |
| 3642 | char **newenv; |
| 3643 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 3644 | return 1; |
| 3645 | |
| 3646 | for (ep=environ, epp=newenv ; *ep ;) |
| 3647 | *epp++ = *ep++; |
| 3648 | *epp++ = newval; |
| 3649 | *epp = (char *) 0; |
| 3650 | environ = newenv; |
| 3651 | } |
| 3652 | else |
| 3653 | { |
| 3654 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 3655 | return 1; |
| 3656 | environ[esiz - 2] = newval; |
| 3657 | environ[esiz - 1] = (char *) 0; |
| 3658 | firstTime = 0; |
| 3659 | } |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 3663 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3664 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3665 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3666 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3667 | static char posix_putenv__doc__[] = |
| 3668 | "putenv(key, value) -> None\n\ |
| 3669 | Change or add an environment variable."; |
| 3670 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3671 | /* Save putenv() parameters as values here, so we can collect them when they |
| 3672 | * get re-set with another call for the same key. */ |
| 3673 | static PyObject *posix_putenv_garbage; |
| 3674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3676 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3677 | { |
| 3678 | char *s1, *s2; |
| 3679 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3680 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3681 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3682 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3683 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3684 | |
| 3685 | #if defined(PYOS_OS2) |
| 3686 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 3687 | APIRET rc; |
| 3688 | |
| 3689 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3690 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3691 | |
| 3692 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 3693 | if (rc != NO_ERROR) |
| 3694 | return os2_error(rc); |
| 3695 | |
| 3696 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 3697 | APIRET rc; |
| 3698 | |
| 3699 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3700 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3701 | |
| 3702 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 3703 | if (rc != NO_ERROR) |
| 3704 | return os2_error(rc); |
| 3705 | } else { |
| 3706 | #endif |
| 3707 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3708 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 3709 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 3710 | if (newstr == NULL) |
| 3711 | return PyErr_NoMemory(); |
| 3712 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3713 | (void) sprintf(new, "%s=%s", s1, s2); |
| 3714 | if (putenv(new)) { |
| 3715 | posix_error(); |
| 3716 | return NULL; |
| 3717 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3718 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 3719 | * this will cause previous value to be collected. This has to |
| 3720 | * happen after the real putenv() call because the old value |
| 3721 | * was still accessible until then. */ |
| 3722 | if (PyDict_SetItem(posix_putenv_garbage, |
| 3723 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 3724 | /* really not much we can do; just leak */ |
| 3725 | PyErr_Clear(); |
| 3726 | } |
| 3727 | else { |
| 3728 | Py_DECREF(newstr); |
| 3729 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3730 | |
| 3731 | #if defined(PYOS_OS2) |
| 3732 | } |
| 3733 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3734 | Py_INCREF(Py_None); |
| 3735 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3736 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3737 | #endif /* putenv */ |
| 3738 | |
| 3739 | #ifdef HAVE_STRERROR |
| 3740 | static char posix_strerror__doc__[] = |
| 3741 | "strerror(code) -> string\n\ |
| 3742 | Translate an error code to a message string."; |
| 3743 | |
| 3744 | PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3745 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3746 | { |
| 3747 | int code; |
| 3748 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3749 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3750 | return NULL; |
| 3751 | message = strerror(code); |
| 3752 | if (message == NULL) { |
| 3753 | PyErr_SetString(PyExc_ValueError, |
| 3754 | "strerror code out of range"); |
| 3755 | return NULL; |
| 3756 | } |
| 3757 | return PyString_FromString(message); |
| 3758 | } |
| 3759 | #endif /* strerror */ |
| 3760 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3761 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3762 | #ifdef HAVE_SYS_WAIT_H |
| 3763 | |
| 3764 | #ifdef WIFSTOPPED |
| 3765 | static char posix_WIFSTOPPED__doc__[] = |
| 3766 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3767 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3768 | |
| 3769 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3770 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3771 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3772 | #ifdef UNION_WAIT |
| 3773 | union wait status; |
| 3774 | #define status_i (status.w_status) |
| 3775 | #else |
| 3776 | int status; |
| 3777 | #define status_i status |
| 3778 | #endif |
| 3779 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3780 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3781 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3782 | { |
| 3783 | return NULL; |
| 3784 | } |
| 3785 | |
| 3786 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3787 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3788 | } |
| 3789 | #endif /* WIFSTOPPED */ |
| 3790 | |
| 3791 | #ifdef WIFSIGNALED |
| 3792 | static char posix_WIFSIGNALED__doc__[] = |
| 3793 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 3794 | 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] | 3795 | |
| 3796 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3797 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3798 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3799 | #ifdef UNION_WAIT |
| 3800 | union wait status; |
| 3801 | #define status_i (status.w_status) |
| 3802 | #else |
| 3803 | int status; |
| 3804 | #define status_i status |
| 3805 | #endif |
| 3806 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3807 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3808 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3809 | { |
| 3810 | return NULL; |
| 3811 | } |
| 3812 | |
| 3813 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3814 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3815 | } |
| 3816 | #endif /* WIFSIGNALED */ |
| 3817 | |
| 3818 | #ifdef WIFEXITED |
| 3819 | static char posix_WIFEXITED__doc__[] = |
| 3820 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3821 | Return true if the process returning 'status' exited using the exit()\n\ |
| 3822 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3823 | |
| 3824 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3825 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3826 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3827 | #ifdef UNION_WAIT |
| 3828 | union wait status; |
| 3829 | #define status_i (status.w_status) |
| 3830 | #else |
| 3831 | int status; |
| 3832 | #define status_i status |
| 3833 | #endif |
| 3834 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3835 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3836 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3837 | { |
| 3838 | return NULL; |
| 3839 | } |
| 3840 | |
| 3841 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3842 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3843 | } |
| 3844 | #endif /* WIFEXITED */ |
| 3845 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3846 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3847 | static char posix_WEXITSTATUS__doc__[] = |
| 3848 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3849 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3850 | |
| 3851 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3852 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3853 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3854 | #ifdef UNION_WAIT |
| 3855 | union wait status; |
| 3856 | #define status_i (status.w_status) |
| 3857 | #else |
| 3858 | int status; |
| 3859 | #define status_i status |
| 3860 | #endif |
| 3861 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3862 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3863 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3864 | { |
| 3865 | return NULL; |
| 3866 | } |
| 3867 | |
| 3868 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3869 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3870 | } |
| 3871 | #endif /* WEXITSTATUS */ |
| 3872 | |
| 3873 | #ifdef WTERMSIG |
| 3874 | static char posix_WTERMSIG__doc__[] = |
| 3875 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3876 | Return the signal that terminated the process that provided the 'status'\n\ |
| 3877 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3878 | |
| 3879 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3880 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3881 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3882 | #ifdef UNION_WAIT |
| 3883 | union wait status; |
| 3884 | #define status_i (status.w_status) |
| 3885 | #else |
| 3886 | int status; |
| 3887 | #define status_i status |
| 3888 | #endif |
| 3889 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3890 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3891 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3892 | { |
| 3893 | return NULL; |
| 3894 | } |
| 3895 | |
| 3896 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3897 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3898 | } |
| 3899 | #endif /* WTERMSIG */ |
| 3900 | |
| 3901 | #ifdef WSTOPSIG |
| 3902 | static char posix_WSTOPSIG__doc__[] = |
| 3903 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3904 | 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] | 3905 | |
| 3906 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3907 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3908 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3909 | #ifdef UNION_WAIT |
| 3910 | union wait status; |
| 3911 | #define status_i (status.w_status) |
| 3912 | #else |
| 3913 | int status; |
| 3914 | #define status_i status |
| 3915 | #endif |
| 3916 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3917 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3918 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3919 | { |
| 3920 | return NULL; |
| 3921 | } |
| 3922 | |
| 3923 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3924 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3925 | } |
| 3926 | #endif /* WSTOPSIG */ |
| 3927 | |
| 3928 | #endif /* HAVE_SYS_WAIT_H */ |
| 3929 | |
| 3930 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3931 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 3932 | #ifdef _SCO_DS |
| 3933 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 3934 | needed definitions in sys/statvfs.h */ |
| 3935 | #define _SVID3 |
| 3936 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3937 | #include <sys/statvfs.h> |
| 3938 | |
| 3939 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3940 | "fstatvfs(fd) -> \n\ |
| 3941 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3942 | Perform an fstatvfs system call on the given fd."; |
| 3943 | |
| 3944 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3945 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3946 | { |
| 3947 | int fd, res; |
| 3948 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3949 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3950 | return NULL; |
| 3951 | Py_BEGIN_ALLOW_THREADS |
| 3952 | res = fstatvfs(fd, &st); |
| 3953 | Py_END_ALLOW_THREADS |
| 3954 | if (res != 0) |
| 3955 | return posix_error(); |
| 3956 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3957 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3958 | (long) st.f_bsize, |
| 3959 | (long) st.f_frsize, |
| 3960 | (long) st.f_blocks, |
| 3961 | (long) st.f_bfree, |
| 3962 | (long) st.f_bavail, |
| 3963 | (long) st.f_files, |
| 3964 | (long) st.f_ffree, |
| 3965 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3966 | (long) st.f_flag, |
| 3967 | (long) st.f_namemax); |
| 3968 | #else |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3969 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3970 | (long) st.f_bsize, |
| 3971 | (long) st.f_frsize, |
| 3972 | (LONG_LONG) st.f_blocks, |
| 3973 | (LONG_LONG) st.f_bfree, |
| 3974 | (LONG_LONG) st.f_bavail, |
| 3975 | (LONG_LONG) st.f_files, |
| 3976 | (LONG_LONG) st.f_ffree, |
| 3977 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3978 | (long) st.f_flag, |
| 3979 | (long) st.f_namemax); |
| 3980 | #endif |
| 3981 | } |
| 3982 | #endif /* HAVE_FSTATVFS */ |
| 3983 | |
| 3984 | |
| 3985 | #if defined(HAVE_STATVFS) |
| 3986 | #include <sys/statvfs.h> |
| 3987 | |
| 3988 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3989 | "statvfs(path) -> \n\ |
| 3990 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3991 | Perform a statvfs system call on the given path."; |
| 3992 | |
| 3993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3994 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3995 | { |
| 3996 | char *path; |
| 3997 | int res; |
| 3998 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3999 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4000 | return NULL; |
| 4001 | Py_BEGIN_ALLOW_THREADS |
| 4002 | res = statvfs(path, &st); |
| 4003 | Py_END_ALLOW_THREADS |
| 4004 | if (res != 0) |
| 4005 | return posix_error_with_filename(path); |
| 4006 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4007 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4008 | (long) st.f_bsize, |
| 4009 | (long) st.f_frsize, |
| 4010 | (long) st.f_blocks, |
| 4011 | (long) st.f_bfree, |
| 4012 | (long) st.f_bavail, |
| 4013 | (long) st.f_files, |
| 4014 | (long) st.f_ffree, |
| 4015 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4016 | (long) st.f_flag, |
| 4017 | (long) st.f_namemax); |
| 4018 | #else /* HAVE_LARGEFILE_SUPPORT */ |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4019 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4020 | (long) st.f_bsize, |
| 4021 | (long) st.f_frsize, |
| 4022 | (LONG_LONG) st.f_blocks, |
| 4023 | (LONG_LONG) st.f_bfree, |
| 4024 | (LONG_LONG) st.f_bavail, |
| 4025 | (LONG_LONG) st.f_files, |
| 4026 | (LONG_LONG) st.f_ffree, |
| 4027 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4028 | (long) st.f_flag, |
| 4029 | (long) st.f_namemax); |
| 4030 | #endif |
| 4031 | } |
| 4032 | #endif /* HAVE_STATVFS */ |
| 4033 | |
| 4034 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4035 | #ifdef HAVE_TEMPNAM |
| 4036 | static char posix_tempnam__doc__[] = "\ |
| 4037 | tempnam([dir[, prefix]]) -> string\n\ |
| 4038 | Return a unique name for a temporary file.\n\ |
| 4039 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4040 | or None if not needed."; |
| 4041 | |
| 4042 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4043 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4044 | { |
| 4045 | PyObject *result = NULL; |
| 4046 | char *dir = NULL; |
| 4047 | char *pfx = NULL; |
| 4048 | char *name; |
| 4049 | |
| 4050 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4051 | return NULL; |
| 4052 | name = tempnam(dir, pfx); |
| 4053 | if (name == NULL) |
| 4054 | return PyErr_NoMemory(); |
| 4055 | result = PyString_FromString(name); |
| 4056 | free(name); |
| 4057 | return result; |
| 4058 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4059 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4060 | |
| 4061 | |
| 4062 | #ifdef HAVE_TMPFILE |
| 4063 | static char posix_tmpfile__doc__[] = "\ |
| 4064 | tmpfile() -> file object\n\ |
| 4065 | Create a temporary file with no directory entries."; |
| 4066 | |
| 4067 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4068 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4069 | { |
| 4070 | FILE *fp; |
| 4071 | |
| 4072 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4073 | return NULL; |
| 4074 | fp = tmpfile(); |
| 4075 | if (fp == NULL) |
| 4076 | return posix_error(); |
| 4077 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4078 | } |
| 4079 | #endif |
| 4080 | |
| 4081 | |
| 4082 | #ifdef HAVE_TMPNAM |
| 4083 | static char posix_tmpnam__doc__[] = "\ |
| 4084 | tmpnam() -> string\n\ |
| 4085 | Return a unique name for a temporary file."; |
| 4086 | |
| 4087 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4088 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4089 | { |
| 4090 | char buffer[L_tmpnam]; |
| 4091 | char *name; |
| 4092 | |
| 4093 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4094 | return NULL; |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4095 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4096 | name = tmpnam_r(buffer); |
| 4097 | #else |
| 4098 | name = tmpnam(buffer); |
| 4099 | #endif |
| 4100 | if (name == NULL) { |
| 4101 | PyErr_SetObject(PyExc_OSError, |
| 4102 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4103 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4104 | "unexpected NULL from tmpnam_r" |
| 4105 | #else |
| 4106 | "unexpected NULL from tmpnam" |
| 4107 | #endif |
| 4108 | )); |
| 4109 | return NULL; |
| 4110 | } |
| 4111 | return PyString_FromString(buffer); |
| 4112 | } |
| 4113 | #endif |
| 4114 | |
| 4115 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4116 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4117 | * It maps strings representing configuration variable names to |
| 4118 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4119 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4120 | * rarely-used constants. There are three separate tables that use |
| 4121 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4122 | * |
| 4123 | * This code is always included, even if none of the interfaces that |
| 4124 | * need it are included. The #if hackery needed to avoid it would be |
| 4125 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4126 | */ |
| 4127 | struct constdef { |
| 4128 | char *name; |
| 4129 | long value; |
| 4130 | }; |
| 4131 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4132 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4133 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4134 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4135 | { |
| 4136 | if (PyInt_Check(arg)) { |
| 4137 | *valuep = PyInt_AS_LONG(arg); |
| 4138 | return 1; |
| 4139 | } |
| 4140 | if (PyString_Check(arg)) { |
| 4141 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4142 | size_t lo = 0; |
| 4143 | size_t mid; |
| 4144 | size_t hi = tablesize; |
| 4145 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4146 | char *confname = PyString_AS_STRING(arg); |
| 4147 | while (lo < hi) { |
| 4148 | mid = (lo + hi) / 2; |
| 4149 | cmp = strcmp(confname, table[mid].name); |
| 4150 | if (cmp < 0) |
| 4151 | hi = mid; |
| 4152 | else if (cmp > 0) |
| 4153 | lo = mid + 1; |
| 4154 | else { |
| 4155 | *valuep = table[mid].value; |
| 4156 | return 1; |
| 4157 | } |
| 4158 | } |
| 4159 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4160 | } |
| 4161 | else |
| 4162 | PyErr_SetString(PyExc_TypeError, |
| 4163 | "configuration names must be strings or integers"); |
| 4164 | return 0; |
| 4165 | } |
| 4166 | |
| 4167 | |
| 4168 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4169 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4170 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4171 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4172 | #endif |
| 4173 | #ifdef _PC_ABI_ASYNC_IO |
| 4174 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4175 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4176 | #ifdef _PC_ASYNC_IO |
| 4177 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4178 | #endif |
| 4179 | #ifdef _PC_CHOWN_RESTRICTED |
| 4180 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4181 | #endif |
| 4182 | #ifdef _PC_FILESIZEBITS |
| 4183 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4184 | #endif |
| 4185 | #ifdef _PC_LAST |
| 4186 | {"PC_LAST", _PC_LAST}, |
| 4187 | #endif |
| 4188 | #ifdef _PC_LINK_MAX |
| 4189 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4190 | #endif |
| 4191 | #ifdef _PC_MAX_CANON |
| 4192 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4193 | #endif |
| 4194 | #ifdef _PC_MAX_INPUT |
| 4195 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4196 | #endif |
| 4197 | #ifdef _PC_NAME_MAX |
| 4198 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4199 | #endif |
| 4200 | #ifdef _PC_NO_TRUNC |
| 4201 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4202 | #endif |
| 4203 | #ifdef _PC_PATH_MAX |
| 4204 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4205 | #endif |
| 4206 | #ifdef _PC_PIPE_BUF |
| 4207 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4208 | #endif |
| 4209 | #ifdef _PC_PRIO_IO |
| 4210 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4211 | #endif |
| 4212 | #ifdef _PC_SOCK_MAXBUF |
| 4213 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4214 | #endif |
| 4215 | #ifdef _PC_SYNC_IO |
| 4216 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4217 | #endif |
| 4218 | #ifdef _PC_VDISABLE |
| 4219 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4220 | #endif |
| 4221 | }; |
| 4222 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4223 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4224 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4225 | { |
| 4226 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4227 | sizeof(posix_constants_pathconf) |
| 4228 | / sizeof(struct constdef)); |
| 4229 | } |
| 4230 | #endif |
| 4231 | |
| 4232 | #ifdef HAVE_FPATHCONF |
| 4233 | static char posix_fpathconf__doc__[] = "\ |
| 4234 | fpathconf(fd, name) -> integer\n\ |
| 4235 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4236 | If there is no limit, return -1."; |
| 4237 | |
| 4238 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4239 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4240 | { |
| 4241 | PyObject *result = NULL; |
| 4242 | int name, fd; |
| 4243 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4244 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4245 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4246 | long limit; |
| 4247 | |
| 4248 | errno = 0; |
| 4249 | limit = fpathconf(fd, name); |
| 4250 | if (limit == -1 && errno != 0) |
| 4251 | posix_error(); |
| 4252 | else |
| 4253 | result = PyInt_FromLong(limit); |
| 4254 | } |
| 4255 | return result; |
| 4256 | } |
| 4257 | #endif |
| 4258 | |
| 4259 | |
| 4260 | #ifdef HAVE_PATHCONF |
| 4261 | static char posix_pathconf__doc__[] = "\ |
| 4262 | pathconf(path, name) -> integer\n\ |
| 4263 | Return the configuration limit name for the file or directory path.\n\ |
| 4264 | If there is no limit, return -1."; |
| 4265 | |
| 4266 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4267 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4268 | { |
| 4269 | PyObject *result = NULL; |
| 4270 | int name; |
| 4271 | char *path; |
| 4272 | |
| 4273 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4274 | conv_path_confname, &name)) { |
| 4275 | long limit; |
| 4276 | |
| 4277 | errno = 0; |
| 4278 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4279 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4280 | if (errno == EINVAL) |
| 4281 | /* could be a path or name problem */ |
| 4282 | posix_error(); |
| 4283 | else |
| 4284 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4285 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4286 | else |
| 4287 | result = PyInt_FromLong(limit); |
| 4288 | } |
| 4289 | return result; |
| 4290 | } |
| 4291 | #endif |
| 4292 | |
| 4293 | #ifdef HAVE_CONFSTR |
| 4294 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4295 | #ifdef _CS_ARCHITECTURE |
| 4296 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4297 | #endif |
| 4298 | #ifdef _CS_HOSTNAME |
| 4299 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4300 | #endif |
| 4301 | #ifdef _CS_HW_PROVIDER |
| 4302 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4303 | #endif |
| 4304 | #ifdef _CS_HW_SERIAL |
| 4305 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4306 | #endif |
| 4307 | #ifdef _CS_INITTAB_NAME |
| 4308 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4309 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4310 | #ifdef _CS_LFS64_CFLAGS |
| 4311 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4312 | #endif |
| 4313 | #ifdef _CS_LFS64_LDFLAGS |
| 4314 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4315 | #endif |
| 4316 | #ifdef _CS_LFS64_LIBS |
| 4317 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4318 | #endif |
| 4319 | #ifdef _CS_LFS64_LINTFLAGS |
| 4320 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4321 | #endif |
| 4322 | #ifdef _CS_LFS_CFLAGS |
| 4323 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4324 | #endif |
| 4325 | #ifdef _CS_LFS_LDFLAGS |
| 4326 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4327 | #endif |
| 4328 | #ifdef _CS_LFS_LIBS |
| 4329 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4330 | #endif |
| 4331 | #ifdef _CS_LFS_LINTFLAGS |
| 4332 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4333 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4334 | #ifdef _CS_MACHINE |
| 4335 | {"CS_MACHINE", _CS_MACHINE}, |
| 4336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4337 | #ifdef _CS_PATH |
| 4338 | {"CS_PATH", _CS_PATH}, |
| 4339 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4340 | #ifdef _CS_RELEASE |
| 4341 | {"CS_RELEASE", _CS_RELEASE}, |
| 4342 | #endif |
| 4343 | #ifdef _CS_SRPC_DOMAIN |
| 4344 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4345 | #endif |
| 4346 | #ifdef _CS_SYSNAME |
| 4347 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4348 | #endif |
| 4349 | #ifdef _CS_VERSION |
| 4350 | {"CS_VERSION", _CS_VERSION}, |
| 4351 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4352 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4353 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4354 | #endif |
| 4355 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4356 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4357 | #endif |
| 4358 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4359 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4360 | #endif |
| 4361 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4362 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4363 | #endif |
| 4364 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4365 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4366 | #endif |
| 4367 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4368 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4369 | #endif |
| 4370 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4371 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4372 | #endif |
| 4373 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4374 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4375 | #endif |
| 4376 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4377 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4378 | #endif |
| 4379 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4380 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4381 | #endif |
| 4382 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4383 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4384 | #endif |
| 4385 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4386 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4387 | #endif |
| 4388 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4389 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4390 | #endif |
| 4391 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4392 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4393 | #endif |
| 4394 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4395 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4396 | #endif |
| 4397 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4398 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4399 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4400 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4401 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4402 | #endif |
| 4403 | #ifdef _MIPS_CS_BASE |
| 4404 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4405 | #endif |
| 4406 | #ifdef _MIPS_CS_HOSTID |
| 4407 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4408 | #endif |
| 4409 | #ifdef _MIPS_CS_HW_NAME |
| 4410 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4411 | #endif |
| 4412 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4413 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4414 | #endif |
| 4415 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4416 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4417 | #endif |
| 4418 | #ifdef _MIPS_CS_OSREL_MIN |
| 4419 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4420 | #endif |
| 4421 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4422 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4423 | #endif |
| 4424 | #ifdef _MIPS_CS_OS_NAME |
| 4425 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4426 | #endif |
| 4427 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4428 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4429 | #endif |
| 4430 | #ifdef _MIPS_CS_PROCESSORS |
| 4431 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4432 | #endif |
| 4433 | #ifdef _MIPS_CS_SERIAL |
| 4434 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4435 | #endif |
| 4436 | #ifdef _MIPS_CS_VENDOR |
| 4437 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4438 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4439 | }; |
| 4440 | |
| 4441 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4442 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4443 | { |
| 4444 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4445 | sizeof(posix_constants_confstr) |
| 4446 | / sizeof(struct constdef)); |
| 4447 | } |
| 4448 | |
| 4449 | static char posix_confstr__doc__[] = "\ |
| 4450 | confstr(name) -> string\n\ |
| 4451 | Return a string-valued system configuration variable."; |
| 4452 | |
| 4453 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4454 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4455 | { |
| 4456 | PyObject *result = NULL; |
| 4457 | int name; |
| 4458 | char buffer[64]; |
| 4459 | |
| 4460 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4461 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4462 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4463 | errno = 0; |
| 4464 | if (len == 0) { |
| 4465 | if (errno != 0) |
| 4466 | posix_error(); |
| 4467 | else |
| 4468 | result = PyString_FromString(""); |
| 4469 | } |
| 4470 | else { |
| 4471 | if (len >= sizeof(buffer)) { |
| 4472 | result = PyString_FromStringAndSize(NULL, len); |
| 4473 | if (result != NULL) |
| 4474 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4475 | } |
| 4476 | else |
| 4477 | result = PyString_FromString(buffer); |
| 4478 | } |
| 4479 | } |
| 4480 | return result; |
| 4481 | } |
| 4482 | #endif |
| 4483 | |
| 4484 | |
| 4485 | #ifdef HAVE_SYSCONF |
| 4486 | static struct constdef posix_constants_sysconf[] = { |
| 4487 | #ifdef _SC_2_CHAR_TERM |
| 4488 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4489 | #endif |
| 4490 | #ifdef _SC_2_C_BIND |
| 4491 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4492 | #endif |
| 4493 | #ifdef _SC_2_C_DEV |
| 4494 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4495 | #endif |
| 4496 | #ifdef _SC_2_C_VERSION |
| 4497 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4498 | #endif |
| 4499 | #ifdef _SC_2_FORT_DEV |
| 4500 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4501 | #endif |
| 4502 | #ifdef _SC_2_FORT_RUN |
| 4503 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4504 | #endif |
| 4505 | #ifdef _SC_2_LOCALEDEF |
| 4506 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4507 | #endif |
| 4508 | #ifdef _SC_2_SW_DEV |
| 4509 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4510 | #endif |
| 4511 | #ifdef _SC_2_UPE |
| 4512 | {"SC_2_UPE", _SC_2_UPE}, |
| 4513 | #endif |
| 4514 | #ifdef _SC_2_VERSION |
| 4515 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4516 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4517 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4518 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4519 | #endif |
| 4520 | #ifdef _SC_ACL |
| 4521 | {"SC_ACL", _SC_ACL}, |
| 4522 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4523 | #ifdef _SC_AIO_LISTIO_MAX |
| 4524 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4525 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4526 | #ifdef _SC_AIO_MAX |
| 4527 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4528 | #endif |
| 4529 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4530 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4531 | #endif |
| 4532 | #ifdef _SC_ARG_MAX |
| 4533 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4534 | #endif |
| 4535 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4536 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4537 | #endif |
| 4538 | #ifdef _SC_ATEXIT_MAX |
| 4539 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4540 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4541 | #ifdef _SC_AUDIT |
| 4542 | {"SC_AUDIT", _SC_AUDIT}, |
| 4543 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4544 | #ifdef _SC_AVPHYS_PAGES |
| 4545 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4546 | #endif |
| 4547 | #ifdef _SC_BC_BASE_MAX |
| 4548 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4549 | #endif |
| 4550 | #ifdef _SC_BC_DIM_MAX |
| 4551 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4552 | #endif |
| 4553 | #ifdef _SC_BC_SCALE_MAX |
| 4554 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4555 | #endif |
| 4556 | #ifdef _SC_BC_STRING_MAX |
| 4557 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4558 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4559 | #ifdef _SC_CAP |
| 4560 | {"SC_CAP", _SC_CAP}, |
| 4561 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4562 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4563 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4564 | #endif |
| 4565 | #ifdef _SC_CHAR_BIT |
| 4566 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4567 | #endif |
| 4568 | #ifdef _SC_CHAR_MAX |
| 4569 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4570 | #endif |
| 4571 | #ifdef _SC_CHAR_MIN |
| 4572 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4573 | #endif |
| 4574 | #ifdef _SC_CHILD_MAX |
| 4575 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4576 | #endif |
| 4577 | #ifdef _SC_CLK_TCK |
| 4578 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4579 | #endif |
| 4580 | #ifdef _SC_COHER_BLKSZ |
| 4581 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4582 | #endif |
| 4583 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4584 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4585 | #endif |
| 4586 | #ifdef _SC_DCACHE_ASSOC |
| 4587 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4588 | #endif |
| 4589 | #ifdef _SC_DCACHE_BLKSZ |
| 4590 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4591 | #endif |
| 4592 | #ifdef _SC_DCACHE_LINESZ |
| 4593 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4594 | #endif |
| 4595 | #ifdef _SC_DCACHE_SZ |
| 4596 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 4597 | #endif |
| 4598 | #ifdef _SC_DCACHE_TBLKSZ |
| 4599 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 4600 | #endif |
| 4601 | #ifdef _SC_DELAYTIMER_MAX |
| 4602 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 4603 | #endif |
| 4604 | #ifdef _SC_EQUIV_CLASS_MAX |
| 4605 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 4606 | #endif |
| 4607 | #ifdef _SC_EXPR_NEST_MAX |
| 4608 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 4609 | #endif |
| 4610 | #ifdef _SC_FSYNC |
| 4611 | {"SC_FSYNC", _SC_FSYNC}, |
| 4612 | #endif |
| 4613 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 4614 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 4615 | #endif |
| 4616 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 4617 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 4618 | #endif |
| 4619 | #ifdef _SC_ICACHE_ASSOC |
| 4620 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 4621 | #endif |
| 4622 | #ifdef _SC_ICACHE_BLKSZ |
| 4623 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 4624 | #endif |
| 4625 | #ifdef _SC_ICACHE_LINESZ |
| 4626 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 4627 | #endif |
| 4628 | #ifdef _SC_ICACHE_SZ |
| 4629 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 4630 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4631 | #ifdef _SC_INF |
| 4632 | {"SC_INF", _SC_INF}, |
| 4633 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4634 | #ifdef _SC_INT_MAX |
| 4635 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 4636 | #endif |
| 4637 | #ifdef _SC_INT_MIN |
| 4638 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 4639 | #endif |
| 4640 | #ifdef _SC_IOV_MAX |
| 4641 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 4642 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4643 | #ifdef _SC_IP_SECOPTS |
| 4644 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 4645 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4646 | #ifdef _SC_JOB_CONTROL |
| 4647 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 4648 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4649 | #ifdef _SC_KERN_POINTERS |
| 4650 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 4651 | #endif |
| 4652 | #ifdef _SC_KERN_SIM |
| 4653 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 4654 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4655 | #ifdef _SC_LINE_MAX |
| 4656 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 4657 | #endif |
| 4658 | #ifdef _SC_LOGIN_NAME_MAX |
| 4659 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 4660 | #endif |
| 4661 | #ifdef _SC_LOGNAME_MAX |
| 4662 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 4663 | #endif |
| 4664 | #ifdef _SC_LONG_BIT |
| 4665 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 4666 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4667 | #ifdef _SC_MAC |
| 4668 | {"SC_MAC", _SC_MAC}, |
| 4669 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4670 | #ifdef _SC_MAPPED_FILES |
| 4671 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 4672 | #endif |
| 4673 | #ifdef _SC_MAXPID |
| 4674 | {"SC_MAXPID", _SC_MAXPID}, |
| 4675 | #endif |
| 4676 | #ifdef _SC_MB_LEN_MAX |
| 4677 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 4678 | #endif |
| 4679 | #ifdef _SC_MEMLOCK |
| 4680 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 4681 | #endif |
| 4682 | #ifdef _SC_MEMLOCK_RANGE |
| 4683 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 4684 | #endif |
| 4685 | #ifdef _SC_MEMORY_PROTECTION |
| 4686 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 4687 | #endif |
| 4688 | #ifdef _SC_MESSAGE_PASSING |
| 4689 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 4690 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4691 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 4692 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 4693 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4694 | #ifdef _SC_MQ_OPEN_MAX |
| 4695 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 4696 | #endif |
| 4697 | #ifdef _SC_MQ_PRIO_MAX |
| 4698 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 4699 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4700 | #ifdef _SC_NACLS_MAX |
| 4701 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 4702 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4703 | #ifdef _SC_NGROUPS_MAX |
| 4704 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 4705 | #endif |
| 4706 | #ifdef _SC_NL_ARGMAX |
| 4707 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 4708 | #endif |
| 4709 | #ifdef _SC_NL_LANGMAX |
| 4710 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 4711 | #endif |
| 4712 | #ifdef _SC_NL_MSGMAX |
| 4713 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 4714 | #endif |
| 4715 | #ifdef _SC_NL_NMAX |
| 4716 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 4717 | #endif |
| 4718 | #ifdef _SC_NL_SETMAX |
| 4719 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 4720 | #endif |
| 4721 | #ifdef _SC_NL_TEXTMAX |
| 4722 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 4723 | #endif |
| 4724 | #ifdef _SC_NPROCESSORS_CONF |
| 4725 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 4726 | #endif |
| 4727 | #ifdef _SC_NPROCESSORS_ONLN |
| 4728 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 4729 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4730 | #ifdef _SC_NPROC_CONF |
| 4731 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 4732 | #endif |
| 4733 | #ifdef _SC_NPROC_ONLN |
| 4734 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 4735 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4736 | #ifdef _SC_NZERO |
| 4737 | {"SC_NZERO", _SC_NZERO}, |
| 4738 | #endif |
| 4739 | #ifdef _SC_OPEN_MAX |
| 4740 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 4741 | #endif |
| 4742 | #ifdef _SC_PAGESIZE |
| 4743 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 4744 | #endif |
| 4745 | #ifdef _SC_PAGE_SIZE |
| 4746 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 4747 | #endif |
| 4748 | #ifdef _SC_PASS_MAX |
| 4749 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 4750 | #endif |
| 4751 | #ifdef _SC_PHYS_PAGES |
| 4752 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 4753 | #endif |
| 4754 | #ifdef _SC_PII |
| 4755 | {"SC_PII", _SC_PII}, |
| 4756 | #endif |
| 4757 | #ifdef _SC_PII_INTERNET |
| 4758 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 4759 | #endif |
| 4760 | #ifdef _SC_PII_INTERNET_DGRAM |
| 4761 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 4762 | #endif |
| 4763 | #ifdef _SC_PII_INTERNET_STREAM |
| 4764 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 4765 | #endif |
| 4766 | #ifdef _SC_PII_OSI |
| 4767 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 4768 | #endif |
| 4769 | #ifdef _SC_PII_OSI_CLTS |
| 4770 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 4771 | #endif |
| 4772 | #ifdef _SC_PII_OSI_COTS |
| 4773 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 4774 | #endif |
| 4775 | #ifdef _SC_PII_OSI_M |
| 4776 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 4777 | #endif |
| 4778 | #ifdef _SC_PII_SOCKET |
| 4779 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 4780 | #endif |
| 4781 | #ifdef _SC_PII_XTI |
| 4782 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 4783 | #endif |
| 4784 | #ifdef _SC_POLL |
| 4785 | {"SC_POLL", _SC_POLL}, |
| 4786 | #endif |
| 4787 | #ifdef _SC_PRIORITIZED_IO |
| 4788 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 4789 | #endif |
| 4790 | #ifdef _SC_PRIORITY_SCHEDULING |
| 4791 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 4792 | #endif |
| 4793 | #ifdef _SC_REALTIME_SIGNALS |
| 4794 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 4795 | #endif |
| 4796 | #ifdef _SC_RE_DUP_MAX |
| 4797 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 4798 | #endif |
| 4799 | #ifdef _SC_RTSIG_MAX |
| 4800 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 4801 | #endif |
| 4802 | #ifdef _SC_SAVED_IDS |
| 4803 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 4804 | #endif |
| 4805 | #ifdef _SC_SCHAR_MAX |
| 4806 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 4807 | #endif |
| 4808 | #ifdef _SC_SCHAR_MIN |
| 4809 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 4810 | #endif |
| 4811 | #ifdef _SC_SELECT |
| 4812 | {"SC_SELECT", _SC_SELECT}, |
| 4813 | #endif |
| 4814 | #ifdef _SC_SEMAPHORES |
| 4815 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 4816 | #endif |
| 4817 | #ifdef _SC_SEM_NSEMS_MAX |
| 4818 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 4819 | #endif |
| 4820 | #ifdef _SC_SEM_VALUE_MAX |
| 4821 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 4822 | #endif |
| 4823 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 4824 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 4825 | #endif |
| 4826 | #ifdef _SC_SHRT_MAX |
| 4827 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 4828 | #endif |
| 4829 | #ifdef _SC_SHRT_MIN |
| 4830 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 4831 | #endif |
| 4832 | #ifdef _SC_SIGQUEUE_MAX |
| 4833 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 4834 | #endif |
| 4835 | #ifdef _SC_SIGRT_MAX |
| 4836 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 4837 | #endif |
| 4838 | #ifdef _SC_SIGRT_MIN |
| 4839 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 4840 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4841 | #ifdef _SC_SOFTPOWER |
| 4842 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 4843 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4844 | #ifdef _SC_SPLIT_CACHE |
| 4845 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 4846 | #endif |
| 4847 | #ifdef _SC_SSIZE_MAX |
| 4848 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 4849 | #endif |
| 4850 | #ifdef _SC_STACK_PROT |
| 4851 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 4852 | #endif |
| 4853 | #ifdef _SC_STREAM_MAX |
| 4854 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 4855 | #endif |
| 4856 | #ifdef _SC_SYNCHRONIZED_IO |
| 4857 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 4858 | #endif |
| 4859 | #ifdef _SC_THREADS |
| 4860 | {"SC_THREADS", _SC_THREADS}, |
| 4861 | #endif |
| 4862 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 4863 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 4864 | #endif |
| 4865 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 4866 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 4867 | #endif |
| 4868 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 4869 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 4870 | #endif |
| 4871 | #ifdef _SC_THREAD_KEYS_MAX |
| 4872 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 4873 | #endif |
| 4874 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 4875 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 4876 | #endif |
| 4877 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 4878 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 4879 | #endif |
| 4880 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 4881 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 4882 | #endif |
| 4883 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 4884 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 4885 | #endif |
| 4886 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 4887 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 4888 | #endif |
| 4889 | #ifdef _SC_THREAD_STACK_MIN |
| 4890 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 4891 | #endif |
| 4892 | #ifdef _SC_THREAD_THREADS_MAX |
| 4893 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 4894 | #endif |
| 4895 | #ifdef _SC_TIMERS |
| 4896 | {"SC_TIMERS", _SC_TIMERS}, |
| 4897 | #endif |
| 4898 | #ifdef _SC_TIMER_MAX |
| 4899 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 4900 | #endif |
| 4901 | #ifdef _SC_TTY_NAME_MAX |
| 4902 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 4903 | #endif |
| 4904 | #ifdef _SC_TZNAME_MAX |
| 4905 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 4906 | #endif |
| 4907 | #ifdef _SC_T_IOV_MAX |
| 4908 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 4909 | #endif |
| 4910 | #ifdef _SC_UCHAR_MAX |
| 4911 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 4912 | #endif |
| 4913 | #ifdef _SC_UINT_MAX |
| 4914 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 4915 | #endif |
| 4916 | #ifdef _SC_UIO_MAXIOV |
| 4917 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 4918 | #endif |
| 4919 | #ifdef _SC_ULONG_MAX |
| 4920 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 4921 | #endif |
| 4922 | #ifdef _SC_USHRT_MAX |
| 4923 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 4924 | #endif |
| 4925 | #ifdef _SC_VERSION |
| 4926 | {"SC_VERSION", _SC_VERSION}, |
| 4927 | #endif |
| 4928 | #ifdef _SC_WORD_BIT |
| 4929 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 4930 | #endif |
| 4931 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 4932 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 4933 | #endif |
| 4934 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 4935 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 4936 | #endif |
| 4937 | #ifdef _SC_XBS5_LP64_OFF64 |
| 4938 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 4939 | #endif |
| 4940 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 4941 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 4942 | #endif |
| 4943 | #ifdef _SC_XOPEN_CRYPT |
| 4944 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 4945 | #endif |
| 4946 | #ifdef _SC_XOPEN_ENH_I18N |
| 4947 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 4948 | #endif |
| 4949 | #ifdef _SC_XOPEN_LEGACY |
| 4950 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 4951 | #endif |
| 4952 | #ifdef _SC_XOPEN_REALTIME |
| 4953 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 4954 | #endif |
| 4955 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 4956 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 4957 | #endif |
| 4958 | #ifdef _SC_XOPEN_SHM |
| 4959 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 4960 | #endif |
| 4961 | #ifdef _SC_XOPEN_UNIX |
| 4962 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 4963 | #endif |
| 4964 | #ifdef _SC_XOPEN_VERSION |
| 4965 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 4966 | #endif |
| 4967 | #ifdef _SC_XOPEN_XCU_VERSION |
| 4968 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 4969 | #endif |
| 4970 | #ifdef _SC_XOPEN_XPG2 |
| 4971 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 4972 | #endif |
| 4973 | #ifdef _SC_XOPEN_XPG3 |
| 4974 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 4975 | #endif |
| 4976 | #ifdef _SC_XOPEN_XPG4 |
| 4977 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 4978 | #endif |
| 4979 | }; |
| 4980 | |
| 4981 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4982 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4983 | { |
| 4984 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 4985 | sizeof(posix_constants_sysconf) |
| 4986 | / sizeof(struct constdef)); |
| 4987 | } |
| 4988 | |
| 4989 | static char posix_sysconf__doc__[] = "\ |
| 4990 | sysconf(name) -> integer\n\ |
| 4991 | Return an integer-valued system configuration variable."; |
| 4992 | |
| 4993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4994 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4995 | { |
| 4996 | PyObject *result = NULL; |
| 4997 | int name; |
| 4998 | |
| 4999 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5000 | int value; |
| 5001 | |
| 5002 | errno = 0; |
| 5003 | value = sysconf(name); |
| 5004 | if (value == -1 && errno != 0) |
| 5005 | posix_error(); |
| 5006 | else |
| 5007 | result = PyInt_FromLong(value); |
| 5008 | } |
| 5009 | return result; |
| 5010 | } |
| 5011 | #endif |
| 5012 | |
| 5013 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5014 | /* This code is used to ensure that the tables of configuration value names |
| 5015 | * are in sorted order as required by conv_confname(), and also to build the |
| 5016 | * the exported dictionaries that are used to publish information about the |
| 5017 | * names available on the host platform. |
| 5018 | * |
| 5019 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5020 | * when used, even for platforms we're not able to test on. It also makes |
| 5021 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5022 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5023 | |
| 5024 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5025 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5026 | { |
| 5027 | const struct constdef *c1 = |
| 5028 | (const struct constdef *) v1; |
| 5029 | const struct constdef *c2 = |
| 5030 | (const struct constdef *) v2; |
| 5031 | |
| 5032 | return strcmp(c1->name, c2->name); |
| 5033 | } |
| 5034 | |
| 5035 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5036 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5037 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5038 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5039 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5040 | size_t i; |
| 5041 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5042 | |
| 5043 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5044 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5045 | if (d == NULL) |
| 5046 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5047 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5048 | for (i=0; i < tablesize; ++i) { |
| 5049 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5050 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5051 | Py_XDECREF(o); |
| 5052 | Py_DECREF(d); |
| 5053 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5054 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5055 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5056 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5057 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5058 | Py_DECREF(d); |
| 5059 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5062 | /* Return -1 on failure, 0 on success. */ |
| 5063 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5064 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5065 | { |
| 5066 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5067 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5068 | sizeof(posix_constants_pathconf) |
| 5069 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5070 | "pathconf_names", moddict)) |
| 5071 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5072 | #endif |
| 5073 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5074 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5075 | sizeof(posix_constants_confstr) |
| 5076 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5077 | "confstr_names", moddict)) |
| 5078 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5079 | #endif |
| 5080 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5081 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5082 | sizeof(posix_constants_sysconf) |
| 5083 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5084 | "sysconf_names", moddict)) |
| 5085 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5086 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5087 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5088 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5089 | |
| 5090 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5091 | static char posix_abort__doc__[] = "\ |
| 5092 | abort() -> does not return!\n\ |
| 5093 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5094 | in the hardest way possible on the hosting operating system."; |
| 5095 | |
| 5096 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5097 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5098 | { |
| 5099 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5100 | return NULL; |
| 5101 | abort(); |
| 5102 | /*NOTREACHED*/ |
| 5103 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5104 | return NULL; |
| 5105 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5106 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5107 | |
| 5108 | static PyMethodDef posix_methods[] = { |
| 5109 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5110 | #ifdef HAVE_TTYNAME |
| 5111 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5112 | #endif |
| 5113 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5114 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5115 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5116 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5117 | #endif /* HAVE_CHOWN */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5118 | #ifdef HAVE_CTERMID |
| 5119 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5120 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5121 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5122 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5123 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5124 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5125 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5126 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5127 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5128 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5129 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5130 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5131 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5132 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5133 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5134 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5135 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5136 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5137 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5138 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5139 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5140 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5141 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5142 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5143 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5144 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5145 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5146 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5147 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5148 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5149 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5150 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5151 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5152 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5153 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5154 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5155 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5156 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5157 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5158 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5159 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5160 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5161 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5162 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5163 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5164 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5165 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5166 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5167 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5168 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5169 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5170 | #ifdef HAVE_FORKPTY |
| 5171 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5172 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5173 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5174 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5175 | #endif /* HAVE_GETEGID */ |
| 5176 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5177 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5178 | #endif /* HAVE_GETEUID */ |
| 5179 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5180 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5181 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5182 | #ifdef HAVE_GETGROUPS |
| 5183 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5184 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5185 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5186 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5187 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5188 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5189 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5190 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5191 | #endif /* HAVE_GETPPID */ |
| 5192 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5193 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5194 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5195 | #ifdef HAVE_GETLOGIN |
| 5196 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5197 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5198 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5199 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5200 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5201 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5202 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5203 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5204 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5205 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5206 | #ifdef MS_WIN32 |
| 5207 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5208 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5209 | {"popen4", win32_popen4, METH_VARARGS}, |
| 5210 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5211 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5212 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5213 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5214 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5215 | #ifdef HAVE_SETEUID |
| 5216 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5217 | #endif /* HAVE_SETEUID */ |
| 5218 | #ifdef HAVE_SETEGID |
| 5219 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5220 | #endif /* HAVE_SETEGID */ |
| 5221 | #ifdef HAVE_SETREUID |
| 5222 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5223 | #endif /* HAVE_SETREUID */ |
| 5224 | #ifdef HAVE_SETREGID |
| 5225 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5226 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5227 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5228 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5229 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5230 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5231 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5232 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5233 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5234 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5235 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5236 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5237 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5238 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5239 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5240 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5241 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5242 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5243 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5244 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5245 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5246 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5247 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5248 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5249 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5250 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5251 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5252 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5253 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5254 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5255 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5256 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5257 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5258 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5259 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5260 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5261 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5262 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5263 | #endif |
| 5264 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5265 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5266 | #endif |
| 5267 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5268 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5269 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5270 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5271 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5272 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5273 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5274 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5275 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5276 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5277 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5278 | #endif |
| 5279 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5280 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5281 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5282 | #ifdef HAVE_SYS_WAIT_H |
| 5283 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5284 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5285 | #endif /* WIFSTOPPED */ |
| 5286 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5287 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5288 | #endif /* WIFSIGNALED */ |
| 5289 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5290 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5291 | #endif /* WIFEXITED */ |
| 5292 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5293 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5294 | #endif /* WEXITSTATUS */ |
| 5295 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5296 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5297 | #endif /* WTERMSIG */ |
| 5298 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5299 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5300 | #endif /* WSTOPSIG */ |
| 5301 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5302 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5303 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5304 | #endif |
| 5305 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5306 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5307 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5308 | #ifdef HAVE_TMPNAM |
| 5309 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5310 | #endif |
| 5311 | #ifdef HAVE_TEMPNAM |
| 5312 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5313 | #endif |
| 5314 | #ifdef HAVE_TMPNAM |
| 5315 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5316 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5317 | #ifdef HAVE_CONFSTR |
| 5318 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5319 | #endif |
| 5320 | #ifdef HAVE_SYSCONF |
| 5321 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5322 | #endif |
| 5323 | #ifdef HAVE_FPATHCONF |
| 5324 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5325 | #endif |
| 5326 | #ifdef HAVE_PATHCONF |
| 5327 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5328 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5329 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5330 | {NULL, NULL} /* Sentinel */ |
| 5331 | }; |
| 5332 | |
| 5333 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5334 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5335 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5336 | { |
| 5337 | PyObject* v = PyInt_FromLong(value); |
| 5338 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5339 | return -1; /* triggers fatal error */ |
| 5340 | |
| 5341 | Py_DECREF(v); |
| 5342 | return 0; |
| 5343 | } |
| 5344 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5345 | #if defined(PYOS_OS2) |
| 5346 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5347 | static int insertvalues(PyObject *d) |
| 5348 | { |
| 5349 | APIRET rc; |
| 5350 | ULONG values[QSV_MAX+1]; |
| 5351 | PyObject *v; |
| 5352 | char *ver, tmp[10]; |
| 5353 | |
| 5354 | Py_BEGIN_ALLOW_THREADS |
| 5355 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5356 | Py_END_ALLOW_THREADS |
| 5357 | |
| 5358 | if (rc != NO_ERROR) { |
| 5359 | os2_error(rc); |
| 5360 | return -1; |
| 5361 | } |
| 5362 | |
| 5363 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5364 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5365 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5366 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5367 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5368 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5369 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5370 | |
| 5371 | switch (values[QSV_VERSION_MINOR]) { |
| 5372 | case 0: ver = "2.00"; break; |
| 5373 | case 10: ver = "2.10"; break; |
| 5374 | case 11: ver = "2.11"; break; |
| 5375 | case 30: ver = "3.00"; break; |
| 5376 | case 40: ver = "4.00"; break; |
| 5377 | case 50: ver = "5.00"; break; |
| 5378 | default: |
| 5379 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 5380 | values[QSV_VERSION_MINOR]); |
| 5381 | ver = &tmp[0]; |
| 5382 | } |
| 5383 | |
| 5384 | /* Add Indicator of the Version of the Operating System */ |
| 5385 | v = PyString_FromString(ver); |
| 5386 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5387 | return -1; |
| 5388 | Py_DECREF(v); |
| 5389 | |
| 5390 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5391 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5392 | tmp[1] = ':'; |
| 5393 | tmp[2] = '\0'; |
| 5394 | |
| 5395 | v = PyString_FromString(tmp); |
| 5396 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5397 | return -1; |
| 5398 | Py_DECREF(v); |
| 5399 | |
| 5400 | return 0; |
| 5401 | } |
| 5402 | #endif |
| 5403 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5404 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5405 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5406 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5407 | #ifdef F_OK |
| 5408 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5409 | #endif |
| 5410 | #ifdef R_OK |
| 5411 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5412 | #endif |
| 5413 | #ifdef W_OK |
| 5414 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5415 | #endif |
| 5416 | #ifdef X_OK |
| 5417 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5418 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5419 | #ifdef NGROUPS_MAX |
| 5420 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5421 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5422 | #ifdef TMP_MAX |
| 5423 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5424 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5425 | #ifdef WNOHANG |
| 5426 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5427 | #endif |
| 5428 | #ifdef O_RDONLY |
| 5429 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5430 | #endif |
| 5431 | #ifdef O_WRONLY |
| 5432 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5433 | #endif |
| 5434 | #ifdef O_RDWR |
| 5435 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5436 | #endif |
| 5437 | #ifdef O_NDELAY |
| 5438 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5439 | #endif |
| 5440 | #ifdef O_NONBLOCK |
| 5441 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5442 | #endif |
| 5443 | #ifdef O_APPEND |
| 5444 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5445 | #endif |
| 5446 | #ifdef O_DSYNC |
| 5447 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5448 | #endif |
| 5449 | #ifdef O_RSYNC |
| 5450 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5451 | #endif |
| 5452 | #ifdef O_SYNC |
| 5453 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5454 | #endif |
| 5455 | #ifdef O_NOCTTY |
| 5456 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5457 | #endif |
| 5458 | #ifdef O_CREAT |
| 5459 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5460 | #endif |
| 5461 | #ifdef O_EXCL |
| 5462 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5463 | #endif |
| 5464 | #ifdef O_TRUNC |
| 5465 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5466 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5467 | #ifdef O_BINARY |
| 5468 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5469 | #endif |
| 5470 | #ifdef O_TEXT |
| 5471 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5472 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5473 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5474 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5475 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5476 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5477 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5478 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5479 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5480 | #endif |
| 5481 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5482 | #if defined(PYOS_OS2) |
| 5483 | if (insertvalues(d)) return -1; |
| 5484 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5485 | return 0; |
| 5486 | } |
| 5487 | |
| 5488 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5489 | #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5490 | #define INITFUNC initnt |
| 5491 | #define MODNAME "nt" |
| 5492 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5493 | #if defined(PYOS_OS2) |
| 5494 | #define INITFUNC initos2 |
| 5495 | #define MODNAME "os2" |
| 5496 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5497 | #define INITFUNC initposix |
| 5498 | #define MODNAME "posix" |
| 5499 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5500 | #endif |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5501 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5502 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5503 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5504 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5505 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5506 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5507 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5508 | posix_methods, |
| 5509 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5510 | (PyObject *)NULL, |
| 5511 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5512 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5513 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5514 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5515 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5516 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5517 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5518 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5519 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5520 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5521 | return; |
| 5522 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5523 | if (setup_confname_tables(d)) |
| 5524 | return; |
| 5525 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5526 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5527 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5528 | #ifdef HAVE_PUTENV |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5529 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5530 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5531 | } |