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 | |
Skip Montanaro | 8216c18 | 2001-02-27 17:04:34 +0000 | [diff] [blame] | 49 | /* pick up declaration of confstr on some systems? */ |
| 50 | #ifdef HAVE_UNISTD_H |
| 51 | #include <unistd.h> |
| 52 | #endif /* HAVE_UNISTD_H */ |
| 53 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 54 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 55 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 56 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 57 | #include <process.h> |
| 58 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 59 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 60 | #define HAVE_GETCWD 1 |
| 61 | #define HAVE_OPENDIR 1 |
| 62 | #define HAVE_SYSTEM 1 |
| 63 | #if defined(__OS2__) |
| 64 | #define HAVE_EXECV 1 |
| 65 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 66 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 67 | #include <process.h> |
| 68 | #else |
| 69 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 70 | #define HAVE_EXECV 1 |
| 71 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 72 | #define HAVE_OPENDIR 1 |
| 73 | #define HAVE_PIPE 1 |
| 74 | #define HAVE_POPEN 1 |
| 75 | #define HAVE_SYSTEM 1 |
| 76 | #define HAVE_WAIT 1 |
| 77 | #else |
| 78 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 79 | #define HAVE_GETCWD 1 |
| 80 | #ifdef MS_WIN32 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 81 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 82 | #define HAVE_EXECV 1 |
| 83 | #define HAVE_PIPE 1 |
| 84 | #define HAVE_POPEN 1 |
| 85 | #define HAVE_SYSTEM 1 |
| 86 | #else /* 16-bit Windows */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 87 | #endif /* !MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 88 | #else /* all other compilers */ |
| 89 | /* Unix functions that the configure script doesn't check for */ |
| 90 | #define HAVE_EXECV 1 |
| 91 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 92 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 93 | #define HAVE_FORK1 1 |
| 94 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 95 | #define HAVE_GETCWD 1 |
| 96 | #define HAVE_GETEGID 1 |
| 97 | #define HAVE_GETEUID 1 |
| 98 | #define HAVE_GETGID 1 |
| 99 | #define HAVE_GETPPID 1 |
| 100 | #define HAVE_GETUID 1 |
| 101 | #define HAVE_KILL 1 |
| 102 | #define HAVE_OPENDIR 1 |
| 103 | #define HAVE_PIPE 1 |
| 104 | #define HAVE_POPEN 1 |
| 105 | #define HAVE_SYSTEM 1 |
| 106 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 107 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 108 | #endif /* _MSC_VER */ |
| 109 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 110 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 111 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 113 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 116 | #include <unistd.h> |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 119 | #if defined(sun) && !defined(__SVR4) |
| 120 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 121 | extern int rename(const char *, const char *); |
| 122 | extern int pclose(FILE *); |
| 123 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 124 | extern int fsync(int); |
| 125 | extern int lstat(const char *, struct stat *); |
| 126 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 129 | #ifdef NeXT |
| 130 | /* NeXT's <unistd.h> and <utime.h> aren't worth much */ |
| 131 | #undef HAVE_UNISTD_H |
| 132 | #undef HAVE_UTIME_H |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 133 | #define HAVE_WAITPID |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 134 | /* #undef HAVE_GETCWD */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 135 | #define UNION_WAIT /* This should really be checked for by autoconf */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 138 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 139 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 140 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 141 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 142 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 143 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 145 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 147 | #endif |
| 148 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int chdir(char *); |
| 150 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 151 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int chdir(const char *); |
| 153 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 154 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 155 | #ifdef __BORLANDC__ |
| 156 | extern int chmod(const char *, int); |
| 157 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 159 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int chown(const char *, uid_t, gid_t); |
| 161 | extern char *getcwd(char *, int); |
| 162 | extern char *strerror(int); |
| 163 | extern int link(const char *, const char *); |
| 164 | extern int rename(const char *, const char *); |
| 165 | extern int stat(const char *, struct stat *); |
| 166 | extern int unlink(const char *); |
| 167 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 170 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 171 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 173 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | #ifdef HAVE_UTIME_H |
| 179 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 180 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 182 | #ifdef HAVE_SYS_UTIME_H |
| 183 | #include <sys/utime.h> |
| 184 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 185 | #endif /* HAVE_SYS_UTIME_H */ |
| 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #ifdef HAVE_SYS_TIMES_H |
| 188 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | |
| 191 | #ifdef HAVE_SYS_PARAM_H |
| 192 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 193 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | |
| 195 | #ifdef HAVE_SYS_UTSNAME_H |
| 196 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
| 199 | #ifndef MAXPATHLEN |
| 200 | #define MAXPATHLEN 1024 |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 201 | #endif /* MAXPATHLEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 203 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 205 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 206 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 207 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 208 | #include <direct.h> |
| 209 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 210 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 212 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 213 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 214 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #endif |
| 217 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 218 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 219 | #endif |
| 220 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 222 | #endif |
| 223 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 225 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | #include <direct.h> |
| 227 | #include <io.h> |
| 228 | #include <process.h> |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 229 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | #include <windows.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 231 | #ifdef MS_WIN32 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 233 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 234 | #else /* 16-bit Windows */ |
| 235 | #include <dos.h> |
| 236 | #include <ctype.h> |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 237 | #endif /* MS_WIN32 */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 240 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 242 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 244 | #ifdef UNION_WAIT |
| 245 | /* Emulate some macros on systems that have a union instead of macros */ |
| 246 | |
| 247 | #ifndef WIFEXITED |
| 248 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 249 | #endif |
| 250 | |
| 251 | #ifndef WEXITSTATUS |
| 252 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 253 | #endif |
| 254 | |
| 255 | #ifndef WTERMSIG |
| 256 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 257 | #endif |
| 258 | |
| 259 | #endif /* UNION_WAIT */ |
| 260 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 261 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 262 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 263 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 264 | #define USE_CTERMID_R |
| 265 | #endif |
| 266 | |
| 267 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 268 | #define USE_TMPNAM_R |
| 269 | #endif |
| 270 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 271 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 272 | #undef STAT |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 273 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 274 | # define STAT _stati64 |
| 275 | # define FSTAT _fstati64 |
| 276 | # define STRUCT_STAT struct _stati64 |
| 277 | #else |
| 278 | # define STAT stat |
| 279 | # define FSTAT fstat |
| 280 | # define STRUCT_STAT struct stat |
| 281 | #endif |
| 282 | |
| 283 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 284 | /* Return a dictionary corresponding to the POSIX environment table */ |
| 285 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 286 | #if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 288 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 290 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 291 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 293 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 295 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | if (d == NULL) |
| 297 | return NULL; |
| 298 | if (environ == NULL) |
| 299 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 300 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 302 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 303 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | char *p = strchr(*e, '='); |
| 305 | if (p == NULL) |
| 306 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 307 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 308 | if (k == NULL) { |
| 309 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 311 | } |
| 312 | v = PyString_FromString(p+1); |
| 313 | if (v == NULL) { |
| 314 | PyErr_Clear(); |
| 315 | Py_DECREF(k); |
| 316 | continue; |
| 317 | } |
| 318 | if (PyDict_GetItem(d, k) == NULL) { |
| 319 | if (PyDict_SetItem(d, k, v) != 0) |
| 320 | PyErr_Clear(); |
| 321 | } |
| 322 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 323 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 325 | #if defined(PYOS_OS2) |
| 326 | { |
| 327 | APIRET rc; |
| 328 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 329 | |
| 330 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 331 | 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] | 332 | PyObject *v = PyString_FromString(buffer); |
| 333 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 334 | Py_DECREF(v); |
| 335 | } |
| 336 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 337 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 338 | PyObject *v = PyString_FromString(buffer); |
| 339 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 340 | Py_DECREF(v); |
| 341 | } |
| 342 | } |
| 343 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | return d; |
| 345 | } |
| 346 | |
| 347 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 349 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 350 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 351 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 352 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 353 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 355 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 356 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 357 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 358 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 361 | static PyObject * |
| 362 | posix_error_with_allocated_filename(char* name) |
| 363 | { |
| 364 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 365 | PyMem_Free(name); |
| 366 | return rc; |
| 367 | } |
| 368 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 369 | #ifdef MS_WIN32 |
| 370 | static PyObject * |
| 371 | win32_error(char* function, char* filename) |
| 372 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 373 | /* XXX We should pass the function name along in the future. |
| 374 | (_winreg.c also wants to pass the function name.) |
| 375 | This would however require an additional param to the |
| 376 | Windows error object, which is non-trivial. |
| 377 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 378 | errno = GetLastError(); |
| 379 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 380 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 381 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 382 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 383 | } |
| 384 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 386 | #if defined(PYOS_OS2) |
| 387 | /********************************************************************** |
| 388 | * Helper Function to Trim and Format OS/2 Messages |
| 389 | **********************************************************************/ |
| 390 | static void |
| 391 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 392 | { |
| 393 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 394 | |
| 395 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 396 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 397 | |
| 398 | while (lastc > msgbuf && isspace(*lastc)) |
| 399 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 400 | } |
| 401 | |
| 402 | /* Add Optional Reason Text */ |
| 403 | if (reason) { |
| 404 | strcat(msgbuf, " : "); |
| 405 | strcat(msgbuf, reason); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /********************************************************************** |
| 410 | * Decode an OS/2 Operating System Error Code |
| 411 | * |
| 412 | * A convenience function to lookup an OS/2 error code and return a |
| 413 | * text message we can use to raise a Python exception. |
| 414 | * |
| 415 | * Notes: |
| 416 | * The messages for errors returned from the OS/2 kernel reside in |
| 417 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 418 | * |
| 419 | **********************************************************************/ |
| 420 | static char * |
| 421 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 422 | { |
| 423 | APIRET rc; |
| 424 | ULONG msglen; |
| 425 | |
| 426 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 427 | Py_BEGIN_ALLOW_THREADS |
| 428 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 429 | errorcode, "oso001.msg", &msglen); |
| 430 | Py_END_ALLOW_THREADS |
| 431 | |
| 432 | if (rc == NO_ERROR) |
| 433 | os2_formatmsg(msgbuf, msglen, reason); |
| 434 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 435 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 436 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 437 | |
| 438 | return msgbuf; |
| 439 | } |
| 440 | |
| 441 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 442 | errors are not in a global variable e.g. 'errno' nor are |
| 443 | they congruent with posix error numbers. */ |
| 444 | |
| 445 | static PyObject * os2_error(int code) |
| 446 | { |
| 447 | char text[1024]; |
| 448 | PyObject *v; |
| 449 | |
| 450 | os2_strerror(text, sizeof(text), code, ""); |
| 451 | |
| 452 | v = Py_BuildValue("(is)", code, text); |
| 453 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 454 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 455 | Py_DECREF(v); |
| 456 | } |
| 457 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 458 | } |
| 459 | |
| 460 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | |
| 462 | /* POSIX generic methods */ |
| 463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 465 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 466 | { |
| 467 | int fd; |
| 468 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 469 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 470 | return NULL; |
| 471 | Py_BEGIN_ALLOW_THREADS |
| 472 | res = (*func)(fd); |
| 473 | Py_END_ALLOW_THREADS |
| 474 | if (res < 0) |
| 475 | return posix_error(); |
| 476 | Py_INCREF(Py_None); |
| 477 | return Py_None; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 482 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 483 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 484 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 485 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 486 | if (!PyArg_ParseTuple(args, format, |
| 487 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 488 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 489 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 490 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 491 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 492 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 493 | return posix_error_with_allocated_filename(path1); |
| 494 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 495 | Py_INCREF(Py_None); |
| 496 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 499 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 500 | posix_2str(PyObject *args, char *format, |
| 501 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 503 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 504 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 505 | if (!PyArg_ParseTuple(args, format, |
| 506 | Py_FileSystemDefaultEncoding, &path1, |
| 507 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 508 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 509 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 510 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 511 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 512 | PyMem_Free(path1); |
| 513 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 514 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 515 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 516 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 517 | Py_INCREF(Py_None); |
| 518 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 521 | static char stat_result__doc__[] = |
| 522 | "stat_result: Result from stat or lstat.\n\n\ |
| 523 | This object may be accessed either as a tuple of\n\ |
| 524 | (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 525 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 526 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 527 | 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] | 528 | they are available as attributes only.\n\ |
| 529 | \n\ |
| 530 | See os.stat for more information.\n"; |
| 531 | |
| 532 | static PyStructSequence_Field stat_result_fields[] = { |
| 533 | {"st_mode", "protection bits"}, |
| 534 | {"st_ino", "inode"}, |
| 535 | {"st_dev", "device"}, |
| 536 | {"st_nlink", "number of hard links"}, |
| 537 | {"st_uid", "user ID of owner"}, |
| 538 | {"st_gid", "group ID of owner"}, |
| 539 | {"st_size", "total size, in bytes"}, |
| 540 | {"st_atime", "time of last access"}, |
| 541 | {"st_mtime", "time of last modification"}, |
| 542 | {"st_ctime", "time of last change"}, |
| 543 | #ifdef HAVE_ST_BLKSIZE |
| 544 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 545 | #endif |
| 546 | #ifdef HAVE_ST_BLOCKS |
| 547 | {"st_blocks", "number of blocks allocated"}, |
| 548 | #endif |
| 549 | #ifdef HAVE_ST_RDEV |
| 550 | {"st_rdev", "device type (if inode device)"}, |
| 551 | #endif |
| 552 | {0} |
| 553 | }; |
| 554 | |
| 555 | #ifdef HAVE_ST_BLKSIZE |
| 556 | #define ST_BLKSIZE_IDX 10 |
| 557 | #else |
| 558 | #define ST_BLKSIZE_IDX 9 |
| 559 | #endif |
| 560 | |
| 561 | #ifdef HAVE_ST_BLOCKS |
| 562 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 563 | #else |
| 564 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 565 | #endif |
| 566 | |
| 567 | #ifdef HAVE_ST_RDEV |
| 568 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 569 | #else |
| 570 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 571 | #endif |
| 572 | |
| 573 | static PyStructSequence_Desc stat_result_desc = { |
| 574 | "stat_result", /* name */ |
| 575 | stat_result__doc__, /* doc */ |
| 576 | stat_result_fields, |
| 577 | 10 |
| 578 | }; |
| 579 | |
| 580 | static char statvfs_result__doc__[] = |
| 581 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 582 | This object may be accessed either as a tuple of\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 583 | (bsize,frsize,blocks,bfree,bavail,files,ffree,favail,flag,namemax),\n\ |
| 584 | 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] | 585 | \n\ |
| 586 | See os.statvfs for more information.\n"; |
| 587 | |
| 588 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 589 | {"f_bsize", }, |
| 590 | {"f_frsize", }, |
| 591 | {"f_blocks", }, |
| 592 | {"f_bfree", }, |
| 593 | {"f_bavail", }, |
| 594 | {"f_files", }, |
| 595 | {"f_ffree", }, |
| 596 | {"f_favail", }, |
| 597 | {"f_flag", }, |
| 598 | {"f_namemax",}, |
| 599 | {0} |
| 600 | }; |
| 601 | |
| 602 | static PyStructSequence_Desc statvfs_result_desc = { |
| 603 | "statvfs_result", /* name */ |
| 604 | statvfs_result__doc__, /* doc */ |
| 605 | statvfs_result_fields, |
| 606 | 10 |
| 607 | }; |
| 608 | |
| 609 | static PyTypeObject StatResultType; |
| 610 | static PyTypeObject StatVFSResultType; |
| 611 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 612 | /* pack a system stat C structure into the Python stat tuple |
| 613 | (used by posix_stat() and posix_fstat()) */ |
| 614 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 615 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 616 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 617 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 618 | if (v == NULL) |
| 619 | return NULL; |
| 620 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 621 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 622 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 623 | PyStructSequence_SET_ITEM(v, 1, |
| 624 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 625 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 626 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 627 | #endif |
| 628 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 629 | PyStructSequence_SET_ITEM(v, 2, |
| 630 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
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, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 633 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 634 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 635 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 636 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 637 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 638 | PyStructSequence_SET_ITEM(v, 6, |
| 639 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 640 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 641 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 642 | #endif |
| 643 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 644 | PyStructSequence_SET_ITEM(v, 7, |
| 645 | PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 646 | PyStructSequence_SET_ITEM(v, 8, |
| 647 | PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 648 | PyStructSequence_SET_ITEM(v, 9, |
| 649 | PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 650 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 651 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 652 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 653 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 654 | #endif |
| 655 | |
| 656 | #ifdef HAVE_ST_BLKSIZE |
| 657 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 658 | PyInt_FromLong((long)st.st_blksize)); |
| 659 | #endif |
| 660 | #ifdef HAVE_ST_BLOCKS |
| 661 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 662 | PyInt_FromLong((long)st.st_blocks)); |
| 663 | #endif |
| 664 | #ifdef HAVE_ST_RDEV |
| 665 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 666 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 667 | #endif |
| 668 | |
| 669 | if (PyErr_Occurred()) { |
| 670 | Py_DECREF(v); |
| 671 | return NULL; |
| 672 | } |
| 673 | |
| 674 | return v; |
| 675 | } |
| 676 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 677 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 678 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 679 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 681 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 682 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 683 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 684 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 685 | |
| 686 | #ifdef MS_WIN32 |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 687 | int pathlen; |
| 688 | char pathcopy[MAX_PATH]; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 689 | #endif /* MS_WIN32 */ |
| 690 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 691 | if (!PyArg_ParseTuple(args, format, |
| 692 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 693 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 694 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 695 | |
| 696 | #ifdef MS_WIN32 |
| 697 | pathlen = strlen(path); |
| 698 | /* the library call can blow up if the file name is too long! */ |
| 699 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 700 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 701 | errno = ENAMETOOLONG; |
| 702 | return posix_error(); |
| 703 | } |
| 704 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 705 | /* Remove trailing slash or backslash, unless it's the current |
| 706 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 707 | */ |
| 708 | if (pathlen > 0 && |
| 709 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 710 | /* It does end with a slash -- exempt the root drive cases. */ |
| 711 | /* XXX UNC root drives should also be exempted? */ |
| 712 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 713 | /* leave it alone */; |
| 714 | else { |
| 715 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 716 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 717 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 718 | path = pathcopy; |
| 719 | } |
| 720 | } |
| 721 | #endif /* MS_WIN32 */ |
| 722 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 723 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 724 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 725 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 726 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 727 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 728 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 729 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 730 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | /* POSIX methods */ |
| 735 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 736 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 737 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 738 | Test for access to a file."; |
| 739 | |
| 740 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 741 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 742 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 743 | char *path; |
| 744 | int mode; |
| 745 | int res; |
| 746 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 747 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 748 | return NULL; |
| 749 | Py_BEGIN_ALLOW_THREADS |
| 750 | res = access(path, mode); |
| 751 | Py_END_ALLOW_THREADS |
| 752 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 755 | #ifndef F_OK |
| 756 | #define F_OK 0 |
| 757 | #endif |
| 758 | #ifndef R_OK |
| 759 | #define R_OK 4 |
| 760 | #endif |
| 761 | #ifndef W_OK |
| 762 | #define W_OK 2 |
| 763 | #endif |
| 764 | #ifndef X_OK |
| 765 | #define X_OK 1 |
| 766 | #endif |
| 767 | |
| 768 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 769 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 770 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 771 | Return the name of the terminal device connected to 'fd'."; |
| 772 | |
| 773 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 774 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 775 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 776 | int id; |
| 777 | char *ret; |
| 778 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 779 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 780 | return NULL; |
| 781 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 782 | ret = ttyname(id); |
| 783 | if (ret == NULL) |
| 784 | return(posix_error()); |
| 785 | return(PyString_FromString(ret)); |
| 786 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 787 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 788 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 789 | #ifdef HAVE_CTERMID |
| 790 | static char posix_ctermid__doc__[] = |
| 791 | "ctermid() -> String\n\ |
| 792 | Return the name of the controlling terminal for this process."; |
| 793 | |
| 794 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 795 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 796 | { |
| 797 | char *ret; |
| 798 | char buffer[L_ctermid]; |
| 799 | |
| 800 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 801 | return NULL; |
| 802 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 803 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 804 | ret = ctermid_r(buffer); |
| 805 | #else |
| 806 | ret = ctermid(buffer); |
| 807 | #endif |
| 808 | if (ret == NULL) |
| 809 | return(posix_error()); |
| 810 | return(PyString_FromString(buffer)); |
| 811 | } |
| 812 | #endif |
| 813 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 814 | static char posix_chdir__doc__[] = |
| 815 | "chdir(path) -> None\n\ |
| 816 | Change the current working directory to the specified path."; |
| 817 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 818 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 819 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 820 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 821 | return posix_1str(args, "et:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 824 | |
| 825 | static char posix_chmod__doc__[] = |
| 826 | "chmod(path, mode) -> None\n\ |
| 827 | Change the access permissions of a file."; |
| 828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 829 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 830 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 831 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 832 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 833 | int i; |
| 834 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 835 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
| 836 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 837 | return NULL; |
| 838 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 839 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 840 | Py_END_ALLOW_THREADS |
| 841 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 842 | return posix_error_with_allocated_filename(path); |
| 843 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 844 | Py_INCREF(Py_None); |
| 845 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 848 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 849 | #ifdef HAVE_CHROOT |
| 850 | static char posix_chroot__doc__[] = |
| 851 | "chroot(path) -> None\n\ |
| 852 | Change root directory to path."; |
| 853 | |
| 854 | static PyObject * |
| 855 | posix_chroot(PyObject *self, PyObject *args) |
| 856 | { |
| 857 | return posix_1str(args, "et:chroot", chroot); |
| 858 | } |
| 859 | #endif |
| 860 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 861 | #ifdef HAVE_FSYNC |
| 862 | static char posix_fsync__doc__[] = |
| 863 | "fsync(fildes) -> None\n\ |
| 864 | force write of file with filedescriptor to disk."; |
| 865 | |
| 866 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 867 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 868 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 869 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 870 | } |
| 871 | #endif /* HAVE_FSYNC */ |
| 872 | |
| 873 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 875 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 876 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 877 | #endif |
| 878 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 879 | static char posix_fdatasync__doc__[] = |
| 880 | "fdatasync(fildes) -> None\n\ |
| 881 | force write of file with filedescriptor to disk.\n\ |
| 882 | does not force update of metadata."; |
| 883 | |
| 884 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 885 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 886 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 887 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 888 | } |
| 889 | #endif /* HAVE_FDATASYNC */ |
| 890 | |
| 891 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 892 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 893 | static char posix_chown__doc__[] = |
| 894 | "chown(path, uid, gid) -> None\n\ |
| 895 | Change the owner and group id of path to the numeric uid and gid."; |
| 896 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 897 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 898 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 899 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 900 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 901 | int uid, gid; |
| 902 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 903 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 904 | Py_FileSystemDefaultEncoding, &path, |
| 905 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 906 | return NULL; |
| 907 | Py_BEGIN_ALLOW_THREADS |
| 908 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 909 | Py_END_ALLOW_THREADS |
| 910 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 911 | return posix_error_with_allocated_filename(path); |
| 912 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 913 | Py_INCREF(Py_None); |
| 914 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 915 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 916 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 917 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 919 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 920 | static char posix_getcwd__doc__[] = |
| 921 | "getcwd() -> path\n\ |
| 922 | Return a string representing the current working directory."; |
| 923 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 925 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 926 | { |
| 927 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 928 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 929 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 931 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 932 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 933 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 934 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 935 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 936 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 937 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 938 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 941 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 942 | static char posix_link__doc__[] = |
| 943 | "link(src, dst) -> None\n\ |
| 944 | Create a hard link to a file."; |
| 945 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 946 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 947 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 948 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 949 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 950 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 951 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 952 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 953 | |
| 954 | static char posix_listdir__doc__[] = |
| 955 | "listdir(path) -> list_of_strings\n\ |
| 956 | Return a list containing the names of the entries in the directory.\n\ |
| 957 | \n\ |
| 958 | path: path of directory to list\n\ |
| 959 | \n\ |
| 960 | The list is in arbitrary order. It does not include the special\n\ |
| 961 | entries '.' and '..' even if they are present in the directory."; |
| 962 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 963 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 964 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 965 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 966 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 967 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 968 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 969 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 970 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 971 | HANDLE hFindFile; |
| 972 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 973 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 974 | char namebuf[MAX_PATH*2+5]; |
| 975 | char *bufptr = namebuf; |
| 976 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 977 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 978 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 979 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 980 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 981 | return NULL; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 982 | ch = namebuf[len-1]; |
| 983 | if (ch != '/' && ch != '\\' && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 984 | namebuf[len++] = '/'; |
| 985 | strcpy(namebuf + len, "*.*"); |
| 986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 987 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 988 | return NULL; |
| 989 | |
| 990 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 991 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 992 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 993 | if (errno == ERROR_FILE_NOT_FOUND) |
| 994 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 995 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 996 | } |
| 997 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 998 | if (FileData.cFileName[0] == '.' && |
| 999 | (FileData.cFileName[1] == '\0' || |
| 1000 | FileData.cFileName[1] == '.' && |
| 1001 | FileData.cFileName[2] == '\0')) |
| 1002 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1003 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1004 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1005 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1006 | d = NULL; |
| 1007 | break; |
| 1008 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1009 | if (PyList_Append(d, v) != 0) { |
| 1010 | Py_DECREF(v); |
| 1011 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1012 | d = NULL; |
| 1013 | break; |
| 1014 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1015 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1016 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1017 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1018 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1019 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1020 | |
| 1021 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1022 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1023 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1024 | |
| 1025 | #ifndef MAX_PATH |
| 1026 | #define MAX_PATH 250 |
| 1027 | #endif |
| 1028 | char *name, *pt; |
| 1029 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1030 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1031 | char namebuf[MAX_PATH+5]; |
| 1032 | struct _find_t ep; |
| 1033 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1034 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1035 | return NULL; |
| 1036 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
| 1040 | strcpy(namebuf, name); |
| 1041 | for (pt = namebuf; *pt; pt++) |
| 1042 | if (*pt == '/') |
| 1043 | *pt = '\\'; |
| 1044 | if (namebuf[len-1] != '\\') |
| 1045 | namebuf[len++] = '\\'; |
| 1046 | strcpy(namebuf + len, "*.*"); |
| 1047 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1048 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1049 | return NULL; |
| 1050 | |
| 1051 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1052 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 1053 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1054 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1055 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1056 | } |
| 1057 | do { |
| 1058 | if (ep.name[0] == '.' && |
| 1059 | (ep.name[1] == '\0' || |
| 1060 | ep.name[1] == '.' && |
| 1061 | ep.name[2] == '\0')) |
| 1062 | continue; |
| 1063 | strcpy(namebuf, ep.name); |
| 1064 | for (pt = namebuf; *pt; pt++) |
| 1065 | if (isupper(*pt)) |
| 1066 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1067 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1068 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1069 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1070 | d = NULL; |
| 1071 | break; |
| 1072 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1073 | if (PyList_Append(d, v) != 0) { |
| 1074 | Py_DECREF(v); |
| 1075 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1076 | d = NULL; |
| 1077 | break; |
| 1078 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1079 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1080 | } while (_dos_findnext(&ep) == 0); |
| 1081 | |
| 1082 | return d; |
| 1083 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1084 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1085 | |
| 1086 | #ifndef MAX_PATH |
| 1087 | #define MAX_PATH CCHMAXPATH |
| 1088 | #endif |
| 1089 | char *name, *pt; |
| 1090 | int len; |
| 1091 | PyObject *d, *v; |
| 1092 | char namebuf[MAX_PATH+5]; |
| 1093 | HDIR hdir = 1; |
| 1094 | ULONG srchcnt = 1; |
| 1095 | FILEFINDBUF3 ep; |
| 1096 | APIRET rc; |
| 1097 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1098 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1099 | return NULL; |
| 1100 | if (len >= MAX_PATH) { |
| 1101 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | strcpy(namebuf, name); |
| 1105 | for (pt = namebuf; *pt; pt++) |
| 1106 | if (*pt == '/') |
| 1107 | *pt = '\\'; |
| 1108 | if (namebuf[len-1] != '\\') |
| 1109 | namebuf[len++] = '\\'; |
| 1110 | strcpy(namebuf + len, "*.*"); |
| 1111 | |
| 1112 | if ((d = PyList_New(0)) == NULL) |
| 1113 | return NULL; |
| 1114 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1115 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1116 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1117 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1118 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1119 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1120 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1121 | |
| 1122 | if (rc != NO_ERROR) { |
| 1123 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1124 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1127 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1128 | do { |
| 1129 | if (ep.achName[0] == '.' |
| 1130 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1131 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1132 | |
| 1133 | strcpy(namebuf, ep.achName); |
| 1134 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1135 | /* Leave Case of Name Alone -- In Native Form */ |
| 1136 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1137 | |
| 1138 | v = PyString_FromString(namebuf); |
| 1139 | if (v == NULL) { |
| 1140 | Py_DECREF(d); |
| 1141 | d = NULL; |
| 1142 | break; |
| 1143 | } |
| 1144 | if (PyList_Append(d, v) != 0) { |
| 1145 | Py_DECREF(v); |
| 1146 | Py_DECREF(d); |
| 1147 | d = NULL; |
| 1148 | break; |
| 1149 | } |
| 1150 | Py_DECREF(v); |
| 1151 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1152 | } |
| 1153 | |
| 1154 | return d; |
| 1155 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1156 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1157 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1158 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1159 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1160 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1161 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1162 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1163 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1164 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1165 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1166 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1167 | closedir(dirp); |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1171 | if (ep->d_name[0] == '.' && |
| 1172 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1173 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1174 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1175 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1176 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1177 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1178 | d = NULL; |
| 1179 | break; |
| 1180 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1181 | if (PyList_Append(d, v) != 0) { |
| 1182 | Py_DECREF(v); |
| 1183 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | d = NULL; |
| 1185 | break; |
| 1186 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1187 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1188 | } |
| 1189 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1190 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1191 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1192 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1193 | #endif /* which OS */ |
| 1194 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1195 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1196 | #ifdef MS_WIN32 |
| 1197 | /* A helper function for abspath on win32 */ |
| 1198 | static PyObject * |
| 1199 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1200 | { |
| 1201 | /* assume encoded strings wont more than double no of chars */ |
| 1202 | char inbuf[MAX_PATH*2]; |
| 1203 | char *inbufp = inbuf; |
| 1204 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1205 | char outbuf[MAX_PATH*2]; |
| 1206 | char *temp; |
| 1207 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1208 | Py_FileSystemDefaultEncoding, &inbufp, |
| 1209 | &insize)) |
| 1210 | return NULL; |
| 1211 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1212 | outbuf, &temp)) |
| 1213 | return win32_error("GetFullPathName", inbuf); |
| 1214 | return PyString_FromString(outbuf); |
| 1215 | } /* end of posix__getfullpathname */ |
| 1216 | #endif /* MS_WIN32 */ |
| 1217 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1218 | static char posix_mkdir__doc__[] = |
| 1219 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1220 | Create a directory."; |
| 1221 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1222 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1223 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1224 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1225 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1226 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1227 | int mode = 0777; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1228 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 1229 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1230 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1231 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1232 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1233 | res = mkdir(path); |
| 1234 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1235 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1236 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1237 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1238 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1239 | return posix_error_with_allocated_filename(path); |
| 1240 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1241 | Py_INCREF(Py_None); |
| 1242 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1245 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1246 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1247 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1248 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1249 | #include <sys/resource.h> |
| 1250 | #endif |
| 1251 | #endif |
| 1252 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1253 | static char posix_nice__doc__[] = |
| 1254 | "nice(inc) -> new_priority\n\ |
| 1255 | Decrease the priority of process and return new priority."; |
| 1256 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1257 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1258 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1259 | { |
| 1260 | int increment, value; |
| 1261 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1262 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1263 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* There are two flavours of 'nice': one that returns the new |
| 1266 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1267 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1268 | the use of getpriority() to get the new priority. |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1269 | |
| 1270 | If we are of the nice family that returns the new priority, we |
| 1271 | need to clear errno before the call, and check if errno is filled |
| 1272 | before calling posix_error() on a returnvalue of -1, because the |
| 1273 | -1 may be the actual new priority! */ |
| 1274 | |
| 1275 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1276 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1277 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1278 | if (value == 0) |
| 1279 | value = getpriority(PRIO_PROCESS, 0); |
| 1280 | #endif |
| 1281 | if (value == -1 && errno != 0) |
| 1282 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1283 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1284 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1285 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1286 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1287 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1288 | |
| 1289 | static char posix_rename__doc__[] = |
| 1290 | "rename(old, new) -> None\n\ |
| 1291 | Rename a file or directory."; |
| 1292 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1293 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1294 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1295 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1296 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1299 | |
| 1300 | static char posix_rmdir__doc__[] = |
| 1301 | "rmdir(path) -> None\n\ |
| 1302 | Remove a directory."; |
| 1303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1304 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1305 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1306 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1307 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1310 | |
| 1311 | static char posix_stat__doc__[] = |
| 1312 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1313 | Perform a stat system call on the given path."; |
| 1314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1315 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1316 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1317 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1318 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1321 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1322 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1323 | static char posix_system__doc__[] = |
| 1324 | "system(command) -> exit_status\n\ |
| 1325 | Execute the command (a string) in a subshell."; |
| 1326 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1327 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1328 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1329 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1330 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1331 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1332 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1333 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1334 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1335 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1336 | Py_END_ALLOW_THREADS |
| 1337 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1338 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1339 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1341 | |
| 1342 | static char posix_umask__doc__[] = |
| 1343 | "umask(new_mask) -> old_mask\n\ |
| 1344 | Set the current numeric umask and return the previous umask."; |
| 1345 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1347 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1348 | { |
| 1349 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1350 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1351 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1352 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1353 | if (i < 0) |
| 1354 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1355 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1358 | |
| 1359 | static char posix_unlink__doc__[] = |
| 1360 | "unlink(path) -> None\n\ |
| 1361 | Remove a file (same as remove(path))."; |
| 1362 | |
| 1363 | static char posix_remove__doc__[] = |
| 1364 | "remove(path) -> None\n\ |
| 1365 | Remove a file (same as unlink(path))."; |
| 1366 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1367 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1368 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1369 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1370 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1373 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1374 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1375 | static char posix_uname__doc__[] = |
| 1376 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1377 | Return a tuple identifying the current operating system."; |
| 1378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1379 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1380 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1381 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1382 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1383 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1384 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1385 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1386 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1387 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1388 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1389 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1390 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1391 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1392 | u.sysname, |
| 1393 | u.nodename, |
| 1394 | u.release, |
| 1395 | u.version, |
| 1396 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1397 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1398 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1399 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1400 | |
| 1401 | static char posix_utime__doc__[] = |
| 1402 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1403 | utime(path, None) -> None\n\ |
| 1404 | Set the access and modified time of the file to the given values. If the\n\ |
| 1405 | 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] | 1406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1407 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1408 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1409 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1410 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1411 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1412 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1413 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1414 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1415 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1416 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1417 | struct utimbuf buf; |
| 1418 | #define ATIME buf.actime |
| 1419 | #define MTIME buf.modtime |
| 1420 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1421 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1422 | time_t buf[2]; |
| 1423 | #define ATIME buf[0] |
| 1424 | #define MTIME buf[1] |
| 1425 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1426 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1427 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1428 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1429 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1430 | if (arg == Py_None) { |
| 1431 | /* optional time values not given */ |
| 1432 | Py_BEGIN_ALLOW_THREADS |
| 1433 | res = utime(path, NULL); |
| 1434 | Py_END_ALLOW_THREADS |
| 1435 | } |
| 1436 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1437 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1438 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1439 | return NULL; |
| 1440 | } |
| 1441 | else { |
| 1442 | ATIME = atime; |
| 1443 | MTIME = mtime; |
| 1444 | Py_BEGIN_ALLOW_THREADS |
| 1445 | res = utime(path, UTIME_ARG); |
| 1446 | Py_END_ALLOW_THREADS |
| 1447 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1448 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1449 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1450 | Py_INCREF(Py_None); |
| 1451 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1452 | #undef UTIME_ARG |
| 1453 | #undef ATIME |
| 1454 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1457 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1458 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1459 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1460 | static char posix__exit__doc__[] = |
| 1461 | "_exit(status)\n\ |
| 1462 | Exit to the system with specified status, without normal exit processing."; |
| 1463 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1464 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1465 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1466 | { |
| 1467 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1468 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1469 | return NULL; |
| 1470 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1471 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1474 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1475 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1476 | static char posix_execv__doc__[] = |
| 1477 | "execv(path, args)\n\ |
| 1478 | Execute an executable path with arguments, replacing current process.\n\ |
| 1479 | \n\ |
| 1480 | path: path of executable file\n\ |
| 1481 | args: tuple or list of strings"; |
| 1482 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1484 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1485 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1486 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1487 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1488 | char **argvlist; |
| 1489 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1490 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1491 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1492 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1493 | argv is a list or tuple of strings. */ |
| 1494 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1495 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1496 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1497 | if (PyList_Check(argv)) { |
| 1498 | argc = PyList_Size(argv); |
| 1499 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1500 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1501 | else if (PyTuple_Check(argv)) { |
| 1502 | argc = PyTuple_Size(argv); |
| 1503 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1504 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1505 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1506 | 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] | 1507 | return NULL; |
| 1508 | } |
| 1509 | |
| 1510 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1511 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1512 | return NULL; |
| 1513 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1514 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1515 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1516 | if (argvlist == NULL) |
| 1517 | return NULL; |
| 1518 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1519 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1520 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1521 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1522 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1523 | return NULL; |
| 1524 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1525 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1526 | } |
| 1527 | argvlist[argc] = NULL; |
| 1528 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1529 | #ifdef BAD_EXEC_PROTOTYPES |
| 1530 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1531 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1532 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1533 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1534 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1535 | /* If we get here it's definitely an error */ |
| 1536 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1537 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1538 | return posix_error(); |
| 1539 | } |
| 1540 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1541 | |
| 1542 | static char posix_execve__doc__[] = |
| 1543 | "execve(path, args, env)\n\ |
| 1544 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1545 | \n\ |
| 1546 | path: path of executable file\n\ |
| 1547 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1548 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1549 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1550 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1551 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1552 | { |
| 1553 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1554 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1555 | char **argvlist; |
| 1556 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1557 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1558 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1559 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1560 | |
| 1561 | /* execve has three arguments: (path, argv, env), where |
| 1562 | argv is a list or tuple of strings and env is a dictionary |
| 1563 | like posix.environ. */ |
| 1564 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1565 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1566 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1567 | if (PyList_Check(argv)) { |
| 1568 | argc = PyList_Size(argv); |
| 1569 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1570 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1571 | else if (PyTuple_Check(argv)) { |
| 1572 | argc = PyTuple_Size(argv); |
| 1573 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1574 | } |
| 1575 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1576 | 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] | 1577 | return NULL; |
| 1578 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1579 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1580 | 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] | 1581 | return NULL; |
| 1582 | } |
| 1583 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1584 | if (argc == 0) { |
| 1585 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1586 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1587 | return NULL; |
| 1588 | } |
| 1589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1590 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1591 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1592 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1593 | return NULL; |
| 1594 | } |
| 1595 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1596 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1597 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1598 | &argvlist[i])) |
| 1599 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1600 | goto fail_1; |
| 1601 | } |
| 1602 | } |
| 1603 | argvlist[argc] = NULL; |
| 1604 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1605 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1606 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1607 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1608 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1609 | goto fail_1; |
| 1610 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1611 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1612 | keys = PyMapping_Keys(env); |
| 1613 | vals = PyMapping_Values(env); |
| 1614 | if (!keys || !vals) |
| 1615 | goto fail_2; |
| 1616 | |
| 1617 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1618 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1619 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1620 | |
| 1621 | key = PyList_GetItem(keys, pos); |
| 1622 | val = PyList_GetItem(vals, pos); |
| 1623 | if (!key || !val) |
| 1624 | goto fail_2; |
| 1625 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1626 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1627 | !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] | 1628 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1629 | goto fail_2; |
| 1630 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1631 | |
| 1632 | #if defined(PYOS_OS2) |
| 1633 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1634 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1635 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1636 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1637 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1638 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1639 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1640 | goto fail_2; |
| 1641 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1642 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1643 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1644 | #if defined(PYOS_OS2) |
| 1645 | } |
| 1646 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1647 | } |
| 1648 | envlist[envc] = 0; |
| 1649 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1650 | |
| 1651 | #ifdef BAD_EXEC_PROTOTYPES |
| 1652 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1653 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1654 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1655 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1656 | |
| 1657 | /* If we get here it's definitely an error */ |
| 1658 | |
| 1659 | (void) posix_error(); |
| 1660 | |
| 1661 | fail_2: |
| 1662 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1663 | PyMem_DEL(envlist[envc]); |
| 1664 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1665 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1666 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1667 | Py_XDECREF(vals); |
| 1668 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1669 | return NULL; |
| 1670 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1671 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1673 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1674 | #ifdef HAVE_SPAWNV |
| 1675 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1676 | "spawnv(mode, path, args)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1677 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1678 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1679 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1680 | path: path of executable file\n\ |
| 1681 | args: tuple or list of strings"; |
| 1682 | |
| 1683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1684 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1685 | { |
| 1686 | char *path; |
| 1687 | PyObject *argv; |
| 1688 | char **argvlist; |
| 1689 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1690 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1691 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1692 | |
| 1693 | /* spawnv has three arguments: (mode, path, argv), where |
| 1694 | argv is a list or tuple of strings. */ |
| 1695 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1696 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1697 | return NULL; |
| 1698 | if (PyList_Check(argv)) { |
| 1699 | argc = PyList_Size(argv); |
| 1700 | getitem = PyList_GetItem; |
| 1701 | } |
| 1702 | else if (PyTuple_Check(argv)) { |
| 1703 | argc = PyTuple_Size(argv); |
| 1704 | getitem = PyTuple_GetItem; |
| 1705 | } |
| 1706 | else { |
Martin v. Löwis | e75f0e4 | 2001-11-24 09:31:44 +0000 | [diff] [blame] | 1707 | 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] | 1708 | return NULL; |
| 1709 | } |
| 1710 | |
| 1711 | argvlist = PyMem_NEW(char *, argc+1); |
| 1712 | if (argvlist == NULL) |
| 1713 | return NULL; |
| 1714 | for (i = 0; i < argc; i++) { |
| 1715 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1716 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1717 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1718 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1719 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | argvlist[argc] = NULL; |
| 1723 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1724 | if (mode == _OLD_P_OVERLAY) |
| 1725 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1726 | |
| 1727 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1728 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1729 | Py_END_ALLOW_THREADS |
| 1730 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1731 | PyMem_DEL(argvlist); |
| 1732 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1733 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1734 | return posix_error(); |
| 1735 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1736 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1737 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1738 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1739 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1740 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1745 | "spawnve(mode, path, args, env)\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1746 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1747 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1748 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1749 | path: path of executable file\n\ |
| 1750 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1751 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1752 | |
| 1753 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1754 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1755 | { |
| 1756 | char *path; |
| 1757 | PyObject *argv, *env; |
| 1758 | char **argvlist; |
| 1759 | char **envlist; |
| 1760 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1761 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1762 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1763 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1764 | |
| 1765 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1766 | argv is a list or tuple of strings and env is a dictionary |
| 1767 | like posix.environ. */ |
| 1768 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1769 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1770 | return NULL; |
| 1771 | if (PyList_Check(argv)) { |
| 1772 | argc = PyList_Size(argv); |
| 1773 | getitem = PyList_GetItem; |
| 1774 | } |
| 1775 | else if (PyTuple_Check(argv)) { |
| 1776 | argc = PyTuple_Size(argv); |
| 1777 | getitem = PyTuple_GetItem; |
| 1778 | } |
| 1779 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1780 | 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] | 1781 | return NULL; |
| 1782 | } |
| 1783 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1784 | 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] | 1785 | return NULL; |
| 1786 | } |
| 1787 | |
| 1788 | argvlist = PyMem_NEW(char *, argc+1); |
| 1789 | if (argvlist == NULL) { |
| 1790 | PyErr_NoMemory(); |
| 1791 | return NULL; |
| 1792 | } |
| 1793 | for (i = 0; i < argc; i++) { |
| 1794 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1795 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1796 | &argvlist[i])) |
| 1797 | { |
| 1798 | goto fail_1; |
| 1799 | } |
| 1800 | } |
| 1801 | argvlist[argc] = NULL; |
| 1802 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1803 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1804 | envlist = PyMem_NEW(char *, i + 1); |
| 1805 | if (envlist == NULL) { |
| 1806 | PyErr_NoMemory(); |
| 1807 | goto fail_1; |
| 1808 | } |
| 1809 | envc = 0; |
| 1810 | keys = PyMapping_Keys(env); |
| 1811 | vals = PyMapping_Values(env); |
| 1812 | if (!keys || !vals) |
| 1813 | goto fail_2; |
| 1814 | |
| 1815 | for (pos = 0; pos < i; pos++) { |
| 1816 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1817 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1818 | |
| 1819 | key = PyList_GetItem(keys, pos); |
| 1820 | val = PyList_GetItem(vals, pos); |
| 1821 | if (!key || !val) |
| 1822 | goto fail_2; |
| 1823 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1824 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1825 | !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] | 1826 | { |
| 1827 | goto fail_2; |
| 1828 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1829 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 1830 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1831 | if (p == NULL) { |
| 1832 | PyErr_NoMemory(); |
| 1833 | goto fail_2; |
| 1834 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 1835 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1836 | envlist[envc++] = p; |
| 1837 | } |
| 1838 | envlist[envc] = 0; |
| 1839 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1840 | if (mode == _OLD_P_OVERLAY) |
| 1841 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1842 | |
| 1843 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1844 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 1845 | Py_END_ALLOW_THREADS |
| 1846 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1847 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1848 | (void) posix_error(); |
| 1849 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1850 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1851 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1852 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1853 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1854 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1855 | |
| 1856 | fail_2: |
| 1857 | while (--envc >= 0) |
| 1858 | PyMem_DEL(envlist[envc]); |
| 1859 | PyMem_DEL(envlist); |
| 1860 | fail_1: |
| 1861 | PyMem_DEL(argvlist); |
| 1862 | Py_XDECREF(vals); |
| 1863 | Py_XDECREF(keys); |
| 1864 | return res; |
| 1865 | } |
| 1866 | #endif /* HAVE_SPAWNV */ |
| 1867 | |
| 1868 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1869 | #ifdef HAVE_FORK1 |
| 1870 | static char posix_fork1__doc__[] = |
| 1871 | "fork1() -> pid\n\ |
| 1872 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1873 | \n\ |
| 1874 | Return 0 to child process and PID of child to parent process."; |
| 1875 | |
| 1876 | static PyObject * |
| 1877 | posix_fork1(self, args) |
| 1878 | PyObject *self; |
| 1879 | PyObject *args; |
| 1880 | { |
| 1881 | int pid; |
| 1882 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1883 | return NULL; |
| 1884 | pid = fork1(); |
| 1885 | if (pid == -1) |
| 1886 | return posix_error(); |
| 1887 | PyOS_AfterFork(); |
| 1888 | return PyInt_FromLong((long)pid); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
| 1892 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1893 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1894 | static char posix_fork__doc__[] = |
| 1895 | "fork() -> pid\n\ |
| 1896 | Fork a child process.\n\ |
| 1897 | \n\ |
| 1898 | Return 0 to child process and PID of child to parent process."; |
| 1899 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1900 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1901 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1902 | { |
| 1903 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1904 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1905 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1906 | pid = fork(); |
| 1907 | if (pid == -1) |
| 1908 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1909 | if (pid == 0) |
| 1910 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1911 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1912 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1913 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1914 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1915 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1916 | #ifdef HAVE_PTY_H |
| 1917 | #include <pty.h> |
| 1918 | #else |
| 1919 | #ifdef HAVE_LIBUTIL_H |
| 1920 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1921 | #endif /* HAVE_LIBUTIL_H */ |
| 1922 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1923 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1924 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1925 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1926 | static char posix_openpty__doc__[] = |
| 1927 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1928 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1929 | |
| 1930 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1931 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1932 | { |
| 1933 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1934 | #ifndef HAVE_OPENPTY |
| 1935 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1936 | #endif |
| 1937 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1938 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1939 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1940 | |
| 1941 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1942 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1943 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1944 | #else |
| 1945 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1946 | if (slave_name == NULL) |
| 1947 | return posix_error(); |
| 1948 | |
| 1949 | slave_fd = open(slave_name, O_RDWR); |
| 1950 | if (slave_fd < 0) |
| 1951 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1952 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1953 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1954 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1955 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1956 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1957 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1958 | |
| 1959 | #ifdef HAVE_FORKPTY |
| 1960 | static char posix_forkpty__doc__[] = |
| 1961 | "forkpty() -> (pid, master_fd)\n\ |
| 1962 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1963 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1964 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1965 | |
| 1966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1967 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1968 | { |
| 1969 | int master_fd, pid; |
| 1970 | |
| 1971 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1972 | return NULL; |
| 1973 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1974 | if (pid == -1) |
| 1975 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1976 | if (pid == 0) |
| 1977 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1978 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1979 | } |
| 1980 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1981 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1983 | static char posix_getegid__doc__[] = |
| 1984 | "getegid() -> egid\n\ |
| 1985 | Return the current process's effective group id."; |
| 1986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1988 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1989 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1990 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1991 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1992 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1993 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1994 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1995 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1996 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1997 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1998 | static char posix_geteuid__doc__[] = |
| 1999 | "geteuid() -> euid\n\ |
| 2000 | Return the current process's effective user id."; |
| 2001 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2002 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2003 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2004 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2005 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2006 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2007 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2008 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2009 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2010 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2011 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2012 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2013 | static char posix_getgid__doc__[] = |
| 2014 | "getgid() -> gid\n\ |
| 2015 | Return the current process's group id."; |
| 2016 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2018 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2019 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2020 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2021 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2022 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2023 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2024 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2026 | |
| 2027 | static char posix_getpid__doc__[] = |
| 2028 | "getpid() -> pid\n\ |
| 2029 | Return the current process id"; |
| 2030 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2031 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2032 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2033 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2034 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2035 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2036 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2039 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2040 | #ifdef HAVE_GETGROUPS |
| 2041 | static char posix_getgroups__doc__[] = "\ |
| 2042 | getgroups() -> list of group IDs\n\ |
| 2043 | Return list of supplemental group IDs for the process."; |
| 2044 | |
| 2045 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2046 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2047 | { |
| 2048 | PyObject *result = NULL; |
| 2049 | |
| 2050 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2051 | #ifdef NGROUPS_MAX |
| 2052 | #define MAX_GROUPS NGROUPS_MAX |
| 2053 | #else |
| 2054 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2055 | #define MAX_GROUPS 64 |
| 2056 | #endif |
| 2057 | gid_t grouplist[MAX_GROUPS]; |
| 2058 | int n; |
| 2059 | |
| 2060 | n = getgroups(MAX_GROUPS, grouplist); |
| 2061 | if (n < 0) |
| 2062 | posix_error(); |
| 2063 | else { |
| 2064 | result = PyList_New(n); |
| 2065 | if (result != NULL) { |
| 2066 | PyObject *o; |
| 2067 | int i; |
| 2068 | for (i = 0; i < n; ++i) { |
| 2069 | o = PyInt_FromLong((long)grouplist[i]); |
| 2070 | if (o == NULL) { |
| 2071 | Py_DECREF(result); |
| 2072 | result = NULL; |
| 2073 | break; |
| 2074 | } |
| 2075 | PyList_SET_ITEM(result, i, o); |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | return result; |
| 2081 | } |
| 2082 | #endif |
| 2083 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2084 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2085 | static char posix_getpgrp__doc__[] = |
| 2086 | "getpgrp() -> pgrp\n\ |
| 2087 | Return the current process group id."; |
| 2088 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2089 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2090 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2091 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2092 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2093 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2094 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2095 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2096 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2097 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2098 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2099 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2100 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2101 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2102 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2103 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2104 | static char posix_setpgrp__doc__[] = |
| 2105 | "setpgrp() -> None\n\ |
| 2106 | Make this process a session leader."; |
| 2107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2109 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2110 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2111 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2112 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2113 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2114 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2115 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2116 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2117 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2118 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2119 | Py_INCREF(Py_None); |
| 2120 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2123 | #endif /* HAVE_SETPGRP */ |
| 2124 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2125 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2126 | static char posix_getppid__doc__[] = |
| 2127 | "getppid() -> ppid\n\ |
| 2128 | Return the parent's process id."; |
| 2129 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2130 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2131 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2132 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2133 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2134 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2135 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2136 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2137 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2139 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2140 | #ifdef HAVE_GETLOGIN |
| 2141 | static char posix_getlogin__doc__[] = "\ |
| 2142 | getlogin() -> string\n\ |
| 2143 | Return the actual login name."; |
| 2144 | |
| 2145 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2146 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2147 | { |
| 2148 | PyObject *result = NULL; |
| 2149 | |
| 2150 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2151 | char *name; |
| 2152 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2153 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2154 | errno = 0; |
| 2155 | name = getlogin(); |
| 2156 | if (name == NULL) { |
| 2157 | if (errno) |
| 2158 | posix_error(); |
| 2159 | else |
| 2160 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2161 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2162 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2163 | else |
| 2164 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2165 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2166 | } |
| 2167 | return result; |
| 2168 | } |
| 2169 | #endif |
| 2170 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2171 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2172 | static char posix_getuid__doc__[] = |
| 2173 | "getuid() -> uid\n\ |
| 2174 | Return the current process's user id."; |
| 2175 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2176 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2177 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2178 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2179 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2180 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2181 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2183 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2184 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2185 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2186 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2187 | static char posix_kill__doc__[] = |
| 2188 | "kill(pid, sig) -> None\n\ |
| 2189 | Kill a process with a signal."; |
| 2190 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2191 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2192 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2193 | { |
| 2194 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2195 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2196 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2197 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2198 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2199 | APIRET rc; |
| 2200 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2201 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2202 | |
| 2203 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2204 | APIRET rc; |
| 2205 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2206 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2207 | |
| 2208 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2209 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2210 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2211 | if (kill(pid, sig) == -1) |
| 2212 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2213 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2214 | Py_INCREF(Py_None); |
| 2215 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2216 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2217 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2218 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2219 | #ifdef HAVE_PLOCK |
| 2220 | |
| 2221 | #ifdef HAVE_SYS_LOCK_H |
| 2222 | #include <sys/lock.h> |
| 2223 | #endif |
| 2224 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2225 | static char posix_plock__doc__[] = |
| 2226 | "plock(op) -> None\n\ |
| 2227 | Lock program segments into memory."; |
| 2228 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2229 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2230 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2231 | { |
| 2232 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2233 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2234 | return NULL; |
| 2235 | if (plock(op) == -1) |
| 2236 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2237 | Py_INCREF(Py_None); |
| 2238 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2239 | } |
| 2240 | #endif |
| 2241 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2242 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2243 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2244 | static char posix_popen__doc__[] = |
| 2245 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2246 | Open a pipe to/from a command returning a file object."; |
| 2247 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2248 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2249 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2250 | async_system(const char *command) |
| 2251 | { |
| 2252 | char *p, errormsg[256], args[1024]; |
| 2253 | RESULTCODES rcodes; |
| 2254 | APIRET rc; |
| 2255 | char *shell = getenv("COMSPEC"); |
| 2256 | if (!shell) |
| 2257 | shell = "cmd"; |
| 2258 | |
| 2259 | strcpy(args, shell); |
| 2260 | p = &args[ strlen(args)+1 ]; |
| 2261 | strcpy(p, "/c "); |
| 2262 | strcat(p, command); |
| 2263 | p += strlen(p) + 1; |
| 2264 | *p = '\0'; |
| 2265 | |
| 2266 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2267 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2268 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2269 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2270 | &rcodes, shell); |
| 2271 | return rc; |
| 2272 | } |
| 2273 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2274 | static FILE * |
| 2275 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2276 | { |
| 2277 | HFILE rhan, whan; |
| 2278 | FILE *retfd = NULL; |
| 2279 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2280 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2281 | if (rc != NO_ERROR) { |
| 2282 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2283 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2284 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2285 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2286 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2287 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2288 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2289 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2290 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2291 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2292 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2293 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2294 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2295 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2298 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2299 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2300 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2301 | if (rc == NO_ERROR) |
| 2302 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2303 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2304 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2305 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2306 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2307 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2308 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2309 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2310 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2311 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2313 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2314 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2315 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2316 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2319 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2320 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2321 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2322 | if (rc == NO_ERROR) |
| 2323 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2324 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2325 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2326 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2327 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2328 | } else { |
| 2329 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2330 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2331 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2335 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2336 | { |
| 2337 | char *name; |
| 2338 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2339 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2340 | FILE *fp; |
| 2341 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2342 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2343 | return NULL; |
| 2344 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2345 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2346 | Py_END_ALLOW_THREADS |
| 2347 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2348 | return os2_error(err); |
| 2349 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2350 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2351 | if (f != NULL) |
| 2352 | PyFile_SetBufSize(f, bufsize); |
| 2353 | return f; |
| 2354 | } |
| 2355 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2356 | #elif defined(MS_WIN32) |
| 2357 | |
| 2358 | /* |
| 2359 | * Portable 'popen' replacement for Win32. |
| 2360 | * |
| 2361 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2362 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2363 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2364 | */ |
| 2365 | |
| 2366 | #include <malloc.h> |
| 2367 | #include <io.h> |
| 2368 | #include <fcntl.h> |
| 2369 | |
| 2370 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2371 | #define POPEN_1 1 |
| 2372 | #define POPEN_2 2 |
| 2373 | #define POPEN_3 3 |
| 2374 | #define POPEN_4 4 |
| 2375 | |
| 2376 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2377 | static int _PyPclose(FILE *file); |
| 2378 | |
| 2379 | /* |
| 2380 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2381 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2382 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2383 | */ |
| 2384 | static PyObject *_PyPopenProcs = NULL; |
| 2385 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2386 | |
| 2387 | /* popen that works from a GUI. |
| 2388 | * |
| 2389 | * The result of this function is a pipe (file) connected to the |
| 2390 | * processes stdin or stdout, depending on the requested mode. |
| 2391 | */ |
| 2392 | |
| 2393 | static PyObject * |
| 2394 | posix_popen(PyObject *self, PyObject *args) |
| 2395 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2396 | PyObject *f, *s; |
| 2397 | int tm = 0; |
| 2398 | |
| 2399 | char *cmdstring; |
| 2400 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2401 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2402 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2403 | return NULL; |
| 2404 | |
| 2405 | s = PyTuple_New(0); |
| 2406 | |
| 2407 | if (*mode == 'r') |
| 2408 | tm = _O_RDONLY; |
| 2409 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2410 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2411 | return NULL; |
| 2412 | } else |
| 2413 | tm = _O_WRONLY; |
| 2414 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2415 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2416 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2417 | return NULL; |
| 2418 | } |
| 2419 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2420 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2421 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2422 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2423 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2424 | else |
| 2425 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2426 | |
| 2427 | return f; |
| 2428 | } |
| 2429 | |
| 2430 | /* Variation on win32pipe.popen |
| 2431 | * |
| 2432 | * The result of this function is a pipe (file) connected to the |
| 2433 | * process's stdin, and a pipe connected to the process's stdout. |
| 2434 | */ |
| 2435 | |
| 2436 | static PyObject * |
| 2437 | win32_popen2(PyObject *self, PyObject *args) |
| 2438 | { |
| 2439 | PyObject *f; |
| 2440 | int tm=0; |
| 2441 | |
| 2442 | char *cmdstring; |
| 2443 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2444 | int bufsize = -1; |
| 2445 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2446 | return NULL; |
| 2447 | |
| 2448 | if (*mode == 't') |
| 2449 | tm = _O_TEXT; |
| 2450 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2451 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2452 | return NULL; |
| 2453 | } else |
| 2454 | tm = _O_BINARY; |
| 2455 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2456 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2457 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2458 | return NULL; |
| 2459 | } |
| 2460 | |
| 2461 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2462 | |
| 2463 | return f; |
| 2464 | } |
| 2465 | |
| 2466 | /* |
| 2467 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2468 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2469 | * The result of this function is 3 pipes - the process's stdin, |
| 2470 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2471 | */ |
| 2472 | |
| 2473 | static PyObject * |
| 2474 | win32_popen3(PyObject *self, PyObject *args) |
| 2475 | { |
| 2476 | PyObject *f; |
| 2477 | int tm = 0; |
| 2478 | |
| 2479 | char *cmdstring; |
| 2480 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2481 | int bufsize = -1; |
| 2482 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2483 | return NULL; |
| 2484 | |
| 2485 | if (*mode == 't') |
| 2486 | tm = _O_TEXT; |
| 2487 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2488 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2489 | return NULL; |
| 2490 | } else |
| 2491 | tm = _O_BINARY; |
| 2492 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2493 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2494 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2495 | return NULL; |
| 2496 | } |
| 2497 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2498 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2499 | |
| 2500 | return f; |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * Variation on win32pipe.popen |
| 2505 | * |
| 2506 | * The result of this function is 2 pipes - the processes stdin, |
| 2507 | * and stdout+stderr combined as a single pipe. |
| 2508 | */ |
| 2509 | |
| 2510 | static PyObject * |
| 2511 | win32_popen4(PyObject *self, PyObject *args) |
| 2512 | { |
| 2513 | PyObject *f; |
| 2514 | int tm = 0; |
| 2515 | |
| 2516 | char *cmdstring; |
| 2517 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2518 | int bufsize = -1; |
| 2519 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2520 | return NULL; |
| 2521 | |
| 2522 | if (*mode == 't') |
| 2523 | tm = _O_TEXT; |
| 2524 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2525 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2526 | return NULL; |
| 2527 | } else |
| 2528 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2529 | |
| 2530 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2531 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2532 | return NULL; |
| 2533 | } |
| 2534 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2535 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2536 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2537 | return f; |
| 2538 | } |
| 2539 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2540 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2541 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2542 | HANDLE hStdin, |
| 2543 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2544 | HANDLE hStderr, |
| 2545 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2546 | { |
| 2547 | PROCESS_INFORMATION piProcInfo; |
| 2548 | STARTUPINFO siStartInfo; |
| 2549 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2550 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2551 | int i; |
| 2552 | int x; |
| 2553 | |
| 2554 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2555 | char *comshell; |
| 2556 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2557 | s1 = (char *)_alloca(i); |
| 2558 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2559 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 2562 | * then use the w9xpopen hack. |
| 2563 | */ |
| 2564 | comshell = s1 + x; |
| 2565 | while (comshell >= s1 && *comshell != '\\') |
| 2566 | --comshell; |
| 2567 | ++comshell; |
| 2568 | |
| 2569 | if (GetVersion() < 0x80000000 && |
| 2570 | _stricmp(comshell, "command.com") != 0) { |
| 2571 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2572 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2573 | s2 = (char *)_alloca(x); |
| 2574 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 2575 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2576 | } |
| 2577 | else { |
| 2578 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2579 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 2580 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2581 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2582 | char modulepath[_MAX_PATH]; |
| 2583 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2584 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2585 | for (i = x = 0; modulepath[i]; i++) |
| 2586 | if (modulepath[i] == '\\') |
| 2587 | x = i+1; |
| 2588 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2589 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 2590 | strncat(modulepath, |
| 2591 | szConsoleSpawn, |
| 2592 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2593 | -strlen(modulepath)); |
| 2594 | if (stat(modulepath, &statinfo) != 0) { |
| 2595 | /* Eeek - file-not-found - possibly an embedding |
| 2596 | situation - see if we can locate it in sys.prefix |
| 2597 | */ |
| 2598 | strncpy(modulepath, |
| 2599 | Py_GetExecPrefix(), |
| 2600 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 2601 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 2602 | strcat(modulepath, "\\"); |
| 2603 | strncat(modulepath, |
| 2604 | szConsoleSpawn, |
| 2605 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2606 | -strlen(modulepath)); |
| 2607 | /* No where else to look - raise an easily identifiable |
| 2608 | error, rather than leaving Windows to report |
| 2609 | "file not found" - as the user is probably blissfully |
| 2610 | unaware this shim EXE is used, and it will confuse them. |
| 2611 | (well, it confused me for a while ;-) |
| 2612 | */ |
| 2613 | if (stat(modulepath, &statinfo) != 0) { |
| 2614 | PyErr_Format(PyExc_RuntimeError, |
| 2615 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2616 | "for popen to work with your shell " |
| 2617 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2618 | szConsoleSpawn); |
| 2619 | return FALSE; |
| 2620 | } |
| 2621 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2622 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2623 | strlen(modulepath) + |
| 2624 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2625 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2626 | s2 = (char *)_alloca(x); |
| 2627 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 2628 | PyOS_snprintf( |
| 2629 | s2, x, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2630 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2631 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2632 | s1, |
| 2633 | s3, |
| 2634 | cmdstring); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2639 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2640 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2641 | PyErr_SetString(PyExc_RuntimeError, |
| 2642 | "Cannot locate a COMSPEC environment variable to " |
| 2643 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2644 | return FALSE; |
| 2645 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2646 | |
| 2647 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2648 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2649 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2650 | siStartInfo.hStdInput = hStdin; |
| 2651 | siStartInfo.hStdOutput = hStdout; |
| 2652 | siStartInfo.hStdError = hStderr; |
| 2653 | siStartInfo.wShowWindow = SW_HIDE; |
| 2654 | |
| 2655 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2656 | s2, |
| 2657 | NULL, |
| 2658 | NULL, |
| 2659 | TRUE, |
| 2660 | CREATE_NEW_CONSOLE, |
| 2661 | NULL, |
| 2662 | NULL, |
| 2663 | &siStartInfo, |
| 2664 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2665 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2666 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2667 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2668 | /* Return process handle */ |
| 2669 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2670 | return TRUE; |
| 2671 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2672 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2673 | return FALSE; |
| 2674 | } |
| 2675 | |
| 2676 | /* The following code is based off of KB: Q190351 */ |
| 2677 | |
| 2678 | static PyObject * |
| 2679 | _PyPopen(char *cmdstring, int mode, int n) |
| 2680 | { |
| 2681 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2682 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2683 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2684 | |
| 2685 | SECURITY_ATTRIBUTES saAttr; |
| 2686 | BOOL fSuccess; |
| 2687 | int fd1, fd2, fd3; |
| 2688 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2689 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2690 | PyObject *f; |
| 2691 | |
| 2692 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2693 | saAttr.bInheritHandle = TRUE; |
| 2694 | saAttr.lpSecurityDescriptor = NULL; |
| 2695 | |
| 2696 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2697 | return win32_error("CreatePipe", NULL); |
| 2698 | |
| 2699 | /* Create new output read handle and the input write handle. Set |
| 2700 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2701 | * the these handles; resulting in non-closeable handles to the pipes |
| 2702 | * being created. */ |
| 2703 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2704 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2705 | FALSE, |
| 2706 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2707 | if (!fSuccess) |
| 2708 | return win32_error("DuplicateHandle", NULL); |
| 2709 | |
| 2710 | /* Close the inheritable version of ChildStdin |
| 2711 | that we're using. */ |
| 2712 | CloseHandle(hChildStdinWr); |
| 2713 | |
| 2714 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2715 | return win32_error("CreatePipe", NULL); |
| 2716 | |
| 2717 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2718 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2719 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2720 | if (!fSuccess) |
| 2721 | return win32_error("DuplicateHandle", NULL); |
| 2722 | |
| 2723 | /* Close the inheritable version of ChildStdout |
| 2724 | that we're using. */ |
| 2725 | CloseHandle(hChildStdoutRd); |
| 2726 | |
| 2727 | if (n != POPEN_4) { |
| 2728 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2729 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2730 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2731 | hChildStderrRd, |
| 2732 | GetCurrentProcess(), |
| 2733 | &hChildStderrRdDup, 0, |
| 2734 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2735 | if (!fSuccess) |
| 2736 | return win32_error("DuplicateHandle", NULL); |
| 2737 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2738 | CloseHandle(hChildStderrRd); |
| 2739 | } |
| 2740 | |
| 2741 | switch (n) { |
| 2742 | case POPEN_1: |
| 2743 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2744 | case _O_WRONLY | _O_TEXT: |
| 2745 | /* Case for writing to child Stdin in text mode. */ |
| 2746 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2747 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2748 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2749 | PyFile_SetBufSize(f, 0); |
| 2750 | /* We don't care about these pipes anymore, so close them. */ |
| 2751 | CloseHandle(hChildStdoutRdDup); |
| 2752 | CloseHandle(hChildStderrRdDup); |
| 2753 | break; |
| 2754 | |
| 2755 | case _O_RDONLY | _O_TEXT: |
| 2756 | /* Case for reading from child Stdout in text mode. */ |
| 2757 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2758 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2759 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2760 | PyFile_SetBufSize(f, 0); |
| 2761 | /* We don't care about these pipes anymore, so close them. */ |
| 2762 | CloseHandle(hChildStdinWrDup); |
| 2763 | CloseHandle(hChildStderrRdDup); |
| 2764 | break; |
| 2765 | |
| 2766 | case _O_RDONLY | _O_BINARY: |
| 2767 | /* Case for readinig from child Stdout in binary mode. */ |
| 2768 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2769 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2770 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2771 | PyFile_SetBufSize(f, 0); |
| 2772 | /* We don't care about these pipes anymore, so close them. */ |
| 2773 | CloseHandle(hChildStdinWrDup); |
| 2774 | CloseHandle(hChildStderrRdDup); |
| 2775 | break; |
| 2776 | |
| 2777 | case _O_WRONLY | _O_BINARY: |
| 2778 | /* Case for writing to child Stdin in binary mode. */ |
| 2779 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2780 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2781 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2782 | PyFile_SetBufSize(f, 0); |
| 2783 | /* We don't care about these pipes anymore, so close them. */ |
| 2784 | CloseHandle(hChildStdoutRdDup); |
| 2785 | CloseHandle(hChildStderrRdDup); |
| 2786 | break; |
| 2787 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2788 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2789 | break; |
| 2790 | |
| 2791 | case POPEN_2: |
| 2792 | case POPEN_4: |
| 2793 | { |
| 2794 | char *m1, *m2; |
| 2795 | PyObject *p1, *p2; |
| 2796 | |
| 2797 | if (mode && _O_TEXT) { |
| 2798 | m1 = "r"; |
| 2799 | m2 = "w"; |
| 2800 | } else { |
| 2801 | m1 = "rb"; |
| 2802 | m2 = "wb"; |
| 2803 | } |
| 2804 | |
| 2805 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2806 | f1 = _fdopen(fd1, m2); |
| 2807 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2808 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2809 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2810 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2811 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2812 | PyFile_SetBufSize(p2, 0); |
| 2813 | |
| 2814 | if (n != 4) |
| 2815 | CloseHandle(hChildStderrRdDup); |
| 2816 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2817 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2818 | Py_XDECREF(p1); |
| 2819 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2820 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2821 | break; |
| 2822 | } |
| 2823 | |
| 2824 | case POPEN_3: |
| 2825 | { |
| 2826 | char *m1, *m2; |
| 2827 | PyObject *p1, *p2, *p3; |
| 2828 | |
| 2829 | if (mode && _O_TEXT) { |
| 2830 | m1 = "r"; |
| 2831 | m2 = "w"; |
| 2832 | } else { |
| 2833 | m1 = "rb"; |
| 2834 | m2 = "wb"; |
| 2835 | } |
| 2836 | |
| 2837 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2838 | f1 = _fdopen(fd1, m2); |
| 2839 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2840 | f2 = _fdopen(fd2, m1); |
| 2841 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2842 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2843 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2844 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2845 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2846 | PyFile_SetBufSize(p1, 0); |
| 2847 | PyFile_SetBufSize(p2, 0); |
| 2848 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2849 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2850 | Py_XDECREF(p1); |
| 2851 | Py_XDECREF(p2); |
| 2852 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2853 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2854 | break; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | if (n == POPEN_4) { |
| 2859 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2860 | hChildStdinRd, |
| 2861 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2862 | hChildStdoutWr, |
| 2863 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2864 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2865 | } |
| 2866 | else { |
| 2867 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2868 | hChildStdinRd, |
| 2869 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2870 | hChildStderrWr, |
| 2871 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2872 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2875 | /* |
| 2876 | * Insert the files we've created into the process dictionary |
| 2877 | * all referencing the list with the process handle and the |
| 2878 | * initial number of files (see description below in _PyPclose). |
| 2879 | * Since if _PyPclose later tried to wait on a process when all |
| 2880 | * handles weren't closed, it could create a deadlock with the |
| 2881 | * child, we spend some energy here to try to ensure that we |
| 2882 | * either insert all file handles into the dictionary or none |
| 2883 | * at all. It's a little clumsy with the various popen modes |
| 2884 | * and variable number of files involved. |
| 2885 | */ |
| 2886 | if (!_PyPopenProcs) { |
| 2887 | _PyPopenProcs = PyDict_New(); |
| 2888 | } |
| 2889 | |
| 2890 | if (_PyPopenProcs) { |
| 2891 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2892 | int ins_rc[3]; |
| 2893 | |
| 2894 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2895 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2896 | |
| 2897 | procObj = PyList_New(2); |
| 2898 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2899 | intObj = PyInt_FromLong(file_count); |
| 2900 | |
| 2901 | if (procObj && hProcessObj && intObj) { |
| 2902 | PyList_SetItem(procObj,0,hProcessObj); |
| 2903 | PyList_SetItem(procObj,1,intObj); |
| 2904 | |
| 2905 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2906 | if (fileObj[0]) { |
| 2907 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2908 | fileObj[0], |
| 2909 | procObj); |
| 2910 | } |
| 2911 | if (file_count >= 2) { |
| 2912 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2913 | if (fileObj[1]) { |
| 2914 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2915 | fileObj[1], |
| 2916 | procObj); |
| 2917 | } |
| 2918 | } |
| 2919 | if (file_count >= 3) { |
| 2920 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2921 | if (fileObj[2]) { |
| 2922 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2923 | fileObj[2], |
| 2924 | procObj); |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2929 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2930 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2931 | /* Something failed - remove any dictionary |
| 2932 | * entries that did make it. |
| 2933 | */ |
| 2934 | if (!ins_rc[0] && fileObj[0]) { |
| 2935 | PyDict_DelItem(_PyPopenProcs, |
| 2936 | fileObj[0]); |
| 2937 | } |
| 2938 | if (!ins_rc[1] && fileObj[1]) { |
| 2939 | PyDict_DelItem(_PyPopenProcs, |
| 2940 | fileObj[1]); |
| 2941 | } |
| 2942 | if (!ins_rc[2] && fileObj[2]) { |
| 2943 | PyDict_DelItem(_PyPopenProcs, |
| 2944 | fileObj[2]); |
| 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | /* |
| 2950 | * Clean up our localized references for the dictionary keys |
| 2951 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2952 | * that got placed in the dictionary. |
| 2953 | */ |
| 2954 | Py_XDECREF(procObj); |
| 2955 | Py_XDECREF(fileObj[0]); |
| 2956 | Py_XDECREF(fileObj[1]); |
| 2957 | Py_XDECREF(fileObj[2]); |
| 2958 | } |
| 2959 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2960 | /* Child is launched. Close the parents copy of those pipe |
| 2961 | * handles that only the child should have open. You need to |
| 2962 | * make sure that no handles to the write end of the output pipe |
| 2963 | * are maintained in this process or else the pipe will not close |
| 2964 | * when the child process exits and the ReadFile will hang. */ |
| 2965 | |
| 2966 | if (!CloseHandle(hChildStdinRd)) |
| 2967 | return win32_error("CloseHandle", NULL); |
| 2968 | |
| 2969 | if (!CloseHandle(hChildStdoutWr)) |
| 2970 | return win32_error("CloseHandle", NULL); |
| 2971 | |
| 2972 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2973 | return win32_error("CloseHandle", NULL); |
| 2974 | |
| 2975 | return f; |
| 2976 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2977 | |
| 2978 | /* |
| 2979 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2980 | * 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] | 2981 | * |
| 2982 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2983 | * input file pointer to information about the process that was |
| 2984 | * originally created by the popen* call that created the file pointer. |
| 2985 | * The dictionary uses the file pointer as a key (with one entry |
| 2986 | * inserted for each file returned by the original popen* call) and a |
| 2987 | * single list object as the value for all files from a single call. |
| 2988 | * The list object contains the Win32 process handle at [0], and a file |
| 2989 | * count at [1], which is initialized to the total number of file |
| 2990 | * handles using that list. |
| 2991 | * |
| 2992 | * This function closes whichever handle it is passed, and decrements |
| 2993 | * the file count in the dictionary for the process handle pointed to |
| 2994 | * by this file. On the last close (when the file count reaches zero), |
| 2995 | * this function will wait for the child process and then return its |
| 2996 | * exit code as the result of the close() operation. This permits the |
| 2997 | * files to be closed in any order - it is always the close() of the |
| 2998 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2999 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3000 | |
| 3001 | /* RED_FLAG 31-Aug-2000 Tim |
| 3002 | * This is always called (today!) between a pair of |
| 3003 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3004 | * macros. So the thread running this has no valid thread state, as |
| 3005 | * far as Python is concerned. However, this calls some Python API |
| 3006 | * functions that cannot be called safely without a valid thread |
| 3007 | * state, in particular PyDict_GetItem. |
| 3008 | * As a temporary hack (although it may last for years ...), we |
| 3009 | * *rely* on not having a valid thread state in this function, in |
| 3010 | * order to create our own "from scratch". |
| 3011 | * This will deadlock if _PyPclose is ever called by a thread |
| 3012 | * holding the global lock. |
| 3013 | */ |
| 3014 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3015 | static int _PyPclose(FILE *file) |
| 3016 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3017 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3018 | DWORD exit_code; |
| 3019 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3020 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 3021 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3022 | #ifdef WITH_THREAD |
| 3023 | PyInterpreterState* pInterpreterState; |
| 3024 | PyThreadState* pThreadState; |
| 3025 | #endif |
| 3026 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3027 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3028 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3029 | */ |
| 3030 | result = fclose(file); |
| 3031 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3032 | #ifdef WITH_THREAD |
| 3033 | /* Bootstrap a valid thread state into existence. */ |
| 3034 | pInterpreterState = PyInterpreterState_New(); |
| 3035 | if (!pInterpreterState) { |
| 3036 | /* Well, we're hosed now! We don't have a thread |
| 3037 | * state, so can't call a nice error routine, or raise |
| 3038 | * an exception. Just die. |
| 3039 | */ |
| 3040 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3041 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3042 | return -1; /* unreachable */ |
| 3043 | } |
| 3044 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3045 | if (!pThreadState) { |
| 3046 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3047 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3048 | return -1; /* unreachable */ |
| 3049 | } |
| 3050 | /* Grab the global lock. Note that this will deadlock if the |
| 3051 | * current thread already has the lock! (see RED_FLAG comments |
| 3052 | * before this function) |
| 3053 | */ |
| 3054 | PyEval_RestoreThread(pThreadState); |
| 3055 | #endif |
| 3056 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3057 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3058 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3059 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3060 | fileObj)) != NULL && |
| 3061 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 3062 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 3063 | |
| 3064 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 3065 | file_count = PyInt_AsLong(intObj); |
| 3066 | |
| 3067 | if (file_count > 1) { |
| 3068 | /* Still other files referencing process */ |
| 3069 | file_count--; |
| 3070 | PyList_SetItem(procObj,1, |
| 3071 | PyInt_FromLong(file_count)); |
| 3072 | } else { |
| 3073 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3074 | if (result != EOF && |
| 3075 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 3076 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3077 | /* Possible truncation here in 16-bit environments, but |
| 3078 | * real exit codes are just the lower byte in any event. |
| 3079 | */ |
| 3080 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3081 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3082 | /* Indicate failure - this will cause the file object |
| 3083 | * to raise an I/O error and translate the last Win32 |
| 3084 | * error code from errno. We do have a problem with |
| 3085 | * last errors that overlap the normal errno table, |
| 3086 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3087 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3088 | if (result != EOF) { |
| 3089 | /* If the error wasn't from the fclose(), then |
| 3090 | * set errno for the file object error handling. |
| 3091 | */ |
| 3092 | errno = GetLastError(); |
| 3093 | } |
| 3094 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | /* Free up the native handle at this point */ |
| 3098 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3099 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3100 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3101 | /* Remove this file pointer from dictionary */ |
| 3102 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3103 | |
| 3104 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 3105 | Py_DECREF(_PyPopenProcs); |
| 3106 | _PyPopenProcs = NULL; |
| 3107 | } |
| 3108 | |
| 3109 | } /* if object retrieval ok */ |
| 3110 | |
| 3111 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3112 | } /* if _PyPopenProcs */ |
| 3113 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3114 | #ifdef WITH_THREAD |
| 3115 | /* Tear down the thread & interpreter states. |
| 3116 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3117 | * call the thread clear & delete functions, and indeed insist on |
| 3118 | * doing that themselves. The lock must be held during the clear, but |
| 3119 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3120 | */ |
| 3121 | PyInterpreterState_Clear(pInterpreterState); |
| 3122 | PyEval_ReleaseThread(pThreadState); |
| 3123 | PyInterpreterState_Delete(pInterpreterState); |
| 3124 | #endif |
| 3125 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3126 | return result; |
| 3127 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3128 | |
| 3129 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3130 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3131 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3132 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3133 | char *name; |
| 3134 | char *mode = "r"; |
| 3135 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3136 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3137 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3138 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3139 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3140 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3141 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3142 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3143 | if (fp == NULL) |
| 3144 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3145 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3146 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3147 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3148 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3149 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3150 | #endif |
| 3151 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3152 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3154 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3155 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3156 | static char posix_setuid__doc__[] = |
| 3157 | "setuid(uid) -> None\n\ |
| 3158 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3160 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3161 | { |
| 3162 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3163 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3164 | return NULL; |
| 3165 | if (setuid(uid) < 0) |
| 3166 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3167 | Py_INCREF(Py_None); |
| 3168 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3169 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3170 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3171 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3172 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3173 | #ifdef HAVE_SETEUID |
| 3174 | static char posix_seteuid__doc__[] = |
| 3175 | "seteuid(uid) -> None\n\ |
| 3176 | Set the current process's effective user id."; |
| 3177 | static PyObject * |
| 3178 | posix_seteuid (PyObject *self, PyObject *args) |
| 3179 | { |
| 3180 | int euid; |
| 3181 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3182 | return NULL; |
| 3183 | } else if (seteuid(euid) < 0) { |
| 3184 | return posix_error(); |
| 3185 | } else { |
| 3186 | Py_INCREF(Py_None); |
| 3187 | return Py_None; |
| 3188 | } |
| 3189 | } |
| 3190 | #endif /* HAVE_SETEUID */ |
| 3191 | |
| 3192 | #ifdef HAVE_SETEGID |
| 3193 | static char posix_setegid__doc__[] = |
| 3194 | "setegid(gid) -> None\n\ |
| 3195 | Set the current process's effective group id."; |
| 3196 | static PyObject * |
| 3197 | posix_setegid (PyObject *self, PyObject *args) |
| 3198 | { |
| 3199 | int egid; |
| 3200 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3201 | return NULL; |
| 3202 | } else if (setegid(egid) < 0) { |
| 3203 | return posix_error(); |
| 3204 | } else { |
| 3205 | Py_INCREF(Py_None); |
| 3206 | return Py_None; |
| 3207 | } |
| 3208 | } |
| 3209 | #endif /* HAVE_SETEGID */ |
| 3210 | |
| 3211 | #ifdef HAVE_SETREUID |
| 3212 | static char posix_setreuid__doc__[] = |
| 3213 | "seteuid(ruid, euid) -> None\n\ |
| 3214 | Set the current process's real and effective user ids."; |
| 3215 | static PyObject * |
| 3216 | posix_setreuid (PyObject *self, PyObject *args) |
| 3217 | { |
| 3218 | int ruid, euid; |
| 3219 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3220 | return NULL; |
| 3221 | } else if (setreuid(ruid, euid) < 0) { |
| 3222 | return posix_error(); |
| 3223 | } else { |
| 3224 | Py_INCREF(Py_None); |
| 3225 | return Py_None; |
| 3226 | } |
| 3227 | } |
| 3228 | #endif /* HAVE_SETREUID */ |
| 3229 | |
| 3230 | #ifdef HAVE_SETREGID |
| 3231 | static char posix_setregid__doc__[] = |
| 3232 | "setegid(rgid, egid) -> None\n\ |
| 3233 | Set the current process's real and effective group ids."; |
| 3234 | static PyObject * |
| 3235 | posix_setregid (PyObject *self, PyObject *args) |
| 3236 | { |
| 3237 | int rgid, egid; |
| 3238 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3239 | return NULL; |
| 3240 | } else if (setregid(rgid, egid) < 0) { |
| 3241 | return posix_error(); |
| 3242 | } else { |
| 3243 | Py_INCREF(Py_None); |
| 3244 | return Py_None; |
| 3245 | } |
| 3246 | } |
| 3247 | #endif /* HAVE_SETREGID */ |
| 3248 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3249 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3250 | static char posix_setgid__doc__[] = |
| 3251 | "setgid(gid) -> None\n\ |
| 3252 | Set the current process's group id."; |
| 3253 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3254 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3255 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3256 | { |
| 3257 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3258 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3259 | return NULL; |
| 3260 | if (setgid(gid) < 0) |
| 3261 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3262 | Py_INCREF(Py_None); |
| 3263 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3264 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3265 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3266 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3267 | #ifdef HAVE_SETGROUPS |
| 3268 | static char posix_setgroups__doc__[] = |
| 3269 | "setgroups(list) -> None\n\ |
| 3270 | Set the groups of the current process to list."; |
| 3271 | |
| 3272 | static PyObject * |
| 3273 | posix_setgroups(PyObject *self, PyObject *args) |
| 3274 | { |
| 3275 | PyObject *groups; |
| 3276 | int i, len; |
| 3277 | gid_t grouplist[MAX_GROUPS]; |
| 3278 | |
| 3279 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 3280 | return NULL; |
| 3281 | if (!PySequence_Check(groups)) { |
| 3282 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | len = PySequence_Size(groups); |
| 3286 | if (len > MAX_GROUPS) { |
| 3287 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 3288 | return NULL; |
| 3289 | } |
| 3290 | for(i = 0; i < len; i++) { |
| 3291 | PyObject *elem; |
| 3292 | elem = PySequence_GetItem(groups, i); |
| 3293 | if (!elem) |
| 3294 | return NULL; |
| 3295 | if (!PyInt_Check(elem)) { |
| 3296 | PyErr_SetString(PyExc_TypeError, |
| 3297 | "groups must be integers"); |
| 3298 | Py_DECREF(elem); |
| 3299 | return NULL; |
| 3300 | } |
| 3301 | /* XXX: check that value fits into gid_t. */ |
| 3302 | grouplist[i] = PyInt_AsLong(elem); |
| 3303 | Py_DECREF(elem); |
| 3304 | } |
| 3305 | |
| 3306 | if (setgroups(len, grouplist) < 0) |
| 3307 | return posix_error(); |
| 3308 | Py_INCREF(Py_None); |
| 3309 | return Py_None; |
| 3310 | } |
| 3311 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3312 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3313 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3314 | static char posix_waitpid__doc__[] = |
| 3315 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3316 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3318 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3319 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3320 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3321 | int pid, options; |
| 3322 | #ifdef UNION_WAIT |
| 3323 | union wait status; |
| 3324 | #define status_i (status.w_status) |
| 3325 | #else |
| 3326 | int status; |
| 3327 | #define status_i status |
| 3328 | #endif |
| 3329 | status_i = 0; |
| 3330 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3331 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3332 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3333 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3334 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3335 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3336 | #else |
| 3337 | pid = waitpid(pid, &status, options); |
| 3338 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3339 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3340 | if (pid == -1) |
| 3341 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3342 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3343 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3344 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3345 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3347 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3348 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3349 | static char posix_wait__doc__[] = |
| 3350 | "wait() -> (pid, status)\n\ |
| 3351 | Wait for completion of a child process."; |
| 3352 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3353 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3354 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3355 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3356 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3357 | #ifdef UNION_WAIT |
| 3358 | union wait status; |
| 3359 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3360 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3361 | int status; |
| 3362 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3363 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3364 | if (!PyArg_ParseTuple(args, ":wait")) |
| 3365 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3366 | status_i = 0; |
| 3367 | Py_BEGIN_ALLOW_THREADS |
| 3368 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3369 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3370 | if (pid == -1) |
| 3371 | return posix_error(); |
| 3372 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3373 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3374 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3375 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3376 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3377 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3378 | |
| 3379 | static char posix_lstat__doc__[] = |
| 3380 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3381 | Like stat(path), but do not follow symbolic links."; |
| 3382 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3384 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3385 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3386 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3387 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3388 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3389 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3390 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3393 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3394 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3395 | static char posix_readlink__doc__[] = |
| 3396 | "readlink(path) -> path\n\ |
| 3397 | Return a string representing the path to which the symbolic link points."; |
| 3398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3399 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3400 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3401 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3402 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3403 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3404 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3405 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3406 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3407 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3408 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3409 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3410 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3411 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3412 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3413 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3414 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3415 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3416 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3417 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3418 | static char posix_symlink__doc__[] = |
| 3419 | "symlink(src, dst) -> None\n\ |
| 3420 | Create a symbolic link."; |
| 3421 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3422 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3423 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3424 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3425 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3426 | } |
| 3427 | #endif /* HAVE_SYMLINK */ |
| 3428 | |
| 3429 | |
| 3430 | #ifdef HAVE_TIMES |
| 3431 | #ifndef HZ |
| 3432 | #define HZ 60 /* Universal constant :-) */ |
| 3433 | #endif /* HZ */ |
| 3434 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3435 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3436 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3437 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3438 | { |
| 3439 | ULONG value = 0; |
| 3440 | |
| 3441 | Py_BEGIN_ALLOW_THREADS |
| 3442 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3443 | Py_END_ALLOW_THREADS |
| 3444 | |
| 3445 | return value; |
| 3446 | } |
| 3447 | |
| 3448 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3449 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3450 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3451 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3452 | return NULL; |
| 3453 | |
| 3454 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3455 | return Py_BuildValue("ddddd", |
| 3456 | (double)0 /* t.tms_utime / HZ */, |
| 3457 | (double)0 /* t.tms_stime / HZ */, |
| 3458 | (double)0 /* t.tms_cutime / HZ */, |
| 3459 | (double)0 /* t.tms_cstime / HZ */, |
| 3460 | (double)system_uptime() / 1000); |
| 3461 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3462 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3463 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3464 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3465 | { |
| 3466 | struct tms t; |
| 3467 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3468 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3469 | return NULL; |
| 3470 | errno = 0; |
| 3471 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3472 | if (c == (clock_t) -1) |
| 3473 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3474 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3475 | (double)t.tms_utime / HZ, |
| 3476 | (double)t.tms_stime / HZ, |
| 3477 | (double)t.tms_cutime / HZ, |
| 3478 | (double)t.tms_cstime / HZ, |
| 3479 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3480 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3481 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3482 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3483 | |
| 3484 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3485 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3486 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3487 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3488 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3489 | { |
| 3490 | FILETIME create, exit, kernel, user; |
| 3491 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3492 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3493 | return NULL; |
| 3494 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3495 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3496 | /* The fields of a FILETIME structure are the hi and lo part |
| 3497 | of a 64-bit value expressed in 100 nanosecond units. |
| 3498 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3499 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3500 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3501 | return Py_BuildValue( |
| 3502 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3503 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3504 | kernel.dwLowDateTime*1e-7), |
| 3505 | (double)(user.dwHighDateTime*429.4967296 + |
| 3506 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3507 | (double)0, |
| 3508 | (double)0, |
| 3509 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3510 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3511 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3512 | |
| 3513 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3514 | static char posix_times__doc__[] = |
| 3515 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3516 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3517 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3518 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3519 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3520 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3521 | static char posix_setsid__doc__[] = |
| 3522 | "setsid() -> None\n\ |
| 3523 | Call the system call setsid()."; |
| 3524 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3525 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3526 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3527 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3528 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3529 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3530 | if (setsid() < 0) |
| 3531 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3532 | Py_INCREF(Py_None); |
| 3533 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3534 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3535 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3536 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3537 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3538 | static char posix_setpgid__doc__[] = |
| 3539 | "setpgid(pid, pgrp) -> None\n\ |
| 3540 | Call the system call setpgid()."; |
| 3541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3543 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3544 | { |
| 3545 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3546 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3547 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3548 | if (setpgid(pid, pgrp) < 0) |
| 3549 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3550 | Py_INCREF(Py_None); |
| 3551 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3552 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3553 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3555 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3556 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3557 | static char posix_tcgetpgrp__doc__[] = |
| 3558 | "tcgetpgrp(fd) -> pgid\n\ |
| 3559 | Return the process group associated with the terminal given by a fd."; |
| 3560 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3561 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3562 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3563 | { |
| 3564 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3565 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3566 | return NULL; |
| 3567 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3568 | if (pgid < 0) |
| 3569 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3570 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3571 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3572 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3573 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3574 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3575 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3576 | static char posix_tcsetpgrp__doc__[] = |
| 3577 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3578 | Set the process group associated with the terminal given by a fd."; |
| 3579 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3580 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3581 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3582 | { |
| 3583 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3584 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3585 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3586 | if (tcsetpgrp(fd, pgid) < 0) |
| 3587 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3588 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3589 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3590 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3591 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3592 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3593 | /* Functions acting on file descriptors */ |
| 3594 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3595 | static char posix_open__doc__[] = |
| 3596 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3597 | Open a file (for low level IO)."; |
| 3598 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3599 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3600 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3601 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3602 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3603 | int flag; |
| 3604 | int mode = 0777; |
| 3605 | int fd; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3606 | if (!PyArg_ParseTuple(args, "eti|i", |
| 3607 | Py_FileSystemDefaultEncoding, &file, |
| 3608 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3609 | return NULL; |
| 3610 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3611 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3612 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3613 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3614 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3615 | return posix_error_with_allocated_filename(file); |
| 3616 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3617 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3620 | |
| 3621 | static char posix_close__doc__[] = |
| 3622 | "close(fd) -> None\n\ |
| 3623 | Close a file descriptor (for low level IO)."; |
| 3624 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3625 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3626 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3627 | { |
| 3628 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3629 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3630 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3631 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3632 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3633 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3634 | if (res < 0) |
| 3635 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3636 | Py_INCREF(Py_None); |
| 3637 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3640 | |
| 3641 | static char posix_dup__doc__[] = |
| 3642 | "dup(fd) -> fd2\n\ |
| 3643 | Return a duplicate of a file descriptor."; |
| 3644 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3645 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3646 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3647 | { |
| 3648 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3649 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3650 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3651 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3652 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3653 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3654 | if (fd < 0) |
| 3655 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3656 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3659 | |
| 3660 | static char posix_dup2__doc__[] = |
| 3661 | "dup2(fd, fd2) -> None\n\ |
| 3662 | Duplicate file descriptor."; |
| 3663 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3664 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3665 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3666 | { |
| 3667 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3668 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3669 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3670 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3671 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3672 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3673 | if (res < 0) |
| 3674 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3675 | Py_INCREF(Py_None); |
| 3676 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3679 | |
| 3680 | static char posix_lseek__doc__[] = |
| 3681 | "lseek(fd, pos, how) -> newpos\n\ |
| 3682 | Set the current position of a file descriptor."; |
| 3683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3684 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3685 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3686 | { |
| 3687 | int fd, how; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3688 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3689 | LONG_LONG pos, res; |
| 3690 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3691 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3692 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3693 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3694 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3695 | return NULL; |
| 3696 | #ifdef SEEK_SET |
| 3697 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3698 | switch (how) { |
| 3699 | case 0: how = SEEK_SET; break; |
| 3700 | case 1: how = SEEK_CUR; break; |
| 3701 | case 2: how = SEEK_END; break; |
| 3702 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3703 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3704 | |
| 3705 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3706 | pos = PyInt_AsLong(posobj); |
| 3707 | #else |
| 3708 | pos = PyLong_Check(posobj) ? |
| 3709 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3710 | #endif |
| 3711 | if (PyErr_Occurred()) |
| 3712 | return NULL; |
| 3713 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3714 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3715 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3716 | res = _lseeki64(fd, pos, how); |
| 3717 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3718 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3719 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3720 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3721 | if (res < 0) |
| 3722 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3723 | |
| 3724 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3725 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3726 | #else |
| 3727 | return PyLong_FromLongLong(res); |
| 3728 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3731 | |
| 3732 | static char posix_read__doc__[] = |
| 3733 | "read(fd, buffersize) -> string\n\ |
| 3734 | Read a file descriptor."; |
| 3735 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3736 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3737 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3738 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3739 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3740 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3741 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3742 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3743 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3744 | if (buffer == NULL) |
| 3745 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3746 | Py_BEGIN_ALLOW_THREADS |
| 3747 | n = read(fd, PyString_AsString(buffer), size); |
| 3748 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3749 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3750 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3751 | return posix_error(); |
| 3752 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3753 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3754 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3755 | return buffer; |
| 3756 | } |
| 3757 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3758 | |
| 3759 | static char posix_write__doc__[] = |
| 3760 | "write(fd, string) -> byteswritten\n\ |
| 3761 | Write a string to a file descriptor."; |
| 3762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3764 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3765 | { |
| 3766 | int fd, size; |
| 3767 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3768 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3769 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3770 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3771 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3772 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3773 | if (size < 0) |
| 3774 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3775 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3778 | |
| 3779 | static char posix_fstat__doc__[]= |
| 3780 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3781 | Like stat(), but for an open file descriptor."; |
| 3782 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3783 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3784 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3785 | { |
| 3786 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3787 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3788 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3789 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3790 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3791 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3792 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3793 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3794 | if (res != 0) |
| 3795 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3796 | |
| 3797 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3800 | |
| 3801 | static char posix_fdopen__doc__[] = |
| 3802 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3803 | Return an open file object connected to a file descriptor."; |
| 3804 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3805 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3806 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3807 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3808 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3809 | char *mode = "r"; |
| 3810 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3811 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3812 | PyObject *f; |
| 3813 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3814 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3816 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3817 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3818 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3819 | if (fp == NULL) |
| 3820 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3821 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3822 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3823 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3824 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3827 | static char posix_isatty__doc__[] = |
| 3828 | "isatty(fd) -> Boolean\n\ |
| 3829 | 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] | 3830 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3831 | |
| 3832 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3833 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3834 | { |
| 3835 | int fd; |
| 3836 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3837 | return NULL; |
| 3838 | return Py_BuildValue("i", isatty(fd)); |
| 3839 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3840 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3841 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3842 | static char posix_pipe__doc__[] = |
| 3843 | "pipe() -> (read_end, write_end)\n\ |
| 3844 | Create a pipe."; |
| 3845 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3846 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3847 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3848 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3849 | #if defined(PYOS_OS2) |
| 3850 | HFILE read, write; |
| 3851 | APIRET rc; |
| 3852 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3853 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3854 | return NULL; |
| 3855 | |
| 3856 | Py_BEGIN_ALLOW_THREADS |
| 3857 | rc = DosCreatePipe( &read, &write, 4096); |
| 3858 | Py_END_ALLOW_THREADS |
| 3859 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3860 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3861 | |
| 3862 | return Py_BuildValue("(ii)", read, write); |
| 3863 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3864 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3865 | int fds[2]; |
| 3866 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3867 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3868 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3869 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3870 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3871 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3872 | if (res != 0) |
| 3873 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3874 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3875 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3876 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3877 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3878 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3879 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3880 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3881 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3882 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3883 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3884 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3885 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3886 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 3887 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3888 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3889 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3890 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3891 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3892 | #endif /* HAVE_PIPE */ |
| 3893 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3894 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3895 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3896 | static char posix_mkfifo__doc__[] = |
| 3897 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3898 | Create a FIFO (a POSIX named pipe)."; |
| 3899 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3900 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3901 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3902 | { |
| 3903 | char *file; |
| 3904 | int mode = 0666; |
| 3905 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3906 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3907 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3908 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3909 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3910 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3911 | if (res < 0) |
| 3912 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3913 | Py_INCREF(Py_None); |
| 3914 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3915 | } |
| 3916 | #endif |
| 3917 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3918 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3919 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3920 | static char posix_ftruncate__doc__[] = |
| 3921 | "ftruncate(fd, length) -> None\n\ |
| 3922 | Truncate a file to a specified length."; |
| 3923 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3925 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3926 | { |
| 3927 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3928 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3929 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3930 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3931 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3932 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3933 | return NULL; |
| 3934 | |
| 3935 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3936 | length = PyInt_AsLong(lenobj); |
| 3937 | #else |
| 3938 | length = PyLong_Check(lenobj) ? |
| 3939 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3940 | #endif |
| 3941 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3942 | return NULL; |
| 3943 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3944 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3945 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3946 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3947 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3948 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3949 | return NULL; |
| 3950 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3951 | Py_INCREF(Py_None); |
| 3952 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3953 | } |
| 3954 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3955 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3956 | #ifdef NeXT |
| 3957 | #define HAVE_PUTENV |
| 3958 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3959 | static int |
| 3960 | putenv(char *newval) |
| 3961 | { |
| 3962 | extern char **environ; |
| 3963 | |
| 3964 | static int firstTime = 1; |
| 3965 | char **ep; |
| 3966 | char *cp; |
| 3967 | int esiz; |
| 3968 | char *np; |
| 3969 | |
| 3970 | if (!(np = strchr(newval, '='))) |
| 3971 | return 1; |
| 3972 | *np = '\0'; |
| 3973 | |
| 3974 | /* look it up */ |
| 3975 | for (ep=environ ; *ep ; ep++) |
| 3976 | { |
| 3977 | /* this should always be true... */ |
| 3978 | if (cp = strchr(*ep, '=')) |
| 3979 | { |
| 3980 | *cp = '\0'; |
| 3981 | if (!strcmp(*ep, newval)) |
| 3982 | { |
| 3983 | /* got it! */ |
| 3984 | *cp = '='; |
| 3985 | break; |
| 3986 | } |
| 3987 | *cp = '='; |
| 3988 | } |
| 3989 | else |
| 3990 | { |
| 3991 | *np = '='; |
| 3992 | return 1; |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | *np = '='; |
| 3997 | if (*ep) |
| 3998 | { |
| 3999 | /* the string was already there: |
| 4000 | just replace it with the new one */ |
| 4001 | *ep = newval; |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | /* expand environ by one */ |
| 4006 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 4007 | esiz++; |
| 4008 | if (firstTime) |
| 4009 | { |
| 4010 | char **epp; |
| 4011 | char **newenv; |
| 4012 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 4013 | return 1; |
| 4014 | |
| 4015 | for (ep=environ, epp=newenv ; *ep ;) |
| 4016 | *epp++ = *ep++; |
| 4017 | *epp++ = newval; |
| 4018 | *epp = (char *) 0; |
| 4019 | environ = newenv; |
| 4020 | } |
| 4021 | else |
| 4022 | { |
| 4023 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 4024 | return 1; |
| 4025 | environ[esiz - 2] = newval; |
| 4026 | environ[esiz - 1] = (char *) 0; |
| 4027 | firstTime = 0; |
| 4028 | } |
| 4029 | |
| 4030 | return 0; |
| 4031 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 4032 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4033 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4034 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4035 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4036 | static char posix_putenv__doc__[] = |
| 4037 | "putenv(key, value) -> None\n\ |
| 4038 | Change or add an environment variable."; |
| 4039 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4040 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4041 | * get re-set with another call for the same key. */ |
| 4042 | static PyObject *posix_putenv_garbage; |
| 4043 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4044 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4045 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4046 | { |
| 4047 | char *s1, *s2; |
| 4048 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4049 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4050 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4051 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4052 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4053 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4054 | |
| 4055 | #if defined(PYOS_OS2) |
| 4056 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4057 | APIRET rc; |
| 4058 | |
| 4059 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4060 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4061 | |
| 4062 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4063 | if (rc != NO_ERROR) |
| 4064 | return os2_error(rc); |
| 4065 | |
| 4066 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4067 | APIRET rc; |
| 4068 | |
| 4069 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4070 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4071 | |
| 4072 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4073 | if (rc != NO_ERROR) |
| 4074 | return os2_error(rc); |
| 4075 | } else { |
| 4076 | #endif |
| 4077 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4078 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4079 | len = strlen(s1) + strlen(s2) + 2; |
| 4080 | /* len includes space for a trailing \0; the size arg to |
| 4081 | PyString_FromStringAndSize does not count that */ |
| 4082 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4083 | if (newstr == NULL) |
| 4084 | return PyErr_NoMemory(); |
| 4085 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 4086 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4087 | if (putenv(new)) { |
| 4088 | posix_error(); |
| 4089 | return NULL; |
| 4090 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4091 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 4092 | * this will cause previous value to be collected. This has to |
| 4093 | * happen after the real putenv() call because the old value |
| 4094 | * was still accessible until then. */ |
| 4095 | if (PyDict_SetItem(posix_putenv_garbage, |
| 4096 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 4097 | /* really not much we can do; just leak */ |
| 4098 | PyErr_Clear(); |
| 4099 | } |
| 4100 | else { |
| 4101 | Py_DECREF(newstr); |
| 4102 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4103 | |
| 4104 | #if defined(PYOS_OS2) |
| 4105 | } |
| 4106 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4107 | Py_INCREF(Py_None); |
| 4108 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4109 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4110 | #endif /* putenv */ |
| 4111 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4112 | #ifdef HAVE_UNSETENV |
| 4113 | static char posix_unsetenv__doc__[] = |
| 4114 | "unsetenv(key) -> None\n\ |
| 4115 | Delete an environment variable."; |
| 4116 | |
| 4117 | static PyObject * |
| 4118 | posix_unsetenv(PyObject *self, PyObject *args) |
| 4119 | { |
| 4120 | char *s1; |
| 4121 | |
| 4122 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 4123 | return NULL; |
| 4124 | |
| 4125 | unsetenv(s1); |
| 4126 | |
| 4127 | /* Remove the key from posix_putenv_garbage; |
| 4128 | * this will cause it to be collected. This has to |
| 4129 | * happen after the real unsetenv() call because the |
| 4130 | * old value was still accessible until then. |
| 4131 | */ |
| 4132 | if (PyDict_DelItem(posix_putenv_garbage, |
| 4133 | PyTuple_GET_ITEM(args, 0))) { |
| 4134 | /* really not much we can do; just leak */ |
| 4135 | PyErr_Clear(); |
| 4136 | } |
| 4137 | |
| 4138 | Py_INCREF(Py_None); |
| 4139 | return Py_None; |
| 4140 | } |
| 4141 | #endif /* unsetenv */ |
| 4142 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4143 | #ifdef HAVE_STRERROR |
| 4144 | static char posix_strerror__doc__[] = |
| 4145 | "strerror(code) -> string\n\ |
| 4146 | Translate an error code to a message string."; |
| 4147 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 4148 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4149 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4150 | { |
| 4151 | int code; |
| 4152 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4153 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4154 | return NULL; |
| 4155 | message = strerror(code); |
| 4156 | if (message == NULL) { |
| 4157 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4158 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4159 | return NULL; |
| 4160 | } |
| 4161 | return PyString_FromString(message); |
| 4162 | } |
| 4163 | #endif /* strerror */ |
| 4164 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4165 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4166 | #ifdef HAVE_SYS_WAIT_H |
| 4167 | |
| 4168 | #ifdef WIFSTOPPED |
| 4169 | static char posix_WIFSTOPPED__doc__[] = |
| 4170 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4171 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4172 | |
| 4173 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4174 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4175 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4176 | #ifdef UNION_WAIT |
| 4177 | union wait status; |
| 4178 | #define status_i (status.w_status) |
| 4179 | #else |
| 4180 | int status; |
| 4181 | #define status_i status |
| 4182 | #endif |
| 4183 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4184 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4185 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4186 | { |
| 4187 | return NULL; |
| 4188 | } |
| 4189 | |
| 4190 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4191 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4192 | } |
| 4193 | #endif /* WIFSTOPPED */ |
| 4194 | |
| 4195 | #ifdef WIFSIGNALED |
| 4196 | static char posix_WIFSIGNALED__doc__[] = |
| 4197 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 4198 | 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] | 4199 | |
| 4200 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4201 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4202 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4203 | #ifdef UNION_WAIT |
| 4204 | union wait status; |
| 4205 | #define status_i (status.w_status) |
| 4206 | #else |
| 4207 | int status; |
| 4208 | #define status_i status |
| 4209 | #endif |
| 4210 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4211 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4212 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4213 | { |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | |
| 4217 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4218 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4219 | } |
| 4220 | #endif /* WIFSIGNALED */ |
| 4221 | |
| 4222 | #ifdef WIFEXITED |
| 4223 | static char posix_WIFEXITED__doc__[] = |
| 4224 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4225 | Return true if the process returning 'status' exited using the exit()\n\ |
| 4226 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4227 | |
| 4228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4229 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4230 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4231 | #ifdef UNION_WAIT |
| 4232 | union wait status; |
| 4233 | #define status_i (status.w_status) |
| 4234 | #else |
| 4235 | int status; |
| 4236 | #define status_i status |
| 4237 | #endif |
| 4238 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4239 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4240 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4241 | { |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | |
| 4245 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4246 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4247 | } |
| 4248 | #endif /* WIFEXITED */ |
| 4249 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4250 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4251 | static char posix_WEXITSTATUS__doc__[] = |
| 4252 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4253 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4254 | |
| 4255 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4256 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4257 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4258 | #ifdef UNION_WAIT |
| 4259 | union wait status; |
| 4260 | #define status_i (status.w_status) |
| 4261 | #else |
| 4262 | int status; |
| 4263 | #define status_i status |
| 4264 | #endif |
| 4265 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4266 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4267 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4268 | { |
| 4269 | return NULL; |
| 4270 | } |
| 4271 | |
| 4272 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4273 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4274 | } |
| 4275 | #endif /* WEXITSTATUS */ |
| 4276 | |
| 4277 | #ifdef WTERMSIG |
| 4278 | static char posix_WTERMSIG__doc__[] = |
| 4279 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4280 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4281 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4282 | |
| 4283 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4284 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4285 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4286 | #ifdef UNION_WAIT |
| 4287 | union wait status; |
| 4288 | #define status_i (status.w_status) |
| 4289 | #else |
| 4290 | int status; |
| 4291 | #define status_i status |
| 4292 | #endif |
| 4293 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4294 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4295 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4296 | { |
| 4297 | return NULL; |
| 4298 | } |
| 4299 | |
| 4300 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4301 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4302 | } |
| 4303 | #endif /* WTERMSIG */ |
| 4304 | |
| 4305 | #ifdef WSTOPSIG |
| 4306 | static char posix_WSTOPSIG__doc__[] = |
| 4307 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4308 | 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] | 4309 | |
| 4310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4311 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4312 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4313 | #ifdef UNION_WAIT |
| 4314 | union wait status; |
| 4315 | #define status_i (status.w_status) |
| 4316 | #else |
| 4317 | int status; |
| 4318 | #define status_i status |
| 4319 | #endif |
| 4320 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4321 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4322 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4323 | { |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | |
| 4327 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4328 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4329 | } |
| 4330 | #endif /* WSTOPSIG */ |
| 4331 | |
| 4332 | #endif /* HAVE_SYS_WAIT_H */ |
| 4333 | |
| 4334 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4335 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4336 | #ifdef _SCO_DS |
| 4337 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4338 | needed definitions in sys/statvfs.h */ |
| 4339 | #define _SVID3 |
| 4340 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4341 | #include <sys/statvfs.h> |
| 4342 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4343 | static PyObject* |
| 4344 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 4345 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 4346 | if (v == NULL) |
| 4347 | return NULL; |
| 4348 | |
| 4349 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4350 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4351 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4352 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 4353 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 4354 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 4355 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 4356 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 4357 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 4358 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4359 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4360 | #else |
| 4361 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4362 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4363 | PyStructSequence_SET_ITEM(v, 2, |
| 4364 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
| 4365 | PyStructSequence_SET_ITEM(v, 3, |
| 4366 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 4367 | PyStructSequence_SET_ITEM(v, 4, |
| 4368 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
| 4369 | PyStructSequence_SET_ITEM(v, 5, |
| 4370 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
| 4371 | PyStructSequence_SET_ITEM(v, 6, |
| 4372 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
| 4373 | PyStructSequence_SET_ITEM(v, 7, |
| 4374 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 4375 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4376 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4377 | #endif |
| 4378 | |
| 4379 | return v; |
| 4380 | } |
| 4381 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4382 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4383 | "fstatvfs(fd) -> \n\ |
| 4384 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4385 | Perform an fstatvfs system call on the given fd."; |
| 4386 | |
| 4387 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4388 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4389 | { |
| 4390 | int fd, res; |
| 4391 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4392 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4393 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4394 | return NULL; |
| 4395 | Py_BEGIN_ALLOW_THREADS |
| 4396 | res = fstatvfs(fd, &st); |
| 4397 | Py_END_ALLOW_THREADS |
| 4398 | if (res != 0) |
| 4399 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4400 | |
| 4401 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4402 | } |
| 4403 | #endif /* HAVE_FSTATVFS */ |
| 4404 | |
| 4405 | |
| 4406 | #if defined(HAVE_STATVFS) |
| 4407 | #include <sys/statvfs.h> |
| 4408 | |
| 4409 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4410 | "statvfs(path) -> \n\ |
| 4411 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4412 | Perform a statvfs system call on the given path."; |
| 4413 | |
| 4414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4415 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4416 | { |
| 4417 | char *path; |
| 4418 | int res; |
| 4419 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4420 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4421 | return NULL; |
| 4422 | Py_BEGIN_ALLOW_THREADS |
| 4423 | res = statvfs(path, &st); |
| 4424 | Py_END_ALLOW_THREADS |
| 4425 | if (res != 0) |
| 4426 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4427 | |
| 4428 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4429 | } |
| 4430 | #endif /* HAVE_STATVFS */ |
| 4431 | |
| 4432 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4433 | #ifdef HAVE_TEMPNAM |
| 4434 | static char posix_tempnam__doc__[] = "\ |
| 4435 | tempnam([dir[, prefix]]) -> string\n\ |
| 4436 | Return a unique name for a temporary file.\n\ |
| 4437 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4438 | or None if not needed."; |
| 4439 | |
| 4440 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4441 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4442 | { |
| 4443 | PyObject *result = NULL; |
| 4444 | char *dir = NULL; |
| 4445 | char *pfx = NULL; |
| 4446 | char *name; |
| 4447 | |
| 4448 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4449 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4450 | |
| 4451 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4452 | "tempnam is a potential security risk to your program") < 0) |
| 4453 | return NULL; |
| 4454 | |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4455 | #ifdef MS_WIN32 |
| 4456 | name = _tempnam(dir, pfx); |
| 4457 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4458 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4459 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4460 | if (name == NULL) |
| 4461 | return PyErr_NoMemory(); |
| 4462 | result = PyString_FromString(name); |
| 4463 | free(name); |
| 4464 | return result; |
| 4465 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4466 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4467 | |
| 4468 | |
| 4469 | #ifdef HAVE_TMPFILE |
| 4470 | static char posix_tmpfile__doc__[] = "\ |
| 4471 | tmpfile() -> file object\n\ |
| 4472 | Create a temporary file with no directory entries."; |
| 4473 | |
| 4474 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4475 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4476 | { |
| 4477 | FILE *fp; |
| 4478 | |
| 4479 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4480 | return NULL; |
| 4481 | fp = tmpfile(); |
| 4482 | if (fp == NULL) |
| 4483 | return posix_error(); |
| 4484 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4485 | } |
| 4486 | #endif |
| 4487 | |
| 4488 | |
| 4489 | #ifdef HAVE_TMPNAM |
| 4490 | static char posix_tmpnam__doc__[] = "\ |
| 4491 | tmpnam() -> string\n\ |
| 4492 | Return a unique name for a temporary file."; |
| 4493 | |
| 4494 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4495 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4496 | { |
| 4497 | char buffer[L_tmpnam]; |
| 4498 | char *name; |
| 4499 | |
| 4500 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4501 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4502 | |
| 4503 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4504 | "tmpnam is a potential security risk to your program") < 0) |
| 4505 | return NULL; |
| 4506 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4507 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4508 | name = tmpnam_r(buffer); |
| 4509 | #else |
| 4510 | name = tmpnam(buffer); |
| 4511 | #endif |
| 4512 | if (name == NULL) { |
| 4513 | PyErr_SetObject(PyExc_OSError, |
| 4514 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4515 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4516 | "unexpected NULL from tmpnam_r" |
| 4517 | #else |
| 4518 | "unexpected NULL from tmpnam" |
| 4519 | #endif |
| 4520 | )); |
| 4521 | return NULL; |
| 4522 | } |
| 4523 | return PyString_FromString(buffer); |
| 4524 | } |
| 4525 | #endif |
| 4526 | |
| 4527 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4528 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4529 | * It maps strings representing configuration variable names to |
| 4530 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4531 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4532 | * rarely-used constants. There are three separate tables that use |
| 4533 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4534 | * |
| 4535 | * This code is always included, even if none of the interfaces that |
| 4536 | * need it are included. The #if hackery needed to avoid it would be |
| 4537 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4538 | */ |
| 4539 | struct constdef { |
| 4540 | char *name; |
| 4541 | long value; |
| 4542 | }; |
| 4543 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4544 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4545 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4546 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4547 | { |
| 4548 | if (PyInt_Check(arg)) { |
| 4549 | *valuep = PyInt_AS_LONG(arg); |
| 4550 | return 1; |
| 4551 | } |
| 4552 | if (PyString_Check(arg)) { |
| 4553 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4554 | size_t lo = 0; |
| 4555 | size_t mid; |
| 4556 | size_t hi = tablesize; |
| 4557 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4558 | char *confname = PyString_AS_STRING(arg); |
| 4559 | while (lo < hi) { |
| 4560 | mid = (lo + hi) / 2; |
| 4561 | cmp = strcmp(confname, table[mid].name); |
| 4562 | if (cmp < 0) |
| 4563 | hi = mid; |
| 4564 | else if (cmp > 0) |
| 4565 | lo = mid + 1; |
| 4566 | else { |
| 4567 | *valuep = table[mid].value; |
| 4568 | return 1; |
| 4569 | } |
| 4570 | } |
| 4571 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4572 | } |
| 4573 | else |
| 4574 | PyErr_SetString(PyExc_TypeError, |
| 4575 | "configuration names must be strings or integers"); |
| 4576 | return 0; |
| 4577 | } |
| 4578 | |
| 4579 | |
| 4580 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4581 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4582 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4583 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4584 | #endif |
| 4585 | #ifdef _PC_ABI_ASYNC_IO |
| 4586 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4587 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4588 | #ifdef _PC_ASYNC_IO |
| 4589 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4590 | #endif |
| 4591 | #ifdef _PC_CHOWN_RESTRICTED |
| 4592 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4593 | #endif |
| 4594 | #ifdef _PC_FILESIZEBITS |
| 4595 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4596 | #endif |
| 4597 | #ifdef _PC_LAST |
| 4598 | {"PC_LAST", _PC_LAST}, |
| 4599 | #endif |
| 4600 | #ifdef _PC_LINK_MAX |
| 4601 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4602 | #endif |
| 4603 | #ifdef _PC_MAX_CANON |
| 4604 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4605 | #endif |
| 4606 | #ifdef _PC_MAX_INPUT |
| 4607 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4608 | #endif |
| 4609 | #ifdef _PC_NAME_MAX |
| 4610 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4611 | #endif |
| 4612 | #ifdef _PC_NO_TRUNC |
| 4613 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4614 | #endif |
| 4615 | #ifdef _PC_PATH_MAX |
| 4616 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4617 | #endif |
| 4618 | #ifdef _PC_PIPE_BUF |
| 4619 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4620 | #endif |
| 4621 | #ifdef _PC_PRIO_IO |
| 4622 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4623 | #endif |
| 4624 | #ifdef _PC_SOCK_MAXBUF |
| 4625 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4626 | #endif |
| 4627 | #ifdef _PC_SYNC_IO |
| 4628 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4629 | #endif |
| 4630 | #ifdef _PC_VDISABLE |
| 4631 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4632 | #endif |
| 4633 | }; |
| 4634 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4635 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4636 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4637 | { |
| 4638 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4639 | sizeof(posix_constants_pathconf) |
| 4640 | / sizeof(struct constdef)); |
| 4641 | } |
| 4642 | #endif |
| 4643 | |
| 4644 | #ifdef HAVE_FPATHCONF |
| 4645 | static char posix_fpathconf__doc__[] = "\ |
| 4646 | fpathconf(fd, name) -> integer\n\ |
| 4647 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4648 | If there is no limit, return -1."; |
| 4649 | |
| 4650 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4651 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4652 | { |
| 4653 | PyObject *result = NULL; |
| 4654 | int name, fd; |
| 4655 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4656 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4657 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4658 | long limit; |
| 4659 | |
| 4660 | errno = 0; |
| 4661 | limit = fpathconf(fd, name); |
| 4662 | if (limit == -1 && errno != 0) |
| 4663 | posix_error(); |
| 4664 | else |
| 4665 | result = PyInt_FromLong(limit); |
| 4666 | } |
| 4667 | return result; |
| 4668 | } |
| 4669 | #endif |
| 4670 | |
| 4671 | |
| 4672 | #ifdef HAVE_PATHCONF |
| 4673 | static char posix_pathconf__doc__[] = "\ |
| 4674 | pathconf(path, name) -> integer\n\ |
| 4675 | Return the configuration limit name for the file or directory path.\n\ |
| 4676 | If there is no limit, return -1."; |
| 4677 | |
| 4678 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4679 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4680 | { |
| 4681 | PyObject *result = NULL; |
| 4682 | int name; |
| 4683 | char *path; |
| 4684 | |
| 4685 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4686 | conv_path_confname, &name)) { |
| 4687 | long limit; |
| 4688 | |
| 4689 | errno = 0; |
| 4690 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4691 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4692 | if (errno == EINVAL) |
| 4693 | /* could be a path or name problem */ |
| 4694 | posix_error(); |
| 4695 | else |
| 4696 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4697 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4698 | else |
| 4699 | result = PyInt_FromLong(limit); |
| 4700 | } |
| 4701 | return result; |
| 4702 | } |
| 4703 | #endif |
| 4704 | |
| 4705 | #ifdef HAVE_CONFSTR |
| 4706 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4707 | #ifdef _CS_ARCHITECTURE |
| 4708 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4709 | #endif |
| 4710 | #ifdef _CS_HOSTNAME |
| 4711 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4712 | #endif |
| 4713 | #ifdef _CS_HW_PROVIDER |
| 4714 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4715 | #endif |
| 4716 | #ifdef _CS_HW_SERIAL |
| 4717 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4718 | #endif |
| 4719 | #ifdef _CS_INITTAB_NAME |
| 4720 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4721 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4722 | #ifdef _CS_LFS64_CFLAGS |
| 4723 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4724 | #endif |
| 4725 | #ifdef _CS_LFS64_LDFLAGS |
| 4726 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4727 | #endif |
| 4728 | #ifdef _CS_LFS64_LIBS |
| 4729 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4730 | #endif |
| 4731 | #ifdef _CS_LFS64_LINTFLAGS |
| 4732 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4733 | #endif |
| 4734 | #ifdef _CS_LFS_CFLAGS |
| 4735 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4736 | #endif |
| 4737 | #ifdef _CS_LFS_LDFLAGS |
| 4738 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4739 | #endif |
| 4740 | #ifdef _CS_LFS_LIBS |
| 4741 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4742 | #endif |
| 4743 | #ifdef _CS_LFS_LINTFLAGS |
| 4744 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4745 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4746 | #ifdef _CS_MACHINE |
| 4747 | {"CS_MACHINE", _CS_MACHINE}, |
| 4748 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4749 | #ifdef _CS_PATH |
| 4750 | {"CS_PATH", _CS_PATH}, |
| 4751 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4752 | #ifdef _CS_RELEASE |
| 4753 | {"CS_RELEASE", _CS_RELEASE}, |
| 4754 | #endif |
| 4755 | #ifdef _CS_SRPC_DOMAIN |
| 4756 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4757 | #endif |
| 4758 | #ifdef _CS_SYSNAME |
| 4759 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4760 | #endif |
| 4761 | #ifdef _CS_VERSION |
| 4762 | {"CS_VERSION", _CS_VERSION}, |
| 4763 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4764 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4765 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4766 | #endif |
| 4767 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4768 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4769 | #endif |
| 4770 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4771 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4772 | #endif |
| 4773 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4774 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4775 | #endif |
| 4776 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4777 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4778 | #endif |
| 4779 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4780 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4781 | #endif |
| 4782 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4783 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4784 | #endif |
| 4785 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4786 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4787 | #endif |
| 4788 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4789 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4790 | #endif |
| 4791 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4792 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4793 | #endif |
| 4794 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4795 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4796 | #endif |
| 4797 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4798 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4799 | #endif |
| 4800 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4801 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4802 | #endif |
| 4803 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4804 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4805 | #endif |
| 4806 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4807 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4808 | #endif |
| 4809 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4810 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4811 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4812 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4813 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4814 | #endif |
| 4815 | #ifdef _MIPS_CS_BASE |
| 4816 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4817 | #endif |
| 4818 | #ifdef _MIPS_CS_HOSTID |
| 4819 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4820 | #endif |
| 4821 | #ifdef _MIPS_CS_HW_NAME |
| 4822 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4823 | #endif |
| 4824 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4825 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4826 | #endif |
| 4827 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4828 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4829 | #endif |
| 4830 | #ifdef _MIPS_CS_OSREL_MIN |
| 4831 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4832 | #endif |
| 4833 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4834 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4835 | #endif |
| 4836 | #ifdef _MIPS_CS_OS_NAME |
| 4837 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4838 | #endif |
| 4839 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4840 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4841 | #endif |
| 4842 | #ifdef _MIPS_CS_PROCESSORS |
| 4843 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4844 | #endif |
| 4845 | #ifdef _MIPS_CS_SERIAL |
| 4846 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4847 | #endif |
| 4848 | #ifdef _MIPS_CS_VENDOR |
| 4849 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4850 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4851 | }; |
| 4852 | |
| 4853 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4854 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4855 | { |
| 4856 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4857 | sizeof(posix_constants_confstr) |
| 4858 | / sizeof(struct constdef)); |
| 4859 | } |
| 4860 | |
| 4861 | static char posix_confstr__doc__[] = "\ |
| 4862 | confstr(name) -> string\n\ |
| 4863 | Return a string-valued system configuration variable."; |
| 4864 | |
| 4865 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4866 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4867 | { |
| 4868 | PyObject *result = NULL; |
| 4869 | int name; |
| 4870 | char buffer[64]; |
| 4871 | |
| 4872 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4873 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4874 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4875 | errno = 0; |
| 4876 | if (len == 0) { |
| 4877 | if (errno != 0) |
| 4878 | posix_error(); |
| 4879 | else |
| 4880 | result = PyString_FromString(""); |
| 4881 | } |
| 4882 | else { |
| 4883 | if (len >= sizeof(buffer)) { |
| 4884 | result = PyString_FromStringAndSize(NULL, len); |
| 4885 | if (result != NULL) |
| 4886 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4887 | } |
| 4888 | else |
| 4889 | result = PyString_FromString(buffer); |
| 4890 | } |
| 4891 | } |
| 4892 | return result; |
| 4893 | } |
| 4894 | #endif |
| 4895 | |
| 4896 | |
| 4897 | #ifdef HAVE_SYSCONF |
| 4898 | static struct constdef posix_constants_sysconf[] = { |
| 4899 | #ifdef _SC_2_CHAR_TERM |
| 4900 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4901 | #endif |
| 4902 | #ifdef _SC_2_C_BIND |
| 4903 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4904 | #endif |
| 4905 | #ifdef _SC_2_C_DEV |
| 4906 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4907 | #endif |
| 4908 | #ifdef _SC_2_C_VERSION |
| 4909 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4910 | #endif |
| 4911 | #ifdef _SC_2_FORT_DEV |
| 4912 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4913 | #endif |
| 4914 | #ifdef _SC_2_FORT_RUN |
| 4915 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4916 | #endif |
| 4917 | #ifdef _SC_2_LOCALEDEF |
| 4918 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4919 | #endif |
| 4920 | #ifdef _SC_2_SW_DEV |
| 4921 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4922 | #endif |
| 4923 | #ifdef _SC_2_UPE |
| 4924 | {"SC_2_UPE", _SC_2_UPE}, |
| 4925 | #endif |
| 4926 | #ifdef _SC_2_VERSION |
| 4927 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4928 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4929 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4930 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4931 | #endif |
| 4932 | #ifdef _SC_ACL |
| 4933 | {"SC_ACL", _SC_ACL}, |
| 4934 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4935 | #ifdef _SC_AIO_LISTIO_MAX |
| 4936 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4937 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4938 | #ifdef _SC_AIO_MAX |
| 4939 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4940 | #endif |
| 4941 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4942 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4943 | #endif |
| 4944 | #ifdef _SC_ARG_MAX |
| 4945 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4946 | #endif |
| 4947 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4948 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4949 | #endif |
| 4950 | #ifdef _SC_ATEXIT_MAX |
| 4951 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4952 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4953 | #ifdef _SC_AUDIT |
| 4954 | {"SC_AUDIT", _SC_AUDIT}, |
| 4955 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4956 | #ifdef _SC_AVPHYS_PAGES |
| 4957 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4958 | #endif |
| 4959 | #ifdef _SC_BC_BASE_MAX |
| 4960 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4961 | #endif |
| 4962 | #ifdef _SC_BC_DIM_MAX |
| 4963 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4964 | #endif |
| 4965 | #ifdef _SC_BC_SCALE_MAX |
| 4966 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4967 | #endif |
| 4968 | #ifdef _SC_BC_STRING_MAX |
| 4969 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4970 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4971 | #ifdef _SC_CAP |
| 4972 | {"SC_CAP", _SC_CAP}, |
| 4973 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4974 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4975 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4976 | #endif |
| 4977 | #ifdef _SC_CHAR_BIT |
| 4978 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4979 | #endif |
| 4980 | #ifdef _SC_CHAR_MAX |
| 4981 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4982 | #endif |
| 4983 | #ifdef _SC_CHAR_MIN |
| 4984 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4985 | #endif |
| 4986 | #ifdef _SC_CHILD_MAX |
| 4987 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4988 | #endif |
| 4989 | #ifdef _SC_CLK_TCK |
| 4990 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4991 | #endif |
| 4992 | #ifdef _SC_COHER_BLKSZ |
| 4993 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4994 | #endif |
| 4995 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4996 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4997 | #endif |
| 4998 | #ifdef _SC_DCACHE_ASSOC |
| 4999 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 5000 | #endif |
| 5001 | #ifdef _SC_DCACHE_BLKSZ |
| 5002 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 5003 | #endif |
| 5004 | #ifdef _SC_DCACHE_LINESZ |
| 5005 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 5006 | #endif |
| 5007 | #ifdef _SC_DCACHE_SZ |
| 5008 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 5009 | #endif |
| 5010 | #ifdef _SC_DCACHE_TBLKSZ |
| 5011 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 5012 | #endif |
| 5013 | #ifdef _SC_DELAYTIMER_MAX |
| 5014 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 5015 | #endif |
| 5016 | #ifdef _SC_EQUIV_CLASS_MAX |
| 5017 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 5018 | #endif |
| 5019 | #ifdef _SC_EXPR_NEST_MAX |
| 5020 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 5021 | #endif |
| 5022 | #ifdef _SC_FSYNC |
| 5023 | {"SC_FSYNC", _SC_FSYNC}, |
| 5024 | #endif |
| 5025 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5026 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5027 | #endif |
| 5028 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5029 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5030 | #endif |
| 5031 | #ifdef _SC_ICACHE_ASSOC |
| 5032 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5033 | #endif |
| 5034 | #ifdef _SC_ICACHE_BLKSZ |
| 5035 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5036 | #endif |
| 5037 | #ifdef _SC_ICACHE_LINESZ |
| 5038 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5039 | #endif |
| 5040 | #ifdef _SC_ICACHE_SZ |
| 5041 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5042 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5043 | #ifdef _SC_INF |
| 5044 | {"SC_INF", _SC_INF}, |
| 5045 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5046 | #ifdef _SC_INT_MAX |
| 5047 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5048 | #endif |
| 5049 | #ifdef _SC_INT_MIN |
| 5050 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5051 | #endif |
| 5052 | #ifdef _SC_IOV_MAX |
| 5053 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5054 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5055 | #ifdef _SC_IP_SECOPTS |
| 5056 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5057 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5058 | #ifdef _SC_JOB_CONTROL |
| 5059 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5060 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5061 | #ifdef _SC_KERN_POINTERS |
| 5062 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5063 | #endif |
| 5064 | #ifdef _SC_KERN_SIM |
| 5065 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5066 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5067 | #ifdef _SC_LINE_MAX |
| 5068 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5069 | #endif |
| 5070 | #ifdef _SC_LOGIN_NAME_MAX |
| 5071 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5072 | #endif |
| 5073 | #ifdef _SC_LOGNAME_MAX |
| 5074 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5075 | #endif |
| 5076 | #ifdef _SC_LONG_BIT |
| 5077 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5078 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5079 | #ifdef _SC_MAC |
| 5080 | {"SC_MAC", _SC_MAC}, |
| 5081 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5082 | #ifdef _SC_MAPPED_FILES |
| 5083 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5084 | #endif |
| 5085 | #ifdef _SC_MAXPID |
| 5086 | {"SC_MAXPID", _SC_MAXPID}, |
| 5087 | #endif |
| 5088 | #ifdef _SC_MB_LEN_MAX |
| 5089 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5090 | #endif |
| 5091 | #ifdef _SC_MEMLOCK |
| 5092 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5093 | #endif |
| 5094 | #ifdef _SC_MEMLOCK_RANGE |
| 5095 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5096 | #endif |
| 5097 | #ifdef _SC_MEMORY_PROTECTION |
| 5098 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5099 | #endif |
| 5100 | #ifdef _SC_MESSAGE_PASSING |
| 5101 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5102 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5103 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5104 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5106 | #ifdef _SC_MQ_OPEN_MAX |
| 5107 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5108 | #endif |
| 5109 | #ifdef _SC_MQ_PRIO_MAX |
| 5110 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5111 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5112 | #ifdef _SC_NACLS_MAX |
| 5113 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5114 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5115 | #ifdef _SC_NGROUPS_MAX |
| 5116 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5117 | #endif |
| 5118 | #ifdef _SC_NL_ARGMAX |
| 5119 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5120 | #endif |
| 5121 | #ifdef _SC_NL_LANGMAX |
| 5122 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5123 | #endif |
| 5124 | #ifdef _SC_NL_MSGMAX |
| 5125 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5126 | #endif |
| 5127 | #ifdef _SC_NL_NMAX |
| 5128 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5129 | #endif |
| 5130 | #ifdef _SC_NL_SETMAX |
| 5131 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5132 | #endif |
| 5133 | #ifdef _SC_NL_TEXTMAX |
| 5134 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5135 | #endif |
| 5136 | #ifdef _SC_NPROCESSORS_CONF |
| 5137 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5138 | #endif |
| 5139 | #ifdef _SC_NPROCESSORS_ONLN |
| 5140 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5141 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5142 | #ifdef _SC_NPROC_CONF |
| 5143 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5144 | #endif |
| 5145 | #ifdef _SC_NPROC_ONLN |
| 5146 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5147 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5148 | #ifdef _SC_NZERO |
| 5149 | {"SC_NZERO", _SC_NZERO}, |
| 5150 | #endif |
| 5151 | #ifdef _SC_OPEN_MAX |
| 5152 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5153 | #endif |
| 5154 | #ifdef _SC_PAGESIZE |
| 5155 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5156 | #endif |
| 5157 | #ifdef _SC_PAGE_SIZE |
| 5158 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5159 | #endif |
| 5160 | #ifdef _SC_PASS_MAX |
| 5161 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5162 | #endif |
| 5163 | #ifdef _SC_PHYS_PAGES |
| 5164 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5165 | #endif |
| 5166 | #ifdef _SC_PII |
| 5167 | {"SC_PII", _SC_PII}, |
| 5168 | #endif |
| 5169 | #ifdef _SC_PII_INTERNET |
| 5170 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5171 | #endif |
| 5172 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5173 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5174 | #endif |
| 5175 | #ifdef _SC_PII_INTERNET_STREAM |
| 5176 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5177 | #endif |
| 5178 | #ifdef _SC_PII_OSI |
| 5179 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 5180 | #endif |
| 5181 | #ifdef _SC_PII_OSI_CLTS |
| 5182 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 5183 | #endif |
| 5184 | #ifdef _SC_PII_OSI_COTS |
| 5185 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 5186 | #endif |
| 5187 | #ifdef _SC_PII_OSI_M |
| 5188 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 5189 | #endif |
| 5190 | #ifdef _SC_PII_SOCKET |
| 5191 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 5192 | #endif |
| 5193 | #ifdef _SC_PII_XTI |
| 5194 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 5195 | #endif |
| 5196 | #ifdef _SC_POLL |
| 5197 | {"SC_POLL", _SC_POLL}, |
| 5198 | #endif |
| 5199 | #ifdef _SC_PRIORITIZED_IO |
| 5200 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 5201 | #endif |
| 5202 | #ifdef _SC_PRIORITY_SCHEDULING |
| 5203 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 5204 | #endif |
| 5205 | #ifdef _SC_REALTIME_SIGNALS |
| 5206 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 5207 | #endif |
| 5208 | #ifdef _SC_RE_DUP_MAX |
| 5209 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 5210 | #endif |
| 5211 | #ifdef _SC_RTSIG_MAX |
| 5212 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 5213 | #endif |
| 5214 | #ifdef _SC_SAVED_IDS |
| 5215 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 5216 | #endif |
| 5217 | #ifdef _SC_SCHAR_MAX |
| 5218 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 5219 | #endif |
| 5220 | #ifdef _SC_SCHAR_MIN |
| 5221 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 5222 | #endif |
| 5223 | #ifdef _SC_SELECT |
| 5224 | {"SC_SELECT", _SC_SELECT}, |
| 5225 | #endif |
| 5226 | #ifdef _SC_SEMAPHORES |
| 5227 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 5228 | #endif |
| 5229 | #ifdef _SC_SEM_NSEMS_MAX |
| 5230 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 5231 | #endif |
| 5232 | #ifdef _SC_SEM_VALUE_MAX |
| 5233 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 5234 | #endif |
| 5235 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 5236 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 5237 | #endif |
| 5238 | #ifdef _SC_SHRT_MAX |
| 5239 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 5240 | #endif |
| 5241 | #ifdef _SC_SHRT_MIN |
| 5242 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 5243 | #endif |
| 5244 | #ifdef _SC_SIGQUEUE_MAX |
| 5245 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5246 | #endif |
| 5247 | #ifdef _SC_SIGRT_MAX |
| 5248 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5249 | #endif |
| 5250 | #ifdef _SC_SIGRT_MIN |
| 5251 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5252 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5253 | #ifdef _SC_SOFTPOWER |
| 5254 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5255 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5256 | #ifdef _SC_SPLIT_CACHE |
| 5257 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5258 | #endif |
| 5259 | #ifdef _SC_SSIZE_MAX |
| 5260 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5261 | #endif |
| 5262 | #ifdef _SC_STACK_PROT |
| 5263 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5264 | #endif |
| 5265 | #ifdef _SC_STREAM_MAX |
| 5266 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5267 | #endif |
| 5268 | #ifdef _SC_SYNCHRONIZED_IO |
| 5269 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5270 | #endif |
| 5271 | #ifdef _SC_THREADS |
| 5272 | {"SC_THREADS", _SC_THREADS}, |
| 5273 | #endif |
| 5274 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5275 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5276 | #endif |
| 5277 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5278 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5279 | #endif |
| 5280 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5281 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5282 | #endif |
| 5283 | #ifdef _SC_THREAD_KEYS_MAX |
| 5284 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5285 | #endif |
| 5286 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5287 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5288 | #endif |
| 5289 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5290 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5291 | #endif |
| 5292 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5293 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5294 | #endif |
| 5295 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5296 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5297 | #endif |
| 5298 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5299 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5300 | #endif |
| 5301 | #ifdef _SC_THREAD_STACK_MIN |
| 5302 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5303 | #endif |
| 5304 | #ifdef _SC_THREAD_THREADS_MAX |
| 5305 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5306 | #endif |
| 5307 | #ifdef _SC_TIMERS |
| 5308 | {"SC_TIMERS", _SC_TIMERS}, |
| 5309 | #endif |
| 5310 | #ifdef _SC_TIMER_MAX |
| 5311 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5312 | #endif |
| 5313 | #ifdef _SC_TTY_NAME_MAX |
| 5314 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5315 | #endif |
| 5316 | #ifdef _SC_TZNAME_MAX |
| 5317 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5318 | #endif |
| 5319 | #ifdef _SC_T_IOV_MAX |
| 5320 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5321 | #endif |
| 5322 | #ifdef _SC_UCHAR_MAX |
| 5323 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5324 | #endif |
| 5325 | #ifdef _SC_UINT_MAX |
| 5326 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5327 | #endif |
| 5328 | #ifdef _SC_UIO_MAXIOV |
| 5329 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5330 | #endif |
| 5331 | #ifdef _SC_ULONG_MAX |
| 5332 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5333 | #endif |
| 5334 | #ifdef _SC_USHRT_MAX |
| 5335 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5336 | #endif |
| 5337 | #ifdef _SC_VERSION |
| 5338 | {"SC_VERSION", _SC_VERSION}, |
| 5339 | #endif |
| 5340 | #ifdef _SC_WORD_BIT |
| 5341 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5342 | #endif |
| 5343 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5344 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5345 | #endif |
| 5346 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5347 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5348 | #endif |
| 5349 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5350 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5351 | #endif |
| 5352 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5353 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5354 | #endif |
| 5355 | #ifdef _SC_XOPEN_CRYPT |
| 5356 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5357 | #endif |
| 5358 | #ifdef _SC_XOPEN_ENH_I18N |
| 5359 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5360 | #endif |
| 5361 | #ifdef _SC_XOPEN_LEGACY |
| 5362 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5363 | #endif |
| 5364 | #ifdef _SC_XOPEN_REALTIME |
| 5365 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5366 | #endif |
| 5367 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5368 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5369 | #endif |
| 5370 | #ifdef _SC_XOPEN_SHM |
| 5371 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5372 | #endif |
| 5373 | #ifdef _SC_XOPEN_UNIX |
| 5374 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5375 | #endif |
| 5376 | #ifdef _SC_XOPEN_VERSION |
| 5377 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5378 | #endif |
| 5379 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5380 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5381 | #endif |
| 5382 | #ifdef _SC_XOPEN_XPG2 |
| 5383 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5384 | #endif |
| 5385 | #ifdef _SC_XOPEN_XPG3 |
| 5386 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5387 | #endif |
| 5388 | #ifdef _SC_XOPEN_XPG4 |
| 5389 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5390 | #endif |
| 5391 | }; |
| 5392 | |
| 5393 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5394 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5395 | { |
| 5396 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5397 | sizeof(posix_constants_sysconf) |
| 5398 | / sizeof(struct constdef)); |
| 5399 | } |
| 5400 | |
| 5401 | static char posix_sysconf__doc__[] = "\ |
| 5402 | sysconf(name) -> integer\n\ |
| 5403 | Return an integer-valued system configuration variable."; |
| 5404 | |
| 5405 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5406 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5407 | { |
| 5408 | PyObject *result = NULL; |
| 5409 | int name; |
| 5410 | |
| 5411 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5412 | int value; |
| 5413 | |
| 5414 | errno = 0; |
| 5415 | value = sysconf(name); |
| 5416 | if (value == -1 && errno != 0) |
| 5417 | posix_error(); |
| 5418 | else |
| 5419 | result = PyInt_FromLong(value); |
| 5420 | } |
| 5421 | return result; |
| 5422 | } |
| 5423 | #endif |
| 5424 | |
| 5425 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5426 | /* This code is used to ensure that the tables of configuration value names |
| 5427 | * are in sorted order as required by conv_confname(), and also to build the |
| 5428 | * the exported dictionaries that are used to publish information about the |
| 5429 | * names available on the host platform. |
| 5430 | * |
| 5431 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5432 | * when used, even for platforms we're not able to test on. It also makes |
| 5433 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5434 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5435 | |
| 5436 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5437 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5438 | { |
| 5439 | const struct constdef *c1 = |
| 5440 | (const struct constdef *) v1; |
| 5441 | const struct constdef *c2 = |
| 5442 | (const struct constdef *) v2; |
| 5443 | |
| 5444 | return strcmp(c1->name, c2->name); |
| 5445 | } |
| 5446 | |
| 5447 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5448 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5449 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5450 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5451 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5452 | size_t i; |
| 5453 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5454 | |
| 5455 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5456 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5457 | if (d == NULL) |
| 5458 | return -1; |
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 | for (i=0; i < tablesize; ++i) { |
| 5461 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5462 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5463 | Py_XDECREF(o); |
| 5464 | Py_DECREF(d); |
| 5465 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5466 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5467 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5468 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5469 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5470 | Py_DECREF(d); |
| 5471 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5474 | /* Return -1 on failure, 0 on success. */ |
| 5475 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5476 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5477 | { |
| 5478 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5479 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5480 | sizeof(posix_constants_pathconf) |
| 5481 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5482 | "pathconf_names", moddict)) |
| 5483 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5484 | #endif |
| 5485 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5486 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5487 | sizeof(posix_constants_confstr) |
| 5488 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5489 | "confstr_names", moddict)) |
| 5490 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5491 | #endif |
| 5492 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5493 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5494 | sizeof(posix_constants_sysconf) |
| 5495 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5496 | "sysconf_names", moddict)) |
| 5497 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5498 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5499 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5500 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5501 | |
| 5502 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5503 | static char posix_abort__doc__[] = "\ |
| 5504 | abort() -> does not return!\n\ |
| 5505 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5506 | in the hardest way possible on the hosting operating system."; |
| 5507 | |
| 5508 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5509 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5510 | { |
| 5511 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5512 | return NULL; |
| 5513 | abort(); |
| 5514 | /*NOTREACHED*/ |
| 5515 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5516 | return NULL; |
| 5517 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5518 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5519 | #ifdef MS_WIN32 |
| 5520 | static char win32_startfile__doc__[] = "\ |
| 5521 | startfile(filepath) - Start a file with its associated application.\n\ |
| 5522 | \n\ |
| 5523 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 5524 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 5525 | with whatever application (if any) its extension is associated.\n\ |
| 5526 | \n\ |
| 5527 | startfile returns as soon as the associated application is launched.\n\ |
| 5528 | There is no option to wait for the application to close, and no way\n\ |
| 5529 | to retrieve the application's exit status.\n\ |
| 5530 | \n\ |
| 5531 | The filepath is relative to the current directory. If you want to use\n\ |
| 5532 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 5533 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 5534 | |
| 5535 | static PyObject * |
| 5536 | win32_startfile(PyObject *self, PyObject *args) |
| 5537 | { |
| 5538 | char *filepath; |
| 5539 | HINSTANCE rc; |
| 5540 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 5541 | return NULL; |
| 5542 | Py_BEGIN_ALLOW_THREADS |
| 5543 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 5544 | Py_END_ALLOW_THREADS |
| 5545 | if (rc <= (HINSTANCE)32) |
| 5546 | return win32_error("startfile", filepath); |
| 5547 | Py_INCREF(Py_None); |
| 5548 | return Py_None; |
| 5549 | } |
| 5550 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5551 | |
| 5552 | static PyMethodDef posix_methods[] = { |
| 5553 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5554 | #ifdef HAVE_TTYNAME |
| 5555 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5556 | #endif |
| 5557 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5558 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5559 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5560 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5561 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 5562 | #ifdef HAVE_CHROOT |
| 5563 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 5564 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5565 | #ifdef HAVE_CTERMID |
| 5566 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5567 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5568 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5569 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5570 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5571 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5572 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5573 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5574 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5575 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5576 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5577 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5578 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5579 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5580 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5581 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5582 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5583 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5584 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5585 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5586 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5587 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5588 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5589 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5590 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5591 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5592 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5593 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5594 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5595 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5596 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5597 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5598 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5599 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5600 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5601 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5602 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5603 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5604 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5605 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5606 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5607 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5608 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5609 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5610 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5611 | #ifdef HAVE_FORK1 |
| 5612 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 5613 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5614 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5615 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5616 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5617 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5618 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5619 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5620 | #ifdef HAVE_FORKPTY |
| 5621 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5622 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5623 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5624 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5625 | #endif /* HAVE_GETEGID */ |
| 5626 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5627 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5628 | #endif /* HAVE_GETEUID */ |
| 5629 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5630 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5631 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5632 | #ifdef HAVE_GETGROUPS |
| 5633 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5634 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5635 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5636 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5637 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5638 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5639 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5640 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5641 | #endif /* HAVE_GETPPID */ |
| 5642 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5643 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5644 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5645 | #ifdef HAVE_GETLOGIN |
| 5646 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5647 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5648 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5649 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5650 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5651 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5652 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5653 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5654 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5655 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5656 | #ifdef MS_WIN32 |
| 5657 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5658 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5659 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5660 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5661 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5662 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5663 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5664 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5665 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5666 | #ifdef HAVE_SETEUID |
| 5667 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5668 | #endif /* HAVE_SETEUID */ |
| 5669 | #ifdef HAVE_SETEGID |
| 5670 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5671 | #endif /* HAVE_SETEGID */ |
| 5672 | #ifdef HAVE_SETREUID |
| 5673 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5674 | #endif /* HAVE_SETREUID */ |
| 5675 | #ifdef HAVE_SETREGID |
| 5676 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5677 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5678 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5679 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5680 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5681 | #ifdef HAVE_SETGROUPS |
| 5682 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 5683 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5684 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5685 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5686 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5687 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5688 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5689 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5690 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5691 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5692 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5693 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5694 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5695 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5696 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5697 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5698 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5699 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5700 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5701 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5702 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5703 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5704 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5705 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5706 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5707 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5708 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5709 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5710 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5711 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5712 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5713 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5714 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5715 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5716 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5717 | #endif |
| 5718 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5719 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5720 | #endif |
| 5721 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5722 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5723 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5724 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5725 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5726 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5727 | #ifdef HAVE_UNSETENV |
| 5728 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 5729 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5730 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5731 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5732 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5733 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5734 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5735 | #endif |
| 5736 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5737 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5738 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5739 | #ifdef HAVE_SYS_WAIT_H |
| 5740 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5741 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5742 | #endif /* WIFSTOPPED */ |
| 5743 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5744 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5745 | #endif /* WIFSIGNALED */ |
| 5746 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5747 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5748 | #endif /* WIFEXITED */ |
| 5749 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5750 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5751 | #endif /* WEXITSTATUS */ |
| 5752 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5753 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5754 | #endif /* WTERMSIG */ |
| 5755 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5756 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5757 | #endif /* WSTOPSIG */ |
| 5758 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5759 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5760 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5761 | #endif |
| 5762 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5763 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5764 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 5765 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5766 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5767 | #endif |
| 5768 | #ifdef HAVE_TEMPNAM |
| 5769 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5770 | #endif |
| 5771 | #ifdef HAVE_TMPNAM |
| 5772 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5773 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5774 | #ifdef HAVE_CONFSTR |
| 5775 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5776 | #endif |
| 5777 | #ifdef HAVE_SYSCONF |
| 5778 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5779 | #endif |
| 5780 | #ifdef HAVE_FPATHCONF |
| 5781 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5782 | #endif |
| 5783 | #ifdef HAVE_PATHCONF |
| 5784 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5785 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5786 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5787 | #ifdef MS_WIN32 |
| 5788 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 5789 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5790 | {NULL, NULL} /* Sentinel */ |
| 5791 | }; |
| 5792 | |
| 5793 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5794 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5795 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5796 | { |
| 5797 | PyObject* v = PyInt_FromLong(value); |
| 5798 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5799 | return -1; /* triggers fatal error */ |
| 5800 | |
| 5801 | Py_DECREF(v); |
| 5802 | return 0; |
| 5803 | } |
| 5804 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5805 | #if defined(PYOS_OS2) |
| 5806 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5807 | static int insertvalues(PyObject *d) |
| 5808 | { |
| 5809 | APIRET rc; |
| 5810 | ULONG values[QSV_MAX+1]; |
| 5811 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 5812 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5813 | |
| 5814 | Py_BEGIN_ALLOW_THREADS |
| 5815 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5816 | Py_END_ALLOW_THREADS |
| 5817 | |
| 5818 | if (rc != NO_ERROR) { |
| 5819 | os2_error(rc); |
| 5820 | return -1; |
| 5821 | } |
| 5822 | |
| 5823 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5824 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5825 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5826 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5827 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5828 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5829 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5830 | |
| 5831 | switch (values[QSV_VERSION_MINOR]) { |
| 5832 | case 0: ver = "2.00"; break; |
| 5833 | case 10: ver = "2.10"; break; |
| 5834 | case 11: ver = "2.11"; break; |
| 5835 | case 30: ver = "3.00"; break; |
| 5836 | case 40: ver = "4.00"; break; |
| 5837 | case 50: ver = "5.00"; break; |
| 5838 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 5839 | PyOS_snprintf(tmp, sizeof(tmp), |
| 5840 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 5841 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5842 | ver = &tmp[0]; |
| 5843 | } |
| 5844 | |
| 5845 | /* Add Indicator of the Version of the Operating System */ |
| 5846 | v = PyString_FromString(ver); |
| 5847 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5848 | return -1; |
| 5849 | Py_DECREF(v); |
| 5850 | |
| 5851 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5852 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5853 | tmp[1] = ':'; |
| 5854 | tmp[2] = '\0'; |
| 5855 | |
| 5856 | v = PyString_FromString(tmp); |
| 5857 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5858 | return -1; |
| 5859 | Py_DECREF(v); |
| 5860 | |
| 5861 | return 0; |
| 5862 | } |
| 5863 | #endif |
| 5864 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5865 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5866 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5867 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5868 | #ifdef F_OK |
| 5869 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5870 | #endif |
| 5871 | #ifdef R_OK |
| 5872 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5873 | #endif |
| 5874 | #ifdef W_OK |
| 5875 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5876 | #endif |
| 5877 | #ifdef X_OK |
| 5878 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5879 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5880 | #ifdef NGROUPS_MAX |
| 5881 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5882 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5883 | #ifdef TMP_MAX |
| 5884 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5885 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5886 | #ifdef WNOHANG |
| 5887 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5888 | #endif |
| 5889 | #ifdef O_RDONLY |
| 5890 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5891 | #endif |
| 5892 | #ifdef O_WRONLY |
| 5893 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5894 | #endif |
| 5895 | #ifdef O_RDWR |
| 5896 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5897 | #endif |
| 5898 | #ifdef O_NDELAY |
| 5899 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5900 | #endif |
| 5901 | #ifdef O_NONBLOCK |
| 5902 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5903 | #endif |
| 5904 | #ifdef O_APPEND |
| 5905 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5906 | #endif |
| 5907 | #ifdef O_DSYNC |
| 5908 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5909 | #endif |
| 5910 | #ifdef O_RSYNC |
| 5911 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5912 | #endif |
| 5913 | #ifdef O_SYNC |
| 5914 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5915 | #endif |
| 5916 | #ifdef O_NOCTTY |
| 5917 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5918 | #endif |
| 5919 | #ifdef O_CREAT |
| 5920 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5921 | #endif |
| 5922 | #ifdef O_EXCL |
| 5923 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5924 | #endif |
| 5925 | #ifdef O_TRUNC |
| 5926 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5927 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5928 | #ifdef O_BINARY |
| 5929 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5930 | #endif |
| 5931 | #ifdef O_TEXT |
| 5932 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5933 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 5934 | #ifdef O_LARGEFILE |
| 5935 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 5936 | #endif |
| 5937 | |
| 5938 | /* GNU extensions. */ |
| 5939 | #ifdef O_DIRECT |
| 5940 | /* Direct disk access. */ |
| 5941 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 5942 | #endif |
| 5943 | #ifdef O_DIRECTORY |
| 5944 | /* Must be a directory. */ |
| 5945 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 5946 | #endif |
| 5947 | #ifdef O_NOFOLLOW |
| 5948 | /* Do not follow links. */ |
| 5949 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 5950 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5951 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5952 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5953 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5954 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5955 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5956 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5957 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5958 | #endif |
| 5959 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5960 | #if defined(PYOS_OS2) |
| 5961 | if (insertvalues(d)) return -1; |
| 5962 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5963 | return 0; |
| 5964 | } |
| 5965 | |
| 5966 | |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5967 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5968 | #define INITFUNC initnt |
| 5969 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5970 | |
| 5971 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5972 | #define INITFUNC initos2 |
| 5973 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5974 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5975 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5976 | #define INITFUNC initposix |
| 5977 | #define MODNAME "posix" |
| 5978 | #endif |
| 5979 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5980 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5981 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5982 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5983 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5984 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5985 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5986 | posix_methods, |
| 5987 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5988 | (PyObject *)NULL, |
| 5989 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5990 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5991 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5992 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5993 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5994 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5995 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5996 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5997 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5998 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5999 | return; |
| 6000 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6001 | if (setup_confname_tables(d)) |
| 6002 | return; |
| 6003 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 6004 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6005 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6006 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 6007 | if (posix_putenv_garbage == NULL) |
| 6008 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 6009 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6010 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6011 | stat_result_desc.name = MODNAME ".stat_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6012 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 6013 | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
| 6014 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 6015 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6016 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 6017 | PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6018 | } |