Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT and MS-Win. In that case the module |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5 | actually calls itself 'nt', not 'posix', and a few functions are |
| 6 | either unimplemented or implemented differently. The source |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WIN32' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 13 | static char posix__doc__ [] = |
| 14 | "This module provides access to operating system functionality that is\n\ |
| 15 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 16 | disguised Unix interface). Refer to the library manual and\n\ |
| 17 | corresponding Unix manual entries for more information on calls."; |
| 18 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 20 | #include "structseq.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 22 | #if defined(PYOS_OS2) |
| 23 | #define INCL_DOS |
| 24 | #define INCL_DOSERRORS |
| 25 | #define INCL_DOSPROCESS |
| 26 | #define INCL_NOPMAPI |
| 27 | #include <os2.h> |
| 28 | #endif |
| 29 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 33 | #ifdef HAVE_SYS_WAIT_H |
| 34 | #include <sys/wait.h> /* For WNOHANG */ |
| 35 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 37 | #ifdef HAVE_SIGNAL_H |
| 38 | #include <signal.h> |
| 39 | #endif |
| 40 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | #ifdef HAVE_FCNTL_H |
| 42 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 43 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 45 | #ifdef HAVE_GRP_H |
| 46 | #include <grp.h> |
| 47 | #endif |
| 48 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 49 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 50 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 51 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 52 | #include <process.h> |
| 53 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 54 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 55 | #define HAVE_GETCWD 1 |
| 56 | #define HAVE_OPENDIR 1 |
| 57 | #define HAVE_SYSTEM 1 |
| 58 | #if defined(__OS2__) |
| 59 | #define HAVE_EXECV 1 |
| 60 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 61 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 62 | #include <process.h> |
| 63 | #else |
| 64 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 65 | #define HAVE_EXECV 1 |
| 66 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 67 | #define HAVE_OPENDIR 1 |
| 68 | #define HAVE_PIPE 1 |
| 69 | #define HAVE_POPEN 1 |
| 70 | #define HAVE_SYSTEM 1 |
| 71 | #define HAVE_WAIT 1 |
| 72 | #else |
| 73 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 74 | #define HAVE_GETCWD 1 |
| 75 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 76 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 77 | #define HAVE_EXECV 1 |
| 78 | #define HAVE_PIPE 1 |
| 79 | #define HAVE_POPEN 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 82 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 83 | #else /* all other compilers */ |
| 84 | /* Unix functions that the configure script doesn't check for */ |
| 85 | #define HAVE_EXECV 1 |
| 86 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 87 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 88 | #define HAVE_FORK1 1 |
| 89 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 90 | #define HAVE_GETCWD 1 |
| 91 | #define HAVE_GETEGID 1 |
| 92 | #define HAVE_GETEUID 1 |
| 93 | #define HAVE_GETGID 1 |
| 94 | #define HAVE_GETPPID 1 |
| 95 | #define HAVE_GETUID 1 |
| 96 | #define HAVE_KILL 1 |
| 97 | #define HAVE_OPENDIR 1 |
| 98 | #define HAVE_PIPE 1 |
| 99 | #define HAVE_POPEN 1 |
| 100 | #define HAVE_SYSTEM 1 |
| 101 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 102 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 103 | #endif /* _MSC_VER */ |
| 104 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 105 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 106 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 108 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 110 | #if defined(sun) && !defined(__SVR4) |
| 111 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 112 | extern int rename(const char *, const char *); |
| 113 | extern int pclose(FILE *); |
| 114 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 115 | extern int fsync(int); |
| 116 | extern int lstat(const char *, struct stat *); |
| 117 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 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 |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 146 | #ifdef __BORLANDC__ |
| 147 | extern int chmod(const char *, int); |
| 148 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 150 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 151 | extern int chown(const char *, uid_t, gid_t); |
| 152 | extern char *getcwd(char *, int); |
| 153 | extern char *strerror(int); |
| 154 | extern int link(const char *, const char *); |
| 155 | extern int rename(const char *, const char *); |
| 156 | extern int stat(const char *, struct stat *); |
| 157 | extern int unlink(const char *); |
| 158 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 159 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 161 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 163 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 164 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 167 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | #ifdef HAVE_UTIME_H |
| 170 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 171 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 173 | #ifdef HAVE_SYS_UTIME_H |
| 174 | #include <sys/utime.h> |
| 175 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 176 | #endif /* HAVE_SYS_UTIME_H */ |
| 177 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | #ifdef HAVE_SYS_TIMES_H |
| 179 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 180 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | |
| 182 | #ifdef HAVE_SYS_PARAM_H |
| 183 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 184 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | |
| 186 | #ifdef HAVE_SYS_UTSNAME_H |
| 187 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 188 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | |
| 190 | #ifndef MAXPATHLEN |
| 191 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 192 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 194 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 196 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 197 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 198 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #include <direct.h> |
| 200 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 201 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 203 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 205 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 207 | #endif |
| 208 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 210 | #endif |
| 211 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 213 | #endif |
| 214 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 216 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #include <direct.h> |
| 218 | #include <io.h> |
| 219 | #include <process.h> |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 220 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 222 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 223 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 224 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #else /* 16-bit Windows */ |
| 226 | #include <dos.h> |
| 227 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 228 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 229 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 231 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 233 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 235 | #ifdef UNION_WAIT |
| 236 | /* Emulate some macros on systems that have a union instead of macros */ |
| 237 | |
| 238 | #ifndef WIFEXITED |
| 239 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 240 | #endif |
| 241 | |
| 242 | #ifndef WEXITSTATUS |
| 243 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 244 | #endif |
| 245 | |
| 246 | #ifndef WTERMSIG |
| 247 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 248 | #endif |
| 249 | |
| 250 | #endif /* UNION_WAIT */ |
| 251 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 252 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 253 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 254 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 255 | #define USE_CTERMID_R |
| 256 | #endif |
| 257 | |
| 258 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 259 | #define USE_TMPNAM_R |
| 260 | #endif |
| 261 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 262 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 263 | #undef STAT |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 264 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 265 | # define STAT _stati64 |
| 266 | # define FSTAT _fstati64 |
| 267 | # define STRUCT_STAT struct _stati64 |
| 268 | #else |
| 269 | # define STAT stat |
| 270 | # define FSTAT fstat |
| 271 | # define STRUCT_STAT struct stat |
| 272 | #endif |
| 273 | |
| 274 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 275 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 276 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 277 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 279 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 281 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 282 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 284 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 286 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | if (d == NULL) |
| 288 | return NULL; |
| 289 | if (environ == NULL) |
| 290 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 291 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 293 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 294 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 295 | char *p = strchr(*e, '='); |
| 296 | if (p == NULL) |
| 297 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 298 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 299 | if (k == NULL) { |
| 300 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 302 | } |
| 303 | v = PyString_FromString(p+1); |
| 304 | if (v == NULL) { |
| 305 | PyErr_Clear(); |
| 306 | Py_DECREF(k); |
| 307 | continue; |
| 308 | } |
| 309 | if (PyDict_GetItem(d, k) == NULL) { |
| 310 | if (PyDict_SetItem(d, k, v) != 0) |
| 311 | PyErr_Clear(); |
| 312 | } |
| 313 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 314 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 316 | #if defined(PYOS_OS2) |
| 317 | { |
| 318 | APIRET rc; |
| 319 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 320 | |
| 321 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 322 | 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] | 323 | PyObject *v = PyString_FromString(buffer); |
| 324 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 325 | Py_DECREF(v); |
| 326 | } |
| 327 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 328 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 329 | PyObject *v = PyString_FromString(buffer); |
| 330 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 331 | Py_DECREF(v); |
| 332 | } |
| 333 | } |
| 334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | return d; |
| 336 | } |
| 337 | |
| 338 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 340 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 341 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 342 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 343 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 344 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 347 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 348 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 349 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 352 | static PyObject * |
| 353 | posix_error_with_allocated_filename(char* name) |
| 354 | { |
| 355 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 356 | PyMem_Free(name); |
| 357 | return rc; |
| 358 | } |
| 359 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 360 | #ifdef MS_WIN32 |
| 361 | static PyObject * |
| 362 | win32_error(char* function, char* filename) |
| 363 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 364 | /* XXX We should pass the function name along in the future. |
| 365 | (_winreg.c also wants to pass the function name.) |
| 366 | This would however require an additional param to the |
| 367 | Windows error object, which is non-trivial. |
| 368 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 369 | errno = GetLastError(); |
| 370 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 371 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 372 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 373 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 374 | } |
| 375 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 377 | #if defined(PYOS_OS2) |
| 378 | /********************************************************************** |
| 379 | * Helper Function to Trim and Format OS/2 Messages |
| 380 | **********************************************************************/ |
| 381 | static void |
| 382 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 383 | { |
| 384 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 385 | |
| 386 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 387 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 388 | |
| 389 | while (lastc > msgbuf && isspace(*lastc)) |
| 390 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 391 | } |
| 392 | |
| 393 | /* Add Optional Reason Text */ |
| 394 | if (reason) { |
| 395 | strcat(msgbuf, " : "); |
| 396 | strcat(msgbuf, reason); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /********************************************************************** |
| 401 | * Decode an OS/2 Operating System Error Code |
| 402 | * |
| 403 | * A convenience function to lookup an OS/2 error code and return a |
| 404 | * text message we can use to raise a Python exception. |
| 405 | * |
| 406 | * Notes: |
| 407 | * The messages for errors returned from the OS/2 kernel reside in |
| 408 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 409 | * |
| 410 | **********************************************************************/ |
| 411 | static char * |
| 412 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 413 | { |
| 414 | APIRET rc; |
| 415 | ULONG msglen; |
| 416 | |
| 417 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 418 | Py_BEGIN_ALLOW_THREADS |
| 419 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 420 | errorcode, "oso001.msg", &msglen); |
| 421 | Py_END_ALLOW_THREADS |
| 422 | |
| 423 | if (rc == NO_ERROR) |
| 424 | os2_formatmsg(msgbuf, msglen, reason); |
| 425 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 426 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 427 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 428 | |
| 429 | return msgbuf; |
| 430 | } |
| 431 | |
| 432 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 433 | errors are not in a global variable e.g. 'errno' nor are |
| 434 | they congruent with posix error numbers. */ |
| 435 | |
| 436 | static PyObject * os2_error(int code) |
| 437 | { |
| 438 | char text[1024]; |
| 439 | PyObject *v; |
| 440 | |
| 441 | os2_strerror(text, sizeof(text), code, ""); |
| 442 | |
| 443 | v = Py_BuildValue("(is)", code, text); |
| 444 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 445 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 446 | Py_DECREF(v); |
| 447 | } |
| 448 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 449 | } |
| 450 | |
| 451 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | |
| 453 | /* POSIX generic methods */ |
| 454 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 455 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 456 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 457 | { |
| 458 | int fd; |
| 459 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 460 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 461 | return NULL; |
| 462 | Py_BEGIN_ALLOW_THREADS |
| 463 | res = (*func)(fd); |
| 464 | Py_END_ALLOW_THREADS |
| 465 | if (res < 0) |
| 466 | return posix_error(); |
| 467 | Py_INCREF(Py_None); |
| 468 | return Py_None; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 473 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 474 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 475 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 476 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 477 | if (!PyArg_ParseTuple(args, format, |
| 478 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 480 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 481 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 482 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 483 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 484 | return posix_error_with_allocated_filename(path1); |
| 485 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 486 | Py_INCREF(Py_None); |
| 487 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 490 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 491 | posix_2str(PyObject *args, char *format, |
| 492 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 494 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 495 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 496 | if (!PyArg_ParseTuple(args, format, |
| 497 | Py_FileSystemDefaultEncoding, &path1, |
| 498 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 499 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 500 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 501 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 502 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 503 | PyMem_Free(path1); |
| 504 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 505 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 506 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 508 | Py_INCREF(Py_None); |
| 509 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 512 | static char stat_result__doc__[] = |
| 513 | "stat_result: Result from stat or lstat.\n\n\ |
| 514 | This object may be accessed either as a tuple of\n\ |
| 515 | (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 516 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 517 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 518 | Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 519 | they are available as attributes only.\n\ |
| 520 | \n\ |
| 521 | See os.stat for more information.\n"; |
| 522 | |
| 523 | static PyStructSequence_Field stat_result_fields[] = { |
| 524 | {"st_mode", "protection bits"}, |
| 525 | {"st_ino", "inode"}, |
| 526 | {"st_dev", "device"}, |
| 527 | {"st_nlink", "number of hard links"}, |
| 528 | {"st_uid", "user ID of owner"}, |
| 529 | {"st_gid", "group ID of owner"}, |
| 530 | {"st_size", "total size, in bytes"}, |
| 531 | {"st_atime", "time of last access"}, |
| 532 | {"st_mtime", "time of last modification"}, |
| 533 | {"st_ctime", "time of last change"}, |
| 534 | #ifdef HAVE_ST_BLKSIZE |
| 535 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 536 | #endif |
| 537 | #ifdef HAVE_ST_BLOCKS |
| 538 | {"st_blocks", "number of blocks allocated"}, |
| 539 | #endif |
| 540 | #ifdef HAVE_ST_RDEV |
| 541 | {"st_rdev", "device type (if inode device)"}, |
| 542 | #endif |
| 543 | {0} |
| 544 | }; |
| 545 | |
| 546 | #ifdef HAVE_ST_BLKSIZE |
| 547 | #define ST_BLKSIZE_IDX 10 |
| 548 | #else |
| 549 | #define ST_BLKSIZE_IDX 9 |
| 550 | #endif |
| 551 | |
| 552 | #ifdef HAVE_ST_BLOCKS |
| 553 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 554 | #else |
| 555 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 556 | #endif |
| 557 | |
| 558 | #ifdef HAVE_ST_RDEV |
| 559 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 560 | #else |
| 561 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 562 | #endif |
| 563 | |
| 564 | static PyStructSequence_Desc stat_result_desc = { |
| 565 | "stat_result", /* name */ |
| 566 | stat_result__doc__, /* doc */ |
| 567 | stat_result_fields, |
| 568 | 10 |
| 569 | }; |
| 570 | |
| 571 | static char statvfs_result__doc__[] = |
| 572 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 573 | This object may be accessed either as a tuple of\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 574 | (bsize,frsize,blocks,bfree,bavail,files,ffree,favail,flag,namemax),\n\ |
| 575 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 576 | \n\ |
| 577 | See os.statvfs for more information.\n"; |
| 578 | |
| 579 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 580 | {"f_bsize", }, |
| 581 | {"f_frsize", }, |
| 582 | {"f_blocks", }, |
| 583 | {"f_bfree", }, |
| 584 | {"f_bavail", }, |
| 585 | {"f_files", }, |
| 586 | {"f_ffree", }, |
| 587 | {"f_favail", }, |
| 588 | {"f_flag", }, |
| 589 | {"f_namemax",}, |
| 590 | {0} |
| 591 | }; |
| 592 | |
| 593 | static PyStructSequence_Desc statvfs_result_desc = { |
| 594 | "statvfs_result", /* name */ |
| 595 | statvfs_result__doc__, /* doc */ |
| 596 | statvfs_result_fields, |
| 597 | 10 |
| 598 | }; |
| 599 | |
| 600 | static PyTypeObject StatResultType; |
| 601 | static PyTypeObject StatVFSResultType; |
| 602 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 603 | /* pack a system stat C structure into the Python stat tuple |
| 604 | (used by posix_stat() and posix_fstat()) */ |
| 605 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 606 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 607 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 608 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 609 | if (v == NULL) |
| 610 | return NULL; |
| 611 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 612 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 613 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 614 | PyStructSequence_SET_ITEM(v, 1, |
| 615 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 616 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 617 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 618 | #endif |
| 619 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 620 | PyStructSequence_SET_ITEM(v, 2, |
| 621 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 622 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 623 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 624 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 625 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 626 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 627 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 628 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 629 | PyStructSequence_SET_ITEM(v, 6, |
| 630 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 631 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 632 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 633 | #endif |
| 634 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 635 | PyStructSequence_SET_ITEM(v, 7, |
| 636 | PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 637 | PyStructSequence_SET_ITEM(v, 8, |
| 638 | PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 639 | PyStructSequence_SET_ITEM(v, 9, |
| 640 | PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 641 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 642 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 643 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 644 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 645 | #endif |
| 646 | |
| 647 | #ifdef HAVE_ST_BLKSIZE |
| 648 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 649 | PyInt_FromLong((long)st.st_blksize)); |
| 650 | #endif |
| 651 | #ifdef HAVE_ST_BLOCKS |
| 652 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 653 | PyInt_FromLong((long)st.st_blocks)); |
| 654 | #endif |
| 655 | #ifdef HAVE_ST_RDEV |
| 656 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 657 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 658 | #endif |
| 659 | |
| 660 | if (PyErr_Occurred()) { |
| 661 | Py_DECREF(v); |
| 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | return v; |
| 666 | } |
| 667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 668 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 669 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 670 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 671 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 672 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 673 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 674 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 675 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 676 | |
| 677 | #ifdef MS_WIN32 |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 678 | int pathlen; |
| 679 | char pathcopy[MAX_PATH]; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 680 | #endif /* MS_WIN32 */ |
| 681 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 682 | if (!PyArg_ParseTuple(args, format, |
| 683 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 684 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 685 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 686 | |
| 687 | #ifdef MS_WIN32 |
| 688 | pathlen = strlen(path); |
| 689 | /* the library call can blow up if the file name is too long! */ |
| 690 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 691 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 692 | errno = ENAMETOOLONG; |
| 693 | return posix_error(); |
| 694 | } |
| 695 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 696 | /* Remove trailing slash or backslash, unless it's the current |
| 697 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 698 | */ |
| 699 | if (pathlen > 0 && |
| 700 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 701 | /* It does end with a slash -- exempt the root drive cases. */ |
| 702 | /* XXX UNC root drives should also be exempted? */ |
| 703 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 704 | /* leave it alone */; |
| 705 | else { |
| 706 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 707 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 708 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 709 | path = pathcopy; |
| 710 | } |
| 711 | } |
| 712 | #endif /* MS_WIN32 */ |
| 713 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 714 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 715 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 716 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 717 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 718 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 719 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 720 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 721 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | |
| 725 | /* POSIX methods */ |
| 726 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 727 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 728 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 729 | Test for access to a file."; |
| 730 | |
| 731 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 732 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 733 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 734 | char *path; |
| 735 | int mode; |
| 736 | int res; |
| 737 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 738 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 739 | return NULL; |
| 740 | Py_BEGIN_ALLOW_THREADS |
| 741 | res = access(path, mode); |
| 742 | Py_END_ALLOW_THREADS |
| 743 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 746 | #ifndef F_OK |
| 747 | #define F_OK 0 |
| 748 | #endif |
| 749 | #ifndef R_OK |
| 750 | #define R_OK 4 |
| 751 | #endif |
| 752 | #ifndef W_OK |
| 753 | #define W_OK 2 |
| 754 | #endif |
| 755 | #ifndef X_OK |
| 756 | #define X_OK 1 |
| 757 | #endif |
| 758 | |
| 759 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 760 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 761 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 762 | Return the name of the terminal device connected to 'fd'."; |
| 763 | |
| 764 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 765 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 766 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 767 | int id; |
| 768 | char *ret; |
| 769 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 770 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 771 | return NULL; |
| 772 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 773 | ret = ttyname(id); |
| 774 | if (ret == NULL) |
| 775 | return(posix_error()); |
| 776 | return(PyString_FromString(ret)); |
| 777 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 778 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 779 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 780 | #ifdef HAVE_CTERMID |
| 781 | static char posix_ctermid__doc__[] = |
| 782 | "ctermid() -> String\n\ |
| 783 | Return the name of the controlling terminal for this process."; |
| 784 | |
| 785 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 786 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 787 | { |
| 788 | char *ret; |
| 789 | char buffer[L_ctermid]; |
| 790 | |
| 791 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 792 | return NULL; |
| 793 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 794 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 795 | ret = ctermid_r(buffer); |
| 796 | #else |
| 797 | ret = ctermid(buffer); |
| 798 | #endif |
| 799 | if (ret == NULL) |
| 800 | return(posix_error()); |
| 801 | return(PyString_FromString(buffer)); |
| 802 | } |
| 803 | #endif |
| 804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 805 | static char posix_chdir__doc__[] = |
| 806 | "chdir(path) -> None\n\ |
| 807 | Change the current working directory to the specified path."; |
| 808 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 810 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 811 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 812 | return posix_1str(args, "et:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 815 | |
| 816 | static char posix_chmod__doc__[] = |
| 817 | "chmod(path, mode) -> None\n\ |
| 818 | Change the access permissions of a file."; |
| 819 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 820 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 821 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 823 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 824 | int i; |
| 825 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 826 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
| 827 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 828 | return NULL; |
| 829 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 830 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 831 | Py_END_ALLOW_THREADS |
| 832 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 833 | return posix_error_with_allocated_filename(path); |
| 834 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 835 | Py_INCREF(Py_None); |
| 836 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 839 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 840 | #ifdef HAVE_CHROOT |
| 841 | static char posix_chroot__doc__[] = |
| 842 | "chroot(path) -> None\n\ |
| 843 | Change root directory to path."; |
| 844 | |
| 845 | static PyObject * |
| 846 | posix_chroot(PyObject *self, PyObject *args) |
| 847 | { |
| 848 | return posix_1str(args, "et:chroot", chroot); |
| 849 | } |
| 850 | #endif |
| 851 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 852 | #ifdef HAVE_FSYNC |
| 853 | static char posix_fsync__doc__[] = |
| 854 | "fsync(fildes) -> None\n\ |
| 855 | force write of file with filedescriptor to disk."; |
| 856 | |
| 857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 858 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 859 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 860 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 861 | } |
| 862 | #endif /* HAVE_FSYNC */ |
| 863 | |
| 864 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 865 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 866 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 867 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 868 | #endif |
| 869 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 870 | static char posix_fdatasync__doc__[] = |
| 871 | "fdatasync(fildes) -> None\n\ |
| 872 | force write of file with filedescriptor to disk.\n\ |
| 873 | does not force update of metadata."; |
| 874 | |
| 875 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 876 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 877 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 878 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 879 | } |
| 880 | #endif /* HAVE_FDATASYNC */ |
| 881 | |
| 882 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 883 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 884 | static char posix_chown__doc__[] = |
| 885 | "chown(path, uid, gid) -> None\n\ |
| 886 | Change the owner and group id of path to the numeric uid and gid."; |
| 887 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 889 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 890 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 891 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 892 | int uid, gid; |
| 893 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 894 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 895 | Py_FileSystemDefaultEncoding, &path, |
| 896 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 897 | return NULL; |
| 898 | Py_BEGIN_ALLOW_THREADS |
| 899 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 900 | Py_END_ALLOW_THREADS |
| 901 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 902 | return posix_error_with_allocated_filename(path); |
| 903 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 904 | Py_INCREF(Py_None); |
| 905 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 906 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 907 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 909 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 910 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 911 | static char posix_getcwd__doc__[] = |
| 912 | "getcwd() -> path\n\ |
| 913 | Return a string representing the current working directory."; |
| 914 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 915 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 916 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 917 | { |
| 918 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 919 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 920 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 921 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 922 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 923 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 924 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 925 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 926 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 927 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 928 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 929 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 931 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 932 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 933 | static char posix_link__doc__[] = |
| 934 | "link(src, dst) -> None\n\ |
| 935 | Create a hard link to a file."; |
| 936 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 937 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 938 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 939 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 940 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 941 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 942 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 944 | |
| 945 | static char posix_listdir__doc__[] = |
| 946 | "listdir(path) -> list_of_strings\n\ |
| 947 | Return a list containing the names of the entries in the directory.\n\ |
| 948 | \n\ |
| 949 | path: path of directory to list\n\ |
| 950 | \n\ |
| 951 | The list is in arbitrary order. It does not include the special\n\ |
| 952 | entries '.' and '..' even if they are present in the directory."; |
| 953 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 954 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 955 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 956 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 957 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 958 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 959 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 960 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 961 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 962 | HANDLE hFindFile; |
| 963 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 964 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 965 | char namebuf[MAX_PATH*2+5]; |
| 966 | char *bufptr = namebuf; |
| 967 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 968 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 969 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 970 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 971 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 972 | return NULL; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 973 | ch = namebuf[len-1]; |
| 974 | if (ch != '/' && ch != '\\' && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 975 | namebuf[len++] = '/'; |
| 976 | strcpy(namebuf + len, "*.*"); |
| 977 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 978 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 979 | return NULL; |
| 980 | |
| 981 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 982 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 983 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 984 | if (errno == ERROR_FILE_NOT_FOUND) |
| 985 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 986 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 987 | } |
| 988 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 989 | if (FileData.cFileName[0] == '.' && |
| 990 | (FileData.cFileName[1] == '\0' || |
| 991 | FileData.cFileName[1] == '.' && |
| 992 | FileData.cFileName[2] == '\0')) |
| 993 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 994 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 995 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 996 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 997 | d = NULL; |
| 998 | break; |
| 999 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1000 | if (PyList_Append(d, v) != 0) { |
| 1001 | Py_DECREF(v); |
| 1002 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1003 | d = NULL; |
| 1004 | break; |
| 1005 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1006 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1007 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1008 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1009 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1010 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1011 | |
| 1012 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1013 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1014 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1015 | |
| 1016 | #ifndef MAX_PATH |
| 1017 | #define MAX_PATH 250 |
| 1018 | #endif |
| 1019 | char *name, *pt; |
| 1020 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1021 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1022 | char namebuf[MAX_PATH+5]; |
| 1023 | struct _find_t ep; |
| 1024 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1025 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1026 | return NULL; |
| 1027 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1028 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1029 | return NULL; |
| 1030 | } |
| 1031 | strcpy(namebuf, name); |
| 1032 | for (pt = namebuf; *pt; pt++) |
| 1033 | if (*pt == '/') |
| 1034 | *pt = '\\'; |
| 1035 | if (namebuf[len-1] != '\\') |
| 1036 | namebuf[len++] = '\\'; |
| 1037 | strcpy(namebuf + len, "*.*"); |
| 1038 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1039 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1040 | return NULL; |
| 1041 | |
| 1042 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1043 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 1044 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1045 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1046 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1047 | } |
| 1048 | do { |
| 1049 | if (ep.name[0] == '.' && |
| 1050 | (ep.name[1] == '\0' || |
| 1051 | ep.name[1] == '.' && |
| 1052 | ep.name[2] == '\0')) |
| 1053 | continue; |
| 1054 | strcpy(namebuf, ep.name); |
| 1055 | for (pt = namebuf; *pt; pt++) |
| 1056 | if (isupper(*pt)) |
| 1057 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1058 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1059 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1060 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1061 | d = NULL; |
| 1062 | break; |
| 1063 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1064 | if (PyList_Append(d, v) != 0) { |
| 1065 | Py_DECREF(v); |
| 1066 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1067 | d = NULL; |
| 1068 | break; |
| 1069 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1070 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1071 | } while (_dos_findnext(&ep) == 0); |
| 1072 | |
| 1073 | return d; |
| 1074 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1075 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1076 | |
| 1077 | #ifndef MAX_PATH |
| 1078 | #define MAX_PATH CCHMAXPATH |
| 1079 | #endif |
| 1080 | char *name, *pt; |
| 1081 | int len; |
| 1082 | PyObject *d, *v; |
| 1083 | char namebuf[MAX_PATH+5]; |
| 1084 | HDIR hdir = 1; |
| 1085 | ULONG srchcnt = 1; |
| 1086 | FILEFINDBUF3 ep; |
| 1087 | APIRET rc; |
| 1088 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1089 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1090 | return NULL; |
| 1091 | if (len >= MAX_PATH) { |
| 1092 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | strcpy(namebuf, name); |
| 1096 | for (pt = namebuf; *pt; pt++) |
| 1097 | if (*pt == '/') |
| 1098 | *pt = '\\'; |
| 1099 | if (namebuf[len-1] != '\\') |
| 1100 | namebuf[len++] = '\\'; |
| 1101 | strcpy(namebuf + len, "*.*"); |
| 1102 | |
| 1103 | if ((d = PyList_New(0)) == NULL) |
| 1104 | return NULL; |
| 1105 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1106 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1107 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1108 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1109 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1110 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1111 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (rc != NO_ERROR) { |
| 1114 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1115 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1118 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1119 | do { |
| 1120 | if (ep.achName[0] == '.' |
| 1121 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1122 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1123 | |
| 1124 | strcpy(namebuf, ep.achName); |
| 1125 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1126 | /* Leave Case of Name Alone -- In Native Form */ |
| 1127 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1128 | |
| 1129 | v = PyString_FromString(namebuf); |
| 1130 | if (v == NULL) { |
| 1131 | Py_DECREF(d); |
| 1132 | d = NULL; |
| 1133 | break; |
| 1134 | } |
| 1135 | if (PyList_Append(d, v) != 0) { |
| 1136 | Py_DECREF(v); |
| 1137 | Py_DECREF(d); |
| 1138 | d = NULL; |
| 1139 | break; |
| 1140 | } |
| 1141 | Py_DECREF(v); |
| 1142 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1143 | } |
| 1144 | |
| 1145 | return d; |
| 1146 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1148 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1149 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1150 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1151 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1152 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1153 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1154 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1155 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1156 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1157 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1158 | closedir(dirp); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1162 | if (ep->d_name[0] == '.' && |
| 1163 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1164 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1165 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1166 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1167 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1168 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | d = NULL; |
| 1170 | break; |
| 1171 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1172 | if (PyList_Append(d, v) != 0) { |
| 1173 | Py_DECREF(v); |
| 1174 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1175 | d = NULL; |
| 1176 | break; |
| 1177 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1178 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1179 | } |
| 1180 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1182 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1183 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1184 | #endif /* which OS */ |
| 1185 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1186 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1187 | #ifdef MS_WIN32 |
| 1188 | /* A helper function for abspath on win32 */ |
| 1189 | static PyObject * |
| 1190 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1191 | { |
| 1192 | /* assume encoded strings wont more than double no of chars */ |
| 1193 | char inbuf[MAX_PATH*2]; |
| 1194 | char *inbufp = inbuf; |
| 1195 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1196 | char outbuf[MAX_PATH*2]; |
| 1197 | char *temp; |
| 1198 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1199 | Py_FileSystemDefaultEncoding, &inbufp, |
| 1200 | &insize)) |
| 1201 | return NULL; |
| 1202 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1203 | outbuf, &temp)) |
| 1204 | return win32_error("GetFullPathName", inbuf); |
| 1205 | return PyString_FromString(outbuf); |
| 1206 | } /* end of posix__getfullpathname */ |
| 1207 | #endif /* MS_WIN32 */ |
| 1208 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1209 | static char posix_mkdir__doc__[] = |
| 1210 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1211 | Create a directory."; |
| 1212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1214 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1215 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1216 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1217 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1218 | int mode = 0777; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1219 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 1220 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1221 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1222 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1223 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1224 | res = mkdir(path); |
| 1225 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1226 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1227 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1228 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1229 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1230 | return posix_error_with_allocated_filename(path); |
| 1231 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1232 | Py_INCREF(Py_None); |
| 1233 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1236 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1237 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1238 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1239 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1240 | #include <sys/resource.h> |
| 1241 | #endif |
| 1242 | #endif |
| 1243 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1244 | static char posix_nice__doc__[] = |
| 1245 | "nice(inc) -> new_priority\n\ |
| 1246 | Decrease the priority of process and return new priority."; |
| 1247 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1248 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1249 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1250 | { |
| 1251 | int increment, value; |
| 1252 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1253 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1254 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1255 | |
| 1256 | /* There are two flavours of 'nice': one that returns the new |
| 1257 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1258 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1259 | the use of getpriority() to get the new priority. |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1260 | |
| 1261 | If we are of the nice family that returns the new priority, we |
| 1262 | need to clear errno before the call, and check if errno is filled |
| 1263 | before calling posix_error() on a returnvalue of -1, because the |
| 1264 | -1 may be the actual new priority! */ |
| 1265 | |
| 1266 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1267 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1268 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1269 | if (value == 0) |
| 1270 | value = getpriority(PRIO_PROCESS, 0); |
| 1271 | #endif |
| 1272 | if (value == -1 && errno != 0) |
| 1273 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1274 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1275 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1276 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1277 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1279 | |
| 1280 | static char posix_rename__doc__[] = |
| 1281 | "rename(old, new) -> None\n\ |
| 1282 | Rename a file or directory."; |
| 1283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1285 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1286 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1287 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1290 | |
| 1291 | static char posix_rmdir__doc__[] = |
| 1292 | "rmdir(path) -> None\n\ |
| 1293 | Remove a directory."; |
| 1294 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1295 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1296 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1297 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1298 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1301 | |
| 1302 | static char posix_stat__doc__[] = |
| 1303 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1304 | Perform a stat system call on the given path."; |
| 1305 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1306 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1307 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1308 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1309 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1313 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1314 | static char posix_system__doc__[] = |
| 1315 | "system(command) -> exit_status\n\ |
| 1316 | Execute the command (a string) in a subshell."; |
| 1317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1318 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1319 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1320 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1321 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1322 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1323 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1324 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1325 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1326 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1327 | Py_END_ALLOW_THREADS |
| 1328 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1329 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1330 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1332 | |
| 1333 | static char posix_umask__doc__[] = |
| 1334 | "umask(new_mask) -> old_mask\n\ |
| 1335 | Set the current numeric umask and return the previous umask."; |
| 1336 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1337 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1338 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1339 | { |
| 1340 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1341 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1342 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1343 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1344 | if (i < 0) |
| 1345 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1346 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1349 | |
| 1350 | static char posix_unlink__doc__[] = |
| 1351 | "unlink(path) -> None\n\ |
| 1352 | Remove a file (same as remove(path))."; |
| 1353 | |
| 1354 | static char posix_remove__doc__[] = |
| 1355 | "remove(path) -> None\n\ |
| 1356 | Remove a file (same as unlink(path))."; |
| 1357 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1358 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1359 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1360 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1361 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1364 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1366 | static char posix_uname__doc__[] = |
| 1367 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1368 | Return a tuple identifying the current operating system."; |
| 1369 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1370 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1371 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1372 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1373 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1374 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1375 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1376 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1377 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1378 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1379 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1380 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1381 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1382 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1383 | u.sysname, |
| 1384 | u.nodename, |
| 1385 | u.release, |
| 1386 | u.version, |
| 1387 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1388 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1389 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1391 | |
| 1392 | static char posix_utime__doc__[] = |
| 1393 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1394 | utime(path, None) -> None\n\ |
| 1395 | Set the access and modified time of the file to the given values. If the\n\ |
| 1396 | 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] | 1397 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1398 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1399 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1400 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1401 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1402 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1403 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1404 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1405 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1406 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1407 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1408 | struct utimbuf buf; |
| 1409 | #define ATIME buf.actime |
| 1410 | #define MTIME buf.modtime |
| 1411 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1412 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1413 | time_t buf[2]; |
| 1414 | #define ATIME buf[0] |
| 1415 | #define MTIME buf[1] |
| 1416 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1417 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1418 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1419 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1420 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1421 | if (arg == Py_None) { |
| 1422 | /* optional time values not given */ |
| 1423 | Py_BEGIN_ALLOW_THREADS |
| 1424 | res = utime(path, NULL); |
| 1425 | Py_END_ALLOW_THREADS |
| 1426 | } |
| 1427 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1428 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1429 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1430 | return NULL; |
| 1431 | } |
| 1432 | else { |
| 1433 | ATIME = atime; |
| 1434 | MTIME = mtime; |
| 1435 | Py_BEGIN_ALLOW_THREADS |
| 1436 | res = utime(path, UTIME_ARG); |
| 1437 | Py_END_ALLOW_THREADS |
| 1438 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1439 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1440 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1441 | Py_INCREF(Py_None); |
| 1442 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1443 | #undef UTIME_ARG |
| 1444 | #undef ATIME |
| 1445 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1448 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1449 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1450 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1451 | static char posix__exit__doc__[] = |
| 1452 | "_exit(status)\n\ |
| 1453 | Exit to the system with specified status, without normal exit processing."; |
| 1454 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1455 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1456 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1457 | { |
| 1458 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1459 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1460 | return NULL; |
| 1461 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1462 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1465 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1466 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1467 | static char posix_execv__doc__[] = |
| 1468 | "execv(path, args)\n\ |
| 1469 | Execute an executable path with arguments, replacing current process.\n\ |
| 1470 | \n\ |
| 1471 | path: path of executable file\n\ |
| 1472 | args: tuple or list of strings"; |
| 1473 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1475 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1476 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1477 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1478 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1479 | char **argvlist; |
| 1480 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1481 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1482 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1483 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1484 | argv is a list or tuple of strings. */ |
| 1485 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1486 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1487 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1488 | if (PyList_Check(argv)) { |
| 1489 | argc = PyList_Size(argv); |
| 1490 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1491 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1492 | else if (PyTuple_Check(argv)) { |
| 1493 | argc = PyTuple_Size(argv); |
| 1494 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1495 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1496 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1497 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1498 | return NULL; |
| 1499 | } |
| 1500 | |
| 1501 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1502 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1503 | return NULL; |
| 1504 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1505 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1506 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1507 | if (argvlist == NULL) |
| 1508 | return NULL; |
| 1509 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1510 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1511 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1512 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1513 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1514 | return NULL; |
| 1515 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1516 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1517 | } |
| 1518 | argvlist[argc] = NULL; |
| 1519 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1520 | #ifdef BAD_EXEC_PROTOTYPES |
| 1521 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1522 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1523 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1524 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1525 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1526 | /* If we get here it's definitely an error */ |
| 1527 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1528 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1529 | return posix_error(); |
| 1530 | } |
| 1531 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1532 | |
| 1533 | static char posix_execve__doc__[] = |
| 1534 | "execve(path, args, env)\n\ |
| 1535 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1536 | \n\ |
| 1537 | path: path of executable file\n\ |
| 1538 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1539 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1540 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1541 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1542 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1543 | { |
| 1544 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1545 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1546 | char **argvlist; |
| 1547 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1548 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1549 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1550 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* execve has three arguments: (path, argv, env), where |
| 1553 | argv is a list or tuple of strings and env is a dictionary |
| 1554 | like posix.environ. */ |
| 1555 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1556 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1557 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1558 | if (PyList_Check(argv)) { |
| 1559 | argc = PyList_Size(argv); |
| 1560 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1561 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1562 | else if (PyTuple_Check(argv)) { |
| 1563 | argc = PyTuple_Size(argv); |
| 1564 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1565 | } |
| 1566 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1567 | PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1570 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1571 | PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object"); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1572 | return NULL; |
| 1573 | } |
| 1574 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1575 | if (argc == 0) { |
| 1576 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1577 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1578 | return NULL; |
| 1579 | } |
| 1580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1581 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1582 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1583 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1584 | return NULL; |
| 1585 | } |
| 1586 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1587 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1588 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1589 | &argvlist[i])) |
| 1590 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1591 | goto fail_1; |
| 1592 | } |
| 1593 | } |
| 1594 | argvlist[argc] = NULL; |
| 1595 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1596 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1597 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1598 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1599 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1600 | goto fail_1; |
| 1601 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1602 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1603 | keys = PyMapping_Keys(env); |
| 1604 | vals = PyMapping_Values(env); |
| 1605 | if (!keys || !vals) |
| 1606 | goto fail_2; |
| 1607 | |
| 1608 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1609 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1610 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1611 | |
| 1612 | key = PyList_GetItem(keys, pos); |
| 1613 | val = PyList_GetItem(vals, pos); |
| 1614 | if (!key || !val) |
| 1615 | goto fail_2; |
| 1616 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1617 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1618 | !PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1619 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1620 | goto fail_2; |
| 1621 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1622 | |
| 1623 | #if defined(PYOS_OS2) |
| 1624 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1625 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1626 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1627 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1628 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1629 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1630 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1631 | goto fail_2; |
| 1632 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1633 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1634 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1635 | #if defined(PYOS_OS2) |
| 1636 | } |
| 1637 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1638 | } |
| 1639 | envlist[envc] = 0; |
| 1640 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1641 | |
| 1642 | #ifdef BAD_EXEC_PROTOTYPES |
| 1643 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1644 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1645 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1646 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1647 | |
| 1648 | /* If we get here it's definitely an error */ |
| 1649 | |
| 1650 | (void) posix_error(); |
| 1651 | |
| 1652 | fail_2: |
| 1653 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1654 | PyMem_DEL(envlist[envc]); |
| 1655 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1656 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1657 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1658 | Py_XDECREF(vals); |
| 1659 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1660 | return NULL; |
| 1661 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1662 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1665 | #ifdef HAVE_SPAWNV |
| 1666 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1667 | "spawnv(mode, path, args)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1668 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1669 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1670 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1671 | path: path of executable file\n\ |
| 1672 | args: tuple or list of strings"; |
| 1673 | |
| 1674 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1675 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1676 | { |
| 1677 | char *path; |
| 1678 | PyObject *argv; |
| 1679 | char **argvlist; |
| 1680 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1681 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1682 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1683 | |
| 1684 | /* spawnv has three arguments: (mode, path, argv), where |
| 1685 | argv is a list or tuple of strings. */ |
| 1686 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1687 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1688 | return NULL; |
| 1689 | if (PyList_Check(argv)) { |
| 1690 | argc = PyList_Size(argv); |
| 1691 | getitem = PyList_GetItem; |
| 1692 | } |
| 1693 | else if (PyTuple_Check(argv)) { |
| 1694 | argc = PyTuple_Size(argv); |
| 1695 | getitem = PyTuple_GetItem; |
| 1696 | } |
| 1697 | else { |
Martin v. Löwis | e75f0e4 | 2001-11-24 09:31:44 +0000 | [diff] [blame] | 1698 | PyErr_SetString(PyExc_TypeError, "spawnv() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1699 | return NULL; |
| 1700 | } |
| 1701 | |
| 1702 | argvlist = PyMem_NEW(char *, argc+1); |
| 1703 | if (argvlist == NULL) |
| 1704 | return NULL; |
| 1705 | for (i = 0; i < argc; i++) { |
| 1706 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1707 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1708 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1709 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1710 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | argvlist[argc] = NULL; |
| 1714 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1715 | if (mode == _OLD_P_OVERLAY) |
| 1716 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1717 | |
| 1718 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1719 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1720 | Py_END_ALLOW_THREADS |
| 1721 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1722 | PyMem_DEL(argvlist); |
| 1723 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1724 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1725 | return posix_error(); |
| 1726 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1727 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1728 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1729 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1730 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1731 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1736 | "spawnve(mode, path, args, env)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1737 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1738 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1739 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1740 | path: path of executable file\n\ |
| 1741 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1742 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1743 | |
| 1744 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1745 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1746 | { |
| 1747 | char *path; |
| 1748 | PyObject *argv, *env; |
| 1749 | char **argvlist; |
| 1750 | char **envlist; |
| 1751 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1752 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1753 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1754 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1755 | |
| 1756 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1757 | argv is a list or tuple of strings and env is a dictionary |
| 1758 | like posix.environ. */ |
| 1759 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1760 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1761 | return NULL; |
| 1762 | if (PyList_Check(argv)) { |
| 1763 | argc = PyList_Size(argv); |
| 1764 | getitem = PyList_GetItem; |
| 1765 | } |
| 1766 | else if (PyTuple_Check(argv)) { |
| 1767 | argc = PyTuple_Size(argv); |
| 1768 | getitem = PyTuple_GetItem; |
| 1769 | } |
| 1770 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1771 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1772 | return NULL; |
| 1773 | } |
| 1774 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1775 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1776 | return NULL; |
| 1777 | } |
| 1778 | |
| 1779 | argvlist = PyMem_NEW(char *, argc+1); |
| 1780 | if (argvlist == NULL) { |
| 1781 | PyErr_NoMemory(); |
| 1782 | return NULL; |
| 1783 | } |
| 1784 | for (i = 0; i < argc; i++) { |
| 1785 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1786 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1787 | &argvlist[i])) |
| 1788 | { |
| 1789 | goto fail_1; |
| 1790 | } |
| 1791 | } |
| 1792 | argvlist[argc] = NULL; |
| 1793 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1794 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1795 | envlist = PyMem_NEW(char *, i + 1); |
| 1796 | if (envlist == NULL) { |
| 1797 | PyErr_NoMemory(); |
| 1798 | goto fail_1; |
| 1799 | } |
| 1800 | envc = 0; |
| 1801 | keys = PyMapping_Keys(env); |
| 1802 | vals = PyMapping_Values(env); |
| 1803 | if (!keys || !vals) |
| 1804 | goto fail_2; |
| 1805 | |
| 1806 | for (pos = 0; pos < i; pos++) { |
| 1807 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1808 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1809 | |
| 1810 | key = PyList_GetItem(keys, pos); |
| 1811 | val = PyList_GetItem(vals, pos); |
| 1812 | if (!key || !val) |
| 1813 | goto fail_2; |
| 1814 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1815 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1816 | !PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1817 | { |
| 1818 | goto fail_2; |
| 1819 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1820 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1821 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1822 | if (p == NULL) { |
| 1823 | PyErr_NoMemory(); |
| 1824 | goto fail_2; |
| 1825 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1826 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1827 | envlist[envc++] = p; |
| 1828 | } |
| 1829 | envlist[envc] = 0; |
| 1830 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1831 | if (mode == _OLD_P_OVERLAY) |
| 1832 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1833 | |
| 1834 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1835 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1836 | Py_END_ALLOW_THREADS |
| 1837 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1838 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1839 | (void) posix_error(); |
| 1840 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1841 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1842 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1843 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1844 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1845 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1846 | |
| 1847 | fail_2: |
| 1848 | while (--envc >= 0) |
| 1849 | PyMem_DEL(envlist[envc]); |
| 1850 | PyMem_DEL(envlist); |
| 1851 | fail_1: |
| 1852 | PyMem_DEL(argvlist); |
| 1853 | Py_XDECREF(vals); |
| 1854 | Py_XDECREF(keys); |
| 1855 | return res; |
| 1856 | } |
| 1857 | #endif /* HAVE_SPAWNV */ |
| 1858 | |
| 1859 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1860 | #ifdef HAVE_FORK1 |
| 1861 | static char posix_fork1__doc__[] = |
| 1862 | "fork1() -> pid\n\ |
| 1863 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1864 | \n\ |
| 1865 | Return 0 to child process and PID of child to parent process."; |
| 1866 | |
| 1867 | static PyObject * |
| 1868 | posix_fork1(self, args) |
| 1869 | PyObject *self; |
| 1870 | PyObject *args; |
| 1871 | { |
| 1872 | int pid; |
| 1873 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1874 | return NULL; |
| 1875 | pid = fork1(); |
| 1876 | if (pid == -1) |
| 1877 | return posix_error(); |
| 1878 | PyOS_AfterFork(); |
| 1879 | return PyInt_FromLong((long)pid); |
| 1880 | } |
| 1881 | #endif |
| 1882 | |
| 1883 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1884 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1885 | static char posix_fork__doc__[] = |
| 1886 | "fork() -> pid\n\ |
| 1887 | Fork a child process.\n\ |
| 1888 | \n\ |
| 1889 | Return 0 to child process and PID of child to parent process."; |
| 1890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1892 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1893 | { |
| 1894 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1895 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1896 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1897 | pid = fork(); |
| 1898 | if (pid == -1) |
| 1899 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1900 | if (pid == 0) |
| 1901 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1902 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1903 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1904 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1905 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1906 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1907 | #ifdef HAVE_PTY_H |
| 1908 | #include <pty.h> |
| 1909 | #else |
| 1910 | #ifdef HAVE_LIBUTIL_H |
| 1911 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1912 | #endif /* HAVE_LIBUTIL_H */ |
| 1913 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1914 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1915 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1916 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1917 | static char posix_openpty__doc__[] = |
| 1918 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1919 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1920 | |
| 1921 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1922 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1923 | { |
| 1924 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1925 | #ifndef HAVE_OPENPTY |
| 1926 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1927 | #endif |
| 1928 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1929 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1930 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1931 | |
| 1932 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1933 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1934 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1935 | #else |
| 1936 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1937 | if (slave_name == NULL) |
| 1938 | return posix_error(); |
| 1939 | |
| 1940 | slave_fd = open(slave_name, O_RDWR); |
| 1941 | if (slave_fd < 0) |
| 1942 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1943 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1944 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1945 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1946 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1947 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1948 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1949 | |
| 1950 | #ifdef HAVE_FORKPTY |
| 1951 | static char posix_forkpty__doc__[] = |
| 1952 | "forkpty() -> (pid, master_fd)\n\ |
| 1953 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1954 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1955 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1956 | |
| 1957 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1958 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1959 | { |
| 1960 | int master_fd, pid; |
| 1961 | |
| 1962 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1963 | return NULL; |
| 1964 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1965 | if (pid == -1) |
| 1966 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1967 | if (pid == 0) |
| 1968 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1969 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1970 | } |
| 1971 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1972 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1974 | static char posix_getegid__doc__[] = |
| 1975 | "getegid() -> egid\n\ |
| 1976 | Return the current process's effective group id."; |
| 1977 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1978 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1979 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1980 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1981 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1982 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1983 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1984 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1985 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1986 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1987 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1988 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1989 | static char posix_geteuid__doc__[] = |
| 1990 | "geteuid() -> euid\n\ |
| 1991 | Return the current process's effective user id."; |
| 1992 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1993 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1994 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1995 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1996 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1997 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1998 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1999 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2000 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2003 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2004 | static char posix_getgid__doc__[] = |
| 2005 | "getgid() -> gid\n\ |
| 2006 | Return the current process's group id."; |
| 2007 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2008 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2009 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2010 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2011 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2012 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2013 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2014 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2015 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2017 | |
| 2018 | static char posix_getpid__doc__[] = |
| 2019 | "getpid() -> pid\n\ |
| 2020 | Return the current process id"; |
| 2021 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2023 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2024 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2025 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2026 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2027 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2030 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2031 | #ifdef HAVE_GETGROUPS |
| 2032 | static char posix_getgroups__doc__[] = "\ |
| 2033 | getgroups() -> list of group IDs\n\ |
| 2034 | Return list of supplemental group IDs for the process."; |
| 2035 | |
| 2036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2037 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2038 | { |
| 2039 | PyObject *result = NULL; |
| 2040 | |
| 2041 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2042 | #ifdef NGROUPS_MAX |
| 2043 | #define MAX_GROUPS NGROUPS_MAX |
| 2044 | #else |
| 2045 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2046 | #define MAX_GROUPS 64 |
| 2047 | #endif |
| 2048 | gid_t grouplist[MAX_GROUPS]; |
| 2049 | int n; |
| 2050 | |
| 2051 | n = getgroups(MAX_GROUPS, grouplist); |
| 2052 | if (n < 0) |
| 2053 | posix_error(); |
| 2054 | else { |
| 2055 | result = PyList_New(n); |
| 2056 | if (result != NULL) { |
| 2057 | PyObject *o; |
| 2058 | int i; |
| 2059 | for (i = 0; i < n; ++i) { |
| 2060 | o = PyInt_FromLong((long)grouplist[i]); |
| 2061 | if (o == NULL) { |
| 2062 | Py_DECREF(result); |
| 2063 | result = NULL; |
| 2064 | break; |
| 2065 | } |
| 2066 | PyList_SET_ITEM(result, i, o); |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | return result; |
| 2072 | } |
| 2073 | #endif |
| 2074 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2075 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2076 | static char posix_getpgrp__doc__[] = |
| 2077 | "getpgrp() -> pgrp\n\ |
| 2078 | Return the current process group id."; |
| 2079 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2080 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2081 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2082 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2083 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2084 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2085 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2086 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2087 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2088 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2089 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2090 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2091 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2092 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2093 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2094 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2095 | static char posix_setpgrp__doc__[] = |
| 2096 | "setpgrp() -> None\n\ |
| 2097 | Make this process a session leader."; |
| 2098 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2099 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2100 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2101 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2102 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2103 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2104 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2105 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2106 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2107 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2108 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2109 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2110 | Py_INCREF(Py_None); |
| 2111 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2114 | #endif /* HAVE_SETPGRP */ |
| 2115 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2116 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2117 | static char posix_getppid__doc__[] = |
| 2118 | "getppid() -> ppid\n\ |
| 2119 | Return the parent's process id."; |
| 2120 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2121 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2122 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2123 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2124 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2125 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2126 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2127 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2128 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2129 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2130 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2131 | #ifdef HAVE_GETLOGIN |
| 2132 | static char posix_getlogin__doc__[] = "\ |
| 2133 | getlogin() -> string\n\ |
| 2134 | Return the actual login name."; |
| 2135 | |
| 2136 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2137 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2138 | { |
| 2139 | PyObject *result = NULL; |
| 2140 | |
| 2141 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2142 | char *name; |
| 2143 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2144 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2145 | errno = 0; |
| 2146 | name = getlogin(); |
| 2147 | if (name == NULL) { |
| 2148 | if (errno) |
| 2149 | posix_error(); |
| 2150 | else |
| 2151 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2152 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2153 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2154 | else |
| 2155 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2156 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2157 | } |
| 2158 | return result; |
| 2159 | } |
| 2160 | #endif |
| 2161 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2162 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2163 | static char posix_getuid__doc__[] = |
| 2164 | "getuid() -> uid\n\ |
| 2165 | Return the current process's user id."; |
| 2166 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2167 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2168 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2169 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2170 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2171 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2172 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2173 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2174 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2175 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2176 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2177 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2178 | static char posix_kill__doc__[] = |
| 2179 | "kill(pid, sig) -> None\n\ |
| 2180 | Kill a process with a signal."; |
| 2181 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2182 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2183 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2184 | { |
| 2185 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2186 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2187 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2188 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2189 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2190 | APIRET rc; |
| 2191 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2192 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2193 | |
| 2194 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2195 | APIRET rc; |
| 2196 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2197 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2198 | |
| 2199 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2200 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2201 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2202 | if (kill(pid, sig) == -1) |
| 2203 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2204 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2205 | Py_INCREF(Py_None); |
| 2206 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2207 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2208 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2210 | #ifdef HAVE_PLOCK |
| 2211 | |
| 2212 | #ifdef HAVE_SYS_LOCK_H |
| 2213 | #include <sys/lock.h> |
| 2214 | #endif |
| 2215 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2216 | static char posix_plock__doc__[] = |
| 2217 | "plock(op) -> None\n\ |
| 2218 | Lock program segments into memory."; |
| 2219 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2220 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2221 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2222 | { |
| 2223 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2224 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2225 | return NULL; |
| 2226 | if (plock(op) == -1) |
| 2227 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2228 | Py_INCREF(Py_None); |
| 2229 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2230 | } |
| 2231 | #endif |
| 2232 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2233 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2234 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2235 | static char posix_popen__doc__[] = |
| 2236 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2237 | Open a pipe to/from a command returning a file object."; |
| 2238 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2239 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2240 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2241 | async_system(const char *command) |
| 2242 | { |
| 2243 | char *p, errormsg[256], args[1024]; |
| 2244 | RESULTCODES rcodes; |
| 2245 | APIRET rc; |
| 2246 | char *shell = getenv("COMSPEC"); |
| 2247 | if (!shell) |
| 2248 | shell = "cmd"; |
| 2249 | |
| 2250 | strcpy(args, shell); |
| 2251 | p = &args[ strlen(args)+1 ]; |
| 2252 | strcpy(p, "/c "); |
| 2253 | strcat(p, command); |
| 2254 | p += strlen(p) + 1; |
| 2255 | *p = '\0'; |
| 2256 | |
| 2257 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2258 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2259 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2260 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2261 | &rcodes, shell); |
| 2262 | return rc; |
| 2263 | } |
| 2264 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2265 | static FILE * |
| 2266 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2267 | { |
| 2268 | HFILE rhan, whan; |
| 2269 | FILE *retfd = NULL; |
| 2270 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2271 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2272 | if (rc != NO_ERROR) { |
| 2273 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2274 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2275 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2276 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2277 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2278 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2279 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2280 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2281 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2282 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2283 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2284 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2285 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2286 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2289 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2290 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2291 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2292 | if (rc == NO_ERROR) |
| 2293 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2294 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2295 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2296 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2297 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2298 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2299 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2300 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2301 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2302 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2304 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2305 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2306 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2307 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2310 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2311 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2312 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2313 | if (rc == NO_ERROR) |
| 2314 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2315 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2316 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2317 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2318 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2319 | } else { |
| 2320 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2321 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2322 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2326 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2327 | { |
| 2328 | char *name; |
| 2329 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2330 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2331 | FILE *fp; |
| 2332 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2333 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2334 | return NULL; |
| 2335 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2336 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2337 | Py_END_ALLOW_THREADS |
| 2338 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2339 | return os2_error(err); |
| 2340 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2341 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2342 | if (f != NULL) |
| 2343 | PyFile_SetBufSize(f, bufsize); |
| 2344 | return f; |
| 2345 | } |
| 2346 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2347 | #elif defined(MS_WIN32) |
| 2348 | |
| 2349 | /* |
| 2350 | * Portable 'popen' replacement for Win32. |
| 2351 | * |
| 2352 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2353 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2354 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2355 | */ |
| 2356 | |
| 2357 | #include <malloc.h> |
| 2358 | #include <io.h> |
| 2359 | #include <fcntl.h> |
| 2360 | |
| 2361 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2362 | #define POPEN_1 1 |
| 2363 | #define POPEN_2 2 |
| 2364 | #define POPEN_3 3 |
| 2365 | #define POPEN_4 4 |
| 2366 | |
| 2367 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2368 | static int _PyPclose(FILE *file); |
| 2369 | |
| 2370 | /* |
| 2371 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2372 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2373 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2374 | */ |
| 2375 | static PyObject *_PyPopenProcs = NULL; |
| 2376 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2377 | |
| 2378 | /* popen that works from a GUI. |
| 2379 | * |
| 2380 | * The result of this function is a pipe (file) connected to the |
| 2381 | * processes stdin or stdout, depending on the requested mode. |
| 2382 | */ |
| 2383 | |
| 2384 | static PyObject * |
| 2385 | posix_popen(PyObject *self, PyObject *args) |
| 2386 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2387 | PyObject *f, *s; |
| 2388 | int tm = 0; |
| 2389 | |
| 2390 | char *cmdstring; |
| 2391 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2392 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2393 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2394 | return NULL; |
| 2395 | |
| 2396 | s = PyTuple_New(0); |
| 2397 | |
| 2398 | if (*mode == 'r') |
| 2399 | tm = _O_RDONLY; |
| 2400 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2401 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2402 | return NULL; |
| 2403 | } else |
| 2404 | tm = _O_WRONLY; |
| 2405 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2406 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2407 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2408 | return NULL; |
| 2409 | } |
| 2410 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2411 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2412 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2413 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2414 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2415 | else |
| 2416 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2417 | |
| 2418 | return f; |
| 2419 | } |
| 2420 | |
| 2421 | /* Variation on win32pipe.popen |
| 2422 | * |
| 2423 | * The result of this function is a pipe (file) connected to the |
| 2424 | * process's stdin, and a pipe connected to the process's stdout. |
| 2425 | */ |
| 2426 | |
| 2427 | static PyObject * |
| 2428 | win32_popen2(PyObject *self, PyObject *args) |
| 2429 | { |
| 2430 | PyObject *f; |
| 2431 | int tm=0; |
| 2432 | |
| 2433 | char *cmdstring; |
| 2434 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2435 | int bufsize = -1; |
| 2436 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2437 | return NULL; |
| 2438 | |
| 2439 | if (*mode == 't') |
| 2440 | tm = _O_TEXT; |
| 2441 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2442 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2443 | return NULL; |
| 2444 | } else |
| 2445 | tm = _O_BINARY; |
| 2446 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2447 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2448 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2449 | return NULL; |
| 2450 | } |
| 2451 | |
| 2452 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2453 | |
| 2454 | return f; |
| 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2459 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2460 | * The result of this function is 3 pipes - the process's stdin, |
| 2461 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2462 | */ |
| 2463 | |
| 2464 | static PyObject * |
| 2465 | win32_popen3(PyObject *self, PyObject *args) |
| 2466 | { |
| 2467 | PyObject *f; |
| 2468 | int tm = 0; |
| 2469 | |
| 2470 | char *cmdstring; |
| 2471 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2472 | int bufsize = -1; |
| 2473 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2474 | return NULL; |
| 2475 | |
| 2476 | if (*mode == 't') |
| 2477 | tm = _O_TEXT; |
| 2478 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2479 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2480 | return NULL; |
| 2481 | } else |
| 2482 | tm = _O_BINARY; |
| 2483 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2484 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2485 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2486 | return NULL; |
| 2487 | } |
| 2488 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2489 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2490 | |
| 2491 | return f; |
| 2492 | } |
| 2493 | |
| 2494 | /* |
| 2495 | * Variation on win32pipe.popen |
| 2496 | * |
| 2497 | * The result of this function is 2 pipes - the processes stdin, |
| 2498 | * and stdout+stderr combined as a single pipe. |
| 2499 | */ |
| 2500 | |
| 2501 | static PyObject * |
| 2502 | win32_popen4(PyObject *self, PyObject *args) |
| 2503 | { |
| 2504 | PyObject *f; |
| 2505 | int tm = 0; |
| 2506 | |
| 2507 | char *cmdstring; |
| 2508 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2509 | int bufsize = -1; |
| 2510 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2511 | return NULL; |
| 2512 | |
| 2513 | if (*mode == 't') |
| 2514 | tm = _O_TEXT; |
| 2515 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2516 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2517 | return NULL; |
| 2518 | } else |
| 2519 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2520 | |
| 2521 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2522 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2523 | return NULL; |
| 2524 | } |
| 2525 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2526 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2527 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2528 | return f; |
| 2529 | } |
| 2530 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2531 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2532 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2533 | HANDLE hStdin, |
| 2534 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2535 | HANDLE hStderr, |
| 2536 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2537 | { |
| 2538 | PROCESS_INFORMATION piProcInfo; |
| 2539 | STARTUPINFO siStartInfo; |
| 2540 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2541 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2542 | int i; |
| 2543 | int x; |
| 2544 | |
| 2545 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2546 | char *comshell; |
| 2547 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2548 | s1 = (char *)_alloca(i); |
| 2549 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2550 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2551 | |
| 2552 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 2553 | * then use the w9xpopen hack. |
| 2554 | */ |
| 2555 | comshell = s1 + x; |
| 2556 | while (comshell >= s1 && *comshell != '\\') |
| 2557 | --comshell; |
| 2558 | ++comshell; |
| 2559 | |
| 2560 | if (GetVersion() < 0x80000000 && |
| 2561 | _stricmp(comshell, "command.com") != 0) { |
| 2562 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2563 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2564 | s2 = (char *)_alloca(x); |
| 2565 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 2566 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2567 | } |
| 2568 | else { |
| 2569 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2570 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 2571 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2572 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2573 | char modulepath[_MAX_PATH]; |
| 2574 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2575 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2576 | for (i = x = 0; modulepath[i]; i++) |
| 2577 | if (modulepath[i] == '\\') |
| 2578 | x = i+1; |
| 2579 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2580 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 2581 | strncat(modulepath, |
| 2582 | szConsoleSpawn, |
| 2583 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2584 | -strlen(modulepath)); |
| 2585 | if (stat(modulepath, &statinfo) != 0) { |
| 2586 | /* Eeek - file-not-found - possibly an embedding |
| 2587 | situation - see if we can locate it in sys.prefix |
| 2588 | */ |
| 2589 | strncpy(modulepath, |
| 2590 | Py_GetExecPrefix(), |
| 2591 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 2592 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 2593 | strcat(modulepath, "\\"); |
| 2594 | strncat(modulepath, |
| 2595 | szConsoleSpawn, |
| 2596 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2597 | -strlen(modulepath)); |
| 2598 | /* No where else to look - raise an easily identifiable |
| 2599 | error, rather than leaving Windows to report |
| 2600 | "file not found" - as the user is probably blissfully |
| 2601 | unaware this shim EXE is used, and it will confuse them. |
| 2602 | (well, it confused me for a while ;-) |
| 2603 | */ |
| 2604 | if (stat(modulepath, &statinfo) != 0) { |
| 2605 | PyErr_Format(PyExc_RuntimeError, |
| 2606 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2607 | "for popen to work with your shell " |
| 2608 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2609 | szConsoleSpawn); |
| 2610 | return FALSE; |
| 2611 | } |
| 2612 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2613 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2614 | strlen(modulepath) + |
| 2615 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2616 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2617 | s2 = (char *)_alloca(x); |
| 2618 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 2619 | PyOS_snprintf( |
| 2620 | s2, x, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2621 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2622 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2623 | s1, |
| 2624 | s3, |
| 2625 | cmdstring); |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2630 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2631 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2632 | PyErr_SetString(PyExc_RuntimeError, |
| 2633 | "Cannot locate a COMSPEC environment variable to " |
| 2634 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2635 | return FALSE; |
| 2636 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2637 | |
| 2638 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2639 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2640 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2641 | siStartInfo.hStdInput = hStdin; |
| 2642 | siStartInfo.hStdOutput = hStdout; |
| 2643 | siStartInfo.hStdError = hStderr; |
| 2644 | siStartInfo.wShowWindow = SW_HIDE; |
| 2645 | |
| 2646 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2647 | s2, |
| 2648 | NULL, |
| 2649 | NULL, |
| 2650 | TRUE, |
| 2651 | CREATE_NEW_CONSOLE, |
| 2652 | NULL, |
| 2653 | NULL, |
| 2654 | &siStartInfo, |
| 2655 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2656 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2657 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2658 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2659 | /* Return process handle */ |
| 2660 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2661 | return TRUE; |
| 2662 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2663 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2664 | return FALSE; |
| 2665 | } |
| 2666 | |
| 2667 | /* The following code is based off of KB: Q190351 */ |
| 2668 | |
| 2669 | static PyObject * |
| 2670 | _PyPopen(char *cmdstring, int mode, int n) |
| 2671 | { |
| 2672 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2673 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2674 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2675 | |
| 2676 | SECURITY_ATTRIBUTES saAttr; |
| 2677 | BOOL fSuccess; |
| 2678 | int fd1, fd2, fd3; |
| 2679 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2680 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2681 | PyObject *f; |
| 2682 | |
| 2683 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2684 | saAttr.bInheritHandle = TRUE; |
| 2685 | saAttr.lpSecurityDescriptor = NULL; |
| 2686 | |
| 2687 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2688 | return win32_error("CreatePipe", NULL); |
| 2689 | |
| 2690 | /* Create new output read handle and the input write handle. Set |
| 2691 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2692 | * the these handles; resulting in non-closeable handles to the pipes |
| 2693 | * being created. */ |
| 2694 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2695 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2696 | FALSE, |
| 2697 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2698 | if (!fSuccess) |
| 2699 | return win32_error("DuplicateHandle", NULL); |
| 2700 | |
| 2701 | /* Close the inheritable version of ChildStdin |
| 2702 | that we're using. */ |
| 2703 | CloseHandle(hChildStdinWr); |
| 2704 | |
| 2705 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2706 | return win32_error("CreatePipe", NULL); |
| 2707 | |
| 2708 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2709 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2710 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2711 | if (!fSuccess) |
| 2712 | return win32_error("DuplicateHandle", NULL); |
| 2713 | |
| 2714 | /* Close the inheritable version of ChildStdout |
| 2715 | that we're using. */ |
| 2716 | CloseHandle(hChildStdoutRd); |
| 2717 | |
| 2718 | if (n != POPEN_4) { |
| 2719 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2720 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2721 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2722 | hChildStderrRd, |
| 2723 | GetCurrentProcess(), |
| 2724 | &hChildStderrRdDup, 0, |
| 2725 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2726 | if (!fSuccess) |
| 2727 | return win32_error("DuplicateHandle", NULL); |
| 2728 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2729 | CloseHandle(hChildStderrRd); |
| 2730 | } |
| 2731 | |
| 2732 | switch (n) { |
| 2733 | case POPEN_1: |
| 2734 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2735 | case _O_WRONLY | _O_TEXT: |
| 2736 | /* Case for writing to child Stdin in text mode. */ |
| 2737 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2738 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2739 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2740 | PyFile_SetBufSize(f, 0); |
| 2741 | /* We don't care about these pipes anymore, so close them. */ |
| 2742 | CloseHandle(hChildStdoutRdDup); |
| 2743 | CloseHandle(hChildStderrRdDup); |
| 2744 | break; |
| 2745 | |
| 2746 | case _O_RDONLY | _O_TEXT: |
| 2747 | /* Case for reading from child Stdout in text mode. */ |
| 2748 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2749 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2750 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2751 | PyFile_SetBufSize(f, 0); |
| 2752 | /* We don't care about these pipes anymore, so close them. */ |
| 2753 | CloseHandle(hChildStdinWrDup); |
| 2754 | CloseHandle(hChildStderrRdDup); |
| 2755 | break; |
| 2756 | |
| 2757 | case _O_RDONLY | _O_BINARY: |
| 2758 | /* Case for readinig from child Stdout in binary mode. */ |
| 2759 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2760 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2761 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2762 | PyFile_SetBufSize(f, 0); |
| 2763 | /* We don't care about these pipes anymore, so close them. */ |
| 2764 | CloseHandle(hChildStdinWrDup); |
| 2765 | CloseHandle(hChildStderrRdDup); |
| 2766 | break; |
| 2767 | |
| 2768 | case _O_WRONLY | _O_BINARY: |
| 2769 | /* Case for writing to child Stdin in binary mode. */ |
| 2770 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2771 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2772 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2773 | PyFile_SetBufSize(f, 0); |
| 2774 | /* We don't care about these pipes anymore, so close them. */ |
| 2775 | CloseHandle(hChildStdoutRdDup); |
| 2776 | CloseHandle(hChildStderrRdDup); |
| 2777 | break; |
| 2778 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2779 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2780 | break; |
| 2781 | |
| 2782 | case POPEN_2: |
| 2783 | case POPEN_4: |
| 2784 | { |
| 2785 | char *m1, *m2; |
| 2786 | PyObject *p1, *p2; |
| 2787 | |
| 2788 | if (mode && _O_TEXT) { |
| 2789 | m1 = "r"; |
| 2790 | m2 = "w"; |
| 2791 | } else { |
| 2792 | m1 = "rb"; |
| 2793 | m2 = "wb"; |
| 2794 | } |
| 2795 | |
| 2796 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2797 | f1 = _fdopen(fd1, m2); |
| 2798 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2799 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2800 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2801 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2802 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2803 | PyFile_SetBufSize(p2, 0); |
| 2804 | |
| 2805 | if (n != 4) |
| 2806 | CloseHandle(hChildStderrRdDup); |
| 2807 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2808 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2809 | Py_XDECREF(p1); |
| 2810 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2811 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2812 | break; |
| 2813 | } |
| 2814 | |
| 2815 | case POPEN_3: |
| 2816 | { |
| 2817 | char *m1, *m2; |
| 2818 | PyObject *p1, *p2, *p3; |
| 2819 | |
| 2820 | if (mode && _O_TEXT) { |
| 2821 | m1 = "r"; |
| 2822 | m2 = "w"; |
| 2823 | } else { |
| 2824 | m1 = "rb"; |
| 2825 | m2 = "wb"; |
| 2826 | } |
| 2827 | |
| 2828 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2829 | f1 = _fdopen(fd1, m2); |
| 2830 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2831 | f2 = _fdopen(fd2, m1); |
| 2832 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2833 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2834 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2835 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2836 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2837 | PyFile_SetBufSize(p1, 0); |
| 2838 | PyFile_SetBufSize(p2, 0); |
| 2839 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2840 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2841 | Py_XDECREF(p1); |
| 2842 | Py_XDECREF(p2); |
| 2843 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2844 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2845 | break; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (n == POPEN_4) { |
| 2850 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2851 | hChildStdinRd, |
| 2852 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2853 | hChildStdoutWr, |
| 2854 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2855 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2856 | } |
| 2857 | else { |
| 2858 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2859 | hChildStdinRd, |
| 2860 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2861 | hChildStderrWr, |
| 2862 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2863 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2866 | /* |
| 2867 | * Insert the files we've created into the process dictionary |
| 2868 | * all referencing the list with the process handle and the |
| 2869 | * initial number of files (see description below in _PyPclose). |
| 2870 | * Since if _PyPclose later tried to wait on a process when all |
| 2871 | * handles weren't closed, it could create a deadlock with the |
| 2872 | * child, we spend some energy here to try to ensure that we |
| 2873 | * either insert all file handles into the dictionary or none |
| 2874 | * at all. It's a little clumsy with the various popen modes |
| 2875 | * and variable number of files involved. |
| 2876 | */ |
| 2877 | if (!_PyPopenProcs) { |
| 2878 | _PyPopenProcs = PyDict_New(); |
| 2879 | } |
| 2880 | |
| 2881 | if (_PyPopenProcs) { |
| 2882 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2883 | int ins_rc[3]; |
| 2884 | |
| 2885 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2886 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2887 | |
| 2888 | procObj = PyList_New(2); |
| 2889 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2890 | intObj = PyInt_FromLong(file_count); |
| 2891 | |
| 2892 | if (procObj && hProcessObj && intObj) { |
| 2893 | PyList_SetItem(procObj,0,hProcessObj); |
| 2894 | PyList_SetItem(procObj,1,intObj); |
| 2895 | |
| 2896 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2897 | if (fileObj[0]) { |
| 2898 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2899 | fileObj[0], |
| 2900 | procObj); |
| 2901 | } |
| 2902 | if (file_count >= 2) { |
| 2903 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2904 | if (fileObj[1]) { |
| 2905 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2906 | fileObj[1], |
| 2907 | procObj); |
| 2908 | } |
| 2909 | } |
| 2910 | if (file_count >= 3) { |
| 2911 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2912 | if (fileObj[2]) { |
| 2913 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2914 | fileObj[2], |
| 2915 | procObj); |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2920 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2921 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2922 | /* Something failed - remove any dictionary |
| 2923 | * entries that did make it. |
| 2924 | */ |
| 2925 | if (!ins_rc[0] && fileObj[0]) { |
| 2926 | PyDict_DelItem(_PyPopenProcs, |
| 2927 | fileObj[0]); |
| 2928 | } |
| 2929 | if (!ins_rc[1] && fileObj[1]) { |
| 2930 | PyDict_DelItem(_PyPopenProcs, |
| 2931 | fileObj[1]); |
| 2932 | } |
| 2933 | if (!ins_rc[2] && fileObj[2]) { |
| 2934 | PyDict_DelItem(_PyPopenProcs, |
| 2935 | fileObj[2]); |
| 2936 | } |
| 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * Clean up our localized references for the dictionary keys |
| 2942 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2943 | * that got placed in the dictionary. |
| 2944 | */ |
| 2945 | Py_XDECREF(procObj); |
| 2946 | Py_XDECREF(fileObj[0]); |
| 2947 | Py_XDECREF(fileObj[1]); |
| 2948 | Py_XDECREF(fileObj[2]); |
| 2949 | } |
| 2950 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2951 | /* Child is launched. Close the parents copy of those pipe |
| 2952 | * handles that only the child should have open. You need to |
| 2953 | * make sure that no handles to the write end of the output pipe |
| 2954 | * are maintained in this process or else the pipe will not close |
| 2955 | * when the child process exits and the ReadFile will hang. */ |
| 2956 | |
| 2957 | if (!CloseHandle(hChildStdinRd)) |
| 2958 | return win32_error("CloseHandle", NULL); |
| 2959 | |
| 2960 | if (!CloseHandle(hChildStdoutWr)) |
| 2961 | return win32_error("CloseHandle", NULL); |
| 2962 | |
| 2963 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2964 | return win32_error("CloseHandle", NULL); |
| 2965 | |
| 2966 | return f; |
| 2967 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2968 | |
| 2969 | /* |
| 2970 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2971 | * 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] | 2972 | * |
| 2973 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2974 | * input file pointer to information about the process that was |
| 2975 | * originally created by the popen* call that created the file pointer. |
| 2976 | * The dictionary uses the file pointer as a key (with one entry |
| 2977 | * inserted for each file returned by the original popen* call) and a |
| 2978 | * single list object as the value for all files from a single call. |
| 2979 | * The list object contains the Win32 process handle at [0], and a file |
| 2980 | * count at [1], which is initialized to the total number of file |
| 2981 | * handles using that list. |
| 2982 | * |
| 2983 | * This function closes whichever handle it is passed, and decrements |
| 2984 | * the file count in the dictionary for the process handle pointed to |
| 2985 | * by this file. On the last close (when the file count reaches zero), |
| 2986 | * this function will wait for the child process and then return its |
| 2987 | * exit code as the result of the close() operation. This permits the |
| 2988 | * files to be closed in any order - it is always the close() of the |
| 2989 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2990 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2991 | |
| 2992 | /* RED_FLAG 31-Aug-2000 Tim |
| 2993 | * This is always called (today!) between a pair of |
| 2994 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2995 | * macros. So the thread running this has no valid thread state, as |
| 2996 | * far as Python is concerned. However, this calls some Python API |
| 2997 | * functions that cannot be called safely without a valid thread |
| 2998 | * state, in particular PyDict_GetItem. |
| 2999 | * As a temporary hack (although it may last for years ...), we |
| 3000 | * *rely* on not having a valid thread state in this function, in |
| 3001 | * order to create our own "from scratch". |
| 3002 | * This will deadlock if _PyPclose is ever called by a thread |
| 3003 | * holding the global lock. |
| 3004 | */ |
| 3005 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3006 | static int _PyPclose(FILE *file) |
| 3007 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3008 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3009 | DWORD exit_code; |
| 3010 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3011 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 3012 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3013 | #ifdef WITH_THREAD |
| 3014 | PyInterpreterState* pInterpreterState; |
| 3015 | PyThreadState* pThreadState; |
| 3016 | #endif |
| 3017 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3018 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3019 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3020 | */ |
| 3021 | result = fclose(file); |
| 3022 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3023 | #ifdef WITH_THREAD |
| 3024 | /* Bootstrap a valid thread state into existence. */ |
| 3025 | pInterpreterState = PyInterpreterState_New(); |
| 3026 | if (!pInterpreterState) { |
| 3027 | /* Well, we're hosed now! We don't have a thread |
| 3028 | * state, so can't call a nice error routine, or raise |
| 3029 | * an exception. Just die. |
| 3030 | */ |
| 3031 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3032 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3033 | return -1; /* unreachable */ |
| 3034 | } |
| 3035 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3036 | if (!pThreadState) { |
| 3037 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3038 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3039 | return -1; /* unreachable */ |
| 3040 | } |
| 3041 | /* Grab the global lock. Note that this will deadlock if the |
| 3042 | * current thread already has the lock! (see RED_FLAG comments |
| 3043 | * before this function) |
| 3044 | */ |
| 3045 | PyEval_RestoreThread(pThreadState); |
| 3046 | #endif |
| 3047 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3048 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3049 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3050 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3051 | fileObj)) != NULL && |
| 3052 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 3053 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 3054 | |
| 3055 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 3056 | file_count = PyInt_AsLong(intObj); |
| 3057 | |
| 3058 | if (file_count > 1) { |
| 3059 | /* Still other files referencing process */ |
| 3060 | file_count--; |
| 3061 | PyList_SetItem(procObj,1, |
| 3062 | PyInt_FromLong(file_count)); |
| 3063 | } else { |
| 3064 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3065 | if (result != EOF && |
| 3066 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 3067 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3068 | /* Possible truncation here in 16-bit environments, but |
| 3069 | * real exit codes are just the lower byte in any event. |
| 3070 | */ |
| 3071 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3072 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3073 | /* Indicate failure - this will cause the file object |
| 3074 | * to raise an I/O error and translate the last Win32 |
| 3075 | * error code from errno. We do have a problem with |
| 3076 | * last errors that overlap the normal errno table, |
| 3077 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3078 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3079 | if (result != EOF) { |
| 3080 | /* If the error wasn't from the fclose(), then |
| 3081 | * set errno for the file object error handling. |
| 3082 | */ |
| 3083 | errno = GetLastError(); |
| 3084 | } |
| 3085 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | /* Free up the native handle at this point */ |
| 3089 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3090 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3091 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3092 | /* Remove this file pointer from dictionary */ |
| 3093 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3094 | |
| 3095 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 3096 | Py_DECREF(_PyPopenProcs); |
| 3097 | _PyPopenProcs = NULL; |
| 3098 | } |
| 3099 | |
| 3100 | } /* if object retrieval ok */ |
| 3101 | |
| 3102 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3103 | } /* if _PyPopenProcs */ |
| 3104 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3105 | #ifdef WITH_THREAD |
| 3106 | /* Tear down the thread & interpreter states. |
| 3107 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3108 | * call the thread clear & delete functions, and indeed insist on |
| 3109 | * doing that themselves. The lock must be held during the clear, but |
| 3110 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3111 | */ |
| 3112 | PyInterpreterState_Clear(pInterpreterState); |
| 3113 | PyEval_ReleaseThread(pThreadState); |
| 3114 | PyInterpreterState_Delete(pInterpreterState); |
| 3115 | #endif |
| 3116 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3117 | return result; |
| 3118 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3119 | |
| 3120 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3121 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3122 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3123 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3124 | char *name; |
| 3125 | char *mode = "r"; |
| 3126 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3127 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3128 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3129 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3130 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3131 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3132 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3133 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3134 | if (fp == NULL) |
| 3135 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3136 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3137 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3138 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3139 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3140 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3141 | #endif |
| 3142 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3143 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3144 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3145 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3146 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3147 | static char posix_setuid__doc__[] = |
| 3148 | "setuid(uid) -> None\n\ |
| 3149 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3150 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3151 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3152 | { |
| 3153 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3154 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3155 | return NULL; |
| 3156 | if (setuid(uid) < 0) |
| 3157 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3158 | Py_INCREF(Py_None); |
| 3159 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3160 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3161 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3162 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3163 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3164 | #ifdef HAVE_SETEUID |
| 3165 | static char posix_seteuid__doc__[] = |
| 3166 | "seteuid(uid) -> None\n\ |
| 3167 | Set the current process's effective user id."; |
| 3168 | static PyObject * |
| 3169 | posix_seteuid (PyObject *self, PyObject *args) |
| 3170 | { |
| 3171 | int euid; |
| 3172 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3173 | return NULL; |
| 3174 | } else if (seteuid(euid) < 0) { |
| 3175 | return posix_error(); |
| 3176 | } else { |
| 3177 | Py_INCREF(Py_None); |
| 3178 | return Py_None; |
| 3179 | } |
| 3180 | } |
| 3181 | #endif /* HAVE_SETEUID */ |
| 3182 | |
| 3183 | #ifdef HAVE_SETEGID |
| 3184 | static char posix_setegid__doc__[] = |
| 3185 | "setegid(gid) -> None\n\ |
| 3186 | Set the current process's effective group id."; |
| 3187 | static PyObject * |
| 3188 | posix_setegid (PyObject *self, PyObject *args) |
| 3189 | { |
| 3190 | int egid; |
| 3191 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3192 | return NULL; |
| 3193 | } else if (setegid(egid) < 0) { |
| 3194 | return posix_error(); |
| 3195 | } else { |
| 3196 | Py_INCREF(Py_None); |
| 3197 | return Py_None; |
| 3198 | } |
| 3199 | } |
| 3200 | #endif /* HAVE_SETEGID */ |
| 3201 | |
| 3202 | #ifdef HAVE_SETREUID |
| 3203 | static char posix_setreuid__doc__[] = |
| 3204 | "seteuid(ruid, euid) -> None\n\ |
| 3205 | Set the current process's real and effective user ids."; |
| 3206 | static PyObject * |
| 3207 | posix_setreuid (PyObject *self, PyObject *args) |
| 3208 | { |
| 3209 | int ruid, euid; |
| 3210 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3211 | return NULL; |
| 3212 | } else if (setreuid(ruid, euid) < 0) { |
| 3213 | return posix_error(); |
| 3214 | } else { |
| 3215 | Py_INCREF(Py_None); |
| 3216 | return Py_None; |
| 3217 | } |
| 3218 | } |
| 3219 | #endif /* HAVE_SETREUID */ |
| 3220 | |
| 3221 | #ifdef HAVE_SETREGID |
| 3222 | static char posix_setregid__doc__[] = |
| 3223 | "setegid(rgid, egid) -> None\n\ |
| 3224 | Set the current process's real and effective group ids."; |
| 3225 | static PyObject * |
| 3226 | posix_setregid (PyObject *self, PyObject *args) |
| 3227 | { |
| 3228 | int rgid, egid; |
| 3229 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3230 | return NULL; |
| 3231 | } else if (setregid(rgid, egid) < 0) { |
| 3232 | return posix_error(); |
| 3233 | } else { |
| 3234 | Py_INCREF(Py_None); |
| 3235 | return Py_None; |
| 3236 | } |
| 3237 | } |
| 3238 | #endif /* HAVE_SETREGID */ |
| 3239 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3240 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3241 | static char posix_setgid__doc__[] = |
| 3242 | "setgid(gid) -> None\n\ |
| 3243 | Set the current process's group id."; |
| 3244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3246 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3247 | { |
| 3248 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3249 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3250 | return NULL; |
| 3251 | if (setgid(gid) < 0) |
| 3252 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3253 | Py_INCREF(Py_None); |
| 3254 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3255 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3256 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3257 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3258 | #ifdef HAVE_SETGROUPS |
| 3259 | static char posix_setgroups__doc__[] = |
| 3260 | "setgroups(list) -> None\n\ |
| 3261 | Set the groups of the current process to list."; |
| 3262 | |
| 3263 | static PyObject * |
| 3264 | posix_setgroups(PyObject *self, PyObject *args) |
| 3265 | { |
| 3266 | PyObject *groups; |
| 3267 | int i, len; |
| 3268 | gid_t grouplist[MAX_GROUPS]; |
| 3269 | |
| 3270 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 3271 | return NULL; |
| 3272 | if (!PySequence_Check(groups)) { |
| 3273 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 3274 | return NULL; |
| 3275 | } |
| 3276 | len = PySequence_Size(groups); |
| 3277 | if (len > MAX_GROUPS) { |
| 3278 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 3279 | return NULL; |
| 3280 | } |
| 3281 | for(i = 0; i < len; i++) { |
| 3282 | PyObject *elem; |
| 3283 | elem = PySequence_GetItem(groups, i); |
| 3284 | if (!elem) |
| 3285 | return NULL; |
| 3286 | if (!PyInt_Check(elem)) { |
| 3287 | PyErr_SetString(PyExc_TypeError, |
| 3288 | "groups must be integers"); |
| 3289 | Py_DECREF(elem); |
| 3290 | return NULL; |
| 3291 | } |
| 3292 | /* XXX: check that value fits into gid_t. */ |
| 3293 | grouplist[i] = PyInt_AsLong(elem); |
| 3294 | Py_DECREF(elem); |
| 3295 | } |
| 3296 | |
| 3297 | if (setgroups(len, grouplist) < 0) |
| 3298 | return posix_error(); |
| 3299 | Py_INCREF(Py_None); |
| 3300 | return Py_None; |
| 3301 | } |
| 3302 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3303 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3304 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3305 | static char posix_waitpid__doc__[] = |
| 3306 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3307 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3308 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3309 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3310 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3311 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3312 | int pid, options; |
| 3313 | #ifdef UNION_WAIT |
| 3314 | union wait status; |
| 3315 | #define status_i (status.w_status) |
| 3316 | #else |
| 3317 | int status; |
| 3318 | #define status_i status |
| 3319 | #endif |
| 3320 | status_i = 0; |
| 3321 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3322 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3323 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3324 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3325 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3326 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3327 | #else |
| 3328 | pid = waitpid(pid, &status, options); |
| 3329 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3330 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3331 | if (pid == -1) |
| 3332 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3333 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3334 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3335 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3336 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3338 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3339 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3340 | static char posix_wait__doc__[] = |
| 3341 | "wait() -> (pid, status)\n\ |
| 3342 | Wait for completion of a child process."; |
| 3343 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3344 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3345 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3346 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3347 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3348 | #ifdef UNION_WAIT |
| 3349 | union wait status; |
| 3350 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3351 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3352 | int status; |
| 3353 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3354 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3355 | if (!PyArg_ParseTuple(args, ":wait")) |
| 3356 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3357 | status_i = 0; |
| 3358 | Py_BEGIN_ALLOW_THREADS |
| 3359 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3360 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3361 | if (pid == -1) |
| 3362 | return posix_error(); |
| 3363 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3364 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3365 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3366 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3367 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3368 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3369 | |
| 3370 | static char posix_lstat__doc__[] = |
| 3371 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3372 | Like stat(path), but do not follow symbolic links."; |
| 3373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3374 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3375 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3376 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3377 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3378 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3379 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3380 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3381 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3384 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3385 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3386 | static char posix_readlink__doc__[] = |
| 3387 | "readlink(path) -> path\n\ |
| 3388 | Return a string representing the path to which the symbolic link points."; |
| 3389 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3390 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3391 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3392 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3393 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3394 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3395 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3396 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3397 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3398 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3399 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3400 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3401 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3402 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3403 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3404 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3405 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3406 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3407 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3408 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3409 | static char posix_symlink__doc__[] = |
| 3410 | "symlink(src, dst) -> None\n\ |
| 3411 | Create a symbolic link."; |
| 3412 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3413 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3414 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3415 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3416 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3417 | } |
| 3418 | #endif /* HAVE_SYMLINK */ |
| 3419 | |
| 3420 | |
| 3421 | #ifdef HAVE_TIMES |
| 3422 | #ifndef HZ |
| 3423 | #define HZ 60 /* Universal constant :-) */ |
| 3424 | #endif /* HZ */ |
| 3425 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3426 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3427 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3428 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3429 | { |
| 3430 | ULONG value = 0; |
| 3431 | |
| 3432 | Py_BEGIN_ALLOW_THREADS |
| 3433 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3434 | Py_END_ALLOW_THREADS |
| 3435 | |
| 3436 | return value; |
| 3437 | } |
| 3438 | |
| 3439 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3440 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3441 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3442 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3443 | return NULL; |
| 3444 | |
| 3445 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3446 | return Py_BuildValue("ddddd", |
| 3447 | (double)0 /* t.tms_utime / HZ */, |
| 3448 | (double)0 /* t.tms_stime / HZ */, |
| 3449 | (double)0 /* t.tms_cutime / HZ */, |
| 3450 | (double)0 /* t.tms_cstime / HZ */, |
| 3451 | (double)system_uptime() / 1000); |
| 3452 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3453 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3455 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3456 | { |
| 3457 | struct tms t; |
| 3458 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3459 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3460 | return NULL; |
| 3461 | errno = 0; |
| 3462 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3463 | if (c == (clock_t) -1) |
| 3464 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3465 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3466 | (double)t.tms_utime / HZ, |
| 3467 | (double)t.tms_stime / HZ, |
| 3468 | (double)t.tms_cutime / HZ, |
| 3469 | (double)t.tms_cstime / HZ, |
| 3470 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3471 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3472 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3473 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3474 | |
| 3475 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3476 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3477 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3478 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3479 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3480 | { |
| 3481 | FILETIME create, exit, kernel, user; |
| 3482 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3483 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3484 | return NULL; |
| 3485 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3486 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3487 | /* The fields of a FILETIME structure are the hi and lo part |
| 3488 | of a 64-bit value expressed in 100 nanosecond units. |
| 3489 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3490 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3491 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3492 | return Py_BuildValue( |
| 3493 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3494 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3495 | kernel.dwLowDateTime*1e-7), |
| 3496 | (double)(user.dwHighDateTime*429.4967296 + |
| 3497 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3498 | (double)0, |
| 3499 | (double)0, |
| 3500 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3501 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3502 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3503 | |
| 3504 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3505 | static char posix_times__doc__[] = |
| 3506 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3507 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3508 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3509 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3510 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3511 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3512 | static char posix_setsid__doc__[] = |
| 3513 | "setsid() -> None\n\ |
| 3514 | Call the system call setsid()."; |
| 3515 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3516 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3517 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3518 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3519 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3520 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3521 | if (setsid() < 0) |
| 3522 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3523 | Py_INCREF(Py_None); |
| 3524 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3525 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3526 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3527 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3528 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3529 | static char posix_setpgid__doc__[] = |
| 3530 | "setpgid(pid, pgrp) -> None\n\ |
| 3531 | Call the system call setpgid()."; |
| 3532 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3533 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3534 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3535 | { |
| 3536 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3537 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3538 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3539 | if (setpgid(pid, pgrp) < 0) |
| 3540 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3541 | Py_INCREF(Py_None); |
| 3542 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3543 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3544 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3545 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3546 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3547 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3548 | static char posix_tcgetpgrp__doc__[] = |
| 3549 | "tcgetpgrp(fd) -> pgid\n\ |
| 3550 | Return the process group associated with the terminal given by a fd."; |
| 3551 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3552 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3553 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3554 | { |
| 3555 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3556 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3557 | return NULL; |
| 3558 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3559 | if (pgid < 0) |
| 3560 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3561 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3562 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3563 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3564 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3565 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3566 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3567 | static char posix_tcsetpgrp__doc__[] = |
| 3568 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3569 | Set the process group associated with the terminal given by a fd."; |
| 3570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3572 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3573 | { |
| 3574 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3575 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3576 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3577 | if (tcsetpgrp(fd, pgid) < 0) |
| 3578 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3579 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3580 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3581 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3582 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3583 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3584 | /* Functions acting on file descriptors */ |
| 3585 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3586 | static char posix_open__doc__[] = |
| 3587 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3588 | Open a file (for low level IO)."; |
| 3589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3590 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3591 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3592 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3593 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3594 | int flag; |
| 3595 | int mode = 0777; |
| 3596 | int fd; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3597 | if (!PyArg_ParseTuple(args, "eti|i", |
| 3598 | Py_FileSystemDefaultEncoding, &file, |
| 3599 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3600 | return NULL; |
| 3601 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3602 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3603 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3604 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3605 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3606 | return posix_error_with_allocated_filename(file); |
| 3607 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3608 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3611 | |
| 3612 | static char posix_close__doc__[] = |
| 3613 | "close(fd) -> None\n\ |
| 3614 | Close a file descriptor (for low level IO)."; |
| 3615 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3616 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3617 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3618 | { |
| 3619 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3620 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3621 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3622 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3623 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3624 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3625 | if (res < 0) |
| 3626 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3627 | Py_INCREF(Py_None); |
| 3628 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3631 | |
| 3632 | static char posix_dup__doc__[] = |
| 3633 | "dup(fd) -> fd2\n\ |
| 3634 | Return a duplicate of a file descriptor."; |
| 3635 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3636 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3637 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3638 | { |
| 3639 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3640 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3641 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3642 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3643 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3644 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3645 | if (fd < 0) |
| 3646 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3647 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3650 | |
| 3651 | static char posix_dup2__doc__[] = |
| 3652 | "dup2(fd, fd2) -> None\n\ |
| 3653 | Duplicate file descriptor."; |
| 3654 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3656 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3657 | { |
| 3658 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3659 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3660 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3661 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3662 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3663 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3664 | if (res < 0) |
| 3665 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3666 | Py_INCREF(Py_None); |
| 3667 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3670 | |
| 3671 | static char posix_lseek__doc__[] = |
| 3672 | "lseek(fd, pos, how) -> newpos\n\ |
| 3673 | Set the current position of a file descriptor."; |
| 3674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3675 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3676 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3677 | { |
| 3678 | int fd, how; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3679 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3680 | LONG_LONG pos, res; |
| 3681 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3682 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3683 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3684 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3685 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3686 | return NULL; |
| 3687 | #ifdef SEEK_SET |
| 3688 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3689 | switch (how) { |
| 3690 | case 0: how = SEEK_SET; break; |
| 3691 | case 1: how = SEEK_CUR; break; |
| 3692 | case 2: how = SEEK_END; break; |
| 3693 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3694 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3695 | |
| 3696 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3697 | pos = PyInt_AsLong(posobj); |
| 3698 | #else |
| 3699 | pos = PyLong_Check(posobj) ? |
| 3700 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3701 | #endif |
| 3702 | if (PyErr_Occurred()) |
| 3703 | return NULL; |
| 3704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3705 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3706 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3707 | res = _lseeki64(fd, pos, how); |
| 3708 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3709 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3710 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3711 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3712 | if (res < 0) |
| 3713 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3714 | |
| 3715 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3716 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3717 | #else |
| 3718 | return PyLong_FromLongLong(res); |
| 3719 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3722 | |
| 3723 | static char posix_read__doc__[] = |
| 3724 | "read(fd, buffersize) -> string\n\ |
| 3725 | Read a file descriptor."; |
| 3726 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3727 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3728 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3729 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3730 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3731 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3732 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3733 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3734 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3735 | if (buffer == NULL) |
| 3736 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3737 | Py_BEGIN_ALLOW_THREADS |
| 3738 | n = read(fd, PyString_AsString(buffer), size); |
| 3739 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3740 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3742 | return posix_error(); |
| 3743 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3744 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3745 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3746 | return buffer; |
| 3747 | } |
| 3748 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3749 | |
| 3750 | static char posix_write__doc__[] = |
| 3751 | "write(fd, string) -> byteswritten\n\ |
| 3752 | Write a string to a file descriptor."; |
| 3753 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3754 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3755 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3756 | { |
| 3757 | int fd, size; |
| 3758 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3759 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3760 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3761 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3762 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3763 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3764 | if (size < 0) |
| 3765 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3766 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3769 | |
| 3770 | static char posix_fstat__doc__[]= |
| 3771 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3772 | Like stat(), but for an open file descriptor."; |
| 3773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3774 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3775 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3776 | { |
| 3777 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3778 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3779 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3780 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3781 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3782 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3783 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3784 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3785 | if (res != 0) |
| 3786 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3787 | |
| 3788 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3791 | |
| 3792 | static char posix_fdopen__doc__[] = |
| 3793 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3794 | Return an open file object connected to a file descriptor."; |
| 3795 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3796 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3797 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3798 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3799 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3800 | char *mode = "r"; |
| 3801 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3802 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3803 | PyObject *f; |
| 3804 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3805 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3806 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3807 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3808 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3809 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3810 | if (fp == NULL) |
| 3811 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3812 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3813 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3814 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3815 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3818 | static char posix_isatty__doc__[] = |
| 3819 | "isatty(fd) -> Boolean\n\ |
| 3820 | Return true if the file descriptor 'fd' is an open file descriptor\n\ |
Thomas Wouters | 12e1595 | 2000-10-03 16:54:24 +0000 | [diff] [blame] | 3821 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3822 | |
| 3823 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3824 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3825 | { |
| 3826 | int fd; |
| 3827 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3828 | return NULL; |
| 3829 | return Py_BuildValue("i", isatty(fd)); |
| 3830 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3831 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3832 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3833 | static char posix_pipe__doc__[] = |
| 3834 | "pipe() -> (read_end, write_end)\n\ |
| 3835 | Create a pipe."; |
| 3836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3837 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3838 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3839 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3840 | #if defined(PYOS_OS2) |
| 3841 | HFILE read, write; |
| 3842 | APIRET rc; |
| 3843 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3844 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3845 | return NULL; |
| 3846 | |
| 3847 | Py_BEGIN_ALLOW_THREADS |
| 3848 | rc = DosCreatePipe( &read, &write, 4096); |
| 3849 | Py_END_ALLOW_THREADS |
| 3850 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3851 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3852 | |
| 3853 | return Py_BuildValue("(ii)", read, write); |
| 3854 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3855 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3856 | int fds[2]; |
| 3857 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3858 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3859 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3860 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3861 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3862 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3863 | if (res != 0) |
| 3864 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3865 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3866 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3867 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3868 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3869 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3870 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3871 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3872 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3873 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3874 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3875 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3876 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3877 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 3878 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3879 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3880 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3881 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3882 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3883 | #endif /* HAVE_PIPE */ |
| 3884 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3885 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3886 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3887 | static char posix_mkfifo__doc__[] = |
| 3888 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3889 | Create a FIFO (a POSIX named pipe)."; |
| 3890 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3892 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3893 | { |
| 3894 | char *file; |
| 3895 | int mode = 0666; |
| 3896 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3897 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3898 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3899 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3900 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3901 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3902 | if (res < 0) |
| 3903 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3904 | Py_INCREF(Py_None); |
| 3905 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3906 | } |
| 3907 | #endif |
| 3908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3909 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3910 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3911 | static char posix_ftruncate__doc__[] = |
| 3912 | "ftruncate(fd, length) -> None\n\ |
| 3913 | Truncate a file to a specified length."; |
| 3914 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3915 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3916 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3917 | { |
| 3918 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3919 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3920 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3921 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3922 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3923 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3924 | return NULL; |
| 3925 | |
| 3926 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3927 | length = PyInt_AsLong(lenobj); |
| 3928 | #else |
| 3929 | length = PyLong_Check(lenobj) ? |
| 3930 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3931 | #endif |
| 3932 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3933 | return NULL; |
| 3934 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3935 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3936 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3937 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3938 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3939 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3940 | return NULL; |
| 3941 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3942 | Py_INCREF(Py_None); |
| 3943 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3944 | } |
| 3945 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3946 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3947 | #ifdef NeXT |
| 3948 | #define HAVE_PUTENV |
| 3949 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3950 | static int |
| 3951 | putenv(char *newval) |
| 3952 | { |
| 3953 | extern char **environ; |
| 3954 | |
| 3955 | static int firstTime = 1; |
| 3956 | char **ep; |
| 3957 | char *cp; |
| 3958 | int esiz; |
| 3959 | char *np; |
| 3960 | |
| 3961 | if (!(np = strchr(newval, '='))) |
| 3962 | return 1; |
| 3963 | *np = '\0'; |
| 3964 | |
| 3965 | /* look it up */ |
| 3966 | for (ep=environ ; *ep ; ep++) |
| 3967 | { |
| 3968 | /* this should always be true... */ |
| 3969 | if (cp = strchr(*ep, '=')) |
| 3970 | { |
| 3971 | *cp = '\0'; |
| 3972 | if (!strcmp(*ep, newval)) |
| 3973 | { |
| 3974 | /* got it! */ |
| 3975 | *cp = '='; |
| 3976 | break; |
| 3977 | } |
| 3978 | *cp = '='; |
| 3979 | } |
| 3980 | else |
| 3981 | { |
| 3982 | *np = '='; |
| 3983 | return 1; |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | *np = '='; |
| 3988 | if (*ep) |
| 3989 | { |
| 3990 | /* the string was already there: |
| 3991 | just replace it with the new one */ |
| 3992 | *ep = newval; |
| 3993 | return 0; |
| 3994 | } |
| 3995 | |
| 3996 | /* expand environ by one */ |
| 3997 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3998 | esiz++; |
| 3999 | if (firstTime) |
| 4000 | { |
| 4001 | char **epp; |
| 4002 | char **newenv; |
| 4003 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 4004 | return 1; |
| 4005 | |
| 4006 | for (ep=environ, epp=newenv ; *ep ;) |
| 4007 | *epp++ = *ep++; |
| 4008 | *epp++ = newval; |
| 4009 | *epp = (char *) 0; |
| 4010 | environ = newenv; |
| 4011 | } |
| 4012 | else |
| 4013 | { |
| 4014 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 4015 | return 1; |
| 4016 | environ[esiz - 2] = newval; |
| 4017 | environ[esiz - 1] = (char *) 0; |
| 4018 | firstTime = 0; |
| 4019 | } |
| 4020 | |
| 4021 | return 0; |
| 4022 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 4023 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4024 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4025 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4026 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4027 | static char posix_putenv__doc__[] = |
| 4028 | "putenv(key, value) -> None\n\ |
| 4029 | Change or add an environment variable."; |
| 4030 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4031 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4032 | * get re-set with another call for the same key. */ |
| 4033 | static PyObject *posix_putenv_garbage; |
| 4034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4035 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4036 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4037 | { |
| 4038 | char *s1, *s2; |
| 4039 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4040 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4041 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4042 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4043 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4044 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4045 | |
| 4046 | #if defined(PYOS_OS2) |
| 4047 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4048 | APIRET rc; |
| 4049 | |
| 4050 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4051 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4052 | |
| 4053 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4054 | if (rc != NO_ERROR) |
| 4055 | return os2_error(rc); |
| 4056 | |
| 4057 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4058 | APIRET rc; |
| 4059 | |
| 4060 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4061 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4062 | |
| 4063 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4064 | if (rc != NO_ERROR) |
| 4065 | return os2_error(rc); |
| 4066 | } else { |
| 4067 | #endif |
| 4068 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4069 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4070 | len = strlen(s1) + strlen(s2) + 2; |
| 4071 | /* len includes space for a trailing \0; the size arg to |
| 4072 | PyString_FromStringAndSize does not count that */ |
| 4073 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4074 | if (newstr == NULL) |
| 4075 | return PyErr_NoMemory(); |
| 4076 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4077 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4078 | if (putenv(new)) { |
| 4079 | posix_error(); |
| 4080 | return NULL; |
| 4081 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4082 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 4083 | * this will cause previous value to be collected. This has to |
| 4084 | * happen after the real putenv() call because the old value |
| 4085 | * was still accessible until then. */ |
| 4086 | if (PyDict_SetItem(posix_putenv_garbage, |
| 4087 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 4088 | /* really not much we can do; just leak */ |
| 4089 | PyErr_Clear(); |
| 4090 | } |
| 4091 | else { |
| 4092 | Py_DECREF(newstr); |
| 4093 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4094 | |
| 4095 | #if defined(PYOS_OS2) |
| 4096 | } |
| 4097 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4098 | Py_INCREF(Py_None); |
| 4099 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4100 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4101 | #endif /* putenv */ |
| 4102 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4103 | #ifdef HAVE_UNSETENV |
| 4104 | static char posix_unsetenv__doc__[] = |
| 4105 | "unsetenv(key) -> None\n\ |
| 4106 | Delete an environment variable."; |
| 4107 | |
| 4108 | static PyObject * |
| 4109 | posix_unsetenv(PyObject *self, PyObject *args) |
| 4110 | { |
| 4111 | char *s1; |
| 4112 | |
| 4113 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 4114 | return NULL; |
| 4115 | |
| 4116 | unsetenv(s1); |
| 4117 | |
| 4118 | /* Remove the key from posix_putenv_garbage; |
| 4119 | * this will cause it to be collected. This has to |
| 4120 | * happen after the real unsetenv() call because the |
| 4121 | * old value was still accessible until then. |
| 4122 | */ |
| 4123 | if (PyDict_DelItem(posix_putenv_garbage, |
| 4124 | PyTuple_GET_ITEM(args, 0))) { |
| 4125 | /* really not much we can do; just leak */ |
| 4126 | PyErr_Clear(); |
| 4127 | } |
| 4128 | |
| 4129 | Py_INCREF(Py_None); |
| 4130 | return Py_None; |
| 4131 | } |
| 4132 | #endif /* unsetenv */ |
| 4133 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4134 | #ifdef HAVE_STRERROR |
| 4135 | static char posix_strerror__doc__[] = |
| 4136 | "strerror(code) -> string\n\ |
| 4137 | Translate an error code to a message string."; |
| 4138 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 4139 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4140 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4141 | { |
| 4142 | int code; |
| 4143 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4144 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4145 | return NULL; |
| 4146 | message = strerror(code); |
| 4147 | if (message == NULL) { |
| 4148 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4149 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4150 | return NULL; |
| 4151 | } |
| 4152 | return PyString_FromString(message); |
| 4153 | } |
| 4154 | #endif /* strerror */ |
| 4155 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4156 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4157 | #ifdef HAVE_SYS_WAIT_H |
| 4158 | |
| 4159 | #ifdef WIFSTOPPED |
| 4160 | static char posix_WIFSTOPPED__doc__[] = |
| 4161 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4162 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4163 | |
| 4164 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4165 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4166 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4167 | #ifdef UNION_WAIT |
| 4168 | union wait status; |
| 4169 | #define status_i (status.w_status) |
| 4170 | #else |
| 4171 | int status; |
| 4172 | #define status_i status |
| 4173 | #endif |
| 4174 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4175 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4176 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4177 | { |
| 4178 | return NULL; |
| 4179 | } |
| 4180 | |
| 4181 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4182 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4183 | } |
| 4184 | #endif /* WIFSTOPPED */ |
| 4185 | |
| 4186 | #ifdef WIFSIGNALED |
| 4187 | static char posix_WIFSIGNALED__doc__[] = |
| 4188 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 4189 | 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] | 4190 | |
| 4191 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4192 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4193 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4194 | #ifdef UNION_WAIT |
| 4195 | union wait status; |
| 4196 | #define status_i (status.w_status) |
| 4197 | #else |
| 4198 | int status; |
| 4199 | #define status_i status |
| 4200 | #endif |
| 4201 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4202 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4203 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4204 | { |
| 4205 | return NULL; |
| 4206 | } |
| 4207 | |
| 4208 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4209 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4210 | } |
| 4211 | #endif /* WIFSIGNALED */ |
| 4212 | |
| 4213 | #ifdef WIFEXITED |
| 4214 | static char posix_WIFEXITED__doc__[] = |
| 4215 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4216 | Return true if the process returning 'status' exited using the exit()\n\ |
| 4217 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4218 | |
| 4219 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4220 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4221 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4222 | #ifdef UNION_WAIT |
| 4223 | union wait status; |
| 4224 | #define status_i (status.w_status) |
| 4225 | #else |
| 4226 | int status; |
| 4227 | #define status_i status |
| 4228 | #endif |
| 4229 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4230 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4231 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4232 | { |
| 4233 | return NULL; |
| 4234 | } |
| 4235 | |
| 4236 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4237 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4238 | } |
| 4239 | #endif /* WIFEXITED */ |
| 4240 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4241 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4242 | static char posix_WEXITSTATUS__doc__[] = |
| 4243 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4244 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4245 | |
| 4246 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4247 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4248 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4249 | #ifdef UNION_WAIT |
| 4250 | union wait status; |
| 4251 | #define status_i (status.w_status) |
| 4252 | #else |
| 4253 | int status; |
| 4254 | #define status_i status |
| 4255 | #endif |
| 4256 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4257 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4258 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4259 | { |
| 4260 | return NULL; |
| 4261 | } |
| 4262 | |
| 4263 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4264 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4265 | } |
| 4266 | #endif /* WEXITSTATUS */ |
| 4267 | |
| 4268 | #ifdef WTERMSIG |
| 4269 | static char posix_WTERMSIG__doc__[] = |
| 4270 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4271 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4272 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4273 | |
| 4274 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4275 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4276 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4277 | #ifdef UNION_WAIT |
| 4278 | union wait status; |
| 4279 | #define status_i (status.w_status) |
| 4280 | #else |
| 4281 | int status; |
| 4282 | #define status_i status |
| 4283 | #endif |
| 4284 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4285 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4286 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4287 | { |
| 4288 | return NULL; |
| 4289 | } |
| 4290 | |
| 4291 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4292 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4293 | } |
| 4294 | #endif /* WTERMSIG */ |
| 4295 | |
| 4296 | #ifdef WSTOPSIG |
| 4297 | static char posix_WSTOPSIG__doc__[] = |
| 4298 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4299 | 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] | 4300 | |
| 4301 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4302 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4303 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4304 | #ifdef UNION_WAIT |
| 4305 | union wait status; |
| 4306 | #define status_i (status.w_status) |
| 4307 | #else |
| 4308 | int status; |
| 4309 | #define status_i status |
| 4310 | #endif |
| 4311 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4312 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4313 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4314 | { |
| 4315 | return NULL; |
| 4316 | } |
| 4317 | |
| 4318 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4319 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4320 | } |
| 4321 | #endif /* WSTOPSIG */ |
| 4322 | |
| 4323 | #endif /* HAVE_SYS_WAIT_H */ |
| 4324 | |
| 4325 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4326 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4327 | #ifdef _SCO_DS |
| 4328 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4329 | needed definitions in sys/statvfs.h */ |
| 4330 | #define _SVID3 |
| 4331 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4332 | #include <sys/statvfs.h> |
| 4333 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4334 | static PyObject* |
| 4335 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 4336 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 4337 | if (v == NULL) |
| 4338 | return NULL; |
| 4339 | |
| 4340 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4341 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4342 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4343 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 4344 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 4345 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 4346 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 4347 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 4348 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 4349 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4350 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4351 | #else |
| 4352 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4353 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4354 | PyStructSequence_SET_ITEM(v, 2, |
| 4355 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
| 4356 | PyStructSequence_SET_ITEM(v, 3, |
| 4357 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 4358 | PyStructSequence_SET_ITEM(v, 4, |
| 4359 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
| 4360 | PyStructSequence_SET_ITEM(v, 5, |
| 4361 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
| 4362 | PyStructSequence_SET_ITEM(v, 6, |
| 4363 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
| 4364 | PyStructSequence_SET_ITEM(v, 7, |
| 4365 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 4366 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4367 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4368 | #endif |
| 4369 | |
| 4370 | return v; |
| 4371 | } |
| 4372 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4373 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4374 | "fstatvfs(fd) -> \n\ |
| 4375 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4376 | Perform an fstatvfs system call on the given fd."; |
| 4377 | |
| 4378 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4379 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4380 | { |
| 4381 | int fd, res; |
| 4382 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4383 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4384 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4385 | return NULL; |
| 4386 | Py_BEGIN_ALLOW_THREADS |
| 4387 | res = fstatvfs(fd, &st); |
| 4388 | Py_END_ALLOW_THREADS |
| 4389 | if (res != 0) |
| 4390 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4391 | |
| 4392 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4393 | } |
| 4394 | #endif /* HAVE_FSTATVFS */ |
| 4395 | |
| 4396 | |
| 4397 | #if defined(HAVE_STATVFS) |
| 4398 | #include <sys/statvfs.h> |
| 4399 | |
| 4400 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4401 | "statvfs(path) -> \n\ |
| 4402 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4403 | Perform a statvfs system call on the given path."; |
| 4404 | |
| 4405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4406 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4407 | { |
| 4408 | char *path; |
| 4409 | int res; |
| 4410 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4411 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4412 | return NULL; |
| 4413 | Py_BEGIN_ALLOW_THREADS |
| 4414 | res = statvfs(path, &st); |
| 4415 | Py_END_ALLOW_THREADS |
| 4416 | if (res != 0) |
| 4417 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4418 | |
| 4419 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4420 | } |
| 4421 | #endif /* HAVE_STATVFS */ |
| 4422 | |
| 4423 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4424 | #ifdef HAVE_TEMPNAM |
| 4425 | static char posix_tempnam__doc__[] = "\ |
| 4426 | tempnam([dir[, prefix]]) -> string\n\ |
| 4427 | Return a unique name for a temporary file.\n\ |
| 4428 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4429 | or None if not needed."; |
| 4430 | |
| 4431 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4432 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4433 | { |
| 4434 | PyObject *result = NULL; |
| 4435 | char *dir = NULL; |
| 4436 | char *pfx = NULL; |
| 4437 | char *name; |
| 4438 | |
| 4439 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4440 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4441 | |
| 4442 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4443 | "tempnam is a potential security risk to your program") < 0) |
| 4444 | return NULL; |
| 4445 | |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4446 | #ifdef MS_WIN32 |
| 4447 | name = _tempnam(dir, pfx); |
| 4448 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4449 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4450 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4451 | if (name == NULL) |
| 4452 | return PyErr_NoMemory(); |
| 4453 | result = PyString_FromString(name); |
| 4454 | free(name); |
| 4455 | return result; |
| 4456 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4457 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4458 | |
| 4459 | |
| 4460 | #ifdef HAVE_TMPFILE |
| 4461 | static char posix_tmpfile__doc__[] = "\ |
| 4462 | tmpfile() -> file object\n\ |
| 4463 | Create a temporary file with no directory entries."; |
| 4464 | |
| 4465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4466 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4467 | { |
| 4468 | FILE *fp; |
| 4469 | |
| 4470 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4471 | return NULL; |
| 4472 | fp = tmpfile(); |
| 4473 | if (fp == NULL) |
| 4474 | return posix_error(); |
| 4475 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4476 | } |
| 4477 | #endif |
| 4478 | |
| 4479 | |
| 4480 | #ifdef HAVE_TMPNAM |
| 4481 | static char posix_tmpnam__doc__[] = "\ |
| 4482 | tmpnam() -> string\n\ |
| 4483 | Return a unique name for a temporary file."; |
| 4484 | |
| 4485 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4486 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4487 | { |
| 4488 | char buffer[L_tmpnam]; |
| 4489 | char *name; |
| 4490 | |
| 4491 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4492 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4493 | |
| 4494 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4495 | "tmpnam is a potential security risk to your program") < 0) |
| 4496 | return NULL; |
| 4497 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4498 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4499 | name = tmpnam_r(buffer); |
| 4500 | #else |
| 4501 | name = tmpnam(buffer); |
| 4502 | #endif |
| 4503 | if (name == NULL) { |
| 4504 | PyErr_SetObject(PyExc_OSError, |
| 4505 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4506 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4507 | "unexpected NULL from tmpnam_r" |
| 4508 | #else |
| 4509 | "unexpected NULL from tmpnam" |
| 4510 | #endif |
| 4511 | )); |
| 4512 | return NULL; |
| 4513 | } |
| 4514 | return PyString_FromString(buffer); |
| 4515 | } |
| 4516 | #endif |
| 4517 | |
| 4518 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4519 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4520 | * It maps strings representing configuration variable names to |
| 4521 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4522 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4523 | * rarely-used constants. There are three separate tables that use |
| 4524 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4525 | * |
| 4526 | * This code is always included, even if none of the interfaces that |
| 4527 | * need it are included. The #if hackery needed to avoid it would be |
| 4528 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4529 | */ |
| 4530 | struct constdef { |
| 4531 | char *name; |
| 4532 | long value; |
| 4533 | }; |
| 4534 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4535 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4536 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4537 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4538 | { |
| 4539 | if (PyInt_Check(arg)) { |
| 4540 | *valuep = PyInt_AS_LONG(arg); |
| 4541 | return 1; |
| 4542 | } |
| 4543 | if (PyString_Check(arg)) { |
| 4544 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4545 | size_t lo = 0; |
| 4546 | size_t mid; |
| 4547 | size_t hi = tablesize; |
| 4548 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4549 | char *confname = PyString_AS_STRING(arg); |
| 4550 | while (lo < hi) { |
| 4551 | mid = (lo + hi) / 2; |
| 4552 | cmp = strcmp(confname, table[mid].name); |
| 4553 | if (cmp < 0) |
| 4554 | hi = mid; |
| 4555 | else if (cmp > 0) |
| 4556 | lo = mid + 1; |
| 4557 | else { |
| 4558 | *valuep = table[mid].value; |
| 4559 | return 1; |
| 4560 | } |
| 4561 | } |
| 4562 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4563 | } |
| 4564 | else |
| 4565 | PyErr_SetString(PyExc_TypeError, |
| 4566 | "configuration names must be strings or integers"); |
| 4567 | return 0; |
| 4568 | } |
| 4569 | |
| 4570 | |
| 4571 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4572 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4573 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4574 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4575 | #endif |
| 4576 | #ifdef _PC_ABI_ASYNC_IO |
| 4577 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4578 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4579 | #ifdef _PC_ASYNC_IO |
| 4580 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4581 | #endif |
| 4582 | #ifdef _PC_CHOWN_RESTRICTED |
| 4583 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4584 | #endif |
| 4585 | #ifdef _PC_FILESIZEBITS |
| 4586 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4587 | #endif |
| 4588 | #ifdef _PC_LAST |
| 4589 | {"PC_LAST", _PC_LAST}, |
| 4590 | #endif |
| 4591 | #ifdef _PC_LINK_MAX |
| 4592 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4593 | #endif |
| 4594 | #ifdef _PC_MAX_CANON |
| 4595 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4596 | #endif |
| 4597 | #ifdef _PC_MAX_INPUT |
| 4598 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4599 | #endif |
| 4600 | #ifdef _PC_NAME_MAX |
| 4601 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4602 | #endif |
| 4603 | #ifdef _PC_NO_TRUNC |
| 4604 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4605 | #endif |
| 4606 | #ifdef _PC_PATH_MAX |
| 4607 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4608 | #endif |
| 4609 | #ifdef _PC_PIPE_BUF |
| 4610 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4611 | #endif |
| 4612 | #ifdef _PC_PRIO_IO |
| 4613 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4614 | #endif |
| 4615 | #ifdef _PC_SOCK_MAXBUF |
| 4616 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4617 | #endif |
| 4618 | #ifdef _PC_SYNC_IO |
| 4619 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4620 | #endif |
| 4621 | #ifdef _PC_VDISABLE |
| 4622 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4623 | #endif |
| 4624 | }; |
| 4625 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4626 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4627 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4628 | { |
| 4629 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4630 | sizeof(posix_constants_pathconf) |
| 4631 | / sizeof(struct constdef)); |
| 4632 | } |
| 4633 | #endif |
| 4634 | |
| 4635 | #ifdef HAVE_FPATHCONF |
| 4636 | static char posix_fpathconf__doc__[] = "\ |
| 4637 | fpathconf(fd, name) -> integer\n\ |
| 4638 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4639 | If there is no limit, return -1."; |
| 4640 | |
| 4641 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4642 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4643 | { |
| 4644 | PyObject *result = NULL; |
| 4645 | int name, fd; |
| 4646 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4647 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4648 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4649 | long limit; |
| 4650 | |
| 4651 | errno = 0; |
| 4652 | limit = fpathconf(fd, name); |
| 4653 | if (limit == -1 && errno != 0) |
| 4654 | posix_error(); |
| 4655 | else |
| 4656 | result = PyInt_FromLong(limit); |
| 4657 | } |
| 4658 | return result; |
| 4659 | } |
| 4660 | #endif |
| 4661 | |
| 4662 | |
| 4663 | #ifdef HAVE_PATHCONF |
| 4664 | static char posix_pathconf__doc__[] = "\ |
| 4665 | pathconf(path, name) -> integer\n\ |
| 4666 | Return the configuration limit name for the file or directory path.\n\ |
| 4667 | If there is no limit, return -1."; |
| 4668 | |
| 4669 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4670 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4671 | { |
| 4672 | PyObject *result = NULL; |
| 4673 | int name; |
| 4674 | char *path; |
| 4675 | |
| 4676 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4677 | conv_path_confname, &name)) { |
| 4678 | long limit; |
| 4679 | |
| 4680 | errno = 0; |
| 4681 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4682 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4683 | if (errno == EINVAL) |
| 4684 | /* could be a path or name problem */ |
| 4685 | posix_error(); |
| 4686 | else |
| 4687 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4688 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4689 | else |
| 4690 | result = PyInt_FromLong(limit); |
| 4691 | } |
| 4692 | return result; |
| 4693 | } |
| 4694 | #endif |
| 4695 | |
| 4696 | #ifdef HAVE_CONFSTR |
| 4697 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4698 | #ifdef _CS_ARCHITECTURE |
| 4699 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4700 | #endif |
| 4701 | #ifdef _CS_HOSTNAME |
| 4702 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4703 | #endif |
| 4704 | #ifdef _CS_HW_PROVIDER |
| 4705 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4706 | #endif |
| 4707 | #ifdef _CS_HW_SERIAL |
| 4708 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4709 | #endif |
| 4710 | #ifdef _CS_INITTAB_NAME |
| 4711 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4712 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4713 | #ifdef _CS_LFS64_CFLAGS |
| 4714 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4715 | #endif |
| 4716 | #ifdef _CS_LFS64_LDFLAGS |
| 4717 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4718 | #endif |
| 4719 | #ifdef _CS_LFS64_LIBS |
| 4720 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4721 | #endif |
| 4722 | #ifdef _CS_LFS64_LINTFLAGS |
| 4723 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4724 | #endif |
| 4725 | #ifdef _CS_LFS_CFLAGS |
| 4726 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4727 | #endif |
| 4728 | #ifdef _CS_LFS_LDFLAGS |
| 4729 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4730 | #endif |
| 4731 | #ifdef _CS_LFS_LIBS |
| 4732 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4733 | #endif |
| 4734 | #ifdef _CS_LFS_LINTFLAGS |
| 4735 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4736 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4737 | #ifdef _CS_MACHINE |
| 4738 | {"CS_MACHINE", _CS_MACHINE}, |
| 4739 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4740 | #ifdef _CS_PATH |
| 4741 | {"CS_PATH", _CS_PATH}, |
| 4742 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4743 | #ifdef _CS_RELEASE |
| 4744 | {"CS_RELEASE", _CS_RELEASE}, |
| 4745 | #endif |
| 4746 | #ifdef _CS_SRPC_DOMAIN |
| 4747 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4748 | #endif |
| 4749 | #ifdef _CS_SYSNAME |
| 4750 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4751 | #endif |
| 4752 | #ifdef _CS_VERSION |
| 4753 | {"CS_VERSION", _CS_VERSION}, |
| 4754 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4755 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4756 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4757 | #endif |
| 4758 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4759 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4760 | #endif |
| 4761 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4762 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4763 | #endif |
| 4764 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4765 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4766 | #endif |
| 4767 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4768 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4769 | #endif |
| 4770 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4771 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4772 | #endif |
| 4773 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4774 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4775 | #endif |
| 4776 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4777 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4778 | #endif |
| 4779 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4780 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4781 | #endif |
| 4782 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4783 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4784 | #endif |
| 4785 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4786 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4787 | #endif |
| 4788 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4789 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4790 | #endif |
| 4791 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4792 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4793 | #endif |
| 4794 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4795 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4796 | #endif |
| 4797 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4798 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4799 | #endif |
| 4800 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4801 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4802 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4803 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4804 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4805 | #endif |
| 4806 | #ifdef _MIPS_CS_BASE |
| 4807 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4808 | #endif |
| 4809 | #ifdef _MIPS_CS_HOSTID |
| 4810 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4811 | #endif |
| 4812 | #ifdef _MIPS_CS_HW_NAME |
| 4813 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4814 | #endif |
| 4815 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4816 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4817 | #endif |
| 4818 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4819 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4820 | #endif |
| 4821 | #ifdef _MIPS_CS_OSREL_MIN |
| 4822 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4823 | #endif |
| 4824 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4825 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4826 | #endif |
| 4827 | #ifdef _MIPS_CS_OS_NAME |
| 4828 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4829 | #endif |
| 4830 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4831 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4832 | #endif |
| 4833 | #ifdef _MIPS_CS_PROCESSORS |
| 4834 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4835 | #endif |
| 4836 | #ifdef _MIPS_CS_SERIAL |
| 4837 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4838 | #endif |
| 4839 | #ifdef _MIPS_CS_VENDOR |
| 4840 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4841 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4842 | }; |
| 4843 | |
| 4844 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4845 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4846 | { |
| 4847 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4848 | sizeof(posix_constants_confstr) |
| 4849 | / sizeof(struct constdef)); |
| 4850 | } |
| 4851 | |
| 4852 | static char posix_confstr__doc__[] = "\ |
| 4853 | confstr(name) -> string\n\ |
| 4854 | Return a string-valued system configuration variable."; |
| 4855 | |
| 4856 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4857 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4858 | { |
| 4859 | PyObject *result = NULL; |
| 4860 | int name; |
| 4861 | char buffer[64]; |
| 4862 | |
| 4863 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4864 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4865 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4866 | errno = 0; |
| 4867 | if (len == 0) { |
| 4868 | if (errno != 0) |
| 4869 | posix_error(); |
| 4870 | else |
| 4871 | result = PyString_FromString(""); |
| 4872 | } |
| 4873 | else { |
| 4874 | if (len >= sizeof(buffer)) { |
| 4875 | result = PyString_FromStringAndSize(NULL, len); |
| 4876 | if (result != NULL) |
| 4877 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4878 | } |
| 4879 | else |
| 4880 | result = PyString_FromString(buffer); |
| 4881 | } |
| 4882 | } |
| 4883 | return result; |
| 4884 | } |
| 4885 | #endif |
| 4886 | |
| 4887 | |
| 4888 | #ifdef HAVE_SYSCONF |
| 4889 | static struct constdef posix_constants_sysconf[] = { |
| 4890 | #ifdef _SC_2_CHAR_TERM |
| 4891 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4892 | #endif |
| 4893 | #ifdef _SC_2_C_BIND |
| 4894 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4895 | #endif |
| 4896 | #ifdef _SC_2_C_DEV |
| 4897 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4898 | #endif |
| 4899 | #ifdef _SC_2_C_VERSION |
| 4900 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4901 | #endif |
| 4902 | #ifdef _SC_2_FORT_DEV |
| 4903 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4904 | #endif |
| 4905 | #ifdef _SC_2_FORT_RUN |
| 4906 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4907 | #endif |
| 4908 | #ifdef _SC_2_LOCALEDEF |
| 4909 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4910 | #endif |
| 4911 | #ifdef _SC_2_SW_DEV |
| 4912 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4913 | #endif |
| 4914 | #ifdef _SC_2_UPE |
| 4915 | {"SC_2_UPE", _SC_2_UPE}, |
| 4916 | #endif |
| 4917 | #ifdef _SC_2_VERSION |
| 4918 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4919 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4920 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4921 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4922 | #endif |
| 4923 | #ifdef _SC_ACL |
| 4924 | {"SC_ACL", _SC_ACL}, |
| 4925 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4926 | #ifdef _SC_AIO_LISTIO_MAX |
| 4927 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4928 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4929 | #ifdef _SC_AIO_MAX |
| 4930 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4931 | #endif |
| 4932 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4933 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4934 | #endif |
| 4935 | #ifdef _SC_ARG_MAX |
| 4936 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4937 | #endif |
| 4938 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4939 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4940 | #endif |
| 4941 | #ifdef _SC_ATEXIT_MAX |
| 4942 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4943 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4944 | #ifdef _SC_AUDIT |
| 4945 | {"SC_AUDIT", _SC_AUDIT}, |
| 4946 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4947 | #ifdef _SC_AVPHYS_PAGES |
| 4948 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4949 | #endif |
| 4950 | #ifdef _SC_BC_BASE_MAX |
| 4951 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4952 | #endif |
| 4953 | #ifdef _SC_BC_DIM_MAX |
| 4954 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4955 | #endif |
| 4956 | #ifdef _SC_BC_SCALE_MAX |
| 4957 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4958 | #endif |
| 4959 | #ifdef _SC_BC_STRING_MAX |
| 4960 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4961 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4962 | #ifdef _SC_CAP |
| 4963 | {"SC_CAP", _SC_CAP}, |
| 4964 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4965 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4966 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4967 | #endif |
| 4968 | #ifdef _SC_CHAR_BIT |
| 4969 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4970 | #endif |
| 4971 | #ifdef _SC_CHAR_MAX |
| 4972 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4973 | #endif |
| 4974 | #ifdef _SC_CHAR_MIN |
| 4975 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4976 | #endif |
| 4977 | #ifdef _SC_CHILD_MAX |
| 4978 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4979 | #endif |
| 4980 | #ifdef _SC_CLK_TCK |
| 4981 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4982 | #endif |
| 4983 | #ifdef _SC_COHER_BLKSZ |
| 4984 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4985 | #endif |
| 4986 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4987 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4988 | #endif |
| 4989 | #ifdef _SC_DCACHE_ASSOC |
| 4990 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4991 | #endif |
| 4992 | #ifdef _SC_DCACHE_BLKSZ |
| 4993 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4994 | #endif |
| 4995 | #ifdef _SC_DCACHE_LINESZ |
| 4996 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4997 | #endif |
| 4998 | #ifdef _SC_DCACHE_SZ |
| 4999 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5000 | #endif |
| 5001 | #ifdef _SC_DCACHE_TBLKSZ |
| 5002 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5003 | #endif |
| 5004 | #ifdef _SC_DELAYTIMER_MAX |
| 5005 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5006 | #endif |
| 5007 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5008 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5009 | #endif |
| 5010 | #ifdef _SC_EXPR_NEST_MAX |
| 5011 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5012 | #endif |
| 5013 | #ifdef _SC_FSYNC |
| 5014 | {"SC_FSYNC", _SC_FSYNC}, |
| 5015 | #endif |
| 5016 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5017 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5018 | #endif |
| 5019 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5020 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5021 | #endif |
| 5022 | #ifdef _SC_ICACHE_ASSOC |
| 5023 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5024 | #endif |
| 5025 | #ifdef _SC_ICACHE_BLKSZ |
| 5026 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5027 | #endif |
| 5028 | #ifdef _SC_ICACHE_LINESZ |
| 5029 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5030 | #endif |
| 5031 | #ifdef _SC_ICACHE_SZ |
| 5032 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5033 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5034 | #ifdef _SC_INF |
| 5035 | {"SC_INF", _SC_INF}, |
| 5036 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5037 | #ifdef _SC_INT_MAX |
| 5038 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5039 | #endif |
| 5040 | #ifdef _SC_INT_MIN |
| 5041 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5042 | #endif |
| 5043 | #ifdef _SC_IOV_MAX |
| 5044 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5045 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5046 | #ifdef _SC_IP_SECOPTS |
| 5047 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5048 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5049 | #ifdef _SC_JOB_CONTROL |
| 5050 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5051 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5052 | #ifdef _SC_KERN_POINTERS |
| 5053 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5054 | #endif |
| 5055 | #ifdef _SC_KERN_SIM |
| 5056 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5057 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5058 | #ifdef _SC_LINE_MAX |
| 5059 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5060 | #endif |
| 5061 | #ifdef _SC_LOGIN_NAME_MAX |
| 5062 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5063 | #endif |
| 5064 | #ifdef _SC_LOGNAME_MAX |
| 5065 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5066 | #endif |
| 5067 | #ifdef _SC_LONG_BIT |
| 5068 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5069 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5070 | #ifdef _SC_MAC |
| 5071 | {"SC_MAC", _SC_MAC}, |
| 5072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5073 | #ifdef _SC_MAPPED_FILES |
| 5074 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5075 | #endif |
| 5076 | #ifdef _SC_MAXPID |
| 5077 | {"SC_MAXPID", _SC_MAXPID}, |
| 5078 | #endif |
| 5079 | #ifdef _SC_MB_LEN_MAX |
| 5080 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5081 | #endif |
| 5082 | #ifdef _SC_MEMLOCK |
| 5083 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5084 | #endif |
| 5085 | #ifdef _SC_MEMLOCK_RANGE |
| 5086 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5087 | #endif |
| 5088 | #ifdef _SC_MEMORY_PROTECTION |
| 5089 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5090 | #endif |
| 5091 | #ifdef _SC_MESSAGE_PASSING |
| 5092 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5093 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5094 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5095 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5096 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5097 | #ifdef _SC_MQ_OPEN_MAX |
| 5098 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5099 | #endif |
| 5100 | #ifdef _SC_MQ_PRIO_MAX |
| 5101 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5102 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5103 | #ifdef _SC_NACLS_MAX |
| 5104 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5106 | #ifdef _SC_NGROUPS_MAX |
| 5107 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5108 | #endif |
| 5109 | #ifdef _SC_NL_ARGMAX |
| 5110 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5111 | #endif |
| 5112 | #ifdef _SC_NL_LANGMAX |
| 5113 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5114 | #endif |
| 5115 | #ifdef _SC_NL_MSGMAX |
| 5116 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5117 | #endif |
| 5118 | #ifdef _SC_NL_NMAX |
| 5119 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5120 | #endif |
| 5121 | #ifdef _SC_NL_SETMAX |
| 5122 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5123 | #endif |
| 5124 | #ifdef _SC_NL_TEXTMAX |
| 5125 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5126 | #endif |
| 5127 | #ifdef _SC_NPROCESSORS_CONF |
| 5128 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5129 | #endif |
| 5130 | #ifdef _SC_NPROCESSORS_ONLN |
| 5131 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5132 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5133 | #ifdef _SC_NPROC_CONF |
| 5134 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5135 | #endif |
| 5136 | #ifdef _SC_NPROC_ONLN |
| 5137 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5138 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5139 | #ifdef _SC_NZERO |
| 5140 | {"SC_NZERO", _SC_NZERO}, |
| 5141 | #endif |
| 5142 | #ifdef _SC_OPEN_MAX |
| 5143 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5144 | #endif |
| 5145 | #ifdef _SC_PAGESIZE |
| 5146 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5147 | #endif |
| 5148 | #ifdef _SC_PAGE_SIZE |
| 5149 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5150 | #endif |
| 5151 | #ifdef _SC_PASS_MAX |
| 5152 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5153 | #endif |
| 5154 | #ifdef _SC_PHYS_PAGES |
| 5155 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5156 | #endif |
| 5157 | #ifdef _SC_PII |
| 5158 | {"SC_PII", _SC_PII}, |
| 5159 | #endif |
| 5160 | #ifdef _SC_PII_INTERNET |
| 5161 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5162 | #endif |
| 5163 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5164 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5165 | #endif |
| 5166 | #ifdef _SC_PII_INTERNET_STREAM |
| 5167 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5168 | #endif |
| 5169 | #ifdef _SC_PII_OSI |
| 5170 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 5171 | #endif |
| 5172 | #ifdef _SC_PII_OSI_CLTS |
| 5173 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 5174 | #endif |
| 5175 | #ifdef _SC_PII_OSI_COTS |
| 5176 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 5177 | #endif |
| 5178 | #ifdef _SC_PII_OSI_M |
| 5179 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 5180 | #endif |
| 5181 | #ifdef _SC_PII_SOCKET |
| 5182 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 5183 | #endif |
| 5184 | #ifdef _SC_PII_XTI |
| 5185 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 5186 | #endif |
| 5187 | #ifdef _SC_POLL |
| 5188 | {"SC_POLL", _SC_POLL}, |
| 5189 | #endif |
| 5190 | #ifdef _SC_PRIORITIZED_IO |
| 5191 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 5192 | #endif |
| 5193 | #ifdef _SC_PRIORITY_SCHEDULING |
| 5194 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 5195 | #endif |
| 5196 | #ifdef _SC_REALTIME_SIGNALS |
| 5197 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 5198 | #endif |
| 5199 | #ifdef _SC_RE_DUP_MAX |
| 5200 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 5201 | #endif |
| 5202 | #ifdef _SC_RTSIG_MAX |
| 5203 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 5204 | #endif |
| 5205 | #ifdef _SC_SAVED_IDS |
| 5206 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 5207 | #endif |
| 5208 | #ifdef _SC_SCHAR_MAX |
| 5209 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 5210 | #endif |
| 5211 | #ifdef _SC_SCHAR_MIN |
| 5212 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 5213 | #endif |
| 5214 | #ifdef _SC_SELECT |
| 5215 | {"SC_SELECT", _SC_SELECT}, |
| 5216 | #endif |
| 5217 | #ifdef _SC_SEMAPHORES |
| 5218 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 5219 | #endif |
| 5220 | #ifdef _SC_SEM_NSEMS_MAX |
| 5221 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 5222 | #endif |
| 5223 | #ifdef _SC_SEM_VALUE_MAX |
| 5224 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 5225 | #endif |
| 5226 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 5227 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 5228 | #endif |
| 5229 | #ifdef _SC_SHRT_MAX |
| 5230 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 5231 | #endif |
| 5232 | #ifdef _SC_SHRT_MIN |
| 5233 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 5234 | #endif |
| 5235 | #ifdef _SC_SIGQUEUE_MAX |
| 5236 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5237 | #endif |
| 5238 | #ifdef _SC_SIGRT_MAX |
| 5239 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5240 | #endif |
| 5241 | #ifdef _SC_SIGRT_MIN |
| 5242 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5243 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5244 | #ifdef _SC_SOFTPOWER |
| 5245 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5246 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5247 | #ifdef _SC_SPLIT_CACHE |
| 5248 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5249 | #endif |
| 5250 | #ifdef _SC_SSIZE_MAX |
| 5251 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5252 | #endif |
| 5253 | #ifdef _SC_STACK_PROT |
| 5254 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5255 | #endif |
| 5256 | #ifdef _SC_STREAM_MAX |
| 5257 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5258 | #endif |
| 5259 | #ifdef _SC_SYNCHRONIZED_IO |
| 5260 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5261 | #endif |
| 5262 | #ifdef _SC_THREADS |
| 5263 | {"SC_THREADS", _SC_THREADS}, |
| 5264 | #endif |
| 5265 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5266 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5267 | #endif |
| 5268 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5269 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5270 | #endif |
| 5271 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5272 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5273 | #endif |
| 5274 | #ifdef _SC_THREAD_KEYS_MAX |
| 5275 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5276 | #endif |
| 5277 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5278 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5279 | #endif |
| 5280 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5281 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5282 | #endif |
| 5283 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5284 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5285 | #endif |
| 5286 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5287 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5288 | #endif |
| 5289 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5290 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5291 | #endif |
| 5292 | #ifdef _SC_THREAD_STACK_MIN |
| 5293 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5294 | #endif |
| 5295 | #ifdef _SC_THREAD_THREADS_MAX |
| 5296 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5297 | #endif |
| 5298 | #ifdef _SC_TIMERS |
| 5299 | {"SC_TIMERS", _SC_TIMERS}, |
| 5300 | #endif |
| 5301 | #ifdef _SC_TIMER_MAX |
| 5302 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5303 | #endif |
| 5304 | #ifdef _SC_TTY_NAME_MAX |
| 5305 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5306 | #endif |
| 5307 | #ifdef _SC_TZNAME_MAX |
| 5308 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5309 | #endif |
| 5310 | #ifdef _SC_T_IOV_MAX |
| 5311 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5312 | #endif |
| 5313 | #ifdef _SC_UCHAR_MAX |
| 5314 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5315 | #endif |
| 5316 | #ifdef _SC_UINT_MAX |
| 5317 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5318 | #endif |
| 5319 | #ifdef _SC_UIO_MAXIOV |
| 5320 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5321 | #endif |
| 5322 | #ifdef _SC_ULONG_MAX |
| 5323 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5324 | #endif |
| 5325 | #ifdef _SC_USHRT_MAX |
| 5326 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5327 | #endif |
| 5328 | #ifdef _SC_VERSION |
| 5329 | {"SC_VERSION", _SC_VERSION}, |
| 5330 | #endif |
| 5331 | #ifdef _SC_WORD_BIT |
| 5332 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5333 | #endif |
| 5334 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5335 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5336 | #endif |
| 5337 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5338 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5339 | #endif |
| 5340 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5341 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5342 | #endif |
| 5343 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5344 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5345 | #endif |
| 5346 | #ifdef _SC_XOPEN_CRYPT |
| 5347 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5348 | #endif |
| 5349 | #ifdef _SC_XOPEN_ENH_I18N |
| 5350 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5351 | #endif |
| 5352 | #ifdef _SC_XOPEN_LEGACY |
| 5353 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5354 | #endif |
| 5355 | #ifdef _SC_XOPEN_REALTIME |
| 5356 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5357 | #endif |
| 5358 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5359 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5360 | #endif |
| 5361 | #ifdef _SC_XOPEN_SHM |
| 5362 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5363 | #endif |
| 5364 | #ifdef _SC_XOPEN_UNIX |
| 5365 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5366 | #endif |
| 5367 | #ifdef _SC_XOPEN_VERSION |
| 5368 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5369 | #endif |
| 5370 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5371 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5372 | #endif |
| 5373 | #ifdef _SC_XOPEN_XPG2 |
| 5374 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5375 | #endif |
| 5376 | #ifdef _SC_XOPEN_XPG3 |
| 5377 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5378 | #endif |
| 5379 | #ifdef _SC_XOPEN_XPG4 |
| 5380 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5381 | #endif |
| 5382 | }; |
| 5383 | |
| 5384 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5385 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5386 | { |
| 5387 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5388 | sizeof(posix_constants_sysconf) |
| 5389 | / sizeof(struct constdef)); |
| 5390 | } |
| 5391 | |
| 5392 | static char posix_sysconf__doc__[] = "\ |
| 5393 | sysconf(name) -> integer\n\ |
| 5394 | Return an integer-valued system configuration variable."; |
| 5395 | |
| 5396 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5397 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5398 | { |
| 5399 | PyObject *result = NULL; |
| 5400 | int name; |
| 5401 | |
| 5402 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5403 | int value; |
| 5404 | |
| 5405 | errno = 0; |
| 5406 | value = sysconf(name); |
| 5407 | if (value == -1 && errno != 0) |
| 5408 | posix_error(); |
| 5409 | else |
| 5410 | result = PyInt_FromLong(value); |
| 5411 | } |
| 5412 | return result; |
| 5413 | } |
| 5414 | #endif |
| 5415 | |
| 5416 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5417 | /* This code is used to ensure that the tables of configuration value names |
| 5418 | * are in sorted order as required by conv_confname(), and also to build the |
| 5419 | * the exported dictionaries that are used to publish information about the |
| 5420 | * names available on the host platform. |
| 5421 | * |
| 5422 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5423 | * when used, even for platforms we're not able to test on. It also makes |
| 5424 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5425 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5426 | |
| 5427 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5428 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5429 | { |
| 5430 | const struct constdef *c1 = |
| 5431 | (const struct constdef *) v1; |
| 5432 | const struct constdef *c2 = |
| 5433 | (const struct constdef *) v2; |
| 5434 | |
| 5435 | return strcmp(c1->name, c2->name); |
| 5436 | } |
| 5437 | |
| 5438 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5439 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5440 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5441 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5442 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5443 | size_t i; |
| 5444 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5445 | |
| 5446 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5447 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5448 | if (d == NULL) |
| 5449 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5450 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5451 | for (i=0; i < tablesize; ++i) { |
| 5452 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5453 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5454 | Py_XDECREF(o); |
| 5455 | Py_DECREF(d); |
| 5456 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5457 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5458 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5459 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5460 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5461 | Py_DECREF(d); |
| 5462 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5465 | /* Return -1 on failure, 0 on success. */ |
| 5466 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5467 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5468 | { |
| 5469 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5470 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5471 | sizeof(posix_constants_pathconf) |
| 5472 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5473 | "pathconf_names", moddict)) |
| 5474 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5475 | #endif |
| 5476 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5477 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5478 | sizeof(posix_constants_confstr) |
| 5479 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5480 | "confstr_names", moddict)) |
| 5481 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5482 | #endif |
| 5483 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5484 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5485 | sizeof(posix_constants_sysconf) |
| 5486 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5487 | "sysconf_names", moddict)) |
| 5488 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5489 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5490 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5491 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5492 | |
| 5493 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5494 | static char posix_abort__doc__[] = "\ |
| 5495 | abort() -> does not return!\n\ |
| 5496 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5497 | in the hardest way possible on the hosting operating system."; |
| 5498 | |
| 5499 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5500 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5501 | { |
| 5502 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5503 | return NULL; |
| 5504 | abort(); |
| 5505 | /*NOTREACHED*/ |
| 5506 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5507 | return NULL; |
| 5508 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5509 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5510 | #ifdef MS_WIN32 |
| 5511 | static char win32_startfile__doc__[] = "\ |
| 5512 | startfile(filepath) - Start a file with its associated application.\n\ |
| 5513 | \n\ |
| 5514 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 5515 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 5516 | with whatever application (if any) its extension is associated.\n\ |
| 5517 | \n\ |
| 5518 | startfile returns as soon as the associated application is launched.\n\ |
| 5519 | There is no option to wait for the application to close, and no way\n\ |
| 5520 | to retrieve the application's exit status.\n\ |
| 5521 | \n\ |
| 5522 | The filepath is relative to the current directory. If you want to use\n\ |
| 5523 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 5524 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 5525 | |
| 5526 | static PyObject * |
| 5527 | win32_startfile(PyObject *self, PyObject *args) |
| 5528 | { |
| 5529 | char *filepath; |
| 5530 | HINSTANCE rc; |
| 5531 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 5532 | return NULL; |
| 5533 | Py_BEGIN_ALLOW_THREADS |
| 5534 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 5535 | Py_END_ALLOW_THREADS |
| 5536 | if (rc <= (HINSTANCE)32) |
| 5537 | return win32_error("startfile", filepath); |
| 5538 | Py_INCREF(Py_None); |
| 5539 | return Py_None; |
| 5540 | } |
| 5541 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5542 | |
| 5543 | static PyMethodDef posix_methods[] = { |
| 5544 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5545 | #ifdef HAVE_TTYNAME |
| 5546 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5547 | #endif |
| 5548 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5549 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5550 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5551 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5552 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 5553 | #ifdef HAVE_CHROOT |
| 5554 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 5555 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5556 | #ifdef HAVE_CTERMID |
| 5557 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5558 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5559 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5560 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5561 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5562 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5563 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5564 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5565 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5566 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5567 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5568 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5569 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5570 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5571 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5572 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5573 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5574 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5575 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5576 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5577 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5578 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5579 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5580 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5581 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5582 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5583 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5584 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5585 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5586 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5587 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5588 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5589 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5590 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5591 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5592 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5593 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5594 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5595 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5596 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5597 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5598 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5599 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5600 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5601 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5602 | #ifdef HAVE_FORK1 |
| 5603 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 5604 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5605 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5606 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5607 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5608 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5609 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5610 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5611 | #ifdef HAVE_FORKPTY |
| 5612 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5613 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5614 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5615 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5616 | #endif /* HAVE_GETEGID */ |
| 5617 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5618 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5619 | #endif /* HAVE_GETEUID */ |
| 5620 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5621 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5622 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5623 | #ifdef HAVE_GETGROUPS |
| 5624 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5625 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5626 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5627 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5628 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5629 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5630 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5631 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5632 | #endif /* HAVE_GETPPID */ |
| 5633 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5634 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5635 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5636 | #ifdef HAVE_GETLOGIN |
| 5637 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5638 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5639 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5640 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5641 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5642 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5643 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5644 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5645 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5646 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5647 | #ifdef MS_WIN32 |
| 5648 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5649 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5650 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5651 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5652 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5653 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5654 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5655 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5656 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5657 | #ifdef HAVE_SETEUID |
| 5658 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5659 | #endif /* HAVE_SETEUID */ |
| 5660 | #ifdef HAVE_SETEGID |
| 5661 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5662 | #endif /* HAVE_SETEGID */ |
| 5663 | #ifdef HAVE_SETREUID |
| 5664 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5665 | #endif /* HAVE_SETREUID */ |
| 5666 | #ifdef HAVE_SETREGID |
| 5667 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5668 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5669 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5670 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5671 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5672 | #ifdef HAVE_SETGROUPS |
| 5673 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 5674 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5675 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5676 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5677 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5678 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5679 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5680 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5681 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5682 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5683 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5684 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5685 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5686 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5687 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5688 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5689 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5690 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5691 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5692 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5693 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5694 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5695 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5696 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5697 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5698 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5699 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5700 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5701 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5702 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5703 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5704 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5705 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5706 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5707 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5708 | #endif |
| 5709 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5710 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5711 | #endif |
| 5712 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5713 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5714 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5715 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5716 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5717 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5718 | #ifdef HAVE_UNSETENV |
| 5719 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 5720 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5721 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5722 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5723 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5724 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5725 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5726 | #endif |
| 5727 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5728 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5729 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5730 | #ifdef HAVE_SYS_WAIT_H |
| 5731 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5732 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5733 | #endif /* WIFSTOPPED */ |
| 5734 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5735 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5736 | #endif /* WIFSIGNALED */ |
| 5737 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5738 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5739 | #endif /* WIFEXITED */ |
| 5740 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5741 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5742 | #endif /* WEXITSTATUS */ |
| 5743 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5744 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5745 | #endif /* WTERMSIG */ |
| 5746 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5747 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5748 | #endif /* WSTOPSIG */ |
| 5749 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5750 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5751 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5752 | #endif |
| 5753 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5754 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5755 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 5756 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5757 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5758 | #endif |
| 5759 | #ifdef HAVE_TEMPNAM |
| 5760 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5761 | #endif |
| 5762 | #ifdef HAVE_TMPNAM |
| 5763 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5764 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5765 | #ifdef HAVE_CONFSTR |
| 5766 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5767 | #endif |
| 5768 | #ifdef HAVE_SYSCONF |
| 5769 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5770 | #endif |
| 5771 | #ifdef HAVE_FPATHCONF |
| 5772 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5773 | #endif |
| 5774 | #ifdef HAVE_PATHCONF |
| 5775 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5776 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5777 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5778 | #ifdef MS_WIN32 |
| 5779 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 5780 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5781 | {NULL, NULL} /* Sentinel */ |
| 5782 | }; |
| 5783 | |
| 5784 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5785 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5786 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5787 | { |
| 5788 | PyObject* v = PyInt_FromLong(value); |
| 5789 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5790 | return -1; /* triggers fatal error */ |
| 5791 | |
| 5792 | Py_DECREF(v); |
| 5793 | return 0; |
| 5794 | } |
| 5795 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5796 | #if defined(PYOS_OS2) |
| 5797 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5798 | static int insertvalues(PyObject *d) |
| 5799 | { |
| 5800 | APIRET rc; |
| 5801 | ULONG values[QSV_MAX+1]; |
| 5802 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 5803 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5804 | |
| 5805 | Py_BEGIN_ALLOW_THREADS |
| 5806 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5807 | Py_END_ALLOW_THREADS |
| 5808 | |
| 5809 | if (rc != NO_ERROR) { |
| 5810 | os2_error(rc); |
| 5811 | return -1; |
| 5812 | } |
| 5813 | |
| 5814 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5815 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5816 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5817 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5818 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5819 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5820 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5821 | |
| 5822 | switch (values[QSV_VERSION_MINOR]) { |
| 5823 | case 0: ver = "2.00"; break; |
| 5824 | case 10: ver = "2.10"; break; |
| 5825 | case 11: ver = "2.11"; break; |
| 5826 | case 30: ver = "3.00"; break; |
| 5827 | case 40: ver = "4.00"; break; |
| 5828 | case 50: ver = "5.00"; break; |
| 5829 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 5830 | PyOS_snprintf(tmp, sizeof(tmp), |
| 5831 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 5832 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5833 | ver = &tmp[0]; |
| 5834 | } |
| 5835 | |
| 5836 | /* Add Indicator of the Version of the Operating System */ |
| 5837 | v = PyString_FromString(ver); |
| 5838 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5839 | return -1; |
| 5840 | Py_DECREF(v); |
| 5841 | |
| 5842 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5843 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5844 | tmp[1] = ':'; |
| 5845 | tmp[2] = '\0'; |
| 5846 | |
| 5847 | v = PyString_FromString(tmp); |
| 5848 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5849 | return -1; |
| 5850 | Py_DECREF(v); |
| 5851 | |
| 5852 | return 0; |
| 5853 | } |
| 5854 | #endif |
| 5855 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5856 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5857 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5858 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5859 | #ifdef F_OK |
| 5860 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5861 | #endif |
| 5862 | #ifdef R_OK |
| 5863 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5864 | #endif |
| 5865 | #ifdef W_OK |
| 5866 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5867 | #endif |
| 5868 | #ifdef X_OK |
| 5869 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5870 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5871 | #ifdef NGROUPS_MAX |
| 5872 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5873 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5874 | #ifdef TMP_MAX |
| 5875 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5876 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5877 | #ifdef WNOHANG |
| 5878 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5879 | #endif |
| 5880 | #ifdef O_RDONLY |
| 5881 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5882 | #endif |
| 5883 | #ifdef O_WRONLY |
| 5884 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5885 | #endif |
| 5886 | #ifdef O_RDWR |
| 5887 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5888 | #endif |
| 5889 | #ifdef O_NDELAY |
| 5890 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5891 | #endif |
| 5892 | #ifdef O_NONBLOCK |
| 5893 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5894 | #endif |
| 5895 | #ifdef O_APPEND |
| 5896 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5897 | #endif |
| 5898 | #ifdef O_DSYNC |
| 5899 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5900 | #endif |
| 5901 | #ifdef O_RSYNC |
| 5902 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5903 | #endif |
| 5904 | #ifdef O_SYNC |
| 5905 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5906 | #endif |
| 5907 | #ifdef O_NOCTTY |
| 5908 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5909 | #endif |
| 5910 | #ifdef O_CREAT |
| 5911 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5912 | #endif |
| 5913 | #ifdef O_EXCL |
| 5914 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5915 | #endif |
| 5916 | #ifdef O_TRUNC |
| 5917 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5918 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5919 | #ifdef O_BINARY |
| 5920 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5921 | #endif |
| 5922 | #ifdef O_TEXT |
| 5923 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5924 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 5925 | #ifdef O_LARGEFILE |
| 5926 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 5927 | #endif |
| 5928 | |
| 5929 | /* GNU extensions. */ |
| 5930 | #ifdef O_DIRECT |
| 5931 | /* Direct disk access. */ |
| 5932 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 5933 | #endif |
| 5934 | #ifdef O_DIRECTORY |
| 5935 | /* Must be a directory. */ |
| 5936 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 5937 | #endif |
| 5938 | #ifdef O_NOFOLLOW |
| 5939 | /* Do not follow links. */ |
| 5940 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 5941 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5942 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5943 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5944 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5945 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5946 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5947 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5948 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5949 | #endif |
| 5950 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5951 | #if defined(PYOS_OS2) |
| 5952 | if (insertvalues(d)) return -1; |
| 5953 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5954 | return 0; |
| 5955 | } |
| 5956 | |
| 5957 | |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5958 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5959 | #define INITFUNC initnt |
| 5960 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5961 | |
| 5962 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5963 | #define INITFUNC initos2 |
| 5964 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5965 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5966 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5967 | #define INITFUNC initposix |
| 5968 | #define MODNAME "posix" |
| 5969 | #endif |
| 5970 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5971 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5972 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5973 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5974 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5975 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5976 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5977 | posix_methods, |
| 5978 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5979 | (PyObject *)NULL, |
| 5980 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5981 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5982 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5983 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5984 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5985 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5986 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5987 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5988 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5989 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5990 | return; |
| 5991 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5992 | if (setup_confname_tables(d)) |
| 5993 | return; |
| 5994 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5995 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5996 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5997 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 5998 | if (posix_putenv_garbage == NULL) |
| 5999 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6000 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6001 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6002 | stat_result_desc.name = MODNAME ".stat_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6003 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 6004 | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
| 6005 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6006 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6007 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Guido van Rossum | bb2501f | 2001-12-27 16:23:28 +0000 | [diff] [blame] | 6008 | PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6009 | } |