Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* POSIX module implementation */ |
| 12 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 13 | /* 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] | 14 | actually calls itself 'nt', not 'posix', and a few functions are |
| 15 | either unimplemented or implemented differently. The source |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 16 | 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] | 17 | of the compiler used. Different compilers define their own feature |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 18 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 20 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 22 | static char posix__doc__ [] = |
| 23 | "This module provides access to operating system functionality that is\n\ |
| 24 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 25 | disguised Unix interface). Refer to the library manual and\n\ |
| 26 | corresponding Unix manual entries for more information on calls."; |
| 27 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 30 | #if defined(PYOS_OS2) |
| 31 | #define INCL_DOS |
| 32 | #define INCL_DOSERRORS |
| 33 | #define INCL_DOSPROCESS |
| 34 | #define INCL_NOPMAPI |
| 35 | #include <os2.h> |
| 36 | #endif |
| 37 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 40 | #ifdef HAVE_SYS_WAIT_H |
| 41 | #include <sys/wait.h> /* For WNOHANG */ |
| 42 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 44 | #ifdef HAVE_SIGNAL_H |
| 45 | #include <signal.h> |
| 46 | #endif |
| 47 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #ifdef HAVE_FCNTL_H |
| 49 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 50 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 52 | /* Various compilers have only certain posix functions */ |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 53 | /* XXX Gosh I wish these were all moved into config.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 54 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 55 | #include <process.h> |
| 56 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 57 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 58 | #define HAVE_GETCWD 1 |
| 59 | #define HAVE_OPENDIR 1 |
| 60 | #define HAVE_SYSTEM 1 |
| 61 | #if defined(__OS2__) |
| 62 | #define HAVE_EXECV 1 |
| 63 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 64 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 65 | #include <process.h> |
| 66 | #else |
| 67 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 68 | #define HAVE_EXECV 1 |
| 69 | #define HAVE_GETCWD 1 |
| 70 | #define HAVE_GETEGID 1 |
| 71 | #define HAVE_GETEUID 1 |
| 72 | #define HAVE_GETGID 1 |
| 73 | #define HAVE_GETPPID 1 |
| 74 | #define HAVE_GETUID 1 |
| 75 | #define HAVE_KILL 1 |
| 76 | #define HAVE_OPENDIR 1 |
| 77 | #define HAVE_PIPE 1 |
| 78 | #define HAVE_POPEN 1 |
| 79 | #define HAVE_SYSTEM 1 |
| 80 | #define HAVE_WAIT 1 |
| 81 | #else |
| 82 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 83 | #define HAVE_GETCWD 1 |
| 84 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 85 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 86 | #define HAVE_EXECV 1 |
| 87 | #define HAVE_PIPE 1 |
| 88 | #define HAVE_POPEN 1 |
| 89 | #define HAVE_SYSTEM 1 |
| 90 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 91 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 92 | #else /* all other compilers */ |
| 93 | /* Unix functions that the configure script doesn't check for */ |
| 94 | #define HAVE_EXECV 1 |
| 95 | #define HAVE_FORK 1 |
| 96 | #define HAVE_GETCWD 1 |
| 97 | #define HAVE_GETEGID 1 |
| 98 | #define HAVE_GETEUID 1 |
| 99 | #define HAVE_GETGID 1 |
| 100 | #define HAVE_GETPPID 1 |
| 101 | #define HAVE_GETUID 1 |
| 102 | #define HAVE_KILL 1 |
| 103 | #define HAVE_OPENDIR 1 |
| 104 | #define HAVE_PIPE 1 |
| 105 | #define HAVE_POPEN 1 |
| 106 | #define HAVE_SYSTEM 1 |
| 107 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 108 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 109 | #endif /* _MSC_VER */ |
| 110 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 111 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 112 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 114 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 116 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 117 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | #ifdef NeXT |
| 121 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 122 | #undef HAVE_UNISTD_H |
| 123 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 124 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 125 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 126 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 129 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 130 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 131 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 132 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 133 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 134 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 135 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 136 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 138 | #endif |
| 139 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 140 | extern int chdir(char *); |
| 141 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 142 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 143 | extern int chdir(const char *); |
| 144 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 145 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 146 | extern int chmod(const char *, mode_t); |
| 147 | extern int chown(const char *, uid_t, gid_t); |
| 148 | extern char *getcwd(char *, int); |
| 149 | extern char *strerror(int); |
| 150 | extern int link(const char *, const char *); |
| 151 | extern int rename(const char *, const char *); |
| 152 | extern int stat(const char *, struct stat *); |
| 153 | extern int unlink(const char *); |
| 154 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 156 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 157 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 158 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 159 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 160 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 161 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 163 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #ifdef HAVE_UTIME_H |
| 166 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 167 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 169 | #ifdef HAVE_SYS_UTIME_H |
| 170 | #include <sys/utime.h> |
| 171 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 172 | #endif /* HAVE_SYS_UTIME_H */ |
| 173 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | #ifdef HAVE_SYS_TIMES_H |
| 175 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 176 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | |
| 178 | #ifdef HAVE_SYS_PARAM_H |
| 179 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 180 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | |
| 182 | #ifdef HAVE_SYS_UTSNAME_H |
| 183 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 184 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | |
| 186 | #ifndef MAXPATHLEN |
| 187 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 188 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 190 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 192 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 193 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 194 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 195 | #include <direct.h> |
| 196 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 197 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 199 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 201 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 203 | #endif |
| 204 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 206 | #endif |
| 207 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 209 | #endif |
| 210 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 212 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | #include <direct.h> |
| 214 | #include <io.h> |
| 215 | #include <process.h> |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 216 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 218 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 219 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 220 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 221 | #else /* 16-bit Windows */ |
| 222 | #include <dos.h> |
| 223 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 224 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 227 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 229 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 231 | #ifdef UNION_WAIT |
| 232 | /* Emulate some macros on systems that have a union instead of macros */ |
| 233 | |
| 234 | #ifndef WIFEXITED |
| 235 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 236 | #endif |
| 237 | |
| 238 | #ifndef WEXITSTATUS |
| 239 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 240 | #endif |
| 241 | |
| 242 | #ifndef WTERMSIG |
| 243 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 244 | #endif |
| 245 | |
| 246 | #endif /* UNION_WAIT */ |
| 247 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 248 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 249 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 250 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 251 | #define USE_CTERMID_R |
| 252 | #endif |
| 253 | |
| 254 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 255 | #define USE_TMPNAM_R |
| 256 | #endif |
| 257 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 258 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 259 | #undef STAT |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 260 | #ifdef MS_WIN64 |
| 261 | # define STAT _stati64 |
| 262 | # define FSTAT _fstati64 |
| 263 | # define STRUCT_STAT struct _stati64 |
| 264 | #else |
| 265 | # define STAT stat |
| 266 | # define FSTAT fstat |
| 267 | # define STRUCT_STAT struct stat |
| 268 | #endif |
| 269 | |
| 270 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 272 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 273 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 275 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 277 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 278 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 279 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 280 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 281 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 282 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | if (d == NULL) |
| 284 | return NULL; |
| 285 | if (environ == NULL) |
| 286 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 287 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 289 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 290 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | char *p = strchr(*e, '='); |
| 292 | if (p == NULL) |
| 293 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 294 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 295 | if (k == NULL) { |
| 296 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 298 | } |
| 299 | v = PyString_FromString(p+1); |
| 300 | if (v == NULL) { |
| 301 | PyErr_Clear(); |
| 302 | Py_DECREF(k); |
| 303 | continue; |
| 304 | } |
| 305 | if (PyDict_GetItem(d, k) == NULL) { |
| 306 | if (PyDict_SetItem(d, k, v) != 0) |
| 307 | PyErr_Clear(); |
| 308 | } |
| 309 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 310 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 312 | #if defined(PYOS_OS2) |
| 313 | { |
| 314 | APIRET rc; |
| 315 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 316 | |
| 317 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 318 | 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] | 319 | PyObject *v = PyString_FromString(buffer); |
| 320 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 321 | Py_DECREF(v); |
| 322 | } |
| 323 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 324 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 325 | PyObject *v = PyString_FromString(buffer); |
| 326 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 327 | Py_DECREF(v); |
| 328 | } |
| 329 | } |
| 330 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | return d; |
| 332 | } |
| 333 | |
| 334 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 336 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 337 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 338 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 339 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 340 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 342 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 343 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 344 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 345 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 348 | #ifdef MS_WIN32 |
| 349 | static PyObject * |
| 350 | win32_error(char* function, char* filename) |
| 351 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 352 | /* XXX We should pass the function name along in the future. |
| 353 | (_winreg.c also wants to pass the function name.) |
| 354 | This would however require an additional param to the |
| 355 | Windows error object, which is non-trivial. |
| 356 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 357 | errno = GetLastError(); |
| 358 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 359 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 360 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 361 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 362 | } |
| 363 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 365 | #if defined(PYOS_OS2) |
| 366 | /********************************************************************** |
| 367 | * Helper Function to Trim and Format OS/2 Messages |
| 368 | **********************************************************************/ |
| 369 | static void |
| 370 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 371 | { |
| 372 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 373 | |
| 374 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 375 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 376 | |
| 377 | while (lastc > msgbuf && isspace(*lastc)) |
| 378 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 379 | } |
| 380 | |
| 381 | /* Add Optional Reason Text */ |
| 382 | if (reason) { |
| 383 | strcat(msgbuf, " : "); |
| 384 | strcat(msgbuf, reason); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /********************************************************************** |
| 389 | * Decode an OS/2 Operating System Error Code |
| 390 | * |
| 391 | * A convenience function to lookup an OS/2 error code and return a |
| 392 | * text message we can use to raise a Python exception. |
| 393 | * |
| 394 | * Notes: |
| 395 | * The messages for errors returned from the OS/2 kernel reside in |
| 396 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 397 | * |
| 398 | **********************************************************************/ |
| 399 | static char * |
| 400 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 401 | { |
| 402 | APIRET rc; |
| 403 | ULONG msglen; |
| 404 | |
| 405 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 406 | Py_BEGIN_ALLOW_THREADS |
| 407 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 408 | errorcode, "oso001.msg", &msglen); |
| 409 | Py_END_ALLOW_THREADS |
| 410 | |
| 411 | if (rc == NO_ERROR) |
| 412 | os2_formatmsg(msgbuf, msglen, reason); |
| 413 | else |
| 414 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 415 | |
| 416 | return msgbuf; |
| 417 | } |
| 418 | |
| 419 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 420 | errors are not in a global variable e.g. 'errno' nor are |
| 421 | they congruent with posix error numbers. */ |
| 422 | |
| 423 | static PyObject * os2_error(int code) |
| 424 | { |
| 425 | char text[1024]; |
| 426 | PyObject *v; |
| 427 | |
| 428 | os2_strerror(text, sizeof(text), code, ""); |
| 429 | |
| 430 | v = Py_BuildValue("(is)", code, text); |
| 431 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 432 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 433 | Py_DECREF(v); |
| 434 | } |
| 435 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 436 | } |
| 437 | |
| 438 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | |
| 440 | /* POSIX generic methods */ |
| 441 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 442 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 443 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 444 | { |
| 445 | int fd; |
| 446 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 447 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 448 | return NULL; |
| 449 | Py_BEGIN_ALLOW_THREADS |
| 450 | res = (*func)(fd); |
| 451 | Py_END_ALLOW_THREADS |
| 452 | if (res < 0) |
| 453 | return posix_error(); |
| 454 | Py_INCREF(Py_None); |
| 455 | return Py_None; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 460 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 462 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 463 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 464 | if (!PyArg_ParseTuple(args, format, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 465 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 466 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 467 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 468 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 469 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 470 | return posix_error_with_filename(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 471 | Py_INCREF(Py_None); |
| 472 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 475 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 476 | posix_2str(PyObject *args, char *format, |
| 477 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 478 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 479 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 480 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 481 | if (!PyArg_ParseTuple(args, format, &path1, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 482 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 483 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 484 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 485 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 486 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 487 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 488 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 489 | Py_INCREF(Py_None); |
| 490 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 493 | /* pack a system stat C structure into the Python stat tuple |
| 494 | (used by posix_stat() and posix_fstat()) */ |
| 495 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 496 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 497 | { |
| 498 | PyObject *v = PyTuple_New(10); |
| 499 | if (v == NULL) |
| 500 | return NULL; |
| 501 | |
| 502 | PyTuple_SetItem(v, 0, PyInt_FromLong((long)st.st_mode)); |
| 503 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 504 | PyTuple_SetItem(v, 1, PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
| 505 | #else |
| 506 | PyTuple_SetItem(v, 1, PyInt_FromLong((long)st.st_ino)); |
| 507 | #endif |
| 508 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
| 509 | PyTuple_SetItem(v, 2, PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
| 510 | #else |
| 511 | PyTuple_SetItem(v, 2, PyInt_FromLong((long)st.st_dev)); |
| 512 | #endif |
| 513 | PyTuple_SetItem(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 514 | PyTuple_SetItem(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 515 | PyTuple_SetItem(v, 5, PyInt_FromLong((long)st.st_gid)); |
| 516 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 517 | PyTuple_SetItem(v, 6, PyLong_FromLongLong((LONG_LONG)st.st_size)); |
| 518 | #else |
| 519 | PyTuple_SetItem(v, 6, PyInt_FromLong(st.st_size)); |
| 520 | #endif |
| 521 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 522 | PyTuple_SetItem(v, 7, PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 523 | PyTuple_SetItem(v, 8, PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 524 | PyTuple_SetItem(v, 9, PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
| 525 | #else |
| 526 | PyTuple_SetItem(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 527 | PyTuple_SetItem(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 528 | PyTuple_SetItem(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 529 | #endif |
| 530 | |
| 531 | if (PyErr_Occurred()) { |
| 532 | Py_DECREF(v); |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | return v; |
| 537 | } |
| 538 | |
| 539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 541 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 542 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 544 | STRUCT_STAT st; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 545 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 546 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 547 | |
| 548 | #ifdef MS_WIN32 |
| 549 | int pathlen; |
| 550 | char pathcopy[MAX_PATH]; |
| 551 | #endif /* MS_WIN32 */ |
| 552 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 553 | if (!PyArg_ParseTuple(args, format, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 554 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 555 | |
| 556 | #ifdef MS_WIN32 |
| 557 | pathlen = strlen(path); |
| 558 | /* the library call can blow up if the file name is too long! */ |
| 559 | if (pathlen > MAX_PATH) { |
| 560 | errno = ENAMETOOLONG; |
| 561 | return posix_error(); |
| 562 | } |
| 563 | |
| 564 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 565 | /* exception for specific or current drive root */ |
| 566 | if (!((pathlen == 1) || |
| 567 | ((pathlen == 3) && |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 568 | (path[1] == ':') && |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 569 | (path[2] == '\\' || path[2] == '/')))) |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 570 | { |
| 571 | strncpy(pathcopy, path, pathlen); |
| 572 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 573 | path = pathcopy; |
| 574 | } |
| 575 | } |
| 576 | #endif /* MS_WIN32 */ |
| 577 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 578 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 579 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 580 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 581 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 582 | return posix_error_with_filename(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 583 | |
| 584 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | /* POSIX methods */ |
| 589 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 590 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 591 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 592 | Test for access to a file."; |
| 593 | |
| 594 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 595 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 596 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 597 | char *path; |
| 598 | int mode; |
| 599 | int res; |
| 600 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 601 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 602 | return NULL; |
| 603 | Py_BEGIN_ALLOW_THREADS |
| 604 | res = access(path, mode); |
| 605 | Py_END_ALLOW_THREADS |
| 606 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 609 | #ifndef F_OK |
| 610 | #define F_OK 0 |
| 611 | #endif |
| 612 | #ifndef R_OK |
| 613 | #define R_OK 4 |
| 614 | #endif |
| 615 | #ifndef W_OK |
| 616 | #define W_OK 2 |
| 617 | #endif |
| 618 | #ifndef X_OK |
| 619 | #define X_OK 1 |
| 620 | #endif |
| 621 | |
| 622 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 623 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 624 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 625 | Return the name of the terminal device connected to 'fd'."; |
| 626 | |
| 627 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 628 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 629 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 630 | int id; |
| 631 | char *ret; |
| 632 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 633 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 634 | return NULL; |
| 635 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 636 | ret = ttyname(id); |
| 637 | if (ret == NULL) |
| 638 | return(posix_error()); |
| 639 | return(PyString_FromString(ret)); |
| 640 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 641 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 642 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 643 | #ifdef HAVE_CTERMID |
| 644 | static char posix_ctermid__doc__[] = |
| 645 | "ctermid() -> String\n\ |
| 646 | Return the name of the controlling terminal for this process."; |
| 647 | |
| 648 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 649 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 650 | { |
| 651 | char *ret; |
| 652 | char buffer[L_ctermid]; |
| 653 | |
| 654 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 655 | return NULL; |
| 656 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 657 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 658 | ret = ctermid_r(buffer); |
| 659 | #else |
| 660 | ret = ctermid(buffer); |
| 661 | #endif |
| 662 | if (ret == NULL) |
| 663 | return(posix_error()); |
| 664 | return(PyString_FromString(buffer)); |
| 665 | } |
| 666 | #endif |
| 667 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 668 | static char posix_chdir__doc__[] = |
| 669 | "chdir(path) -> None\n\ |
| 670 | Change the current working directory to the specified path."; |
| 671 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 672 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 673 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 675 | return posix_1str(args, "s:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 678 | |
| 679 | static char posix_chmod__doc__[] = |
| 680 | "chmod(path, mode) -> None\n\ |
| 681 | Change the access permissions of a file."; |
| 682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 684 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 685 | { |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 686 | char *path; |
| 687 | int i; |
| 688 | int res; |
Guido van Rossum | 49679b4 | 2000-03-31 00:48:21 +0000 | [diff] [blame] | 689 | if (!PyArg_ParseTuple(args, "si", &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 690 | return NULL; |
| 691 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 692 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 693 | Py_END_ALLOW_THREADS |
| 694 | if (res < 0) |
| 695 | return posix_error_with_filename(path); |
| 696 | Py_INCREF(Py_None); |
| 697 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 700 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 701 | #ifdef HAVE_FSYNC |
| 702 | static char posix_fsync__doc__[] = |
| 703 | "fsync(fildes) -> None\n\ |
| 704 | force write of file with filedescriptor to disk."; |
| 705 | |
| 706 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 707 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 708 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 709 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 710 | } |
| 711 | #endif /* HAVE_FSYNC */ |
| 712 | |
| 713 | #ifdef HAVE_FDATASYNC |
| 714 | static char posix_fdatasync__doc__[] = |
| 715 | "fdatasync(fildes) -> None\n\ |
| 716 | force write of file with filedescriptor to disk.\n\ |
| 717 | does not force update of metadata."; |
| 718 | |
| 719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 720 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 721 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 722 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 723 | } |
| 724 | #endif /* HAVE_FDATASYNC */ |
| 725 | |
| 726 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 727 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 728 | static char posix_chown__doc__[] = |
| 729 | "chown(path, uid, gid) -> None\n\ |
| 730 | Change the owner and group id of path to the numeric uid and gid."; |
| 731 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 732 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 733 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 734 | { |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 735 | char *path; |
| 736 | int uid, gid; |
| 737 | int res; |
| 738 | if (!PyArg_ParseTuple(args, "sii:chown", &path, &uid, &gid)) |
| 739 | return NULL; |
| 740 | Py_BEGIN_ALLOW_THREADS |
| 741 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 742 | Py_END_ALLOW_THREADS |
| 743 | if (res < 0) |
| 744 | return posix_error_with_filename(path); |
| 745 | Py_INCREF(Py_None); |
| 746 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 747 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 748 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 749 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 750 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 751 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 752 | static char posix_getcwd__doc__[] = |
| 753 | "getcwd() -> path\n\ |
| 754 | Return a string representing the current working directory."; |
| 755 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 756 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 757 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 758 | { |
| 759 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 760 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 761 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 762 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 763 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 764 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 765 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 766 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 767 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 768 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 769 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 770 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 771 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 772 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 773 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 774 | static char posix_link__doc__[] = |
| 775 | "link(src, dst) -> None\n\ |
| 776 | Create a hard link to a file."; |
| 777 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 778 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 779 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 780 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 781 | return posix_2str(args, "ss:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 782 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 783 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 785 | |
| 786 | static char posix_listdir__doc__[] = |
| 787 | "listdir(path) -> list_of_strings\n\ |
| 788 | Return a list containing the names of the entries in the directory.\n\ |
| 789 | \n\ |
| 790 | path: path of directory to list\n\ |
| 791 | \n\ |
| 792 | The list is in arbitrary order. It does not include the special\n\ |
| 793 | entries '.' and '..' even if they are present in the directory."; |
| 794 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 795 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 796 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 797 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 798 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 799 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 800 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 802 | char *name; |
| 803 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 804 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 805 | HANDLE hFindFile; |
| 806 | WIN32_FIND_DATA FileData; |
| 807 | char namebuf[MAX_PATH+5]; |
| 808 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 809 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 810 | return NULL; |
| 811 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 812 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 813 | return NULL; |
| 814 | } |
| 815 | strcpy(namebuf, name); |
| 816 | if (namebuf[len-1] != '/' && namebuf[len-1] != '\\') |
| 817 | namebuf[len++] = '/'; |
| 818 | strcpy(namebuf + len, "*.*"); |
| 819 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 820 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 821 | return NULL; |
| 822 | |
| 823 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 824 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 825 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 826 | if (errno == ERROR_FILE_NOT_FOUND) |
| 827 | return PyList_New(0); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 828 | return win32_error("FindFirstFile", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 829 | } |
| 830 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 831 | if (FileData.cFileName[0] == '.' && |
| 832 | (FileData.cFileName[1] == '\0' || |
| 833 | FileData.cFileName[1] == '.' && |
| 834 | FileData.cFileName[2] == '\0')) |
| 835 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 836 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 837 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 838 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 839 | d = NULL; |
| 840 | break; |
| 841 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 842 | if (PyList_Append(d, v) != 0) { |
| 843 | Py_DECREF(v); |
| 844 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 845 | d = NULL; |
| 846 | break; |
| 847 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 848 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 849 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 850 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 851 | if (FindClose(hFindFile) == FALSE) |
| 852 | return win32_error("FindClose", name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 853 | |
| 854 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 855 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 856 | #else /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 857 | #ifdef _MSC_VER /* 16-bit Windows */ |
| 858 | |
| 859 | #ifndef MAX_PATH |
| 860 | #define MAX_PATH 250 |
| 861 | #endif |
| 862 | char *name, *pt; |
| 863 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 864 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 865 | char namebuf[MAX_PATH+5]; |
| 866 | struct _find_t ep; |
| 867 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 868 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 869 | return NULL; |
| 870 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 871 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 872 | return NULL; |
| 873 | } |
| 874 | strcpy(namebuf, name); |
| 875 | for (pt = namebuf; *pt; pt++) |
| 876 | if (*pt == '/') |
| 877 | *pt = '\\'; |
| 878 | if (namebuf[len-1] != '\\') |
| 879 | namebuf[len++] = '\\'; |
| 880 | strcpy(namebuf + len, "*.*"); |
| 881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 882 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 883 | return NULL; |
| 884 | |
| 885 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 886 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 887 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 888 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 889 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 890 | } |
| 891 | do { |
| 892 | if (ep.name[0] == '.' && |
| 893 | (ep.name[1] == '\0' || |
| 894 | ep.name[1] == '.' && |
| 895 | ep.name[2] == '\0')) |
| 896 | continue; |
| 897 | strcpy(namebuf, ep.name); |
| 898 | for (pt = namebuf; *pt; pt++) |
| 899 | if (isupper(*pt)) |
| 900 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 901 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 902 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 903 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 904 | d = NULL; |
| 905 | break; |
| 906 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 907 | if (PyList_Append(d, v) != 0) { |
| 908 | Py_DECREF(v); |
| 909 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 910 | d = NULL; |
| 911 | break; |
| 912 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 913 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 914 | } while (_dos_findnext(&ep) == 0); |
| 915 | |
| 916 | return d; |
| 917 | |
| 918 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 919 | #if defined(PYOS_OS2) |
| 920 | |
| 921 | #ifndef MAX_PATH |
| 922 | #define MAX_PATH CCHMAXPATH |
| 923 | #endif |
| 924 | char *name, *pt; |
| 925 | int len; |
| 926 | PyObject *d, *v; |
| 927 | char namebuf[MAX_PATH+5]; |
| 928 | HDIR hdir = 1; |
| 929 | ULONG srchcnt = 1; |
| 930 | FILEFINDBUF3 ep; |
| 931 | APIRET rc; |
| 932 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 933 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 934 | return NULL; |
| 935 | if (len >= MAX_PATH) { |
| 936 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 937 | return NULL; |
| 938 | } |
| 939 | strcpy(namebuf, name); |
| 940 | for (pt = namebuf; *pt; pt++) |
| 941 | if (*pt == '/') |
| 942 | *pt = '\\'; |
| 943 | if (namebuf[len-1] != '\\') |
| 944 | namebuf[len++] = '\\'; |
| 945 | strcpy(namebuf + len, "*.*"); |
| 946 | |
| 947 | if ((d = PyList_New(0)) == NULL) |
| 948 | return NULL; |
| 949 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 950 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 951 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 952 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 953 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 954 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 955 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 956 | |
| 957 | if (rc != NO_ERROR) { |
| 958 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 959 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 962 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 963 | do { |
| 964 | if (ep.achName[0] == '.' |
| 965 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 966 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 967 | |
| 968 | strcpy(namebuf, ep.achName); |
| 969 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 970 | /* Leave Case of Name Alone -- In Native Form */ |
| 971 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 972 | |
| 973 | v = PyString_FromString(namebuf); |
| 974 | if (v == NULL) { |
| 975 | Py_DECREF(d); |
| 976 | d = NULL; |
| 977 | break; |
| 978 | } |
| 979 | if (PyList_Append(d, v) != 0) { |
| 980 | Py_DECREF(v); |
| 981 | Py_DECREF(d); |
| 982 | d = NULL; |
| 983 | break; |
| 984 | } |
| 985 | Py_DECREF(v); |
| 986 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 987 | } |
| 988 | |
| 989 | return d; |
| 990 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 992 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 993 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 994 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 995 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 996 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 997 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 998 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 999 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1000 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1001 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1002 | closedir(dirp); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1006 | if (ep->d_name[0] == '.' && |
| 1007 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1008 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1009 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1010 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1011 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1012 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1013 | d = NULL; |
| 1014 | break; |
| 1015 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1016 | if (PyList_Append(d, v) != 0) { |
| 1017 | Py_DECREF(v); |
| 1018 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1019 | d = NULL; |
| 1020 | break; |
| 1021 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1022 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1023 | } |
| 1024 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1025 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1026 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1027 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1028 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1029 | #endif /* !_MSC_VER */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1030 | #endif /* !MS_WIN32 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1033 | static char posix_mkdir__doc__[] = |
| 1034 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1035 | Create a directory."; |
| 1036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1037 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1038 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1039 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1040 | int res; |
| 1041 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1042 | int mode = 0777; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1043 | if (!PyArg_ParseTuple(args, "s|i:mkdir", &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1044 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1045 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1046 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1047 | res = mkdir(path); |
| 1048 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1049 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1050 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1051 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1052 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1053 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1054 | Py_INCREF(Py_None); |
| 1055 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1059 | #ifdef HAVE_NICE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1060 | static char posix_nice__doc__[] = |
| 1061 | "nice(inc) -> new_priority\n\ |
| 1062 | Decrease the priority of process and return new priority."; |
| 1063 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1064 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1065 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1066 | { |
| 1067 | int increment, value; |
| 1068 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1069 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1070 | return NULL; |
| 1071 | value = nice(increment); |
| 1072 | if (value == -1) |
| 1073 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1074 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1075 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1076 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1077 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1078 | |
| 1079 | static char posix_rename__doc__[] = |
| 1080 | "rename(old, new) -> None\n\ |
| 1081 | Rename a file or directory."; |
| 1082 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1083 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1084 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1085 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1086 | return posix_2str(args, "ss:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1089 | |
| 1090 | static char posix_rmdir__doc__[] = |
| 1091 | "rmdir(path) -> None\n\ |
| 1092 | Remove a directory."; |
| 1093 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1094 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1095 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1096 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1097 | return posix_1str(args, "s:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1100 | |
| 1101 | static char posix_stat__doc__[] = |
| 1102 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1103 | Perform a stat system call on the given path."; |
| 1104 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1105 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1106 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1107 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1108 | return posix_do_stat(self, args, "s:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1112 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1113 | static char posix_system__doc__[] = |
| 1114 | "system(command) -> exit_status\n\ |
| 1115 | Execute the command (a string) in a subshell."; |
| 1116 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1117 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1118 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1119 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1120 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1121 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1122 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1123 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1124 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1125 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1126 | Py_END_ALLOW_THREADS |
| 1127 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1128 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1129 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1130 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1131 | |
| 1132 | static char posix_umask__doc__[] = |
| 1133 | "umask(new_mask) -> old_mask\n\ |
| 1134 | Set the current numeric umask and return the previous umask."; |
| 1135 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1136 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1137 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1138 | { |
| 1139 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1140 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1141 | return NULL; |
| 1142 | i = umask(i); |
| 1143 | if (i < 0) |
| 1144 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1145 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1148 | |
| 1149 | static char posix_unlink__doc__[] = |
| 1150 | "unlink(path) -> None\n\ |
| 1151 | Remove a file (same as remove(path))."; |
| 1152 | |
| 1153 | static char posix_remove__doc__[] = |
| 1154 | "remove(path) -> None\n\ |
| 1155 | Remove a file (same as unlink(path))."; |
| 1156 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1157 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1158 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1159 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1160 | return posix_1str(args, "s:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1164 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1165 | static char posix_uname__doc__[] = |
| 1166 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1167 | Return a tuple identifying the current operating system."; |
| 1168 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1169 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1170 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1171 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1172 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1173 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1174 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1175 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1176 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1177 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1178 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1179 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1180 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1181 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1182 | u.sysname, |
| 1183 | u.nodename, |
| 1184 | u.release, |
| 1185 | u.version, |
| 1186 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1187 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1188 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1189 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1190 | |
| 1191 | static char posix_utime__doc__[] = |
| 1192 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1193 | utime(path, None) -> None\n\ |
| 1194 | Set the access and modified time of the file to the given values. If the\n\ |
| 1195 | 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] | 1196 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1197 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1198 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1199 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1200 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1201 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1202 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1203 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1205 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1206 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1207 | struct utimbuf buf; |
| 1208 | #define ATIME buf.actime |
| 1209 | #define MTIME buf.modtime |
| 1210 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1211 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1212 | time_t buf[2]; |
| 1213 | #define ATIME buf[0] |
| 1214 | #define MTIME buf[1] |
| 1215 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1216 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1217 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1218 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1220 | if (arg == Py_None) { |
| 1221 | /* optional time values not given */ |
| 1222 | Py_BEGIN_ALLOW_THREADS |
| 1223 | res = utime(path, NULL); |
| 1224 | Py_END_ALLOW_THREADS |
| 1225 | } |
| 1226 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1227 | PyErr_SetString(PyExc_TypeError, |
| 1228 | "Second argument must be a 2-tuple of numbers."); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | else { |
| 1232 | ATIME = atime; |
| 1233 | MTIME = mtime; |
| 1234 | Py_BEGIN_ALLOW_THREADS |
| 1235 | res = utime(path, UTIME_ARG); |
| 1236 | Py_END_ALLOW_THREADS |
| 1237 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1238 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1239 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1240 | Py_INCREF(Py_None); |
| 1241 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1242 | #undef UTIME_ARG |
| 1243 | #undef ATIME |
| 1244 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1247 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1248 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1249 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1250 | static char posix__exit__doc__[] = |
| 1251 | "_exit(status)\n\ |
| 1252 | Exit to the system with specified status, without normal exit processing."; |
| 1253 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1254 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1255 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1256 | { |
| 1257 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1258 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1259 | return NULL; |
| 1260 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1261 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1264 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1265 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1266 | static char posix_execv__doc__[] = |
| 1267 | "execv(path, args)\n\ |
| 1268 | Execute an executable path with arguments, replacing current process.\n\ |
| 1269 | \n\ |
| 1270 | path: path of executable file\n\ |
| 1271 | args: tuple or list of strings"; |
| 1272 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1274 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1275 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1276 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1277 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1278 | char **argvlist; |
| 1279 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1280 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1281 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1282 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1283 | argv is a list or tuple of strings. */ |
| 1284 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1285 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1286 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1287 | if (PyList_Check(argv)) { |
| 1288 | argc = PyList_Size(argv); |
| 1289 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1290 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1291 | else if (PyTuple_Check(argv)) { |
| 1292 | argc = PyTuple_Size(argv); |
| 1293 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1294 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1295 | else { |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1296 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | |
| 1300 | if (argc == 0) { |
| 1301 | PyErr_SetString(PyExc_ValueError, "empty argument list"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1302 | return NULL; |
| 1303 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1304 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1305 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1306 | if (argvlist == NULL) |
| 1307 | return NULL; |
| 1308 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1309 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1310 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1311 | PyErr_SetString(PyExc_TypeError, |
| 1312 | "all arguments must be strings"); |
| 1313 | return NULL; |
| 1314 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1315 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1316 | } |
| 1317 | argvlist[argc] = NULL; |
| 1318 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1319 | #ifdef BAD_EXEC_PROTOTYPES |
| 1320 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1321 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1322 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1323 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1324 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1325 | /* If we get here it's definitely an error */ |
| 1326 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1327 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1328 | return posix_error(); |
| 1329 | } |
| 1330 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1331 | |
| 1332 | static char posix_execve__doc__[] = |
| 1333 | "execve(path, args, env)\n\ |
| 1334 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1335 | \n\ |
| 1336 | path: path of executable file\n\ |
| 1337 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1338 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1339 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1341 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1342 | { |
| 1343 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1344 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1345 | char **argvlist; |
| 1346 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1347 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1348 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1349 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* execve has three arguments: (path, argv, env), where |
| 1352 | argv is a list or tuple of strings and env is a dictionary |
| 1353 | like posix.environ. */ |
| 1354 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1355 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1356 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1357 | if (PyList_Check(argv)) { |
| 1358 | argc = PyList_Size(argv); |
| 1359 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1360 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1361 | else if (PyTuple_Check(argv)) { |
| 1362 | argc = PyTuple_Size(argv); |
| 1363 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1364 | } |
| 1365 | else { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1366 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1369 | if (!PyMapping_Check(env)) { |
| 1370 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1371 | return NULL; |
| 1372 | } |
| 1373 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1374 | if (argc == 0) { |
| 1375 | PyErr_SetString(PyExc_ValueError, |
| 1376 | "empty argument list"); |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1380 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1381 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1382 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1383 | return NULL; |
| 1384 | } |
| 1385 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1386 | if (!PyArg_Parse((*getitem)(argv, i), |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1387 | "s;argv must be list of strings", |
| 1388 | &argvlist[i])) |
| 1389 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1390 | goto fail_1; |
| 1391 | } |
| 1392 | } |
| 1393 | argvlist[argc] = NULL; |
| 1394 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1395 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1396 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1397 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1398 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1399 | goto fail_1; |
| 1400 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1401 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1402 | keys = PyMapping_Keys(env); |
| 1403 | vals = PyMapping_Values(env); |
| 1404 | if (!keys || !vals) |
| 1405 | goto fail_2; |
| 1406 | |
| 1407 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1408 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1409 | |
| 1410 | key = PyList_GetItem(keys, pos); |
| 1411 | val = PyList_GetItem(vals, pos); |
| 1412 | if (!key || !val) |
| 1413 | goto fail_2; |
| 1414 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1415 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1416 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1417 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1418 | goto fail_2; |
| 1419 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1420 | |
| 1421 | #if defined(PYOS_OS2) |
| 1422 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1423 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1424 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1425 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1426 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1427 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1428 | goto fail_2; |
| 1429 | } |
| 1430 | sprintf(p, "%s=%s", k, v); |
| 1431 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1432 | #if defined(PYOS_OS2) |
| 1433 | } |
| 1434 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1435 | } |
| 1436 | envlist[envc] = 0; |
| 1437 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1438 | |
| 1439 | #ifdef BAD_EXEC_PROTOTYPES |
| 1440 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1441 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1442 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1443 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* If we get here it's definitely an error */ |
| 1446 | |
| 1447 | (void) posix_error(); |
| 1448 | |
| 1449 | fail_2: |
| 1450 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1451 | PyMem_DEL(envlist[envc]); |
| 1452 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1453 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1454 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1455 | Py_XDECREF(vals); |
| 1456 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1457 | return NULL; |
| 1458 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1459 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1460 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1462 | #ifdef HAVE_SPAWNV |
| 1463 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1464 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1465 | Execute an executable path with arguments, replacing current process.\n\ |
| 1466 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1467 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1468 | path: path of executable file\n\ |
| 1469 | args: tuple or list of strings"; |
| 1470 | |
| 1471 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1472 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1473 | { |
| 1474 | char *path; |
| 1475 | PyObject *argv; |
| 1476 | char **argvlist; |
| 1477 | int mode, i, argc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1478 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1479 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1480 | |
| 1481 | /* spawnv has three arguments: (mode, path, argv), where |
| 1482 | argv is a list or tuple of strings. */ |
| 1483 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1484 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1485 | return NULL; |
| 1486 | if (PyList_Check(argv)) { |
| 1487 | argc = PyList_Size(argv); |
| 1488 | getitem = PyList_GetItem; |
| 1489 | } |
| 1490 | else if (PyTuple_Check(argv)) { |
| 1491 | argc = PyTuple_Size(argv); |
| 1492 | getitem = PyTuple_GetItem; |
| 1493 | } |
| 1494 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1495 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1496 | return NULL; |
| 1497 | } |
| 1498 | |
| 1499 | argvlist = PyMem_NEW(char *, argc+1); |
| 1500 | if (argvlist == NULL) |
| 1501 | return NULL; |
| 1502 | for (i = 0; i < argc; i++) { |
| 1503 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1504 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1505 | PyErr_SetString(PyExc_TypeError, |
| 1506 | "all arguments must be strings"); |
| 1507 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | argvlist[argc] = NULL; |
| 1511 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1512 | if (mode == _OLD_P_OVERLAY) |
| 1513 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1514 | spawnval = _spawnv(mode, path, argvlist); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1515 | |
| 1516 | PyMem_DEL(argvlist); |
| 1517 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1518 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1519 | return posix_error(); |
| 1520 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1521 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1522 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1523 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1524 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1525 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1530 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1531 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1532 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1533 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1534 | path: path of executable file\n\ |
| 1535 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1536 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1537 | |
| 1538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1539 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1540 | { |
| 1541 | char *path; |
| 1542 | PyObject *argv, *env; |
| 1543 | char **argvlist; |
| 1544 | char **envlist; |
| 1545 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1546 | int mode, i, pos, argc, envc; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1547 | intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1548 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1549 | |
| 1550 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1551 | argv is a list or tuple of strings and env is a dictionary |
| 1552 | like posix.environ. */ |
| 1553 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1554 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1555 | return NULL; |
| 1556 | if (PyList_Check(argv)) { |
| 1557 | argc = PyList_Size(argv); |
| 1558 | getitem = PyList_GetItem; |
| 1559 | } |
| 1560 | else if (PyTuple_Check(argv)) { |
| 1561 | argc = PyTuple_Size(argv); |
| 1562 | getitem = PyTuple_GetItem; |
| 1563 | } |
| 1564 | else { |
| 1565 | PyErr_SetString(PyExc_TypeError, "argv must be tuple or list"); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | if (!PyMapping_Check(env)) { |
| 1569 | PyErr_SetString(PyExc_TypeError, "env must be mapping object"); |
| 1570 | return NULL; |
| 1571 | } |
| 1572 | |
| 1573 | argvlist = PyMem_NEW(char *, argc+1); |
| 1574 | if (argvlist == NULL) { |
| 1575 | PyErr_NoMemory(); |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | for (i = 0; i < argc; i++) { |
| 1579 | if (!PyArg_Parse((*getitem)(argv, i), |
| 1580 | "s;argv must be list of strings", |
| 1581 | &argvlist[i])) |
| 1582 | { |
| 1583 | goto fail_1; |
| 1584 | } |
| 1585 | } |
| 1586 | argvlist[argc] = NULL; |
| 1587 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1588 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1589 | envlist = PyMem_NEW(char *, i + 1); |
| 1590 | if (envlist == NULL) { |
| 1591 | PyErr_NoMemory(); |
| 1592 | goto fail_1; |
| 1593 | } |
| 1594 | envc = 0; |
| 1595 | keys = PyMapping_Keys(env); |
| 1596 | vals = PyMapping_Values(env); |
| 1597 | if (!keys || !vals) |
| 1598 | goto fail_2; |
| 1599 | |
| 1600 | for (pos = 0; pos < i; pos++) { |
| 1601 | char *p, *k, *v; |
| 1602 | |
| 1603 | key = PyList_GetItem(keys, pos); |
| 1604 | val = PyList_GetItem(vals, pos); |
| 1605 | if (!key || !val) |
| 1606 | goto fail_2; |
| 1607 | |
| 1608 | if (!PyArg_Parse(key, "s;non-string key in env", &k) || |
| 1609 | !PyArg_Parse(val, "s;non-string value in env", &v)) |
| 1610 | { |
| 1611 | goto fail_2; |
| 1612 | } |
| 1613 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1614 | if (p == NULL) { |
| 1615 | PyErr_NoMemory(); |
| 1616 | goto fail_2; |
| 1617 | } |
| 1618 | sprintf(p, "%s=%s", k, v); |
| 1619 | envlist[envc++] = p; |
| 1620 | } |
| 1621 | envlist[envc] = 0; |
| 1622 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1623 | if (mode == _OLD_P_OVERLAY) |
| 1624 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1625 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 1626 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1627 | (void) posix_error(); |
| 1628 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1629 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1630 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1631 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1632 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1633 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1634 | |
| 1635 | fail_2: |
| 1636 | while (--envc >= 0) |
| 1637 | PyMem_DEL(envlist[envc]); |
| 1638 | PyMem_DEL(envlist); |
| 1639 | fail_1: |
| 1640 | PyMem_DEL(argvlist); |
| 1641 | Py_XDECREF(vals); |
| 1642 | Py_XDECREF(keys); |
| 1643 | return res; |
| 1644 | } |
| 1645 | #endif /* HAVE_SPAWNV */ |
| 1646 | |
| 1647 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1648 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1649 | static char posix_fork__doc__[] = |
| 1650 | "fork() -> pid\n\ |
| 1651 | Fork a child process.\n\ |
| 1652 | \n\ |
| 1653 | Return 0 to child process and PID of child to parent process."; |
| 1654 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1656 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1657 | { |
| 1658 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1659 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1660 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1661 | pid = fork(); |
| 1662 | if (pid == -1) |
| 1663 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1664 | if (pid == 0) |
| 1665 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1666 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1667 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1668 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1669 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1670 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1671 | #ifdef HAVE_PTY_H |
| 1672 | #include <pty.h> |
| 1673 | #else |
| 1674 | #ifdef HAVE_LIBUTIL_H |
| 1675 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1676 | #endif /* HAVE_LIBUTIL_H */ |
| 1677 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1678 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1679 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1680 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1681 | static char posix_openpty__doc__[] = |
| 1682 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1683 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1684 | |
| 1685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1686 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1687 | { |
| 1688 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1689 | #ifndef HAVE_OPENPTY |
| 1690 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1691 | #endif |
| 1692 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1693 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1694 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1695 | |
| 1696 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1697 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1698 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1699 | #else |
| 1700 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1701 | if (slave_name == NULL) |
| 1702 | return posix_error(); |
| 1703 | |
| 1704 | slave_fd = open(slave_name, O_RDWR); |
| 1705 | if (slave_fd < 0) |
| 1706 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1707 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1708 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1709 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1710 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1711 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1712 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1713 | |
| 1714 | #ifdef HAVE_FORKPTY |
| 1715 | static char posix_forkpty__doc__[] = |
| 1716 | "forkpty() -> (pid, master_fd)\n\ |
| 1717 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1718 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1719 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1720 | |
| 1721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1722 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1723 | { |
| 1724 | int master_fd, pid; |
| 1725 | |
| 1726 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1727 | return NULL; |
| 1728 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1729 | if (pid == -1) |
| 1730 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1731 | if (pid == 0) |
| 1732 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1733 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1734 | } |
| 1735 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1737 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1738 | static char posix_getegid__doc__[] = |
| 1739 | "getegid() -> egid\n\ |
| 1740 | Return the current process's effective group id."; |
| 1741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1742 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1743 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1744 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1745 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1746 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1747 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1748 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1749 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1750 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1751 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1752 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1753 | static char posix_geteuid__doc__[] = |
| 1754 | "geteuid() -> euid\n\ |
| 1755 | Return the current process's effective user id."; |
| 1756 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1757 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1758 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1759 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1760 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1761 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1762 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1763 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1764 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1766 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1767 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1768 | static char posix_getgid__doc__[] = |
| 1769 | "getgid() -> gid\n\ |
| 1770 | Return the current process's group id."; |
| 1771 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1772 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1773 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1774 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1775 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1776 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1778 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1779 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1780 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1781 | |
| 1782 | static char posix_getpid__doc__[] = |
| 1783 | "getpid() -> pid\n\ |
| 1784 | Return the current process id"; |
| 1785 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1786 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1787 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1788 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1789 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1790 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1791 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1794 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1795 | #ifdef HAVE_GETGROUPS |
| 1796 | static char posix_getgroups__doc__[] = "\ |
| 1797 | getgroups() -> list of group IDs\n\ |
| 1798 | Return list of supplemental group IDs for the process."; |
| 1799 | |
| 1800 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1801 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 1802 | { |
| 1803 | PyObject *result = NULL; |
| 1804 | |
| 1805 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 1806 | #ifdef NGROUPS_MAX |
| 1807 | #define MAX_GROUPS NGROUPS_MAX |
| 1808 | #else |
| 1809 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 1810 | #define MAX_GROUPS 64 |
| 1811 | #endif |
| 1812 | gid_t grouplist[MAX_GROUPS]; |
| 1813 | int n; |
| 1814 | |
| 1815 | n = getgroups(MAX_GROUPS, grouplist); |
| 1816 | if (n < 0) |
| 1817 | posix_error(); |
| 1818 | else { |
| 1819 | result = PyList_New(n); |
| 1820 | if (result != NULL) { |
| 1821 | PyObject *o; |
| 1822 | int i; |
| 1823 | for (i = 0; i < n; ++i) { |
| 1824 | o = PyInt_FromLong((long)grouplist[i]); |
| 1825 | if (o == NULL) { |
| 1826 | Py_DECREF(result); |
| 1827 | result = NULL; |
| 1828 | break; |
| 1829 | } |
| 1830 | PyList_SET_ITEM(result, i, o); |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | return result; |
| 1836 | } |
| 1837 | #endif |
| 1838 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1839 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1840 | static char posix_getpgrp__doc__[] = |
| 1841 | "getpgrp() -> pgrp\n\ |
| 1842 | Return the current process group id."; |
| 1843 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1844 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1845 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1846 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1847 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1848 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1849 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1850 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1851 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1852 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1853 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1854 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1855 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 1856 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1857 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1858 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1859 | static char posix_setpgrp__doc__[] = |
| 1860 | "setpgrp() -> None\n\ |
| 1861 | Make this process a session leader."; |
| 1862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1864 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1865 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1866 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1867 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1868 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1869 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1870 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1871 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 1872 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 1873 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1874 | Py_INCREF(Py_None); |
| 1875 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1878 | #endif /* HAVE_SETPGRP */ |
| 1879 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1880 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1881 | static char posix_getppid__doc__[] = |
| 1882 | "getppid() -> ppid\n\ |
| 1883 | Return the parent's process id."; |
| 1884 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1885 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1886 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1887 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1888 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1889 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1890 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1891 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1892 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1893 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1894 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1895 | #ifdef HAVE_GETLOGIN |
| 1896 | static char posix_getlogin__doc__[] = "\ |
| 1897 | getlogin() -> string\n\ |
| 1898 | Return the actual login name."; |
| 1899 | |
| 1900 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1901 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 1902 | { |
| 1903 | PyObject *result = NULL; |
| 1904 | |
| 1905 | if (PyArg_ParseTuple(args, ":getlogin")) { |
| 1906 | char *name = getlogin(); |
| 1907 | |
| 1908 | if (name == NULL) |
| 1909 | posix_error(); |
| 1910 | else |
| 1911 | result = PyString_FromString(name); |
| 1912 | } |
| 1913 | return result; |
| 1914 | } |
| 1915 | #endif |
| 1916 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1917 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1918 | static char posix_getuid__doc__[] = |
| 1919 | "getuid() -> uid\n\ |
| 1920 | Return the current process's user id."; |
| 1921 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1922 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1923 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1924 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1925 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1926 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1927 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1928 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1929 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1930 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1931 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1932 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1933 | static char posix_kill__doc__[] = |
| 1934 | "kill(pid, sig) -> None\n\ |
| 1935 | Kill a process with a signal."; |
| 1936 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1937 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1938 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1939 | { |
| 1940 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1941 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1942 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1943 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1944 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 1945 | APIRET rc; |
| 1946 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1947 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1948 | |
| 1949 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 1950 | APIRET rc; |
| 1951 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1952 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1953 | |
| 1954 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1955 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1956 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1957 | if (kill(pid, sig) == -1) |
| 1958 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1959 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1960 | Py_INCREF(Py_None); |
| 1961 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1962 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1963 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1964 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1965 | #ifdef HAVE_PLOCK |
| 1966 | |
| 1967 | #ifdef HAVE_SYS_LOCK_H |
| 1968 | #include <sys/lock.h> |
| 1969 | #endif |
| 1970 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1971 | static char posix_plock__doc__[] = |
| 1972 | "plock(op) -> None\n\ |
| 1973 | Lock program segments into memory."; |
| 1974 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1975 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1976 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1977 | { |
| 1978 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1979 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1980 | return NULL; |
| 1981 | if (plock(op) == -1) |
| 1982 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1983 | Py_INCREF(Py_None); |
| 1984 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 1985 | } |
| 1986 | #endif |
| 1987 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1989 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1990 | static char posix_popen__doc__[] = |
| 1991 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 1992 | Open a pipe to/from a command returning a file object."; |
| 1993 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1994 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1995 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1996 | async_system(const char *command) |
| 1997 | { |
| 1998 | char *p, errormsg[256], args[1024]; |
| 1999 | RESULTCODES rcodes; |
| 2000 | APIRET rc; |
| 2001 | char *shell = getenv("COMSPEC"); |
| 2002 | if (!shell) |
| 2003 | shell = "cmd"; |
| 2004 | |
| 2005 | strcpy(args, shell); |
| 2006 | p = &args[ strlen(args)+1 ]; |
| 2007 | strcpy(p, "/c "); |
| 2008 | strcat(p, command); |
| 2009 | p += strlen(p) + 1; |
| 2010 | *p = '\0'; |
| 2011 | |
| 2012 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2013 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2014 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2015 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2016 | &rcodes, shell); |
| 2017 | return rc; |
| 2018 | } |
| 2019 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2020 | static FILE * |
| 2021 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2022 | { |
| 2023 | HFILE rhan, whan; |
| 2024 | FILE *retfd = NULL; |
| 2025 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2026 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2027 | if (rc != NO_ERROR) { |
| 2028 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2029 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2030 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2032 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2033 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2034 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2035 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2036 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2037 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2038 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2039 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2040 | |
| 2041 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2042 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2045 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2046 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
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 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2049 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2050 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2051 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2052 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2054 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2055 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2057 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2058 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2059 | |
| 2060 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2061 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2064 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2065 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2066 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2067 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2068 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2070 | } else { |
| 2071 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2072 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2073 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2077 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2078 | { |
| 2079 | char *name; |
| 2080 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2081 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2082 | FILE *fp; |
| 2083 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2084 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2085 | return NULL; |
| 2086 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2087 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2088 | Py_END_ALLOW_THREADS |
| 2089 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2090 | return os2_error(err); |
| 2091 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2092 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2093 | if (f != NULL) |
| 2094 | PyFile_SetBufSize(f, bufsize); |
| 2095 | return f; |
| 2096 | } |
| 2097 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2098 | #elif defined(MS_WIN32) |
| 2099 | |
| 2100 | /* |
| 2101 | * Portable 'popen' replacement for Win32. |
| 2102 | * |
| 2103 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2104 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2105 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2106 | */ |
| 2107 | |
| 2108 | #include <malloc.h> |
| 2109 | #include <io.h> |
| 2110 | #include <fcntl.h> |
| 2111 | |
| 2112 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2113 | #define POPEN_1 1 |
| 2114 | #define POPEN_2 2 |
| 2115 | #define POPEN_3 3 |
| 2116 | #define POPEN_4 4 |
| 2117 | |
| 2118 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2119 | static int _PyPclose(FILE *file); |
| 2120 | |
| 2121 | /* |
| 2122 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2123 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2124 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2125 | */ |
| 2126 | static PyObject *_PyPopenProcs = NULL; |
| 2127 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2128 | |
| 2129 | /* popen that works from a GUI. |
| 2130 | * |
| 2131 | * The result of this function is a pipe (file) connected to the |
| 2132 | * processes stdin or stdout, depending on the requested mode. |
| 2133 | */ |
| 2134 | |
| 2135 | static PyObject * |
| 2136 | posix_popen(PyObject *self, PyObject *args) |
| 2137 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2138 | PyObject *f, *s; |
| 2139 | int tm = 0; |
| 2140 | |
| 2141 | char *cmdstring; |
| 2142 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2143 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2144 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2145 | return NULL; |
| 2146 | |
| 2147 | s = PyTuple_New(0); |
| 2148 | |
| 2149 | if (*mode == 'r') |
| 2150 | tm = _O_RDONLY; |
| 2151 | else if (*mode != 'w') { |
| 2152 | PyErr_SetString(PyExc_ValueError, "mode must be 'r' or 'w'"); |
| 2153 | return NULL; |
| 2154 | } else |
| 2155 | tm = _O_WRONLY; |
| 2156 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2157 | if (bufsize != -1) { |
| 2158 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2162 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2163 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2164 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2165 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2166 | else |
| 2167 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2168 | |
| 2169 | return f; |
| 2170 | } |
| 2171 | |
| 2172 | /* Variation on win32pipe.popen |
| 2173 | * |
| 2174 | * The result of this function is a pipe (file) connected to the |
| 2175 | * process's stdin, and a pipe connected to the process's stdout. |
| 2176 | */ |
| 2177 | |
| 2178 | static PyObject * |
| 2179 | win32_popen2(PyObject *self, PyObject *args) |
| 2180 | { |
| 2181 | PyObject *f; |
| 2182 | int tm=0; |
| 2183 | |
| 2184 | char *cmdstring; |
| 2185 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2186 | int bufsize = -1; |
| 2187 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2188 | return NULL; |
| 2189 | |
| 2190 | if (*mode == 't') |
| 2191 | tm = _O_TEXT; |
| 2192 | else if (*mode != 'b') { |
| 2193 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2194 | return NULL; |
| 2195 | } else |
| 2196 | tm = _O_BINARY; |
| 2197 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2198 | if (bufsize != -1) { |
| 2199 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | |
| 2203 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2204 | |
| 2205 | return f; |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2210 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2211 | * The result of this function is 3 pipes - the process's stdin, |
| 2212 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2213 | */ |
| 2214 | |
| 2215 | static PyObject * |
| 2216 | win32_popen3(PyObject *self, PyObject *args) |
| 2217 | { |
| 2218 | PyObject *f; |
| 2219 | int tm = 0; |
| 2220 | |
| 2221 | char *cmdstring; |
| 2222 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2223 | int bufsize = -1; |
| 2224 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2225 | return NULL; |
| 2226 | |
| 2227 | if (*mode == 't') |
| 2228 | tm = _O_TEXT; |
| 2229 | else if (*mode != 'b') { |
| 2230 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2231 | return NULL; |
| 2232 | } else |
| 2233 | tm = _O_BINARY; |
| 2234 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2235 | if (bufsize != -1) { |
| 2236 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2240 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2241 | |
| 2242 | return f; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * Variation on win32pipe.popen |
| 2247 | * |
| 2248 | * The result of this function is 2 pipes - the processes stdin, |
| 2249 | * and stdout+stderr combined as a single pipe. |
| 2250 | */ |
| 2251 | |
| 2252 | static PyObject * |
| 2253 | win32_popen4(PyObject *self, PyObject *args) |
| 2254 | { |
| 2255 | PyObject *f; |
| 2256 | int tm = 0; |
| 2257 | |
| 2258 | char *cmdstring; |
| 2259 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2260 | int bufsize = -1; |
| 2261 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2262 | return NULL; |
| 2263 | |
| 2264 | if (*mode == 't') |
| 2265 | tm = _O_TEXT; |
| 2266 | else if (*mode != 'b') { |
| 2267 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2268 | return NULL; |
| 2269 | } else |
| 2270 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2271 | |
| 2272 | if (bufsize != -1) { |
| 2273 | PyErr_SetString(PyExc_ValueError, "bufsize must be -1"); |
| 2274 | return NULL; |
| 2275 | } |
| 2276 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2277 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2278 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2279 | return f; |
| 2280 | } |
| 2281 | |
| 2282 | static int |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2283 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2284 | HANDLE hStdin, |
| 2285 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2286 | HANDLE hStderr, |
| 2287 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2288 | { |
| 2289 | PROCESS_INFORMATION piProcInfo; |
| 2290 | STARTUPINFO siStartInfo; |
| 2291 | char *s1,*s2, *s3 = " /c "; |
| 2292 | const char *szConsoleSpawn = "w9xpopen.exe \""; |
| 2293 | int i; |
| 2294 | int x; |
| 2295 | |
| 2296 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
| 2297 | s1 = (char *)_alloca(i); |
| 2298 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2299 | return x; |
| 2300 | if (GetVersion() < 0x80000000) { |
| 2301 | /* |
| 2302 | * NT/2000 |
| 2303 | */ |
| 2304 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2305 | s2 = (char *)_alloca(x); |
| 2306 | ZeroMemory(s2, x); |
| 2307 | sprintf(s2, "%s%s%s", s1, s3, cmdstring); |
| 2308 | } |
| 2309 | else { |
| 2310 | /* |
| 2311 | * Oh gag, we're on Win9x. Use the workaround listed in |
| 2312 | * KB: Q150956 |
| 2313 | */ |
| 2314 | char modulepath[256]; |
| 2315 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2316 | for (i = x = 0; modulepath[i]; i++) |
| 2317 | if (modulepath[i] == '\\') |
| 2318 | x = i+1; |
| 2319 | modulepath[x] = '\0'; |
| 2320 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2321 | strlen(modulepath) + |
| 2322 | strlen(szConsoleSpawn) + 1; |
| 2323 | s2 = (char *)_alloca(x); |
| 2324 | ZeroMemory(s2, x); |
| 2325 | sprintf( |
| 2326 | s2, |
| 2327 | "%s%s%s%s%s\"", |
| 2328 | modulepath, |
| 2329 | szConsoleSpawn, |
| 2330 | s1, |
| 2331 | s3, |
| 2332 | cmdstring); |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2337 | Now we'll just error out.. */ |
| 2338 | else |
| 2339 | return -1; |
| 2340 | |
| 2341 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2342 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2343 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2344 | siStartInfo.hStdInput = hStdin; |
| 2345 | siStartInfo.hStdOutput = hStdout; |
| 2346 | siStartInfo.hStdError = hStderr; |
| 2347 | siStartInfo.wShowWindow = SW_HIDE; |
| 2348 | |
| 2349 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2350 | s2, |
| 2351 | NULL, |
| 2352 | NULL, |
| 2353 | TRUE, |
| 2354 | CREATE_NEW_CONSOLE, |
| 2355 | NULL, |
| 2356 | NULL, |
| 2357 | &siStartInfo, |
| 2358 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2359 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2360 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2361 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2362 | /* Return process handle */ |
| 2363 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2364 | return TRUE; |
| 2365 | } |
| 2366 | return FALSE; |
| 2367 | } |
| 2368 | |
| 2369 | /* The following code is based off of KB: Q190351 */ |
| 2370 | |
| 2371 | static PyObject * |
| 2372 | _PyPopen(char *cmdstring, int mode, int n) |
| 2373 | { |
| 2374 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2375 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2376 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2377 | |
| 2378 | SECURITY_ATTRIBUTES saAttr; |
| 2379 | BOOL fSuccess; |
| 2380 | int fd1, fd2, fd3; |
| 2381 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2382 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2383 | PyObject *f; |
| 2384 | |
| 2385 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2386 | saAttr.bInheritHandle = TRUE; |
| 2387 | saAttr.lpSecurityDescriptor = NULL; |
| 2388 | |
| 2389 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2390 | return win32_error("CreatePipe", NULL); |
| 2391 | |
| 2392 | /* Create new output read handle and the input write handle. Set |
| 2393 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2394 | * the these handles; resulting in non-closeable handles to the pipes |
| 2395 | * being created. */ |
| 2396 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2397 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2398 | FALSE, |
| 2399 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2400 | if (!fSuccess) |
| 2401 | return win32_error("DuplicateHandle", NULL); |
| 2402 | |
| 2403 | /* Close the inheritable version of ChildStdin |
| 2404 | that we're using. */ |
| 2405 | CloseHandle(hChildStdinWr); |
| 2406 | |
| 2407 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2408 | return win32_error("CreatePipe", NULL); |
| 2409 | |
| 2410 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2411 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2412 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2413 | if (!fSuccess) |
| 2414 | return win32_error("DuplicateHandle", NULL); |
| 2415 | |
| 2416 | /* Close the inheritable version of ChildStdout |
| 2417 | that we're using. */ |
| 2418 | CloseHandle(hChildStdoutRd); |
| 2419 | |
| 2420 | if (n != POPEN_4) { |
| 2421 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2422 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2423 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2424 | hChildStderrRd, |
| 2425 | GetCurrentProcess(), |
| 2426 | &hChildStderrRdDup, 0, |
| 2427 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2428 | if (!fSuccess) |
| 2429 | return win32_error("DuplicateHandle", NULL); |
| 2430 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2431 | CloseHandle(hChildStderrRd); |
| 2432 | } |
| 2433 | |
| 2434 | switch (n) { |
| 2435 | case POPEN_1: |
| 2436 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2437 | case _O_WRONLY | _O_TEXT: |
| 2438 | /* Case for writing to child Stdin in text mode. */ |
| 2439 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2440 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2441 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2442 | PyFile_SetBufSize(f, 0); |
| 2443 | /* We don't care about these pipes anymore, so close them. */ |
| 2444 | CloseHandle(hChildStdoutRdDup); |
| 2445 | CloseHandle(hChildStderrRdDup); |
| 2446 | break; |
| 2447 | |
| 2448 | case _O_RDONLY | _O_TEXT: |
| 2449 | /* Case for reading from child Stdout in text mode. */ |
| 2450 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2451 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2452 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2453 | PyFile_SetBufSize(f, 0); |
| 2454 | /* We don't care about these pipes anymore, so close them. */ |
| 2455 | CloseHandle(hChildStdinWrDup); |
| 2456 | CloseHandle(hChildStderrRdDup); |
| 2457 | break; |
| 2458 | |
| 2459 | case _O_RDONLY | _O_BINARY: |
| 2460 | /* Case for readinig from child Stdout in binary mode. */ |
| 2461 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2462 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2463 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2464 | PyFile_SetBufSize(f, 0); |
| 2465 | /* We don't care about these pipes anymore, so close them. */ |
| 2466 | CloseHandle(hChildStdinWrDup); |
| 2467 | CloseHandle(hChildStderrRdDup); |
| 2468 | break; |
| 2469 | |
| 2470 | case _O_WRONLY | _O_BINARY: |
| 2471 | /* Case for writing to child Stdin in binary mode. */ |
| 2472 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2473 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2474 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2475 | PyFile_SetBufSize(f, 0); |
| 2476 | /* We don't care about these pipes anymore, so close them. */ |
| 2477 | CloseHandle(hChildStdoutRdDup); |
| 2478 | CloseHandle(hChildStderrRdDup); |
| 2479 | break; |
| 2480 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2481 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | |
| 2484 | case POPEN_2: |
| 2485 | case POPEN_4: |
| 2486 | { |
| 2487 | char *m1, *m2; |
| 2488 | PyObject *p1, *p2; |
| 2489 | |
| 2490 | if (mode && _O_TEXT) { |
| 2491 | m1 = "r"; |
| 2492 | m2 = "w"; |
| 2493 | } else { |
| 2494 | m1 = "rb"; |
| 2495 | m2 = "wb"; |
| 2496 | } |
| 2497 | |
| 2498 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2499 | f1 = _fdopen(fd1, m2); |
| 2500 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2501 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2502 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2503 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2504 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2505 | PyFile_SetBufSize(p2, 0); |
| 2506 | |
| 2507 | if (n != 4) |
| 2508 | CloseHandle(hChildStderrRdDup); |
| 2509 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2510 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2511 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2512 | break; |
| 2513 | } |
| 2514 | |
| 2515 | case POPEN_3: |
| 2516 | { |
| 2517 | char *m1, *m2; |
| 2518 | PyObject *p1, *p2, *p3; |
| 2519 | |
| 2520 | if (mode && _O_TEXT) { |
| 2521 | m1 = "r"; |
| 2522 | m2 = "w"; |
| 2523 | } else { |
| 2524 | m1 = "rb"; |
| 2525 | m2 = "wb"; |
| 2526 | } |
| 2527 | |
| 2528 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2529 | f1 = _fdopen(fd1, m2); |
| 2530 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2531 | f2 = _fdopen(fd2, m1); |
| 2532 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2533 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2534 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2535 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2536 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2537 | PyFile_SetBufSize(p1, 0); |
| 2538 | PyFile_SetBufSize(p2, 0); |
| 2539 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2540 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2541 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2542 | break; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | if (n == POPEN_4) { |
| 2547 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2548 | hChildStdinRd, |
| 2549 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2550 | hChildStdoutWr, |
| 2551 | &hProcess)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2552 | return win32_error("CreateProcess", NULL); |
| 2553 | } |
| 2554 | else { |
| 2555 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2556 | hChildStdinRd, |
| 2557 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2558 | hChildStderrWr, |
| 2559 | &hProcess)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2560 | return win32_error("CreateProcess", NULL); |
| 2561 | } |
| 2562 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2563 | /* |
| 2564 | * Insert the files we've created into the process dictionary |
| 2565 | * all referencing the list with the process handle and the |
| 2566 | * initial number of files (see description below in _PyPclose). |
| 2567 | * Since if _PyPclose later tried to wait on a process when all |
| 2568 | * handles weren't closed, it could create a deadlock with the |
| 2569 | * child, we spend some energy here to try to ensure that we |
| 2570 | * either insert all file handles into the dictionary or none |
| 2571 | * at all. It's a little clumsy with the various popen modes |
| 2572 | * and variable number of files involved. |
| 2573 | */ |
| 2574 | if (!_PyPopenProcs) { |
| 2575 | _PyPopenProcs = PyDict_New(); |
| 2576 | } |
| 2577 | |
| 2578 | if (_PyPopenProcs) { |
| 2579 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2580 | int ins_rc[3]; |
| 2581 | |
| 2582 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2583 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2584 | |
| 2585 | procObj = PyList_New(2); |
| 2586 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2587 | intObj = PyInt_FromLong(file_count); |
| 2588 | |
| 2589 | if (procObj && hProcessObj && intObj) { |
| 2590 | PyList_SetItem(procObj,0,hProcessObj); |
| 2591 | PyList_SetItem(procObj,1,intObj); |
| 2592 | |
| 2593 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2594 | if (fileObj[0]) { |
| 2595 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2596 | fileObj[0], |
| 2597 | procObj); |
| 2598 | } |
| 2599 | if (file_count >= 2) { |
| 2600 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2601 | if (fileObj[1]) { |
| 2602 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2603 | fileObj[1], |
| 2604 | procObj); |
| 2605 | } |
| 2606 | } |
| 2607 | if (file_count >= 3) { |
| 2608 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2609 | if (fileObj[2]) { |
| 2610 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2611 | fileObj[2], |
| 2612 | procObj); |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2617 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2618 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2619 | /* Something failed - remove any dictionary |
| 2620 | * entries that did make it. |
| 2621 | */ |
| 2622 | if (!ins_rc[0] && fileObj[0]) { |
| 2623 | PyDict_DelItem(_PyPopenProcs, |
| 2624 | fileObj[0]); |
| 2625 | } |
| 2626 | if (!ins_rc[1] && fileObj[1]) { |
| 2627 | PyDict_DelItem(_PyPopenProcs, |
| 2628 | fileObj[1]); |
| 2629 | } |
| 2630 | if (!ins_rc[2] && fileObj[2]) { |
| 2631 | PyDict_DelItem(_PyPopenProcs, |
| 2632 | fileObj[2]); |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | /* |
| 2638 | * Clean up our localized references for the dictionary keys |
| 2639 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2640 | * that got placed in the dictionary. |
| 2641 | */ |
| 2642 | Py_XDECREF(procObj); |
| 2643 | Py_XDECREF(fileObj[0]); |
| 2644 | Py_XDECREF(fileObj[1]); |
| 2645 | Py_XDECREF(fileObj[2]); |
| 2646 | } |
| 2647 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2648 | /* Child is launched. Close the parents copy of those pipe |
| 2649 | * handles that only the child should have open. You need to |
| 2650 | * make sure that no handles to the write end of the output pipe |
| 2651 | * are maintained in this process or else the pipe will not close |
| 2652 | * when the child process exits and the ReadFile will hang. */ |
| 2653 | |
| 2654 | if (!CloseHandle(hChildStdinRd)) |
| 2655 | return win32_error("CloseHandle", NULL); |
| 2656 | |
| 2657 | if (!CloseHandle(hChildStdoutWr)) |
| 2658 | return win32_error("CloseHandle", NULL); |
| 2659 | |
| 2660 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2661 | return win32_error("CloseHandle", NULL); |
| 2662 | |
| 2663 | return f; |
| 2664 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2665 | |
| 2666 | /* |
| 2667 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2668 | * 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] | 2669 | * |
| 2670 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2671 | * input file pointer to information about the process that was |
| 2672 | * originally created by the popen* call that created the file pointer. |
| 2673 | * The dictionary uses the file pointer as a key (with one entry |
| 2674 | * inserted for each file returned by the original popen* call) and a |
| 2675 | * single list object as the value for all files from a single call. |
| 2676 | * The list object contains the Win32 process handle at [0], and a file |
| 2677 | * count at [1], which is initialized to the total number of file |
| 2678 | * handles using that list. |
| 2679 | * |
| 2680 | * This function closes whichever handle it is passed, and decrements |
| 2681 | * the file count in the dictionary for the process handle pointed to |
| 2682 | * by this file. On the last close (when the file count reaches zero), |
| 2683 | * this function will wait for the child process and then return its |
| 2684 | * exit code as the result of the close() operation. This permits the |
| 2685 | * files to be closed in any order - it is always the close() of the |
| 2686 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2687 | */ |
| 2688 | static int _PyPclose(FILE *file) |
| 2689 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2690 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2691 | DWORD exit_code; |
| 2692 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2693 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 2694 | long file_count; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2695 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2696 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2697 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2698 | */ |
| 2699 | result = fclose(file); |
| 2700 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2701 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2702 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 2703 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 2704 | fileObj)) != NULL && |
| 2705 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 2706 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 2707 | |
| 2708 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 2709 | file_count = PyInt_AsLong(intObj); |
| 2710 | |
| 2711 | if (file_count > 1) { |
| 2712 | /* Still other files referencing process */ |
| 2713 | file_count--; |
| 2714 | PyList_SetItem(procObj,1, |
| 2715 | PyInt_FromLong(file_count)); |
| 2716 | } else { |
| 2717 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2718 | if (result != EOF && |
| 2719 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 2720 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2721 | /* Possible truncation here in 16-bit environments, but |
| 2722 | * real exit codes are just the lower byte in any event. |
| 2723 | */ |
| 2724 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2725 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2726 | /* Indicate failure - this will cause the file object |
| 2727 | * to raise an I/O error and translate the last Win32 |
| 2728 | * error code from errno. We do have a problem with |
| 2729 | * last errors that overlap the normal errno table, |
| 2730 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2731 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2732 | if (result != EOF) { |
| 2733 | /* If the error wasn't from the fclose(), then |
| 2734 | * set errno for the file object error handling. |
| 2735 | */ |
| 2736 | errno = GetLastError(); |
| 2737 | } |
| 2738 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | /* Free up the native handle at this point */ |
| 2742 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2743 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2744 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2745 | /* Remove this file pointer from dictionary */ |
| 2746 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 2747 | |
| 2748 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 2749 | Py_DECREF(_PyPopenProcs); |
| 2750 | _PyPopenProcs = NULL; |
| 2751 | } |
| 2752 | |
| 2753 | } /* if object retrieval ok */ |
| 2754 | |
| 2755 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2756 | } /* if _PyPopenProcs */ |
| 2757 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2758 | return result; |
| 2759 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2760 | #else |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2761 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2762 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2763 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2764 | char *name; |
| 2765 | char *mode = "r"; |
| 2766 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2767 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2768 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2769 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2770 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2771 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2772 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2773 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2774 | if (fp == NULL) |
| 2775 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2776 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2777 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2778 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 2779 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2780 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2781 | #endif |
| 2782 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2783 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2785 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2786 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2787 | static char posix_setuid__doc__[] = |
| 2788 | "setuid(uid) -> None\n\ |
| 2789 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2790 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2791 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2792 | { |
| 2793 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2794 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2795 | return NULL; |
| 2796 | if (setuid(uid) < 0) |
| 2797 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | Py_INCREF(Py_None); |
| 2799 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2800 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2801 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2803 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 2804 | #ifdef HAVE_SETEUID |
| 2805 | static char posix_seteuid__doc__[] = |
| 2806 | "seteuid(uid) -> None\n\ |
| 2807 | Set the current process's effective user id."; |
| 2808 | static PyObject * |
| 2809 | posix_seteuid (PyObject *self, PyObject *args) |
| 2810 | { |
| 2811 | int euid; |
| 2812 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 2813 | return NULL; |
| 2814 | } else if (seteuid(euid) < 0) { |
| 2815 | return posix_error(); |
| 2816 | } else { |
| 2817 | Py_INCREF(Py_None); |
| 2818 | return Py_None; |
| 2819 | } |
| 2820 | } |
| 2821 | #endif /* HAVE_SETEUID */ |
| 2822 | |
| 2823 | #ifdef HAVE_SETEGID |
| 2824 | static char posix_setegid__doc__[] = |
| 2825 | "setegid(gid) -> None\n\ |
| 2826 | Set the current process's effective group id."; |
| 2827 | static PyObject * |
| 2828 | posix_setegid (PyObject *self, PyObject *args) |
| 2829 | { |
| 2830 | int egid; |
| 2831 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 2832 | return NULL; |
| 2833 | } else if (setegid(egid) < 0) { |
| 2834 | return posix_error(); |
| 2835 | } else { |
| 2836 | Py_INCREF(Py_None); |
| 2837 | return Py_None; |
| 2838 | } |
| 2839 | } |
| 2840 | #endif /* HAVE_SETEGID */ |
| 2841 | |
| 2842 | #ifdef HAVE_SETREUID |
| 2843 | static char posix_setreuid__doc__[] = |
| 2844 | "seteuid(ruid, euid) -> None\n\ |
| 2845 | Set the current process's real and effective user ids."; |
| 2846 | static PyObject * |
| 2847 | posix_setreuid (PyObject *self, PyObject *args) |
| 2848 | { |
| 2849 | int ruid, euid; |
| 2850 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 2851 | return NULL; |
| 2852 | } else if (setreuid(ruid, euid) < 0) { |
| 2853 | return posix_error(); |
| 2854 | } else { |
| 2855 | Py_INCREF(Py_None); |
| 2856 | return Py_None; |
| 2857 | } |
| 2858 | } |
| 2859 | #endif /* HAVE_SETREUID */ |
| 2860 | |
| 2861 | #ifdef HAVE_SETREGID |
| 2862 | static char posix_setregid__doc__[] = |
| 2863 | "setegid(rgid, egid) -> None\n\ |
| 2864 | Set the current process's real and effective group ids."; |
| 2865 | static PyObject * |
| 2866 | posix_setregid (PyObject *self, PyObject *args) |
| 2867 | { |
| 2868 | int rgid, egid; |
| 2869 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 2870 | return NULL; |
| 2871 | } else if (setregid(rgid, egid) < 0) { |
| 2872 | return posix_error(); |
| 2873 | } else { |
| 2874 | Py_INCREF(Py_None); |
| 2875 | return Py_None; |
| 2876 | } |
| 2877 | } |
| 2878 | #endif /* HAVE_SETREGID */ |
| 2879 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2880 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2881 | static char posix_setgid__doc__[] = |
| 2882 | "setgid(gid) -> None\n\ |
| 2883 | Set the current process's group id."; |
| 2884 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2885 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2886 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2887 | { |
| 2888 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2889 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2890 | return NULL; |
| 2891 | if (setgid(gid) < 0) |
| 2892 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2893 | Py_INCREF(Py_None); |
| 2894 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2895 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2896 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 2897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2898 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2899 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2900 | static char posix_waitpid__doc__[] = |
| 2901 | "waitpid(pid, options) -> (pid, status)\n\ |
| 2902 | Wait for completion of a give child process."; |
| 2903 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2904 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2905 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2906 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2907 | int pid, options; |
| 2908 | #ifdef UNION_WAIT |
| 2909 | union wait status; |
| 2910 | #define status_i (status.w_status) |
| 2911 | #else |
| 2912 | int status; |
| 2913 | #define status_i status |
| 2914 | #endif |
| 2915 | status_i = 0; |
| 2916 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2917 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2918 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2919 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2920 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2921 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 2922 | #else |
| 2923 | pid = waitpid(pid, &status, options); |
| 2924 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2925 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2926 | if (pid == -1) |
| 2927 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2928 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2929 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2930 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2931 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2932 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2933 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2934 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2935 | static char posix_wait__doc__[] = |
| 2936 | "wait() -> (pid, status)\n\ |
| 2937 | Wait for completion of a child process."; |
| 2938 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2939 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2940 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2941 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2942 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2943 | #ifdef UNION_WAIT |
| 2944 | union wait status; |
| 2945 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2946 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2947 | int status; |
| 2948 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 2949 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2950 | if (!PyArg_ParseTuple(args, ":wait")) |
| 2951 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2952 | status_i = 0; |
| 2953 | Py_BEGIN_ALLOW_THREADS |
| 2954 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2955 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 2956 | if (pid == -1) |
| 2957 | return posix_error(); |
| 2958 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 2959 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2960 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2961 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2962 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2963 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2964 | |
| 2965 | static char posix_lstat__doc__[] = |
| 2966 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 2967 | Like stat(path), but do not follow symbolic links."; |
| 2968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2969 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2970 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2971 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2972 | #ifdef HAVE_LSTAT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2973 | return posix_do_stat(self, args, "s:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2974 | #else /* !HAVE_LSTAT */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2975 | return posix_do_stat(self, args, "s:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2976 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2980 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2981 | static char posix_readlink__doc__[] = |
| 2982 | "readlink(path) -> path\n\ |
| 2983 | Return a string representing the path to which the symbolic link points."; |
| 2984 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2985 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2986 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2987 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2988 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2989 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2990 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2991 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2992 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2993 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 2994 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2995 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2996 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2997 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2998 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2999 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3000 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3002 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3003 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3004 | static char posix_symlink__doc__[] = |
| 3005 | "symlink(src, dst) -> None\n\ |
| 3006 | Create a symbolic link."; |
| 3007 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3009 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3010 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3011 | return posix_2str(args, "ss:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3012 | } |
| 3013 | #endif /* HAVE_SYMLINK */ |
| 3014 | |
| 3015 | |
| 3016 | #ifdef HAVE_TIMES |
| 3017 | #ifndef HZ |
| 3018 | #define HZ 60 /* Universal constant :-) */ |
| 3019 | #endif /* HZ */ |
| 3020 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3021 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3022 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3023 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3024 | { |
| 3025 | ULONG value = 0; |
| 3026 | |
| 3027 | Py_BEGIN_ALLOW_THREADS |
| 3028 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3029 | Py_END_ALLOW_THREADS |
| 3030 | |
| 3031 | return value; |
| 3032 | } |
| 3033 | |
| 3034 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3035 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3036 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3037 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3038 | return NULL; |
| 3039 | |
| 3040 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3041 | return Py_BuildValue("ddddd", |
| 3042 | (double)0 /* t.tms_utime / HZ */, |
| 3043 | (double)0 /* t.tms_stime / HZ */, |
| 3044 | (double)0 /* t.tms_cutime / HZ */, |
| 3045 | (double)0 /* t.tms_cstime / HZ */, |
| 3046 | (double)system_uptime() / 1000); |
| 3047 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3048 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3049 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3050 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3051 | { |
| 3052 | struct tms t; |
| 3053 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3054 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3055 | return NULL; |
| 3056 | errno = 0; |
| 3057 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3058 | if (c == (clock_t) -1) |
| 3059 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3060 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3061 | (double)t.tms_utime / HZ, |
| 3062 | (double)t.tms_stime / HZ, |
| 3063 | (double)t.tms_cutime / HZ, |
| 3064 | (double)t.tms_cstime / HZ, |
| 3065 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3066 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3067 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3068 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3069 | |
| 3070 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3071 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3072 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3073 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3074 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3075 | { |
| 3076 | FILETIME create, exit, kernel, user; |
| 3077 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3078 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3079 | return NULL; |
| 3080 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3081 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3082 | /* The fields of a FILETIME structure are the hi and lo part |
| 3083 | of a 64-bit value expressed in 100 nanosecond units. |
| 3084 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3085 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3086 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3087 | return Py_BuildValue( |
| 3088 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3089 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3090 | kernel.dwLowDateTime*1e-7), |
| 3091 | (double)(user.dwHighDateTime*429.4967296 + |
| 3092 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3093 | (double)0, |
| 3094 | (double)0, |
| 3095 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3096 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3097 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3098 | |
| 3099 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3100 | static char posix_times__doc__[] = |
| 3101 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3102 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3103 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3104 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3105 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3106 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3107 | static char posix_setsid__doc__[] = |
| 3108 | "setsid() -> None\n\ |
| 3109 | Call the system call setsid()."; |
| 3110 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3112 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3113 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3114 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3115 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3116 | if (setsid() < 0) |
| 3117 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3118 | Py_INCREF(Py_None); |
| 3119 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3120 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3121 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3122 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3123 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3124 | static char posix_setpgid__doc__[] = |
| 3125 | "setpgid(pid, pgrp) -> None\n\ |
| 3126 | Call the system call setpgid()."; |
| 3127 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3128 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3129 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3130 | { |
| 3131 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3132 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3133 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3134 | if (setpgid(pid, pgrp) < 0) |
| 3135 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3136 | Py_INCREF(Py_None); |
| 3137 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3138 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3139 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3141 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3142 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3143 | static char posix_tcgetpgrp__doc__[] = |
| 3144 | "tcgetpgrp(fd) -> pgid\n\ |
| 3145 | Return the process group associated with the terminal given by a fd."; |
| 3146 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3148 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3149 | { |
| 3150 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3151 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3152 | return NULL; |
| 3153 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3154 | if (pgid < 0) |
| 3155 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3156 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3157 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3158 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3160 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3161 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3162 | static char posix_tcsetpgrp__doc__[] = |
| 3163 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3164 | Set the process group associated with the terminal given by a fd."; |
| 3165 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3166 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3167 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3168 | { |
| 3169 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3170 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3171 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3172 | if (tcsetpgrp(fd, pgid) < 0) |
| 3173 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3174 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3175 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3176 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3177 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3178 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3179 | /* Functions acting on file descriptors */ |
| 3180 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3181 | static char posix_open__doc__[] = |
| 3182 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3183 | Open a file (for low level IO)."; |
| 3184 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3185 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3186 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3187 | { |
| 3188 | char *file; |
| 3189 | int flag; |
| 3190 | int mode = 0777; |
| 3191 | int fd; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3192 | if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode)) |
| 3193 | return NULL; |
| 3194 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3195 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3196 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3197 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3198 | if (fd < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3199 | return posix_error_with_filename(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3200 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3203 | |
| 3204 | static char posix_close__doc__[] = |
| 3205 | "close(fd) -> None\n\ |
| 3206 | Close a file descriptor (for low level IO)."; |
| 3207 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3208 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3209 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3210 | { |
| 3211 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3212 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3213 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3214 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3215 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3216 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3217 | if (res < 0) |
| 3218 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3219 | Py_INCREF(Py_None); |
| 3220 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3223 | |
| 3224 | static char posix_dup__doc__[] = |
| 3225 | "dup(fd) -> fd2\n\ |
| 3226 | Return a duplicate of a file descriptor."; |
| 3227 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3229 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3230 | { |
| 3231 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3232 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3233 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3234 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3235 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3236 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3237 | if (fd < 0) |
| 3238 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3239 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3242 | |
| 3243 | static char posix_dup2__doc__[] = |
| 3244 | "dup2(fd, fd2) -> None\n\ |
| 3245 | Duplicate file descriptor."; |
| 3246 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3247 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3248 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3249 | { |
| 3250 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3251 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3252 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3253 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3254 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3255 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3256 | if (res < 0) |
| 3257 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3258 | Py_INCREF(Py_None); |
| 3259 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3262 | |
| 3263 | static char posix_lseek__doc__[] = |
| 3264 | "lseek(fd, pos, how) -> newpos\n\ |
| 3265 | Set the current position of a file descriptor."; |
| 3266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3267 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3268 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3269 | { |
| 3270 | int fd, how; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3271 | #ifdef MS_WIN64 |
| 3272 | LONG_LONG pos, res; |
| 3273 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3274 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3275 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3276 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3277 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3278 | return NULL; |
| 3279 | #ifdef SEEK_SET |
| 3280 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3281 | switch (how) { |
| 3282 | case 0: how = SEEK_SET; break; |
| 3283 | case 1: how = SEEK_CUR; break; |
| 3284 | case 2: how = SEEK_END; break; |
| 3285 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3286 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3287 | |
| 3288 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3289 | pos = PyInt_AsLong(posobj); |
| 3290 | #else |
| 3291 | pos = PyLong_Check(posobj) ? |
| 3292 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3293 | #endif |
| 3294 | if (PyErr_Occurred()) |
| 3295 | return NULL; |
| 3296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3297 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3298 | #ifdef MS_WIN64 |
| 3299 | res = _lseeki64(fd, pos, how); |
| 3300 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3301 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3302 | #endif |
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(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3306 | |
| 3307 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3308 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3309 | #else |
| 3310 | return PyLong_FromLongLong(res); |
| 3311 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3314 | |
| 3315 | static char posix_read__doc__[] = |
| 3316 | "read(fd, buffersize) -> string\n\ |
| 3317 | Read a file descriptor."; |
| 3318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3319 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3320 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3321 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3322 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3323 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3324 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3325 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3326 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3327 | if (buffer == NULL) |
| 3328 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3329 | Py_BEGIN_ALLOW_THREADS |
| 3330 | n = read(fd, PyString_AsString(buffer), size); |
| 3331 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3332 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3333 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3334 | return posix_error(); |
| 3335 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3336 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3337 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3338 | return buffer; |
| 3339 | } |
| 3340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3341 | |
| 3342 | static char posix_write__doc__[] = |
| 3343 | "write(fd, string) -> byteswritten\n\ |
| 3344 | Write a string to a file descriptor."; |
| 3345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3347 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3348 | { |
| 3349 | int fd, size; |
| 3350 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3351 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3352 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3353 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3354 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3355 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3356 | if (size < 0) |
| 3357 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3358 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3361 | |
| 3362 | static char posix_fstat__doc__[]= |
| 3363 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3364 | Like stat(), but for an open file descriptor."; |
| 3365 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3366 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3367 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3368 | { |
| 3369 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3370 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3371 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3372 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
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 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3375 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3376 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3377 | if (res != 0) |
| 3378 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3379 | |
| 3380 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3383 | |
| 3384 | static char posix_fdopen__doc__[] = |
| 3385 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3386 | Return an open file object connected to a file descriptor."; |
| 3387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3389 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3390 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3391 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3392 | char *mode = "r"; |
| 3393 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3394 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3395 | PyObject *f; |
| 3396 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3397 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3399 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3400 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3401 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3402 | if (fp == NULL) |
| 3403 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3404 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3405 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3406 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3407 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3410 | static char posix_isatty__doc__[] = |
| 3411 | "isatty(fd) -> Boolean\n\ |
| 3412 | Return true if the file descriptor 'fd' is an open file descriptor\n\ |
| 3413 | connected to a terminal."; |
| 3414 | |
| 3415 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3416 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3417 | { |
| 3418 | int fd; |
| 3419 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3420 | return NULL; |
| 3421 | return Py_BuildValue("i", isatty(fd)); |
| 3422 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3423 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3424 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3425 | static char posix_pipe__doc__[] = |
| 3426 | "pipe() -> (read_end, write_end)\n\ |
| 3427 | Create a pipe."; |
| 3428 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3429 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3430 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3431 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3432 | #if defined(PYOS_OS2) |
| 3433 | HFILE read, write; |
| 3434 | APIRET rc; |
| 3435 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3436 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3437 | return NULL; |
| 3438 | |
| 3439 | Py_BEGIN_ALLOW_THREADS |
| 3440 | rc = DosCreatePipe( &read, &write, 4096); |
| 3441 | Py_END_ALLOW_THREADS |
| 3442 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3443 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3444 | |
| 3445 | return Py_BuildValue("(ii)", read, write); |
| 3446 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3447 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3448 | int fds[2]; |
| 3449 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3450 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3451 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3452 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3453 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3455 | if (res != 0) |
| 3456 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3457 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3458 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3459 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3460 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3461 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3462 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3463 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3464 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3465 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3466 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3467 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3468 | return win32_error("CreatePipe", NULL); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3469 | read_fd = _open_osfhandle((intptr_t)read, 0); |
| 3470 | write_fd = _open_osfhandle((intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3471 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3472 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3473 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3474 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3475 | #endif /* HAVE_PIPE */ |
| 3476 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3477 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3478 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3479 | static char posix_mkfifo__doc__[] = |
| 3480 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3481 | Create a FIFO (a POSIX named pipe)."; |
| 3482 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3484 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3485 | { |
| 3486 | char *file; |
| 3487 | int mode = 0666; |
| 3488 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3489 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3490 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3491 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3492 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3493 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3494 | if (res < 0) |
| 3495 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3496 | Py_INCREF(Py_None); |
| 3497 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3498 | } |
| 3499 | #endif |
| 3500 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3501 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3502 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3503 | static char posix_ftruncate__doc__[] = |
| 3504 | "ftruncate(fd, length) -> None\n\ |
| 3505 | Truncate a file to a specified length."; |
| 3506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3508 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3509 | { |
| 3510 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3511 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3512 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3513 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3514 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3515 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3516 | return NULL; |
| 3517 | |
| 3518 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3519 | length = PyInt_AsLong(lenobj); |
| 3520 | #else |
| 3521 | length = PyLong_Check(lenobj) ? |
| 3522 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3523 | #endif |
| 3524 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3525 | return NULL; |
| 3526 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3527 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3528 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3529 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3530 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3531 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3532 | return NULL; |
| 3533 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3534 | Py_INCREF(Py_None); |
| 3535 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3536 | } |
| 3537 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3538 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3539 | #ifdef NeXT |
| 3540 | #define HAVE_PUTENV |
| 3541 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3542 | static int |
| 3543 | putenv(char *newval) |
| 3544 | { |
| 3545 | extern char **environ; |
| 3546 | |
| 3547 | static int firstTime = 1; |
| 3548 | char **ep; |
| 3549 | char *cp; |
| 3550 | int esiz; |
| 3551 | char *np; |
| 3552 | |
| 3553 | if (!(np = strchr(newval, '='))) |
| 3554 | return 1; |
| 3555 | *np = '\0'; |
| 3556 | |
| 3557 | /* look it up */ |
| 3558 | for (ep=environ ; *ep ; ep++) |
| 3559 | { |
| 3560 | /* this should always be true... */ |
| 3561 | if (cp = strchr(*ep, '=')) |
| 3562 | { |
| 3563 | *cp = '\0'; |
| 3564 | if (!strcmp(*ep, newval)) |
| 3565 | { |
| 3566 | /* got it! */ |
| 3567 | *cp = '='; |
| 3568 | break; |
| 3569 | } |
| 3570 | *cp = '='; |
| 3571 | } |
| 3572 | else |
| 3573 | { |
| 3574 | *np = '='; |
| 3575 | return 1; |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | *np = '='; |
| 3580 | if (*ep) |
| 3581 | { |
| 3582 | /* the string was already there: |
| 3583 | just replace it with the new one */ |
| 3584 | *ep = newval; |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | /* expand environ by one */ |
| 3589 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3590 | esiz++; |
| 3591 | if (firstTime) |
| 3592 | { |
| 3593 | char **epp; |
| 3594 | char **newenv; |
| 3595 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 3596 | return 1; |
| 3597 | |
| 3598 | for (ep=environ, epp=newenv ; *ep ;) |
| 3599 | *epp++ = *ep++; |
| 3600 | *epp++ = newval; |
| 3601 | *epp = (char *) 0; |
| 3602 | environ = newenv; |
| 3603 | } |
| 3604 | else |
| 3605 | { |
| 3606 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 3607 | return 1; |
| 3608 | environ[esiz - 2] = newval; |
| 3609 | environ[esiz - 1] = (char *) 0; |
| 3610 | firstTime = 0; |
| 3611 | } |
| 3612 | |
| 3613 | return 0; |
| 3614 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 3615 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3616 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3617 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3618 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3619 | static char posix_putenv__doc__[] = |
| 3620 | "putenv(key, value) -> None\n\ |
| 3621 | Change or add an environment variable."; |
| 3622 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3623 | /* Save putenv() parameters as values here, so we can collect them when they |
| 3624 | * get re-set with another call for the same key. */ |
| 3625 | static PyObject *posix_putenv_garbage; |
| 3626 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3627 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3628 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3629 | { |
| 3630 | char *s1, *s2; |
| 3631 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3632 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3633 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3634 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3635 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3636 | |
| 3637 | #if defined(PYOS_OS2) |
| 3638 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 3639 | APIRET rc; |
| 3640 | |
| 3641 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3642 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3643 | |
| 3644 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 3645 | if (rc != NO_ERROR) |
| 3646 | return os2_error(rc); |
| 3647 | |
| 3648 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 3649 | APIRET rc; |
| 3650 | |
| 3651 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 3652 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 3653 | |
| 3654 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 3655 | if (rc != NO_ERROR) |
| 3656 | return os2_error(rc); |
| 3657 | } else { |
| 3658 | #endif |
| 3659 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3660 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 3661 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 3662 | if (newstr == NULL) |
| 3663 | return PyErr_NoMemory(); |
| 3664 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3665 | (void) sprintf(new, "%s=%s", s1, s2); |
| 3666 | if (putenv(new)) { |
| 3667 | posix_error(); |
| 3668 | return NULL; |
| 3669 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 3670 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 3671 | * this will cause previous value to be collected. This has to |
| 3672 | * happen after the real putenv() call because the old value |
| 3673 | * was still accessible until then. */ |
| 3674 | if (PyDict_SetItem(posix_putenv_garbage, |
| 3675 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 3676 | /* really not much we can do; just leak */ |
| 3677 | PyErr_Clear(); |
| 3678 | } |
| 3679 | else { |
| 3680 | Py_DECREF(newstr); |
| 3681 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3682 | |
| 3683 | #if defined(PYOS_OS2) |
| 3684 | } |
| 3685 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3686 | Py_INCREF(Py_None); |
| 3687 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3688 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3689 | #endif /* putenv */ |
| 3690 | |
| 3691 | #ifdef HAVE_STRERROR |
| 3692 | static char posix_strerror__doc__[] = |
| 3693 | "strerror(code) -> string\n\ |
| 3694 | Translate an error code to a message string."; |
| 3695 | |
| 3696 | PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3697 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3698 | { |
| 3699 | int code; |
| 3700 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3701 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 3702 | return NULL; |
| 3703 | message = strerror(code); |
| 3704 | if (message == NULL) { |
| 3705 | PyErr_SetString(PyExc_ValueError, |
| 3706 | "strerror code out of range"); |
| 3707 | return NULL; |
| 3708 | } |
| 3709 | return PyString_FromString(message); |
| 3710 | } |
| 3711 | #endif /* strerror */ |
| 3712 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 3713 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3714 | #ifdef HAVE_SYS_WAIT_H |
| 3715 | |
| 3716 | #ifdef WIFSTOPPED |
| 3717 | static char posix_WIFSTOPPED__doc__[] = |
| 3718 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3719 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3720 | |
| 3721 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3722 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3723 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3724 | #ifdef UNION_WAIT |
| 3725 | union wait status; |
| 3726 | #define status_i (status.w_status) |
| 3727 | #else |
| 3728 | int status; |
| 3729 | #define status_i status |
| 3730 | #endif |
| 3731 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3732 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3733 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3734 | { |
| 3735 | return NULL; |
| 3736 | } |
| 3737 | |
| 3738 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3739 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3740 | } |
| 3741 | #endif /* WIFSTOPPED */ |
| 3742 | |
| 3743 | #ifdef WIFSIGNALED |
| 3744 | static char posix_WIFSIGNALED__doc__[] = |
| 3745 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 3746 | 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] | 3747 | |
| 3748 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3749 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3750 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3751 | #ifdef UNION_WAIT |
| 3752 | union wait status; |
| 3753 | #define status_i (status.w_status) |
| 3754 | #else |
| 3755 | int status; |
| 3756 | #define status_i status |
| 3757 | #endif |
| 3758 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3759 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3760 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3761 | { |
| 3762 | return NULL; |
| 3763 | } |
| 3764 | |
| 3765 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3766 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3767 | } |
| 3768 | #endif /* WIFSIGNALED */ |
| 3769 | |
| 3770 | #ifdef WIFEXITED |
| 3771 | static char posix_WIFEXITED__doc__[] = |
| 3772 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3773 | Return true if the process returning 'status' exited using the exit()\n\ |
| 3774 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3775 | |
| 3776 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3777 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3778 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3779 | #ifdef UNION_WAIT |
| 3780 | union wait status; |
| 3781 | #define status_i (status.w_status) |
| 3782 | #else |
| 3783 | int status; |
| 3784 | #define status_i status |
| 3785 | #endif |
| 3786 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3787 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3788 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3789 | { |
| 3790 | return NULL; |
| 3791 | } |
| 3792 | |
| 3793 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3794 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3795 | } |
| 3796 | #endif /* WIFEXITED */ |
| 3797 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3798 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3799 | static char posix_WEXITSTATUS__doc__[] = |
| 3800 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3801 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3802 | |
| 3803 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3804 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3805 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3806 | #ifdef UNION_WAIT |
| 3807 | union wait status; |
| 3808 | #define status_i (status.w_status) |
| 3809 | #else |
| 3810 | int status; |
| 3811 | #define status_i status |
| 3812 | #endif |
| 3813 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3814 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3815 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3816 | { |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | |
| 3820 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3821 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3822 | } |
| 3823 | #endif /* WEXITSTATUS */ |
| 3824 | |
| 3825 | #ifdef WTERMSIG |
| 3826 | static char posix_WTERMSIG__doc__[] = |
| 3827 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3828 | Return the signal that terminated the process that provided the 'status'\n\ |
| 3829 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3830 | |
| 3831 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3832 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3833 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3834 | #ifdef UNION_WAIT |
| 3835 | union wait status; |
| 3836 | #define status_i (status.w_status) |
| 3837 | #else |
| 3838 | int status; |
| 3839 | #define status_i status |
| 3840 | #endif |
| 3841 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3842 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3843 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3844 | { |
| 3845 | return NULL; |
| 3846 | } |
| 3847 | |
| 3848 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3849 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3850 | } |
| 3851 | #endif /* WTERMSIG */ |
| 3852 | |
| 3853 | #ifdef WSTOPSIG |
| 3854 | static char posix_WSTOPSIG__doc__[] = |
| 3855 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 3856 | 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] | 3857 | |
| 3858 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3859 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3860 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3861 | #ifdef UNION_WAIT |
| 3862 | union wait status; |
| 3863 | #define status_i (status.w_status) |
| 3864 | #else |
| 3865 | int status; |
| 3866 | #define status_i status |
| 3867 | #endif |
| 3868 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3869 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3870 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3871 | { |
| 3872 | return NULL; |
| 3873 | } |
| 3874 | |
| 3875 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3876 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 3877 | } |
| 3878 | #endif /* WSTOPSIG */ |
| 3879 | |
| 3880 | #endif /* HAVE_SYS_WAIT_H */ |
| 3881 | |
| 3882 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3883 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 3884 | #ifdef _SCO_DS |
| 3885 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 3886 | needed definitions in sys/statvfs.h */ |
| 3887 | #define _SVID3 |
| 3888 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3889 | #include <sys/statvfs.h> |
| 3890 | |
| 3891 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3892 | "fstatvfs(fd) -> \n\ |
| 3893 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3894 | Perform an fstatvfs system call on the given fd."; |
| 3895 | |
| 3896 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3897 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3898 | { |
| 3899 | int fd, res; |
| 3900 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3901 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3902 | return NULL; |
| 3903 | Py_BEGIN_ALLOW_THREADS |
| 3904 | res = fstatvfs(fd, &st); |
| 3905 | Py_END_ALLOW_THREADS |
| 3906 | if (res != 0) |
| 3907 | return posix_error(); |
| 3908 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3909 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3910 | (long) st.f_bsize, |
| 3911 | (long) st.f_frsize, |
| 3912 | (long) st.f_blocks, |
| 3913 | (long) st.f_bfree, |
| 3914 | (long) st.f_bavail, |
| 3915 | (long) st.f_files, |
| 3916 | (long) st.f_ffree, |
| 3917 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3918 | (long) st.f_flag, |
| 3919 | (long) st.f_namemax); |
| 3920 | #else |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3921 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3922 | (long) st.f_bsize, |
| 3923 | (long) st.f_frsize, |
| 3924 | (LONG_LONG) st.f_blocks, |
| 3925 | (LONG_LONG) st.f_bfree, |
| 3926 | (LONG_LONG) st.f_bavail, |
| 3927 | (LONG_LONG) st.f_files, |
| 3928 | (LONG_LONG) st.f_ffree, |
| 3929 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3930 | (long) st.f_flag, |
| 3931 | (long) st.f_namemax); |
| 3932 | #endif |
| 3933 | } |
| 3934 | #endif /* HAVE_FSTATVFS */ |
| 3935 | |
| 3936 | |
| 3937 | #if defined(HAVE_STATVFS) |
| 3938 | #include <sys/statvfs.h> |
| 3939 | |
| 3940 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3941 | "statvfs(path) -> \n\ |
| 3942 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3943 | Perform a statvfs system call on the given path."; |
| 3944 | |
| 3945 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3946 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3947 | { |
| 3948 | char *path; |
| 3949 | int res; |
| 3950 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3951 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3952 | return NULL; |
| 3953 | Py_BEGIN_ALLOW_THREADS |
| 3954 | res = statvfs(path, &st); |
| 3955 | Py_END_ALLOW_THREADS |
| 3956 | if (res != 0) |
| 3957 | return posix_error_with_filename(path); |
| 3958 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3959 | return Py_BuildValue("(llllllllll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3960 | (long) st.f_bsize, |
| 3961 | (long) st.f_frsize, |
| 3962 | (long) st.f_blocks, |
| 3963 | (long) st.f_bfree, |
| 3964 | (long) st.f_bavail, |
| 3965 | (long) st.f_files, |
| 3966 | (long) st.f_ffree, |
| 3967 | (long) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3968 | (long) st.f_flag, |
| 3969 | (long) st.f_namemax); |
| 3970 | #else /* HAVE_LARGEFILE_SUPPORT */ |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 3971 | return Py_BuildValue("(llLLLLLLll)", |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3972 | (long) st.f_bsize, |
| 3973 | (long) st.f_frsize, |
| 3974 | (LONG_LONG) st.f_blocks, |
| 3975 | (LONG_LONG) st.f_bfree, |
| 3976 | (LONG_LONG) st.f_bavail, |
| 3977 | (LONG_LONG) st.f_files, |
| 3978 | (LONG_LONG) st.f_ffree, |
| 3979 | (LONG_LONG) st.f_favail, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3980 | (long) st.f_flag, |
| 3981 | (long) st.f_namemax); |
| 3982 | #endif |
| 3983 | } |
| 3984 | #endif /* HAVE_STATVFS */ |
| 3985 | |
| 3986 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3987 | #ifdef HAVE_TEMPNAM |
| 3988 | static char posix_tempnam__doc__[] = "\ |
| 3989 | tempnam([dir[, prefix]]) -> string\n\ |
| 3990 | Return a unique name for a temporary file.\n\ |
| 3991 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 3992 | or None if not needed."; |
| 3993 | |
| 3994 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3995 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3996 | { |
| 3997 | PyObject *result = NULL; |
| 3998 | char *dir = NULL; |
| 3999 | char *pfx = NULL; |
| 4000 | char *name; |
| 4001 | |
| 4002 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4003 | return NULL; |
| 4004 | name = tempnam(dir, pfx); |
| 4005 | if (name == NULL) |
| 4006 | return PyErr_NoMemory(); |
| 4007 | result = PyString_FromString(name); |
| 4008 | free(name); |
| 4009 | return result; |
| 4010 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4011 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4012 | |
| 4013 | |
| 4014 | #ifdef HAVE_TMPFILE |
| 4015 | static char posix_tmpfile__doc__[] = "\ |
| 4016 | tmpfile() -> file object\n\ |
| 4017 | Create a temporary file with no directory entries."; |
| 4018 | |
| 4019 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4020 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4021 | { |
| 4022 | FILE *fp; |
| 4023 | |
| 4024 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4025 | return NULL; |
| 4026 | fp = tmpfile(); |
| 4027 | if (fp == NULL) |
| 4028 | return posix_error(); |
| 4029 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4030 | } |
| 4031 | #endif |
| 4032 | |
| 4033 | |
| 4034 | #ifdef HAVE_TMPNAM |
| 4035 | static char posix_tmpnam__doc__[] = "\ |
| 4036 | tmpnam() -> string\n\ |
| 4037 | Return a unique name for a temporary file."; |
| 4038 | |
| 4039 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4040 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4041 | { |
| 4042 | char buffer[L_tmpnam]; |
| 4043 | char *name; |
| 4044 | |
| 4045 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4046 | return NULL; |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4047 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4048 | name = tmpnam_r(buffer); |
| 4049 | #else |
| 4050 | name = tmpnam(buffer); |
| 4051 | #endif |
| 4052 | if (name == NULL) { |
| 4053 | PyErr_SetObject(PyExc_OSError, |
| 4054 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4055 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4056 | "unexpected NULL from tmpnam_r" |
| 4057 | #else |
| 4058 | "unexpected NULL from tmpnam" |
| 4059 | #endif |
| 4060 | )); |
| 4061 | return NULL; |
| 4062 | } |
| 4063 | return PyString_FromString(buffer); |
| 4064 | } |
| 4065 | #endif |
| 4066 | |
| 4067 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4068 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4069 | * It maps strings representing configuration variable names to |
| 4070 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4071 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4072 | * rarely-used constants. There are three separate tables that use |
| 4073 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4074 | * |
| 4075 | * This code is always included, even if none of the interfaces that |
| 4076 | * need it are included. The #if hackery needed to avoid it would be |
| 4077 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4078 | */ |
| 4079 | struct constdef { |
| 4080 | char *name; |
| 4081 | long value; |
| 4082 | }; |
| 4083 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4084 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4085 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4086 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4087 | { |
| 4088 | if (PyInt_Check(arg)) { |
| 4089 | *valuep = PyInt_AS_LONG(arg); |
| 4090 | return 1; |
| 4091 | } |
| 4092 | if (PyString_Check(arg)) { |
| 4093 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4094 | size_t lo = 0; |
| 4095 | size_t mid; |
| 4096 | size_t hi = tablesize; |
| 4097 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4098 | char *confname = PyString_AS_STRING(arg); |
| 4099 | while (lo < hi) { |
| 4100 | mid = (lo + hi) / 2; |
| 4101 | cmp = strcmp(confname, table[mid].name); |
| 4102 | if (cmp < 0) |
| 4103 | hi = mid; |
| 4104 | else if (cmp > 0) |
| 4105 | lo = mid + 1; |
| 4106 | else { |
| 4107 | *valuep = table[mid].value; |
| 4108 | return 1; |
| 4109 | } |
| 4110 | } |
| 4111 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4112 | } |
| 4113 | else |
| 4114 | PyErr_SetString(PyExc_TypeError, |
| 4115 | "configuration names must be strings or integers"); |
| 4116 | return 0; |
| 4117 | } |
| 4118 | |
| 4119 | |
| 4120 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4121 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4122 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4123 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4124 | #endif |
| 4125 | #ifdef _PC_ABI_ASYNC_IO |
| 4126 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4127 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4128 | #ifdef _PC_ASYNC_IO |
| 4129 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4130 | #endif |
| 4131 | #ifdef _PC_CHOWN_RESTRICTED |
| 4132 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4133 | #endif |
| 4134 | #ifdef _PC_FILESIZEBITS |
| 4135 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4136 | #endif |
| 4137 | #ifdef _PC_LAST |
| 4138 | {"PC_LAST", _PC_LAST}, |
| 4139 | #endif |
| 4140 | #ifdef _PC_LINK_MAX |
| 4141 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4142 | #endif |
| 4143 | #ifdef _PC_MAX_CANON |
| 4144 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4145 | #endif |
| 4146 | #ifdef _PC_MAX_INPUT |
| 4147 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4148 | #endif |
| 4149 | #ifdef _PC_NAME_MAX |
| 4150 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4151 | #endif |
| 4152 | #ifdef _PC_NO_TRUNC |
| 4153 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4154 | #endif |
| 4155 | #ifdef _PC_PATH_MAX |
| 4156 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4157 | #endif |
| 4158 | #ifdef _PC_PIPE_BUF |
| 4159 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4160 | #endif |
| 4161 | #ifdef _PC_PRIO_IO |
| 4162 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4163 | #endif |
| 4164 | #ifdef _PC_SOCK_MAXBUF |
| 4165 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4166 | #endif |
| 4167 | #ifdef _PC_SYNC_IO |
| 4168 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4169 | #endif |
| 4170 | #ifdef _PC_VDISABLE |
| 4171 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4172 | #endif |
| 4173 | }; |
| 4174 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4175 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4176 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4177 | { |
| 4178 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4179 | sizeof(posix_constants_pathconf) |
| 4180 | / sizeof(struct constdef)); |
| 4181 | } |
| 4182 | #endif |
| 4183 | |
| 4184 | #ifdef HAVE_FPATHCONF |
| 4185 | static char posix_fpathconf__doc__[] = "\ |
| 4186 | fpathconf(fd, name) -> integer\n\ |
| 4187 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4188 | If there is no limit, return -1."; |
| 4189 | |
| 4190 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4191 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4192 | { |
| 4193 | PyObject *result = NULL; |
| 4194 | int name, fd; |
| 4195 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4196 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4197 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4198 | long limit; |
| 4199 | |
| 4200 | errno = 0; |
| 4201 | limit = fpathconf(fd, name); |
| 4202 | if (limit == -1 && errno != 0) |
| 4203 | posix_error(); |
| 4204 | else |
| 4205 | result = PyInt_FromLong(limit); |
| 4206 | } |
| 4207 | return result; |
| 4208 | } |
| 4209 | #endif |
| 4210 | |
| 4211 | |
| 4212 | #ifdef HAVE_PATHCONF |
| 4213 | static char posix_pathconf__doc__[] = "\ |
| 4214 | pathconf(path, name) -> integer\n\ |
| 4215 | Return the configuration limit name for the file or directory path.\n\ |
| 4216 | If there is no limit, return -1."; |
| 4217 | |
| 4218 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4219 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4220 | { |
| 4221 | PyObject *result = NULL; |
| 4222 | int name; |
| 4223 | char *path; |
| 4224 | |
| 4225 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4226 | conv_path_confname, &name)) { |
| 4227 | long limit; |
| 4228 | |
| 4229 | errno = 0; |
| 4230 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4231 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4232 | if (errno == EINVAL) |
| 4233 | /* could be a path or name problem */ |
| 4234 | posix_error(); |
| 4235 | else |
| 4236 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4237 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4238 | else |
| 4239 | result = PyInt_FromLong(limit); |
| 4240 | } |
| 4241 | return result; |
| 4242 | } |
| 4243 | #endif |
| 4244 | |
| 4245 | #ifdef HAVE_CONFSTR |
| 4246 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4247 | #ifdef _CS_ARCHITECTURE |
| 4248 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4249 | #endif |
| 4250 | #ifdef _CS_HOSTNAME |
| 4251 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4252 | #endif |
| 4253 | #ifdef _CS_HW_PROVIDER |
| 4254 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4255 | #endif |
| 4256 | #ifdef _CS_HW_SERIAL |
| 4257 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4258 | #endif |
| 4259 | #ifdef _CS_INITTAB_NAME |
| 4260 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4261 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4262 | #ifdef _CS_LFS64_CFLAGS |
| 4263 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4264 | #endif |
| 4265 | #ifdef _CS_LFS64_LDFLAGS |
| 4266 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4267 | #endif |
| 4268 | #ifdef _CS_LFS64_LIBS |
| 4269 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4270 | #endif |
| 4271 | #ifdef _CS_LFS64_LINTFLAGS |
| 4272 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4273 | #endif |
| 4274 | #ifdef _CS_LFS_CFLAGS |
| 4275 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4276 | #endif |
| 4277 | #ifdef _CS_LFS_LDFLAGS |
| 4278 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4279 | #endif |
| 4280 | #ifdef _CS_LFS_LIBS |
| 4281 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4282 | #endif |
| 4283 | #ifdef _CS_LFS_LINTFLAGS |
| 4284 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4285 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4286 | #ifdef _CS_MACHINE |
| 4287 | {"CS_MACHINE", _CS_MACHINE}, |
| 4288 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4289 | #ifdef _CS_PATH |
| 4290 | {"CS_PATH", _CS_PATH}, |
| 4291 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4292 | #ifdef _CS_RELEASE |
| 4293 | {"CS_RELEASE", _CS_RELEASE}, |
| 4294 | #endif |
| 4295 | #ifdef _CS_SRPC_DOMAIN |
| 4296 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4297 | #endif |
| 4298 | #ifdef _CS_SYSNAME |
| 4299 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4300 | #endif |
| 4301 | #ifdef _CS_VERSION |
| 4302 | {"CS_VERSION", _CS_VERSION}, |
| 4303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4304 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4305 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4306 | #endif |
| 4307 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4308 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4309 | #endif |
| 4310 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4311 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4312 | #endif |
| 4313 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4314 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4315 | #endif |
| 4316 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4317 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4318 | #endif |
| 4319 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4320 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4321 | #endif |
| 4322 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4323 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4324 | #endif |
| 4325 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4326 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4327 | #endif |
| 4328 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4329 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4330 | #endif |
| 4331 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4332 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4333 | #endif |
| 4334 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4335 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4336 | #endif |
| 4337 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4338 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4339 | #endif |
| 4340 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4341 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4342 | #endif |
| 4343 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4344 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4345 | #endif |
| 4346 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4347 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4348 | #endif |
| 4349 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4350 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4351 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4352 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4353 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4354 | #endif |
| 4355 | #ifdef _MIPS_CS_BASE |
| 4356 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4357 | #endif |
| 4358 | #ifdef _MIPS_CS_HOSTID |
| 4359 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4360 | #endif |
| 4361 | #ifdef _MIPS_CS_HW_NAME |
| 4362 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4363 | #endif |
| 4364 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4365 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4366 | #endif |
| 4367 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4368 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4369 | #endif |
| 4370 | #ifdef _MIPS_CS_OSREL_MIN |
| 4371 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4372 | #endif |
| 4373 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4374 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4375 | #endif |
| 4376 | #ifdef _MIPS_CS_OS_NAME |
| 4377 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4378 | #endif |
| 4379 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4380 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4381 | #endif |
| 4382 | #ifdef _MIPS_CS_PROCESSORS |
| 4383 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4384 | #endif |
| 4385 | #ifdef _MIPS_CS_SERIAL |
| 4386 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4387 | #endif |
| 4388 | #ifdef _MIPS_CS_VENDOR |
| 4389 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4390 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4391 | }; |
| 4392 | |
| 4393 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4394 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4395 | { |
| 4396 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4397 | sizeof(posix_constants_confstr) |
| 4398 | / sizeof(struct constdef)); |
| 4399 | } |
| 4400 | |
| 4401 | static char posix_confstr__doc__[] = "\ |
| 4402 | confstr(name) -> string\n\ |
| 4403 | Return a string-valued system configuration variable."; |
| 4404 | |
| 4405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4406 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4407 | { |
| 4408 | PyObject *result = NULL; |
| 4409 | int name; |
| 4410 | char buffer[64]; |
| 4411 | |
| 4412 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4413 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4414 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4415 | errno = 0; |
| 4416 | if (len == 0) { |
| 4417 | if (errno != 0) |
| 4418 | posix_error(); |
| 4419 | else |
| 4420 | result = PyString_FromString(""); |
| 4421 | } |
| 4422 | else { |
| 4423 | if (len >= sizeof(buffer)) { |
| 4424 | result = PyString_FromStringAndSize(NULL, len); |
| 4425 | if (result != NULL) |
| 4426 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4427 | } |
| 4428 | else |
| 4429 | result = PyString_FromString(buffer); |
| 4430 | } |
| 4431 | } |
| 4432 | return result; |
| 4433 | } |
| 4434 | #endif |
| 4435 | |
| 4436 | |
| 4437 | #ifdef HAVE_SYSCONF |
| 4438 | static struct constdef posix_constants_sysconf[] = { |
| 4439 | #ifdef _SC_2_CHAR_TERM |
| 4440 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4441 | #endif |
| 4442 | #ifdef _SC_2_C_BIND |
| 4443 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4444 | #endif |
| 4445 | #ifdef _SC_2_C_DEV |
| 4446 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4447 | #endif |
| 4448 | #ifdef _SC_2_C_VERSION |
| 4449 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4450 | #endif |
| 4451 | #ifdef _SC_2_FORT_DEV |
| 4452 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4453 | #endif |
| 4454 | #ifdef _SC_2_FORT_RUN |
| 4455 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4456 | #endif |
| 4457 | #ifdef _SC_2_LOCALEDEF |
| 4458 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4459 | #endif |
| 4460 | #ifdef _SC_2_SW_DEV |
| 4461 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4462 | #endif |
| 4463 | #ifdef _SC_2_UPE |
| 4464 | {"SC_2_UPE", _SC_2_UPE}, |
| 4465 | #endif |
| 4466 | #ifdef _SC_2_VERSION |
| 4467 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4468 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4469 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4470 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4471 | #endif |
| 4472 | #ifdef _SC_ACL |
| 4473 | {"SC_ACL", _SC_ACL}, |
| 4474 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4475 | #ifdef _SC_AIO_LISTIO_MAX |
| 4476 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4477 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4478 | #ifdef _SC_AIO_MAX |
| 4479 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4480 | #endif |
| 4481 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4482 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4483 | #endif |
| 4484 | #ifdef _SC_ARG_MAX |
| 4485 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4486 | #endif |
| 4487 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4488 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4489 | #endif |
| 4490 | #ifdef _SC_ATEXIT_MAX |
| 4491 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4492 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4493 | #ifdef _SC_AUDIT |
| 4494 | {"SC_AUDIT", _SC_AUDIT}, |
| 4495 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4496 | #ifdef _SC_AVPHYS_PAGES |
| 4497 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4498 | #endif |
| 4499 | #ifdef _SC_BC_BASE_MAX |
| 4500 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4501 | #endif |
| 4502 | #ifdef _SC_BC_DIM_MAX |
| 4503 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4504 | #endif |
| 4505 | #ifdef _SC_BC_SCALE_MAX |
| 4506 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4507 | #endif |
| 4508 | #ifdef _SC_BC_STRING_MAX |
| 4509 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4510 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4511 | #ifdef _SC_CAP |
| 4512 | {"SC_CAP", _SC_CAP}, |
| 4513 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4514 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4515 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4516 | #endif |
| 4517 | #ifdef _SC_CHAR_BIT |
| 4518 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4519 | #endif |
| 4520 | #ifdef _SC_CHAR_MAX |
| 4521 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4522 | #endif |
| 4523 | #ifdef _SC_CHAR_MIN |
| 4524 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4525 | #endif |
| 4526 | #ifdef _SC_CHILD_MAX |
| 4527 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4528 | #endif |
| 4529 | #ifdef _SC_CLK_TCK |
| 4530 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4531 | #endif |
| 4532 | #ifdef _SC_COHER_BLKSZ |
| 4533 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4534 | #endif |
| 4535 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4536 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4537 | #endif |
| 4538 | #ifdef _SC_DCACHE_ASSOC |
| 4539 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4540 | #endif |
| 4541 | #ifdef _SC_DCACHE_BLKSZ |
| 4542 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4543 | #endif |
| 4544 | #ifdef _SC_DCACHE_LINESZ |
| 4545 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4546 | #endif |
| 4547 | #ifdef _SC_DCACHE_SZ |
| 4548 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 4549 | #endif |
| 4550 | #ifdef _SC_DCACHE_TBLKSZ |
| 4551 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 4552 | #endif |
| 4553 | #ifdef _SC_DELAYTIMER_MAX |
| 4554 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 4555 | #endif |
| 4556 | #ifdef _SC_EQUIV_CLASS_MAX |
| 4557 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 4558 | #endif |
| 4559 | #ifdef _SC_EXPR_NEST_MAX |
| 4560 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 4561 | #endif |
| 4562 | #ifdef _SC_FSYNC |
| 4563 | {"SC_FSYNC", _SC_FSYNC}, |
| 4564 | #endif |
| 4565 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 4566 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 4567 | #endif |
| 4568 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 4569 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 4570 | #endif |
| 4571 | #ifdef _SC_ICACHE_ASSOC |
| 4572 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 4573 | #endif |
| 4574 | #ifdef _SC_ICACHE_BLKSZ |
| 4575 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 4576 | #endif |
| 4577 | #ifdef _SC_ICACHE_LINESZ |
| 4578 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 4579 | #endif |
| 4580 | #ifdef _SC_ICACHE_SZ |
| 4581 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 4582 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4583 | #ifdef _SC_INF |
| 4584 | {"SC_INF", _SC_INF}, |
| 4585 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4586 | #ifdef _SC_INT_MAX |
| 4587 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 4588 | #endif |
| 4589 | #ifdef _SC_INT_MIN |
| 4590 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 4591 | #endif |
| 4592 | #ifdef _SC_IOV_MAX |
| 4593 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 4594 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4595 | #ifdef _SC_IP_SECOPTS |
| 4596 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 4597 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4598 | #ifdef _SC_JOB_CONTROL |
| 4599 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 4600 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4601 | #ifdef _SC_KERN_POINTERS |
| 4602 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 4603 | #endif |
| 4604 | #ifdef _SC_KERN_SIM |
| 4605 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 4606 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4607 | #ifdef _SC_LINE_MAX |
| 4608 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 4609 | #endif |
| 4610 | #ifdef _SC_LOGIN_NAME_MAX |
| 4611 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 4612 | #endif |
| 4613 | #ifdef _SC_LOGNAME_MAX |
| 4614 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 4615 | #endif |
| 4616 | #ifdef _SC_LONG_BIT |
| 4617 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 4618 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4619 | #ifdef _SC_MAC |
| 4620 | {"SC_MAC", _SC_MAC}, |
| 4621 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4622 | #ifdef _SC_MAPPED_FILES |
| 4623 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 4624 | #endif |
| 4625 | #ifdef _SC_MAXPID |
| 4626 | {"SC_MAXPID", _SC_MAXPID}, |
| 4627 | #endif |
| 4628 | #ifdef _SC_MB_LEN_MAX |
| 4629 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 4630 | #endif |
| 4631 | #ifdef _SC_MEMLOCK |
| 4632 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 4633 | #endif |
| 4634 | #ifdef _SC_MEMLOCK_RANGE |
| 4635 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 4636 | #endif |
| 4637 | #ifdef _SC_MEMORY_PROTECTION |
| 4638 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 4639 | #endif |
| 4640 | #ifdef _SC_MESSAGE_PASSING |
| 4641 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 4642 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4643 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 4644 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 4645 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4646 | #ifdef _SC_MQ_OPEN_MAX |
| 4647 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 4648 | #endif |
| 4649 | #ifdef _SC_MQ_PRIO_MAX |
| 4650 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 4651 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4652 | #ifdef _SC_NACLS_MAX |
| 4653 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 4654 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4655 | #ifdef _SC_NGROUPS_MAX |
| 4656 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 4657 | #endif |
| 4658 | #ifdef _SC_NL_ARGMAX |
| 4659 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 4660 | #endif |
| 4661 | #ifdef _SC_NL_LANGMAX |
| 4662 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 4663 | #endif |
| 4664 | #ifdef _SC_NL_MSGMAX |
| 4665 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 4666 | #endif |
| 4667 | #ifdef _SC_NL_NMAX |
| 4668 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 4669 | #endif |
| 4670 | #ifdef _SC_NL_SETMAX |
| 4671 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 4672 | #endif |
| 4673 | #ifdef _SC_NL_TEXTMAX |
| 4674 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 4675 | #endif |
| 4676 | #ifdef _SC_NPROCESSORS_CONF |
| 4677 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 4678 | #endif |
| 4679 | #ifdef _SC_NPROCESSORS_ONLN |
| 4680 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 4681 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4682 | #ifdef _SC_NPROC_CONF |
| 4683 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 4684 | #endif |
| 4685 | #ifdef _SC_NPROC_ONLN |
| 4686 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 4687 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4688 | #ifdef _SC_NZERO |
| 4689 | {"SC_NZERO", _SC_NZERO}, |
| 4690 | #endif |
| 4691 | #ifdef _SC_OPEN_MAX |
| 4692 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 4693 | #endif |
| 4694 | #ifdef _SC_PAGESIZE |
| 4695 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 4696 | #endif |
| 4697 | #ifdef _SC_PAGE_SIZE |
| 4698 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 4699 | #endif |
| 4700 | #ifdef _SC_PASS_MAX |
| 4701 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 4702 | #endif |
| 4703 | #ifdef _SC_PHYS_PAGES |
| 4704 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 4705 | #endif |
| 4706 | #ifdef _SC_PII |
| 4707 | {"SC_PII", _SC_PII}, |
| 4708 | #endif |
| 4709 | #ifdef _SC_PII_INTERNET |
| 4710 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 4711 | #endif |
| 4712 | #ifdef _SC_PII_INTERNET_DGRAM |
| 4713 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 4714 | #endif |
| 4715 | #ifdef _SC_PII_INTERNET_STREAM |
| 4716 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 4717 | #endif |
| 4718 | #ifdef _SC_PII_OSI |
| 4719 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 4720 | #endif |
| 4721 | #ifdef _SC_PII_OSI_CLTS |
| 4722 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 4723 | #endif |
| 4724 | #ifdef _SC_PII_OSI_COTS |
| 4725 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 4726 | #endif |
| 4727 | #ifdef _SC_PII_OSI_M |
| 4728 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 4729 | #endif |
| 4730 | #ifdef _SC_PII_SOCKET |
| 4731 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 4732 | #endif |
| 4733 | #ifdef _SC_PII_XTI |
| 4734 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 4735 | #endif |
| 4736 | #ifdef _SC_POLL |
| 4737 | {"SC_POLL", _SC_POLL}, |
| 4738 | #endif |
| 4739 | #ifdef _SC_PRIORITIZED_IO |
| 4740 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 4741 | #endif |
| 4742 | #ifdef _SC_PRIORITY_SCHEDULING |
| 4743 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 4744 | #endif |
| 4745 | #ifdef _SC_REALTIME_SIGNALS |
| 4746 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 4747 | #endif |
| 4748 | #ifdef _SC_RE_DUP_MAX |
| 4749 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 4750 | #endif |
| 4751 | #ifdef _SC_RTSIG_MAX |
| 4752 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 4753 | #endif |
| 4754 | #ifdef _SC_SAVED_IDS |
| 4755 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 4756 | #endif |
| 4757 | #ifdef _SC_SCHAR_MAX |
| 4758 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 4759 | #endif |
| 4760 | #ifdef _SC_SCHAR_MIN |
| 4761 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 4762 | #endif |
| 4763 | #ifdef _SC_SELECT |
| 4764 | {"SC_SELECT", _SC_SELECT}, |
| 4765 | #endif |
| 4766 | #ifdef _SC_SEMAPHORES |
| 4767 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 4768 | #endif |
| 4769 | #ifdef _SC_SEM_NSEMS_MAX |
| 4770 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 4771 | #endif |
| 4772 | #ifdef _SC_SEM_VALUE_MAX |
| 4773 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 4774 | #endif |
| 4775 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 4776 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 4777 | #endif |
| 4778 | #ifdef _SC_SHRT_MAX |
| 4779 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 4780 | #endif |
| 4781 | #ifdef _SC_SHRT_MIN |
| 4782 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 4783 | #endif |
| 4784 | #ifdef _SC_SIGQUEUE_MAX |
| 4785 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 4786 | #endif |
| 4787 | #ifdef _SC_SIGRT_MAX |
| 4788 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 4789 | #endif |
| 4790 | #ifdef _SC_SIGRT_MIN |
| 4791 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 4792 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4793 | #ifdef _SC_SOFTPOWER |
| 4794 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 4795 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4796 | #ifdef _SC_SPLIT_CACHE |
| 4797 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 4798 | #endif |
| 4799 | #ifdef _SC_SSIZE_MAX |
| 4800 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 4801 | #endif |
| 4802 | #ifdef _SC_STACK_PROT |
| 4803 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 4804 | #endif |
| 4805 | #ifdef _SC_STREAM_MAX |
| 4806 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 4807 | #endif |
| 4808 | #ifdef _SC_SYNCHRONIZED_IO |
| 4809 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 4810 | #endif |
| 4811 | #ifdef _SC_THREADS |
| 4812 | {"SC_THREADS", _SC_THREADS}, |
| 4813 | #endif |
| 4814 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 4815 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 4816 | #endif |
| 4817 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 4818 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 4819 | #endif |
| 4820 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 4821 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 4822 | #endif |
| 4823 | #ifdef _SC_THREAD_KEYS_MAX |
| 4824 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 4825 | #endif |
| 4826 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 4827 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 4828 | #endif |
| 4829 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 4830 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 4831 | #endif |
| 4832 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 4833 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 4834 | #endif |
| 4835 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 4836 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 4837 | #endif |
| 4838 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 4839 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 4840 | #endif |
| 4841 | #ifdef _SC_THREAD_STACK_MIN |
| 4842 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 4843 | #endif |
| 4844 | #ifdef _SC_THREAD_THREADS_MAX |
| 4845 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 4846 | #endif |
| 4847 | #ifdef _SC_TIMERS |
| 4848 | {"SC_TIMERS", _SC_TIMERS}, |
| 4849 | #endif |
| 4850 | #ifdef _SC_TIMER_MAX |
| 4851 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 4852 | #endif |
| 4853 | #ifdef _SC_TTY_NAME_MAX |
| 4854 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 4855 | #endif |
| 4856 | #ifdef _SC_TZNAME_MAX |
| 4857 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 4858 | #endif |
| 4859 | #ifdef _SC_T_IOV_MAX |
| 4860 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 4861 | #endif |
| 4862 | #ifdef _SC_UCHAR_MAX |
| 4863 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 4864 | #endif |
| 4865 | #ifdef _SC_UINT_MAX |
| 4866 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 4867 | #endif |
| 4868 | #ifdef _SC_UIO_MAXIOV |
| 4869 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 4870 | #endif |
| 4871 | #ifdef _SC_ULONG_MAX |
| 4872 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 4873 | #endif |
| 4874 | #ifdef _SC_USHRT_MAX |
| 4875 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 4876 | #endif |
| 4877 | #ifdef _SC_VERSION |
| 4878 | {"SC_VERSION", _SC_VERSION}, |
| 4879 | #endif |
| 4880 | #ifdef _SC_WORD_BIT |
| 4881 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 4882 | #endif |
| 4883 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 4884 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 4885 | #endif |
| 4886 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 4887 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 4888 | #endif |
| 4889 | #ifdef _SC_XBS5_LP64_OFF64 |
| 4890 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 4891 | #endif |
| 4892 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 4893 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 4894 | #endif |
| 4895 | #ifdef _SC_XOPEN_CRYPT |
| 4896 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 4897 | #endif |
| 4898 | #ifdef _SC_XOPEN_ENH_I18N |
| 4899 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 4900 | #endif |
| 4901 | #ifdef _SC_XOPEN_LEGACY |
| 4902 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 4903 | #endif |
| 4904 | #ifdef _SC_XOPEN_REALTIME |
| 4905 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 4906 | #endif |
| 4907 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 4908 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 4909 | #endif |
| 4910 | #ifdef _SC_XOPEN_SHM |
| 4911 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 4912 | #endif |
| 4913 | #ifdef _SC_XOPEN_UNIX |
| 4914 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 4915 | #endif |
| 4916 | #ifdef _SC_XOPEN_VERSION |
| 4917 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 4918 | #endif |
| 4919 | #ifdef _SC_XOPEN_XCU_VERSION |
| 4920 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 4921 | #endif |
| 4922 | #ifdef _SC_XOPEN_XPG2 |
| 4923 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 4924 | #endif |
| 4925 | #ifdef _SC_XOPEN_XPG3 |
| 4926 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 4927 | #endif |
| 4928 | #ifdef _SC_XOPEN_XPG4 |
| 4929 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 4930 | #endif |
| 4931 | }; |
| 4932 | |
| 4933 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4934 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4935 | { |
| 4936 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 4937 | sizeof(posix_constants_sysconf) |
| 4938 | / sizeof(struct constdef)); |
| 4939 | } |
| 4940 | |
| 4941 | static char posix_sysconf__doc__[] = "\ |
| 4942 | sysconf(name) -> integer\n\ |
| 4943 | Return an integer-valued system configuration variable."; |
| 4944 | |
| 4945 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4946 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4947 | { |
| 4948 | PyObject *result = NULL; |
| 4949 | int name; |
| 4950 | |
| 4951 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 4952 | int value; |
| 4953 | |
| 4954 | errno = 0; |
| 4955 | value = sysconf(name); |
| 4956 | if (value == -1 && errno != 0) |
| 4957 | posix_error(); |
| 4958 | else |
| 4959 | result = PyInt_FromLong(value); |
| 4960 | } |
| 4961 | return result; |
| 4962 | } |
| 4963 | #endif |
| 4964 | |
| 4965 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4966 | /* This code is used to ensure that the tables of configuration value names |
| 4967 | * are in sorted order as required by conv_confname(), and also to build the |
| 4968 | * the exported dictionaries that are used to publish information about the |
| 4969 | * names available on the host platform. |
| 4970 | * |
| 4971 | * Sorting the table at runtime ensures that the table is properly ordered |
| 4972 | * when used, even for platforms we're not able to test on. It also makes |
| 4973 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4974 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4975 | |
| 4976 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4977 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4978 | { |
| 4979 | const struct constdef *c1 = |
| 4980 | (const struct constdef *) v1; |
| 4981 | const struct constdef *c2 = |
| 4982 | (const struct constdef *) v2; |
| 4983 | |
| 4984 | return strcmp(c1->name, c2->name); |
| 4985 | } |
| 4986 | |
| 4987 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4988 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 4989 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4990 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4991 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4992 | size_t i; |
| 4993 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4994 | |
| 4995 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 4996 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 4997 | if (d == NULL) |
| 4998 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4999 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5000 | for (i=0; i < tablesize; ++i) { |
| 5001 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5002 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5003 | Py_XDECREF(o); |
| 5004 | Py_DECREF(d); |
| 5005 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5006 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5007 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5008 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5009 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5010 | Py_DECREF(d); |
| 5011 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5014 | /* Return -1 on failure, 0 on success. */ |
| 5015 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5016 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5017 | { |
| 5018 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5019 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5020 | sizeof(posix_constants_pathconf) |
| 5021 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5022 | "pathconf_names", moddict)) |
| 5023 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5024 | #endif |
| 5025 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5026 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5027 | sizeof(posix_constants_confstr) |
| 5028 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5029 | "confstr_names", moddict)) |
| 5030 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5031 | #endif |
| 5032 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5033 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5034 | sizeof(posix_constants_sysconf) |
| 5035 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5036 | "sysconf_names", moddict)) |
| 5037 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5038 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5039 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5040 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5041 | |
| 5042 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5043 | static char posix_abort__doc__[] = "\ |
| 5044 | abort() -> does not return!\n\ |
| 5045 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5046 | in the hardest way possible on the hosting operating system."; |
| 5047 | |
| 5048 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5049 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5050 | { |
| 5051 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5052 | return NULL; |
| 5053 | abort(); |
| 5054 | /*NOTREACHED*/ |
| 5055 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5056 | return NULL; |
| 5057 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5058 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5059 | |
| 5060 | static PyMethodDef posix_methods[] = { |
| 5061 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5062 | #ifdef HAVE_TTYNAME |
| 5063 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5064 | #endif |
| 5065 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5066 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5067 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5068 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5069 | #endif /* HAVE_CHOWN */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5070 | #ifdef HAVE_CTERMID |
| 5071 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5072 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5073 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5074 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5075 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5076 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5077 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5078 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5079 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5080 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5081 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5082 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5083 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5084 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5085 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5086 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5087 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5088 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5089 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5090 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5091 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5092 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5093 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5094 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5095 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5096 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5097 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5098 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5099 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5100 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5101 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5102 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5103 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5104 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5105 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5106 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5107 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5108 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5109 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5110 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5111 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5112 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5113 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5114 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5115 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5116 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5117 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5118 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5119 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5120 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5121 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5122 | #ifdef HAVE_FORKPTY |
| 5123 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5124 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5125 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5126 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5127 | #endif /* HAVE_GETEGID */ |
| 5128 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5129 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5130 | #endif /* HAVE_GETEUID */ |
| 5131 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5132 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5133 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5134 | #ifdef HAVE_GETGROUPS |
| 5135 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5136 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5137 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5138 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5139 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5140 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5141 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5142 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5143 | #endif /* HAVE_GETPPID */ |
| 5144 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5145 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5146 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5147 | #ifdef HAVE_GETLOGIN |
| 5148 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5149 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5150 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5151 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5152 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5153 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5154 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5155 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5156 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5157 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5158 | #ifdef MS_WIN32 |
| 5159 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5160 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5161 | {"popen4", win32_popen4, METH_VARARGS}, |
| 5162 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5163 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5164 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5165 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5166 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5167 | #ifdef HAVE_SETEUID |
| 5168 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5169 | #endif /* HAVE_SETEUID */ |
| 5170 | #ifdef HAVE_SETEGID |
| 5171 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5172 | #endif /* HAVE_SETEGID */ |
| 5173 | #ifdef HAVE_SETREUID |
| 5174 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5175 | #endif /* HAVE_SETREUID */ |
| 5176 | #ifdef HAVE_SETREGID |
| 5177 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5178 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5179 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5180 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5181 | #endif /* HAVE_SETGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5182 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5183 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5184 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5185 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5186 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5187 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5188 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5189 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5190 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5191 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5192 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5193 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5194 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5195 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5196 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5197 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5198 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5199 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5200 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5201 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5202 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5203 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5204 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5205 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5206 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5207 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5208 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5209 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5210 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5211 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5212 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5213 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5214 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5215 | #endif |
| 5216 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5217 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5218 | #endif |
| 5219 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5220 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5221 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5222 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5223 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5224 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5225 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5226 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5227 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5228 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5229 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5230 | #endif |
| 5231 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5232 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5233 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5234 | #ifdef HAVE_SYS_WAIT_H |
| 5235 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5236 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5237 | #endif /* WIFSTOPPED */ |
| 5238 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5239 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5240 | #endif /* WIFSIGNALED */ |
| 5241 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5242 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5243 | #endif /* WIFEXITED */ |
| 5244 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5245 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5246 | #endif /* WEXITSTATUS */ |
| 5247 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5248 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5249 | #endif /* WTERMSIG */ |
| 5250 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5251 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5252 | #endif /* WSTOPSIG */ |
| 5253 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5254 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5255 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5256 | #endif |
| 5257 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5258 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5259 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5260 | #ifdef HAVE_TMPNAM |
| 5261 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5262 | #endif |
| 5263 | #ifdef HAVE_TEMPNAM |
| 5264 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5265 | #endif |
| 5266 | #ifdef HAVE_TMPNAM |
| 5267 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5268 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5269 | #ifdef HAVE_CONFSTR |
| 5270 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5271 | #endif |
| 5272 | #ifdef HAVE_SYSCONF |
| 5273 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5274 | #endif |
| 5275 | #ifdef HAVE_FPATHCONF |
| 5276 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5277 | #endif |
| 5278 | #ifdef HAVE_PATHCONF |
| 5279 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5280 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5281 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5282 | {NULL, NULL} /* Sentinel */ |
| 5283 | }; |
| 5284 | |
| 5285 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5286 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5287 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5288 | { |
| 5289 | PyObject* v = PyInt_FromLong(value); |
| 5290 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5291 | return -1; /* triggers fatal error */ |
| 5292 | |
| 5293 | Py_DECREF(v); |
| 5294 | return 0; |
| 5295 | } |
| 5296 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5297 | #if defined(PYOS_OS2) |
| 5298 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5299 | static int insertvalues(PyObject *d) |
| 5300 | { |
| 5301 | APIRET rc; |
| 5302 | ULONG values[QSV_MAX+1]; |
| 5303 | PyObject *v; |
| 5304 | char *ver, tmp[10]; |
| 5305 | |
| 5306 | Py_BEGIN_ALLOW_THREADS |
| 5307 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5308 | Py_END_ALLOW_THREADS |
| 5309 | |
| 5310 | if (rc != NO_ERROR) { |
| 5311 | os2_error(rc); |
| 5312 | return -1; |
| 5313 | } |
| 5314 | |
| 5315 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5316 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5317 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5318 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5319 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5320 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5321 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5322 | |
| 5323 | switch (values[QSV_VERSION_MINOR]) { |
| 5324 | case 0: ver = "2.00"; break; |
| 5325 | case 10: ver = "2.10"; break; |
| 5326 | case 11: ver = "2.11"; break; |
| 5327 | case 30: ver = "3.00"; break; |
| 5328 | case 40: ver = "4.00"; break; |
| 5329 | case 50: ver = "5.00"; break; |
| 5330 | default: |
| 5331 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 5332 | values[QSV_VERSION_MINOR]); |
| 5333 | ver = &tmp[0]; |
| 5334 | } |
| 5335 | |
| 5336 | /* Add Indicator of the Version of the Operating System */ |
| 5337 | v = PyString_FromString(ver); |
| 5338 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5339 | return -1; |
| 5340 | Py_DECREF(v); |
| 5341 | |
| 5342 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5343 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5344 | tmp[1] = ':'; |
| 5345 | tmp[2] = '\0'; |
| 5346 | |
| 5347 | v = PyString_FromString(tmp); |
| 5348 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5349 | return -1; |
| 5350 | Py_DECREF(v); |
| 5351 | |
| 5352 | return 0; |
| 5353 | } |
| 5354 | #endif |
| 5355 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5356 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5357 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5358 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5359 | #ifdef F_OK |
| 5360 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5361 | #endif |
| 5362 | #ifdef R_OK |
| 5363 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5364 | #endif |
| 5365 | #ifdef W_OK |
| 5366 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5367 | #endif |
| 5368 | #ifdef X_OK |
| 5369 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5370 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5371 | #ifdef NGROUPS_MAX |
| 5372 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5373 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5374 | #ifdef TMP_MAX |
| 5375 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5376 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5377 | #ifdef WNOHANG |
| 5378 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5379 | #endif |
| 5380 | #ifdef O_RDONLY |
| 5381 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5382 | #endif |
| 5383 | #ifdef O_WRONLY |
| 5384 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5385 | #endif |
| 5386 | #ifdef O_RDWR |
| 5387 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5388 | #endif |
| 5389 | #ifdef O_NDELAY |
| 5390 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5391 | #endif |
| 5392 | #ifdef O_NONBLOCK |
| 5393 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5394 | #endif |
| 5395 | #ifdef O_APPEND |
| 5396 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5397 | #endif |
| 5398 | #ifdef O_DSYNC |
| 5399 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5400 | #endif |
| 5401 | #ifdef O_RSYNC |
| 5402 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5403 | #endif |
| 5404 | #ifdef O_SYNC |
| 5405 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5406 | #endif |
| 5407 | #ifdef O_NOCTTY |
| 5408 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5409 | #endif |
| 5410 | #ifdef O_CREAT |
| 5411 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5412 | #endif |
| 5413 | #ifdef O_EXCL |
| 5414 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5415 | #endif |
| 5416 | #ifdef O_TRUNC |
| 5417 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5418 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5419 | #ifdef O_BINARY |
| 5420 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5421 | #endif |
| 5422 | #ifdef O_TEXT |
| 5423 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5424 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5425 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5426 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5427 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5428 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5429 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5430 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5431 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5432 | #endif |
| 5433 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5434 | #if defined(PYOS_OS2) |
| 5435 | if (insertvalues(d)) return -1; |
| 5436 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5437 | return 0; |
| 5438 | } |
| 5439 | |
| 5440 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5441 | #if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5442 | #define INITFUNC initnt |
| 5443 | #define MODNAME "nt" |
| 5444 | #else |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5445 | #if defined(PYOS_OS2) |
| 5446 | #define INITFUNC initos2 |
| 5447 | #define MODNAME "os2" |
| 5448 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5449 | #define INITFUNC initposix |
| 5450 | #define MODNAME "posix" |
| 5451 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5452 | #endif |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5453 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5454 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5455 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5456 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5457 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5458 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5459 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5460 | posix_methods, |
| 5461 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5462 | (PyObject *)NULL, |
| 5463 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5464 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5465 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5466 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5467 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5468 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5469 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5470 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5471 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5472 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5473 | return; |
| 5474 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5475 | if (setup_confname_tables(d)) |
| 5476 | return; |
| 5477 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5478 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5479 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5480 | #ifdef HAVE_PUTENV |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5481 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5482 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5483 | } |