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 |
| 435 | sprintf(msgbuf, "unknown OS error #%d", errorcode); |
| 436 | |
| 437 | return msgbuf; |
| 438 | } |
| 439 | |
| 440 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 441 | errors are not in a global variable e.g. 'errno' nor are |
| 442 | they congruent with posix error numbers. */ |
| 443 | |
| 444 | static PyObject * os2_error(int code) |
| 445 | { |
| 446 | char text[1024]; |
| 447 | PyObject *v; |
| 448 | |
| 449 | os2_strerror(text, sizeof(text), code, ""); |
| 450 | |
| 451 | v = Py_BuildValue("(is)", code, text); |
| 452 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 453 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 454 | Py_DECREF(v); |
| 455 | } |
| 456 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 457 | } |
| 458 | |
| 459 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | |
| 461 | /* POSIX generic methods */ |
| 462 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 463 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 464 | posix_int(PyObject *args, char *format, int (*func)(int)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 465 | { |
| 466 | int fd; |
| 467 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 468 | if (!PyArg_ParseTuple(args, format, &fd)) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 469 | return NULL; |
| 470 | Py_BEGIN_ALLOW_THREADS |
| 471 | res = (*func)(fd); |
| 472 | Py_END_ALLOW_THREADS |
| 473 | if (res < 0) |
| 474 | return posix_error(); |
| 475 | Py_INCREF(Py_None); |
| 476 | return Py_None; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 481 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 482 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 483 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 484 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 485 | if (!PyArg_ParseTuple(args, format, |
| 486 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 488 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 489 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 490 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 491 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 492 | return posix_error_with_allocated_filename(path1); |
| 493 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 494 | Py_INCREF(Py_None); |
| 495 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 498 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 499 | posix_2str(PyObject *args, char *format, |
| 500 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 502 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 503 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 504 | if (!PyArg_ParseTuple(args, format, |
| 505 | Py_FileSystemDefaultEncoding, &path1, |
| 506 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 508 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 509 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 510 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 511 | PyMem_Free(path1); |
| 512 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 513 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 514 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 516 | Py_INCREF(Py_None); |
| 517 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 520 | static char stat_result__doc__[] = |
| 521 | "stat_result: Result from stat or lstat.\n\n\ |
| 522 | This object may be accessed either as a tuple of\n\ |
| 523 | (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 524 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 525 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 526 | 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] | 527 | they are available as attributes only.\n\ |
| 528 | \n\ |
| 529 | See os.stat for more information.\n"; |
| 530 | |
| 531 | static PyStructSequence_Field stat_result_fields[] = { |
| 532 | {"st_mode", "protection bits"}, |
| 533 | {"st_ino", "inode"}, |
| 534 | {"st_dev", "device"}, |
| 535 | {"st_nlink", "number of hard links"}, |
| 536 | {"st_uid", "user ID of owner"}, |
| 537 | {"st_gid", "group ID of owner"}, |
| 538 | {"st_size", "total size, in bytes"}, |
| 539 | {"st_atime", "time of last access"}, |
| 540 | {"st_mtime", "time of last modification"}, |
| 541 | {"st_ctime", "time of last change"}, |
| 542 | #ifdef HAVE_ST_BLKSIZE |
| 543 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 544 | #endif |
| 545 | #ifdef HAVE_ST_BLOCKS |
| 546 | {"st_blocks", "number of blocks allocated"}, |
| 547 | #endif |
| 548 | #ifdef HAVE_ST_RDEV |
| 549 | {"st_rdev", "device type (if inode device)"}, |
| 550 | #endif |
| 551 | {0} |
| 552 | }; |
| 553 | |
| 554 | #ifdef HAVE_ST_BLKSIZE |
| 555 | #define ST_BLKSIZE_IDX 10 |
| 556 | #else |
| 557 | #define ST_BLKSIZE_IDX 9 |
| 558 | #endif |
| 559 | |
| 560 | #ifdef HAVE_ST_BLOCKS |
| 561 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 562 | #else |
| 563 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 564 | #endif |
| 565 | |
| 566 | #ifdef HAVE_ST_RDEV |
| 567 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 568 | #else |
| 569 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 570 | #endif |
| 571 | |
| 572 | static PyStructSequence_Desc stat_result_desc = { |
| 573 | "stat_result", /* name */ |
| 574 | stat_result__doc__, /* doc */ |
| 575 | stat_result_fields, |
| 576 | 10 |
| 577 | }; |
| 578 | |
| 579 | static char statvfs_result__doc__[] = |
| 580 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 581 | This object may be accessed either as a tuple of\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 582 | (bsize,frsize,blocks,bfree,bavail,files,ffree,favail,flag,namemax),\n\ |
| 583 | 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] | 584 | \n\ |
| 585 | See os.statvfs for more information.\n"; |
| 586 | |
| 587 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 588 | {"f_bsize", }, |
| 589 | {"f_frsize", }, |
| 590 | {"f_blocks", }, |
| 591 | {"f_bfree", }, |
| 592 | {"f_bavail", }, |
| 593 | {"f_files", }, |
| 594 | {"f_ffree", }, |
| 595 | {"f_favail", }, |
| 596 | {"f_flag", }, |
| 597 | {"f_namemax",}, |
| 598 | {0} |
| 599 | }; |
| 600 | |
| 601 | static PyStructSequence_Desc statvfs_result_desc = { |
| 602 | "statvfs_result", /* name */ |
| 603 | statvfs_result__doc__, /* doc */ |
| 604 | statvfs_result_fields, |
| 605 | 10 |
| 606 | }; |
| 607 | |
| 608 | static PyTypeObject StatResultType; |
| 609 | static PyTypeObject StatVFSResultType; |
| 610 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 611 | /* pack a system stat C structure into the Python stat tuple |
| 612 | (used by posix_stat() and posix_fstat()) */ |
| 613 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 614 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 615 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 616 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 617 | if (v == NULL) |
| 618 | return NULL; |
| 619 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 620 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 621 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 622 | PyStructSequence_SET_ITEM(v, 1, |
| 623 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 624 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 625 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 626 | #endif |
| 627 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 628 | PyStructSequence_SET_ITEM(v, 2, |
| 629 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 630 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 631 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 632 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 633 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 634 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 635 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 636 | #ifdef HAVE_LARGEFILE_SUPPORT |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 637 | PyStructSequence_SET_ITEM(v, 6, |
| 638 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 639 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 640 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 641 | #endif |
| 642 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 643 | PyStructSequence_SET_ITEM(v, 7, |
| 644 | PyLong_FromLongLong((LONG_LONG)st.st_atime)); |
| 645 | PyStructSequence_SET_ITEM(v, 8, |
| 646 | PyLong_FromLongLong((LONG_LONG)st.st_mtime)); |
| 647 | PyStructSequence_SET_ITEM(v, 9, |
| 648 | PyLong_FromLongLong((LONG_LONG)st.st_ctime)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 649 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 650 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long)st.st_atime)); |
| 651 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long)st.st_mtime)); |
| 652 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long)st.st_ctime)); |
| 653 | #endif |
| 654 | |
| 655 | #ifdef HAVE_ST_BLKSIZE |
| 656 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 657 | PyInt_FromLong((long)st.st_blksize)); |
| 658 | #endif |
| 659 | #ifdef HAVE_ST_BLOCKS |
| 660 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 661 | PyInt_FromLong((long)st.st_blocks)); |
| 662 | #endif |
| 663 | #ifdef HAVE_ST_RDEV |
| 664 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 665 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
| 668 | if (PyErr_Occurred()) { |
| 669 | Py_DECREF(v); |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | return v; |
| 674 | } |
| 675 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 677 | posix_do_stat(PyObject *self, PyObject *args, char *format, |
| 678 | int (*statfunc)(const char *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 679 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 680 | STRUCT_STAT st; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 681 | char *path = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 682 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 683 | |
| 684 | #ifdef MS_WIN32 |
| 685 | int pathlen; |
| 686 | char pathcopy[MAX_PATH]; |
| 687 | #endif /* MS_WIN32 */ |
| 688 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 689 | if (!PyArg_ParseTuple(args, format, |
| 690 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 691 | return NULL; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 692 | |
| 693 | #ifdef MS_WIN32 |
| 694 | pathlen = strlen(path); |
| 695 | /* the library call can blow up if the file name is too long! */ |
| 696 | if (pathlen > MAX_PATH) { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 697 | PyMem_Free(path); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 698 | errno = ENAMETOOLONG; |
| 699 | return posix_error(); |
| 700 | } |
| 701 | |
| 702 | if ((pathlen > 0) && (path[pathlen-1] == '\\' || path[pathlen-1] == '/')) { |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 703 | /* exception for specific or current drive root */ |
| 704 | if (!((pathlen == 1) || |
| 705 | ((pathlen == 3) && |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 706 | (path[1] == ':') && |
Guido van Rossum | 19dde10 | 2000-05-03 02:44:55 +0000 | [diff] [blame] | 707 | (path[2] == '\\' || path[2] == '/')))) |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 708 | { |
| 709 | strncpy(pathcopy, path, pathlen); |
| 710 | pathcopy[pathlen-1] = '\0'; /* nuke the trailing backslash */ |
| 711 | path = pathcopy; |
| 712 | } |
| 713 | } |
| 714 | #endif /* MS_WIN32 */ |
| 715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 716 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 717 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 718 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 719 | if (res != 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 720 | return posix_error_with_allocated_filename(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 721 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 722 | PyMem_Free(path); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 723 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
| 727 | /* POSIX methods */ |
| 728 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 729 | static char posix_access__doc__[] = |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 730 | "access(path, mode) -> 1 if granted, 0 otherwise\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 731 | Test for access to a file."; |
| 732 | |
| 733 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 734 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 735 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 736 | char *path; |
| 737 | int mode; |
| 738 | int res; |
| 739 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 740 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 741 | return NULL; |
| 742 | Py_BEGIN_ALLOW_THREADS |
| 743 | res = access(path, mode); |
| 744 | Py_END_ALLOW_THREADS |
| 745 | return(PyInt_FromLong(res == 0 ? 1L : 0L)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 748 | #ifndef F_OK |
| 749 | #define F_OK 0 |
| 750 | #endif |
| 751 | #ifndef R_OK |
| 752 | #define R_OK 4 |
| 753 | #endif |
| 754 | #ifndef W_OK |
| 755 | #define W_OK 2 |
| 756 | #endif |
| 757 | #ifndef X_OK |
| 758 | #define X_OK 1 |
| 759 | #endif |
| 760 | |
| 761 | #ifdef HAVE_TTYNAME |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 762 | static char posix_ttyname__doc__[] = |
Guido van Rossum | 61eeb04 | 1999-02-22 15:29:15 +0000 | [diff] [blame] | 763 | "ttyname(fd) -> String\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 764 | Return the name of the terminal device connected to 'fd'."; |
| 765 | |
| 766 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 767 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 768 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 769 | int id; |
| 770 | char *ret; |
| 771 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 772 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 773 | return NULL; |
| 774 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 775 | ret = ttyname(id); |
| 776 | if (ret == NULL) |
| 777 | return(posix_error()); |
| 778 | return(PyString_FromString(ret)); |
| 779 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 780 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 781 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 782 | #ifdef HAVE_CTERMID |
| 783 | static char posix_ctermid__doc__[] = |
| 784 | "ctermid() -> String\n\ |
| 785 | Return the name of the controlling terminal for this process."; |
| 786 | |
| 787 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 788 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 789 | { |
| 790 | char *ret; |
| 791 | char buffer[L_ctermid]; |
| 792 | |
| 793 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 794 | return NULL; |
| 795 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 796 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 797 | ret = ctermid_r(buffer); |
| 798 | #else |
| 799 | ret = ctermid(buffer); |
| 800 | #endif |
| 801 | if (ret == NULL) |
| 802 | return(posix_error()); |
| 803 | return(PyString_FromString(buffer)); |
| 804 | } |
| 805 | #endif |
| 806 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 807 | static char posix_chdir__doc__[] = |
| 808 | "chdir(path) -> None\n\ |
| 809 | Change the current working directory to the specified path."; |
| 810 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 811 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 812 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 813 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 814 | return posix_1str(args, "et:chdir", chdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 817 | |
| 818 | static char posix_chmod__doc__[] = |
| 819 | "chmod(path, mode) -> None\n\ |
| 820 | Change the access permissions of a file."; |
| 821 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 822 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 823 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 824 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 825 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 826 | int i; |
| 827 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 828 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
| 829 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 830 | return NULL; |
| 831 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 832 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 833 | Py_END_ALLOW_THREADS |
| 834 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 835 | return posix_error_with_allocated_filename(path); |
| 836 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 837 | Py_INCREF(Py_None); |
| 838 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 841 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 842 | #ifdef HAVE_CHROOT |
| 843 | static char posix_chroot__doc__[] = |
| 844 | "chroot(path) -> None\n\ |
| 845 | Change root directory to path."; |
| 846 | |
| 847 | static PyObject * |
| 848 | posix_chroot(PyObject *self, PyObject *args) |
| 849 | { |
| 850 | return posix_1str(args, "et:chroot", chroot); |
| 851 | } |
| 852 | #endif |
| 853 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 854 | #ifdef HAVE_FSYNC |
| 855 | static char posix_fsync__doc__[] = |
| 856 | "fsync(fildes) -> None\n\ |
| 857 | force write of file with filedescriptor to disk."; |
| 858 | |
| 859 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 860 | posix_fsync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 861 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 862 | return posix_int(args, "i:fsync", fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 863 | } |
| 864 | #endif /* HAVE_FSYNC */ |
| 865 | |
| 866 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 867 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 868 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 869 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 870 | #endif |
| 871 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 872 | static char posix_fdatasync__doc__[] = |
| 873 | "fdatasync(fildes) -> None\n\ |
| 874 | force write of file with filedescriptor to disk.\n\ |
| 875 | does not force update of metadata."; |
| 876 | |
| 877 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 878 | posix_fdatasync(PyObject *self, PyObject *args) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 879 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 880 | return posix_int(args, "i:fdatasync", fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 881 | } |
| 882 | #endif /* HAVE_FDATASYNC */ |
| 883 | |
| 884 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 885 | #ifdef HAVE_CHOWN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 886 | static char posix_chown__doc__[] = |
| 887 | "chown(path, uid, gid) -> None\n\ |
| 888 | Change the owner and group id of path to the numeric uid and gid."; |
| 889 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 890 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 891 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 892 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 893 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 894 | int uid, gid; |
| 895 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 896 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 897 | Py_FileSystemDefaultEncoding, &path, |
| 898 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 899 | return NULL; |
| 900 | Py_BEGIN_ALLOW_THREADS |
| 901 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 902 | Py_END_ALLOW_THREADS |
| 903 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 904 | return posix_error_with_allocated_filename(path); |
| 905 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 906 | Py_INCREF(Py_None); |
| 907 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 908 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 909 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 910 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 911 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 912 | #ifdef HAVE_GETCWD |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 913 | static char posix_getcwd__doc__[] = |
| 914 | "getcwd() -> path\n\ |
| 915 | Return a string representing the current working directory."; |
| 916 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 917 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 918 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 919 | { |
| 920 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 921 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 922 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 923 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 924 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 925 | res = getcwd(buf, sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 926 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 927 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 928 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 929 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 931 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 932 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 933 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 934 | #ifdef HAVE_LINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 935 | static char posix_link__doc__[] = |
| 936 | "link(src, dst) -> None\n\ |
| 937 | Create a hard link to a file."; |
| 938 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 939 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 940 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 941 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 942 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 944 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 946 | |
| 947 | static char posix_listdir__doc__[] = |
| 948 | "listdir(path) -> list_of_strings\n\ |
| 949 | Return a list containing the names of the entries in the directory.\n\ |
| 950 | \n\ |
| 951 | path: path of directory to list\n\ |
| 952 | \n\ |
| 953 | The list is in arbitrary order. It does not include the special\n\ |
| 954 | entries '.' and '..' even if they are present in the directory."; |
| 955 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 956 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 957 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 958 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 959 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 960 | in separate files instead of having them all here... */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 961 | #if defined(MS_WIN32) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 962 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 963 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 964 | HANDLE hFindFile; |
| 965 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 966 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 967 | char namebuf[MAX_PATH*2+5]; |
| 968 | char *bufptr = namebuf; |
| 969 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 970 | char ch; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 971 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 972 | if (!PyArg_ParseTuple(args, "et#:listdir", |
| 973 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 974 | return NULL; |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 975 | ch = namebuf[len-1]; |
| 976 | if (ch != '/' && ch != '\\' && ch != ':') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 977 | namebuf[len++] = '/'; |
| 978 | strcpy(namebuf + len, "*.*"); |
| 979 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 980 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 981 | return NULL; |
| 982 | |
| 983 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 984 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 985 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 986 | if (errno == ERROR_FILE_NOT_FOUND) |
| 987 | return PyList_New(0); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 988 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 989 | } |
| 990 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 991 | if (FileData.cFileName[0] == '.' && |
| 992 | (FileData.cFileName[1] == '\0' || |
| 993 | FileData.cFileName[1] == '.' && |
| 994 | FileData.cFileName[2] == '\0')) |
| 995 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 996 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 997 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 998 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 999 | d = NULL; |
| 1000 | break; |
| 1001 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1002 | if (PyList_Append(d, v) != 0) { |
| 1003 | Py_DECREF(v); |
| 1004 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1005 | d = NULL; |
| 1006 | break; |
| 1007 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1008 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1009 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1010 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1011 | if (FindClose(hFindFile) == FALSE) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1012 | return win32_error("FindClose", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1013 | |
| 1014 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1015 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1016 | #elif defined(_MSC_VER) /* 16-bit Windows */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1017 | |
| 1018 | #ifndef MAX_PATH |
| 1019 | #define MAX_PATH 250 |
| 1020 | #endif |
| 1021 | char *name, *pt; |
| 1022 | int len; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1023 | PyObject *d, *v; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1024 | char namebuf[MAX_PATH+5]; |
| 1025 | struct _find_t ep; |
| 1026 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1027 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1028 | return NULL; |
| 1029 | if (len >= MAX_PATH) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1030 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1031 | return NULL; |
| 1032 | } |
| 1033 | strcpy(namebuf, name); |
| 1034 | for (pt = namebuf; *pt; pt++) |
| 1035 | if (*pt == '/') |
| 1036 | *pt = '\\'; |
| 1037 | if (namebuf[len-1] != '\\') |
| 1038 | namebuf[len++] = '\\'; |
| 1039 | strcpy(namebuf + len, "*.*"); |
| 1040 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1041 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1042 | return NULL; |
| 1043 | |
| 1044 | if (_dos_findfirst(namebuf, _A_RDONLY | |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1045 | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0) |
| 1046 | { |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1047 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1048 | return posix_error_with_filename(name); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1049 | } |
| 1050 | do { |
| 1051 | if (ep.name[0] == '.' && |
| 1052 | (ep.name[1] == '\0' || |
| 1053 | ep.name[1] == '.' && |
| 1054 | ep.name[2] == '\0')) |
| 1055 | continue; |
| 1056 | strcpy(namebuf, ep.name); |
| 1057 | for (pt = namebuf; *pt; pt++) |
| 1058 | if (isupper(*pt)) |
| 1059 | *pt = tolower(*pt); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1060 | v = PyString_FromString(namebuf); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1061 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1062 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1063 | d = NULL; |
| 1064 | break; |
| 1065 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1066 | if (PyList_Append(d, v) != 0) { |
| 1067 | Py_DECREF(v); |
| 1068 | Py_DECREF(d); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1069 | d = NULL; |
| 1070 | break; |
| 1071 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1072 | Py_DECREF(v); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1073 | } while (_dos_findnext(&ep) == 0); |
| 1074 | |
| 1075 | return d; |
| 1076 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1077 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1078 | |
| 1079 | #ifndef MAX_PATH |
| 1080 | #define MAX_PATH CCHMAXPATH |
| 1081 | #endif |
| 1082 | char *name, *pt; |
| 1083 | int len; |
| 1084 | PyObject *d, *v; |
| 1085 | char namebuf[MAX_PATH+5]; |
| 1086 | HDIR hdir = 1; |
| 1087 | ULONG srchcnt = 1; |
| 1088 | FILEFINDBUF3 ep; |
| 1089 | APIRET rc; |
| 1090 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1091 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1092 | return NULL; |
| 1093 | if (len >= MAX_PATH) { |
| 1094 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | strcpy(namebuf, name); |
| 1098 | for (pt = namebuf; *pt; pt++) |
| 1099 | if (*pt == '/') |
| 1100 | *pt = '\\'; |
| 1101 | if (namebuf[len-1] != '\\') |
| 1102 | namebuf[len++] = '\\'; |
| 1103 | strcpy(namebuf + len, "*.*"); |
| 1104 | |
| 1105 | if ((d = PyList_New(0)) == NULL) |
| 1106 | return NULL; |
| 1107 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1108 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1109 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1110 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1111 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1112 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1113 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (rc != NO_ERROR) { |
| 1116 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1117 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1120 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1121 | do { |
| 1122 | if (ep.achName[0] == '.' |
| 1123 | && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0')) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1124 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1125 | |
| 1126 | strcpy(namebuf, ep.achName); |
| 1127 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1128 | /* Leave Case of Name Alone -- In Native Form */ |
| 1129 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1130 | |
| 1131 | v = PyString_FromString(namebuf); |
| 1132 | if (v == NULL) { |
| 1133 | Py_DECREF(d); |
| 1134 | d = NULL; |
| 1135 | break; |
| 1136 | } |
| 1137 | if (PyList_Append(d, v) != 0) { |
| 1138 | Py_DECREF(v); |
| 1139 | Py_DECREF(d); |
| 1140 | d = NULL; |
| 1141 | break; |
| 1142 | } |
| 1143 | Py_DECREF(v); |
| 1144 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1145 | } |
| 1146 | |
| 1147 | return d; |
| 1148 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1149 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1150 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1151 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1152 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1153 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1154 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1155 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1156 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1157 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1158 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1159 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1160 | closedir(dirp); |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1164 | if (ep->d_name[0] == '.' && |
| 1165 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1166 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1167 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1168 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1170 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1171 | d = NULL; |
| 1172 | break; |
| 1173 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1174 | if (PyList_Append(d, v) != 0) { |
| 1175 | Py_DECREF(v); |
| 1176 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1177 | d = NULL; |
| 1178 | break; |
| 1179 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1180 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1181 | } |
| 1182 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1184 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1185 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1186 | #endif /* which OS */ |
| 1187 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1188 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1189 | #ifdef MS_WIN32 |
| 1190 | /* A helper function for abspath on win32 */ |
| 1191 | static PyObject * |
| 1192 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1193 | { |
| 1194 | /* assume encoded strings wont more than double no of chars */ |
| 1195 | char inbuf[MAX_PATH*2]; |
| 1196 | char *inbufp = inbuf; |
| 1197 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1198 | char outbuf[MAX_PATH*2]; |
| 1199 | char *temp; |
| 1200 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1201 | Py_FileSystemDefaultEncoding, &inbufp, |
| 1202 | &insize)) |
| 1203 | return NULL; |
| 1204 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1205 | outbuf, &temp)) |
| 1206 | return win32_error("GetFullPathName", inbuf); |
| 1207 | return PyString_FromString(outbuf); |
| 1208 | } /* end of posix__getfullpathname */ |
| 1209 | #endif /* MS_WIN32 */ |
| 1210 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1211 | static char posix_mkdir__doc__[] = |
| 1212 | "mkdir(path [, mode=0777]) -> None\n\ |
| 1213 | Create a directory."; |
| 1214 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1215 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1216 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1217 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1218 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1219 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1220 | int mode = 0777; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1221 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 1222 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1223 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1224 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1225 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1226 | res = mkdir(path); |
| 1227 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1228 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1229 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1230 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1231 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1232 | return posix_error_with_allocated_filename(path); |
| 1233 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1234 | Py_INCREF(Py_None); |
| 1235 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1238 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1239 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1240 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1241 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1242 | #include <sys/resource.h> |
| 1243 | #endif |
| 1244 | #endif |
| 1245 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1246 | static char posix_nice__doc__[] = |
| 1247 | "nice(inc) -> new_priority\n\ |
| 1248 | Decrease the priority of process and return new priority."; |
| 1249 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1250 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1251 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1252 | { |
| 1253 | int increment, value; |
| 1254 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1255 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1256 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* There are two flavours of 'nice': one that returns the new |
| 1259 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1260 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1261 | the use of getpriority() to get the new priority. |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1262 | |
| 1263 | If we are of the nice family that returns the new priority, we |
| 1264 | need to clear errno before the call, and check if errno is filled |
| 1265 | before calling posix_error() on a returnvalue of -1, because the |
| 1266 | -1 may be the actual new priority! */ |
| 1267 | |
| 1268 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1269 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1270 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1271 | if (value == 0) |
| 1272 | value = getpriority(PRIO_PROCESS, 0); |
| 1273 | #endif |
| 1274 | if (value == -1 && errno != 0) |
| 1275 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1276 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1277 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1278 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1279 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1280 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1281 | |
| 1282 | static char posix_rename__doc__[] = |
| 1283 | "rename(old, new) -> None\n\ |
| 1284 | Rename a file or directory."; |
| 1285 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1286 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1287 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1288 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1289 | return posix_2str(args, "etet:rename", rename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1292 | |
| 1293 | static char posix_rmdir__doc__[] = |
| 1294 | "rmdir(path) -> None\n\ |
| 1295 | Remove a directory."; |
| 1296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1297 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1298 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1300 | return posix_1str(args, "et:rmdir", rmdir); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1303 | |
| 1304 | static char posix_stat__doc__[] = |
| 1305 | "stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 1306 | Perform a stat system call on the given path."; |
| 1307 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1308 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1309 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1310 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1311 | return posix_do_stat(self, args, "et:stat", STAT); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1314 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1315 | #ifdef HAVE_SYSTEM |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1316 | static char posix_system__doc__[] = |
| 1317 | "system(command) -> exit_status\n\ |
| 1318 | Execute the command (a string) in a subshell."; |
| 1319 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1320 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1321 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1322 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1323 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1324 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1325 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1326 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1327 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1328 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1329 | Py_END_ALLOW_THREADS |
| 1330 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1331 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1332 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1334 | |
| 1335 | static char posix_umask__doc__[] = |
| 1336 | "umask(new_mask) -> old_mask\n\ |
| 1337 | Set the current numeric umask and return the previous umask."; |
| 1338 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1339 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1340 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | { |
| 1342 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1343 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1344 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1345 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1346 | if (i < 0) |
| 1347 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1348 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1351 | |
| 1352 | static char posix_unlink__doc__[] = |
| 1353 | "unlink(path) -> None\n\ |
| 1354 | Remove a file (same as remove(path))."; |
| 1355 | |
| 1356 | static char posix_remove__doc__[] = |
| 1357 | "remove(path) -> None\n\ |
| 1358 | Remove a file (same as unlink(path))."; |
| 1359 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1360 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1361 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1362 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1363 | return posix_1str(args, "et:remove", unlink); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1366 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1367 | #ifdef HAVE_UNAME |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1368 | static char posix_uname__doc__[] = |
| 1369 | "uname() -> (sysname, nodename, release, version, machine)\n\ |
| 1370 | Return a tuple identifying the current operating system."; |
| 1371 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1373 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1374 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1375 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1376 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1377 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1378 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1379 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1380 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1381 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1382 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1383 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1384 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1385 | u.sysname, |
| 1386 | u.nodename, |
| 1387 | u.release, |
| 1388 | u.version, |
| 1389 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1390 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1391 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1393 | |
| 1394 | static char posix_utime__doc__[] = |
| 1395 | "utime(path, (atime, utime)) -> None\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1396 | utime(path, None) -> None\n\ |
| 1397 | Set the access and modified time of the file to the given values. If the\n\ |
| 1398 | 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] | 1399 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1400 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1401 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1402 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1403 | char *path; |
Guido van Rossum | f8803dd | 1995-01-26 00:37:45 +0000 | [diff] [blame] | 1404 | long atime, mtime; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1405 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1406 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1407 | |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1408 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1409 | #ifdef HAVE_UTIME_H |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1410 | struct utimbuf buf; |
| 1411 | #define ATIME buf.actime |
| 1412 | #define MTIME buf.modtime |
| 1413 | #define UTIME_ARG &buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1414 | #else /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1415 | time_t buf[2]; |
| 1416 | #define ATIME buf[0] |
| 1417 | #define MTIME buf[1] |
| 1418 | #define UTIME_ARG buf |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1419 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1420 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1421 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1422 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1423 | if (arg == Py_None) { |
| 1424 | /* optional time values not given */ |
| 1425 | Py_BEGIN_ALLOW_THREADS |
| 1426 | res = utime(path, NULL); |
| 1427 | Py_END_ALLOW_THREADS |
| 1428 | } |
| 1429 | else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) { |
| 1430 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1431 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1432 | return NULL; |
| 1433 | } |
| 1434 | else { |
| 1435 | ATIME = atime; |
| 1436 | MTIME = mtime; |
| 1437 | Py_BEGIN_ALLOW_THREADS |
| 1438 | res = utime(path, UTIME_ARG); |
| 1439 | Py_END_ALLOW_THREADS |
| 1440 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1441 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1442 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1443 | Py_INCREF(Py_None); |
| 1444 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1445 | #undef UTIME_ARG |
| 1446 | #undef ATIME |
| 1447 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1450 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1451 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1452 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1453 | static char posix__exit__doc__[] = |
| 1454 | "_exit(status)\n\ |
| 1455 | Exit to the system with specified status, without normal exit processing."; |
| 1456 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1457 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1458 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1459 | { |
| 1460 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1461 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1464 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1467 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1468 | #ifdef HAVE_EXECV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1469 | static char posix_execv__doc__[] = |
| 1470 | "execv(path, args)\n\ |
| 1471 | Execute an executable path with arguments, replacing current process.\n\ |
| 1472 | \n\ |
| 1473 | path: path of executable file\n\ |
| 1474 | args: tuple or list of strings"; |
| 1475 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1476 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1477 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1478 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1479 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1480 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1481 | char **argvlist; |
| 1482 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1483 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1484 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1485 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1486 | argv is a list or tuple of strings. */ |
| 1487 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1488 | if (!PyArg_ParseTuple(args, "sO:execv", &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1489 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1490 | if (PyList_Check(argv)) { |
| 1491 | argc = PyList_Size(argv); |
| 1492 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1493 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1494 | else if (PyTuple_Check(argv)) { |
| 1495 | argc = PyTuple_Size(argv); |
| 1496 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1497 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1498 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1499 | 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] | 1500 | return NULL; |
| 1501 | } |
| 1502 | |
| 1503 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1504 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1505 | return NULL; |
| 1506 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1507 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1508 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1509 | if (argvlist == NULL) |
| 1510 | return NULL; |
| 1511 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1512 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1513 | PyMem_DEL(argvlist); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1514 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1515 | "execv() arg 2 must contain only strings"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1516 | return NULL; |
| 1517 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1518 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1519 | } |
| 1520 | argvlist[argc] = NULL; |
| 1521 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1522 | #ifdef BAD_EXEC_PROTOTYPES |
| 1523 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1524 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1525 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1526 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1527 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1528 | /* If we get here it's definitely an error */ |
| 1529 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1530 | PyMem_DEL(argvlist); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1531 | return posix_error(); |
| 1532 | } |
| 1533 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1534 | |
| 1535 | static char posix_execve__doc__[] = |
| 1536 | "execve(path, args, env)\n\ |
| 1537 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1538 | \n\ |
| 1539 | path: path of executable file\n\ |
| 1540 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1541 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1542 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1543 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1544 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1545 | { |
| 1546 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1547 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1548 | char **argvlist; |
| 1549 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1550 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1551 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1552 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1553 | |
| 1554 | /* execve has three arguments: (path, argv, env), where |
| 1555 | argv is a list or tuple of strings and env is a dictionary |
| 1556 | like posix.environ. */ |
| 1557 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1558 | if (!PyArg_ParseTuple(args, "sOO:execve", &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1559 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1560 | if (PyList_Check(argv)) { |
| 1561 | argc = PyList_Size(argv); |
| 1562 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1563 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1564 | else if (PyTuple_Check(argv)) { |
| 1565 | argc = PyTuple_Size(argv); |
| 1566 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1567 | } |
| 1568 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1569 | 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] | 1570 | return NULL; |
| 1571 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1572 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1573 | 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] | 1574 | return NULL; |
| 1575 | } |
| 1576 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1577 | if (argc == 0) { |
| 1578 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1579 | "execve() arg 2 must not be empty"); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1580 | return NULL; |
| 1581 | } |
| 1582 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1583 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1584 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1585 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1586 | return NULL; |
| 1587 | } |
| 1588 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1589 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1590 | "s;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1591 | &argvlist[i])) |
| 1592 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1593 | goto fail_1; |
| 1594 | } |
| 1595 | } |
| 1596 | argvlist[argc] = NULL; |
| 1597 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1598 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1599 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1600 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1601 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1602 | goto fail_1; |
| 1603 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1604 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1605 | keys = PyMapping_Keys(env); |
| 1606 | vals = PyMapping_Values(env); |
| 1607 | if (!keys || !vals) |
| 1608 | goto fail_2; |
| 1609 | |
| 1610 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1611 | char *p, *k, *v; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1612 | |
| 1613 | key = PyList_GetItem(keys, pos); |
| 1614 | val = PyList_GetItem(vals, pos); |
| 1615 | if (!key || !val) |
| 1616 | goto fail_2; |
| 1617 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1618 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 1619 | !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] | 1620 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1621 | goto fail_2; |
| 1622 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1623 | |
| 1624 | #if defined(PYOS_OS2) |
| 1625 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 1626 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 1627 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1628 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1629 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1630 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1631 | goto fail_2; |
| 1632 | } |
| 1633 | sprintf(p, "%s=%s", k, v); |
| 1634 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1635 | #if defined(PYOS_OS2) |
| 1636 | } |
| 1637 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1638 | } |
| 1639 | envlist[envc] = 0; |
| 1640 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1641 | |
| 1642 | #ifdef BAD_EXEC_PROTOTYPES |
| 1643 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1644 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1645 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1646 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1647 | |
| 1648 | /* If we get here it's definitely an error */ |
| 1649 | |
| 1650 | (void) posix_error(); |
| 1651 | |
| 1652 | fail_2: |
| 1653 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1654 | PyMem_DEL(envlist[envc]); |
| 1655 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1656 | fail_1: |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1657 | PyMem_DEL(argvlist); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1658 | Py_XDECREF(vals); |
| 1659 | Py_XDECREF(keys); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1660 | return NULL; |
| 1661 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1662 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1665 | #ifdef HAVE_SPAWNV |
| 1666 | static char posix_spawnv__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1667 | "spawnv(mode, path, args)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1668 | Execute an executable path with arguments, replacing current process.\n\ |
| 1669 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1670 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1671 | path: path of executable file\n\ |
| 1672 | args: tuple or list of strings"; |
| 1673 | |
| 1674 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1675 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1676 | { |
| 1677 | char *path; |
| 1678 | PyObject *argv; |
| 1679 | char **argvlist; |
| 1680 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1681 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1682 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1683 | |
| 1684 | /* spawnv has three arguments: (mode, path, argv), where |
| 1685 | argv is a list or tuple of strings. */ |
| 1686 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1687 | if (!PyArg_ParseTuple(args, "isO:spawnv", &mode, &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1688 | return NULL; |
| 1689 | if (PyList_Check(argv)) { |
| 1690 | argc = PyList_Size(argv); |
| 1691 | getitem = PyList_GetItem; |
| 1692 | } |
| 1693 | else if (PyTuple_Check(argv)) { |
| 1694 | argc = PyTuple_Size(argv); |
| 1695 | getitem = PyTuple_GetItem; |
| 1696 | } |
| 1697 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1698 | PyErr_SetString(PyExc_TypeError, "spawmv() arg 2 must be a tuple or list"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1699 | return NULL; |
| 1700 | } |
| 1701 | |
| 1702 | argvlist = PyMem_NEW(char *, argc+1); |
| 1703 | if (argvlist == NULL) |
| 1704 | return NULL; |
| 1705 | for (i = 0; i < argc; i++) { |
| 1706 | if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) { |
| 1707 | PyMem_DEL(argvlist); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1708 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1709 | "spawnv() arg 2 must contain only strings"); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1710 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | argvlist[argc] = NULL; |
| 1714 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1715 | if (mode == _OLD_P_OVERLAY) |
| 1716 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1717 | spawnval = _spawnv(mode, path, argvlist); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1718 | |
| 1719 | PyMem_DEL(argvlist); |
| 1720 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1721 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1722 | return posix_error(); |
| 1723 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1724 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1725 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1726 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1727 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1728 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | static char posix_spawnve__doc__[] = |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1733 | "spawnve(mode, path, args, env)\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1734 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1735 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 1736 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1737 | path: path of executable file\n\ |
| 1738 | args: tuple or list of arguments\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1739 | env: dictionary of strings mapping to strings"; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1740 | |
| 1741 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1742 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1743 | { |
| 1744 | char *path; |
| 1745 | PyObject *argv, *env; |
| 1746 | char **argvlist; |
| 1747 | char **envlist; |
| 1748 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 1749 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1750 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1751 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1752 | |
| 1753 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 1754 | argv is a list or tuple of strings and env is a dictionary |
| 1755 | like posix.environ. */ |
| 1756 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1757 | if (!PyArg_ParseTuple(args, "isOO:spawnve", &mode, &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1758 | return NULL; |
| 1759 | if (PyList_Check(argv)) { |
| 1760 | argc = PyList_Size(argv); |
| 1761 | getitem = PyList_GetItem; |
| 1762 | } |
| 1763 | else if (PyTuple_Check(argv)) { |
| 1764 | argc = PyTuple_Size(argv); |
| 1765 | getitem = PyTuple_GetItem; |
| 1766 | } |
| 1767 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1768 | 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] | 1769 | return NULL; |
| 1770 | } |
| 1771 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1772 | 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] | 1773 | return NULL; |
| 1774 | } |
| 1775 | |
| 1776 | argvlist = PyMem_NEW(char *, argc+1); |
| 1777 | if (argvlist == NULL) { |
| 1778 | PyErr_NoMemory(); |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | for (i = 0; i < argc; i++) { |
| 1782 | if (!PyArg_Parse((*getitem)(argv, i), |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1783 | "s;spawnve() arg 2 must contain only strings", |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1784 | &argvlist[i])) |
| 1785 | { |
| 1786 | goto fail_1; |
| 1787 | } |
| 1788 | } |
| 1789 | argvlist[argc] = NULL; |
| 1790 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1791 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1792 | envlist = PyMem_NEW(char *, i + 1); |
| 1793 | if (envlist == NULL) { |
| 1794 | PyErr_NoMemory(); |
| 1795 | goto fail_1; |
| 1796 | } |
| 1797 | envc = 0; |
| 1798 | keys = PyMapping_Keys(env); |
| 1799 | vals = PyMapping_Values(env); |
| 1800 | if (!keys || !vals) |
| 1801 | goto fail_2; |
| 1802 | |
| 1803 | for (pos = 0; pos < i; pos++) { |
| 1804 | char *p, *k, *v; |
| 1805 | |
| 1806 | key = PyList_GetItem(keys, pos); |
| 1807 | val = PyList_GetItem(vals, pos); |
| 1808 | if (!key || !val) |
| 1809 | goto fail_2; |
| 1810 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1811 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 1812 | !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] | 1813 | { |
| 1814 | goto fail_2; |
| 1815 | } |
| 1816 | p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2); |
| 1817 | if (p == NULL) { |
| 1818 | PyErr_NoMemory(); |
| 1819 | goto fail_2; |
| 1820 | } |
| 1821 | sprintf(p, "%s=%s", k, v); |
| 1822 | envlist[envc++] = p; |
| 1823 | } |
| 1824 | envlist[envc] = 0; |
| 1825 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 1826 | if (mode == _OLD_P_OVERLAY) |
| 1827 | mode = _P_OVERLAY; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1828 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 1829 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1830 | (void) posix_error(); |
| 1831 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1832 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 1833 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1834 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 1835 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1836 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 1837 | |
| 1838 | fail_2: |
| 1839 | while (--envc >= 0) |
| 1840 | PyMem_DEL(envlist[envc]); |
| 1841 | PyMem_DEL(envlist); |
| 1842 | fail_1: |
| 1843 | PyMem_DEL(argvlist); |
| 1844 | Py_XDECREF(vals); |
| 1845 | Py_XDECREF(keys); |
| 1846 | return res; |
| 1847 | } |
| 1848 | #endif /* HAVE_SPAWNV */ |
| 1849 | |
| 1850 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 1851 | #ifdef HAVE_FORK1 |
| 1852 | static char posix_fork1__doc__[] = |
| 1853 | "fork1() -> pid\n\ |
| 1854 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 1855 | \n\ |
| 1856 | Return 0 to child process and PID of child to parent process."; |
| 1857 | |
| 1858 | static PyObject * |
| 1859 | posix_fork1(self, args) |
| 1860 | PyObject *self; |
| 1861 | PyObject *args; |
| 1862 | { |
| 1863 | int pid; |
| 1864 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 1865 | return NULL; |
| 1866 | pid = fork1(); |
| 1867 | if (pid == -1) |
| 1868 | return posix_error(); |
| 1869 | PyOS_AfterFork(); |
| 1870 | return PyInt_FromLong((long)pid); |
| 1871 | } |
| 1872 | #endif |
| 1873 | |
| 1874 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1875 | #ifdef HAVE_FORK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1876 | static char posix_fork__doc__[] = |
| 1877 | "fork() -> pid\n\ |
| 1878 | Fork a child process.\n\ |
| 1879 | \n\ |
| 1880 | Return 0 to child process and PID of child to parent process."; |
| 1881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1882 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1883 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1884 | { |
| 1885 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1886 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1887 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1888 | pid = fork(); |
| 1889 | if (pid == -1) |
| 1890 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1891 | if (pid == 0) |
| 1892 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1893 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1894 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1895 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1896 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1897 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 1898 | #ifdef HAVE_PTY_H |
| 1899 | #include <pty.h> |
| 1900 | #else |
| 1901 | #ifdef HAVE_LIBUTIL_H |
| 1902 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1903 | #endif /* HAVE_LIBUTIL_H */ |
| 1904 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1905 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1906 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1907 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1908 | static char posix_openpty__doc__[] = |
| 1909 | "openpty() -> (master_fd, slave_fd)\n\ |
| 1910 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"; |
| 1911 | |
| 1912 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1913 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1914 | { |
| 1915 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1916 | #ifndef HAVE_OPENPTY |
| 1917 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1918 | #endif |
| 1919 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1920 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 1921 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1922 | |
| 1923 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1924 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 1925 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1926 | #else |
| 1927 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 1928 | if (slave_name == NULL) |
| 1929 | return posix_error(); |
| 1930 | |
| 1931 | slave_fd = open(slave_name, O_RDWR); |
| 1932 | if (slave_fd < 0) |
| 1933 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 1934 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1935 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1936 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1937 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1938 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 1939 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1940 | |
| 1941 | #ifdef HAVE_FORKPTY |
| 1942 | static char posix_forkpty__doc__[] = |
| 1943 | "forkpty() -> (pid, master_fd)\n\ |
| 1944 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 1945 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
| 1946 | To both, return fd of newly opened pseudo-terminal.\n"; |
| 1947 | |
| 1948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1949 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1950 | { |
| 1951 | int master_fd, pid; |
| 1952 | |
| 1953 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 1954 | return NULL; |
| 1955 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 1956 | if (pid == -1) |
| 1957 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 1958 | if (pid == 0) |
| 1959 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 1960 | return Py_BuildValue("(ii)", pid, master_fd); |
| 1961 | } |
| 1962 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1963 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_GETEGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1965 | static char posix_getegid__doc__[] = |
| 1966 | "getegid() -> egid\n\ |
| 1967 | Return the current process's effective group id."; |
| 1968 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1969 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1970 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1971 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1972 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1973 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1974 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1975 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1976 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1977 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1978 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_GETEUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1980 | static char posix_geteuid__doc__[] = |
| 1981 | "geteuid() -> euid\n\ |
| 1982 | Return the current process's effective user id."; |
| 1983 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1984 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1985 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1986 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1987 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1988 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1989 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1990 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1991 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 1992 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1993 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1994 | #ifdef HAVE_GETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1995 | static char posix_getgid__doc__[] = |
| 1996 | "getgid() -> gid\n\ |
| 1997 | Return the current process's group id."; |
| 1998 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1999 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2000 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2001 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2002 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2003 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2004 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2005 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2006 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2007 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2008 | |
| 2009 | static char posix_getpid__doc__[] = |
| 2010 | "getpid() -> pid\n\ |
| 2011 | Return the current process id"; |
| 2012 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2014 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2015 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2016 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2017 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2018 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2021 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2022 | #ifdef HAVE_GETGROUPS |
| 2023 | static char posix_getgroups__doc__[] = "\ |
| 2024 | getgroups() -> list of group IDs\n\ |
| 2025 | Return list of supplemental group IDs for the process."; |
| 2026 | |
| 2027 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2028 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2029 | { |
| 2030 | PyObject *result = NULL; |
| 2031 | |
| 2032 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2033 | #ifdef NGROUPS_MAX |
| 2034 | #define MAX_GROUPS NGROUPS_MAX |
| 2035 | #else |
| 2036 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2037 | #define MAX_GROUPS 64 |
| 2038 | #endif |
| 2039 | gid_t grouplist[MAX_GROUPS]; |
| 2040 | int n; |
| 2041 | |
| 2042 | n = getgroups(MAX_GROUPS, grouplist); |
| 2043 | if (n < 0) |
| 2044 | posix_error(); |
| 2045 | else { |
| 2046 | result = PyList_New(n); |
| 2047 | if (result != NULL) { |
| 2048 | PyObject *o; |
| 2049 | int i; |
| 2050 | for (i = 0; i < n; ++i) { |
| 2051 | o = PyInt_FromLong((long)grouplist[i]); |
| 2052 | if (o == NULL) { |
| 2053 | Py_DECREF(result); |
| 2054 | result = NULL; |
| 2055 | break; |
| 2056 | } |
| 2057 | PyList_SET_ITEM(result, i, o); |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | return result; |
| 2063 | } |
| 2064 | #endif |
| 2065 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2066 | #ifdef HAVE_GETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2067 | static char posix_getpgrp__doc__[] = |
| 2068 | "getpgrp() -> pgrp\n\ |
| 2069 | Return the current process group id."; |
| 2070 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2071 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2072 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2073 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2074 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2075 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2076 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2077 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2078 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2079 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2080 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2081 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2082 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2083 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2084 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2085 | #ifdef HAVE_SETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2086 | static char posix_setpgrp__doc__[] = |
| 2087 | "setpgrp() -> None\n\ |
| 2088 | Make this process a session leader."; |
| 2089 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2090 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2091 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2092 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2093 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2094 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2095 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2096 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2097 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2098 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2099 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2100 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2101 | Py_INCREF(Py_None); |
| 2102 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2105 | #endif /* HAVE_SETPGRP */ |
| 2106 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2107 | #ifdef HAVE_GETPPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2108 | static char posix_getppid__doc__[] = |
| 2109 | "getppid() -> ppid\n\ |
| 2110 | Return the parent's process id."; |
| 2111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2112 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2113 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2114 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2115 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2116 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2117 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2118 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2119 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2121 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2122 | #ifdef HAVE_GETLOGIN |
| 2123 | static char posix_getlogin__doc__[] = "\ |
| 2124 | getlogin() -> string\n\ |
| 2125 | Return the actual login name."; |
| 2126 | |
| 2127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2128 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2129 | { |
| 2130 | PyObject *result = NULL; |
| 2131 | |
| 2132 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2133 | char *name; |
| 2134 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2135 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2136 | errno = 0; |
| 2137 | name = getlogin(); |
| 2138 | if (name == NULL) { |
| 2139 | if (errno) |
| 2140 | posix_error(); |
| 2141 | else |
| 2142 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2143 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2144 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2145 | else |
| 2146 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2147 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2148 | } |
| 2149 | return result; |
| 2150 | } |
| 2151 | #endif |
| 2152 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2153 | #ifdef HAVE_GETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2154 | static char posix_getuid__doc__[] = |
| 2155 | "getuid() -> uid\n\ |
| 2156 | Return the current process's user id."; |
| 2157 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2158 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2159 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2160 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2161 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2162 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2163 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2164 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2165 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2166 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2167 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2168 | #ifdef HAVE_KILL |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2169 | static char posix_kill__doc__[] = |
| 2170 | "kill(pid, sig) -> None\n\ |
| 2171 | Kill a process with a signal."; |
| 2172 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2173 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2174 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2175 | { |
| 2176 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2177 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2178 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2179 | #if defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2180 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2181 | APIRET rc; |
| 2182 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2183 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2184 | |
| 2185 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2186 | APIRET rc; |
| 2187 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2188 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2189 | |
| 2190 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2191 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2192 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2193 | if (kill(pid, sig) == -1) |
| 2194 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2195 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2196 | Py_INCREF(Py_None); |
| 2197 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2198 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2199 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2200 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_PLOCK |
| 2202 | |
| 2203 | #ifdef HAVE_SYS_LOCK_H |
| 2204 | #include <sys/lock.h> |
| 2205 | #endif |
| 2206 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2207 | static char posix_plock__doc__[] = |
| 2208 | "plock(op) -> None\n\ |
| 2209 | Lock program segments into memory."; |
| 2210 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2211 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2212 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2213 | { |
| 2214 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2215 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2216 | return NULL; |
| 2217 | if (plock(op) == -1) |
| 2218 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2219 | Py_INCREF(Py_None); |
| 2220 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2221 | } |
| 2222 | #endif |
| 2223 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2224 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2225 | #ifdef HAVE_POPEN |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2226 | static char posix_popen__doc__[] = |
| 2227 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\ |
| 2228 | Open a pipe to/from a command returning a file object."; |
| 2229 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2230 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2231 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2232 | async_system(const char *command) |
| 2233 | { |
| 2234 | char *p, errormsg[256], args[1024]; |
| 2235 | RESULTCODES rcodes; |
| 2236 | APIRET rc; |
| 2237 | char *shell = getenv("COMSPEC"); |
| 2238 | if (!shell) |
| 2239 | shell = "cmd"; |
| 2240 | |
| 2241 | strcpy(args, shell); |
| 2242 | p = &args[ strlen(args)+1 ]; |
| 2243 | strcpy(p, "/c "); |
| 2244 | strcat(p, command); |
| 2245 | p += strlen(p) + 1; |
| 2246 | *p = '\0'; |
| 2247 | |
| 2248 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2249 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2250 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2251 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2252 | &rcodes, shell); |
| 2253 | return rc; |
| 2254 | } |
| 2255 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2256 | static FILE * |
| 2257 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2258 | { |
| 2259 | HFILE rhan, whan; |
| 2260 | FILE *retfd = NULL; |
| 2261 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2262 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2263 | if (rc != NO_ERROR) { |
| 2264 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2265 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2266 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2267 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2268 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2269 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2270 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2271 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2272 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2273 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2274 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2275 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2276 | |
| 2277 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2278 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2281 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2282 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2284 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2285 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2286 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2287 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2288 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2289 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2290 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2291 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2292 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2293 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2294 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2295 | |
| 2296 | if (async_system(command) == NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2297 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2300 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2301 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2303 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2304 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2305 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2306 | } else { |
| 2307 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2308 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2309 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2313 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2314 | { |
| 2315 | char *name; |
| 2316 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2317 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2318 | FILE *fp; |
| 2319 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2320 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2321 | return NULL; |
| 2322 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2323 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2324 | Py_END_ALLOW_THREADS |
| 2325 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2326 | return os2_error(err); |
| 2327 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2328 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2329 | if (f != NULL) |
| 2330 | PyFile_SetBufSize(f, bufsize); |
| 2331 | return f; |
| 2332 | } |
| 2333 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2334 | #elif defined(MS_WIN32) |
| 2335 | |
| 2336 | /* |
| 2337 | * Portable 'popen' replacement for Win32. |
| 2338 | * |
| 2339 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 2340 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2341 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2342 | */ |
| 2343 | |
| 2344 | #include <malloc.h> |
| 2345 | #include <io.h> |
| 2346 | #include <fcntl.h> |
| 2347 | |
| 2348 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 2349 | #define POPEN_1 1 |
| 2350 | #define POPEN_2 2 |
| 2351 | #define POPEN_3 3 |
| 2352 | #define POPEN_4 4 |
| 2353 | |
| 2354 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2355 | static int _PyPclose(FILE *file); |
| 2356 | |
| 2357 | /* |
| 2358 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2359 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2360 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2361 | */ |
| 2362 | static PyObject *_PyPopenProcs = NULL; |
| 2363 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* popen that works from a GUI. |
| 2366 | * |
| 2367 | * The result of this function is a pipe (file) connected to the |
| 2368 | * processes stdin or stdout, depending on the requested mode. |
| 2369 | */ |
| 2370 | |
| 2371 | static PyObject * |
| 2372 | posix_popen(PyObject *self, PyObject *args) |
| 2373 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2374 | PyObject *f, *s; |
| 2375 | int tm = 0; |
| 2376 | |
| 2377 | char *cmdstring; |
| 2378 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2379 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2380 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2381 | return NULL; |
| 2382 | |
| 2383 | s = PyTuple_New(0); |
| 2384 | |
| 2385 | if (*mode == 'r') |
| 2386 | tm = _O_RDONLY; |
| 2387 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2388 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2389 | return NULL; |
| 2390 | } else |
| 2391 | tm = _O_WRONLY; |
| 2392 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2393 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2394 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2395 | return NULL; |
| 2396 | } |
| 2397 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2398 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2399 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2400 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2401 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2402 | else |
| 2403 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 2404 | |
| 2405 | return f; |
| 2406 | } |
| 2407 | |
| 2408 | /* Variation on win32pipe.popen |
| 2409 | * |
| 2410 | * The result of this function is a pipe (file) connected to the |
| 2411 | * process's stdin, and a pipe connected to the process's stdout. |
| 2412 | */ |
| 2413 | |
| 2414 | static PyObject * |
| 2415 | win32_popen2(PyObject *self, PyObject *args) |
| 2416 | { |
| 2417 | PyObject *f; |
| 2418 | int tm=0; |
| 2419 | |
| 2420 | char *cmdstring; |
| 2421 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2422 | int bufsize = -1; |
| 2423 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2424 | return NULL; |
| 2425 | |
| 2426 | if (*mode == 't') |
| 2427 | tm = _O_TEXT; |
| 2428 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2429 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2430 | return NULL; |
| 2431 | } else |
| 2432 | tm = _O_BINARY; |
| 2433 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2434 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2435 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2436 | return NULL; |
| 2437 | } |
| 2438 | |
| 2439 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2440 | |
| 2441 | return f; |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2446 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2447 | * The result of this function is 3 pipes - the process's stdin, |
| 2448 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2449 | */ |
| 2450 | |
| 2451 | static PyObject * |
| 2452 | win32_popen3(PyObject *self, PyObject *args) |
| 2453 | { |
| 2454 | PyObject *f; |
| 2455 | int tm = 0; |
| 2456 | |
| 2457 | char *cmdstring; |
| 2458 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2459 | int bufsize = -1; |
| 2460 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2461 | return NULL; |
| 2462 | |
| 2463 | if (*mode == 't') |
| 2464 | tm = _O_TEXT; |
| 2465 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2466 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2467 | return NULL; |
| 2468 | } else |
| 2469 | tm = _O_BINARY; |
| 2470 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2471 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2472 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2473 | return NULL; |
| 2474 | } |
| 2475 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2476 | f = _PyPopen(cmdstring, tm, POPEN_3); |
| 2477 | |
| 2478 | return f; |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * Variation on win32pipe.popen |
| 2483 | * |
| 2484 | * The result of this function is 2 pipes - the processes stdin, |
| 2485 | * and stdout+stderr combined as a single pipe. |
| 2486 | */ |
| 2487 | |
| 2488 | static PyObject * |
| 2489 | win32_popen4(PyObject *self, PyObject *args) |
| 2490 | { |
| 2491 | PyObject *f; |
| 2492 | int tm = 0; |
| 2493 | |
| 2494 | char *cmdstring; |
| 2495 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2496 | int bufsize = -1; |
| 2497 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2498 | return NULL; |
| 2499 | |
| 2500 | if (*mode == 't') |
| 2501 | tm = _O_TEXT; |
| 2502 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2503 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2504 | return NULL; |
| 2505 | } else |
| 2506 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2507 | |
| 2508 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2509 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2510 | return NULL; |
| 2511 | } |
| 2512 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 2513 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 2514 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2515 | return f; |
| 2516 | } |
| 2517 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2518 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2519 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2520 | HANDLE hStdin, |
| 2521 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2522 | HANDLE hStderr, |
| 2523 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2524 | { |
| 2525 | PROCESS_INFORMATION piProcInfo; |
| 2526 | STARTUPINFO siStartInfo; |
| 2527 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2528 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2529 | int i; |
| 2530 | int x; |
| 2531 | |
| 2532 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2533 | char *comshell; |
| 2534 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2535 | s1 = (char *)_alloca(i); |
| 2536 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 2537 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2538 | |
| 2539 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 2540 | * then use the w9xpopen hack. |
| 2541 | */ |
| 2542 | comshell = s1 + x; |
| 2543 | while (comshell >= s1 && *comshell != '\\') |
| 2544 | --comshell; |
| 2545 | ++comshell; |
| 2546 | |
| 2547 | if (GetVersion() < 0x80000000 && |
| 2548 | _stricmp(comshell, "command.com") != 0) { |
| 2549 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2550 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
| 2551 | s2 = (char *)_alloca(x); |
| 2552 | ZeroMemory(s2, x); |
| 2553 | sprintf(s2, "%s%s%s", s1, s3, cmdstring); |
| 2554 | } |
| 2555 | else { |
| 2556 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2557 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 2558 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2559 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2560 | char modulepath[_MAX_PATH]; |
| 2561 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2562 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 2563 | for (i = x = 0; modulepath[i]; i++) |
| 2564 | if (modulepath[i] == '\\') |
| 2565 | x = i+1; |
| 2566 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2567 | /* Create the full-name to w9xpopen, so we can test it exists */ |
| 2568 | strncat(modulepath, |
| 2569 | szConsoleSpawn, |
| 2570 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2571 | -strlen(modulepath)); |
| 2572 | if (stat(modulepath, &statinfo) != 0) { |
| 2573 | /* Eeek - file-not-found - possibly an embedding |
| 2574 | situation - see if we can locate it in sys.prefix |
| 2575 | */ |
| 2576 | strncpy(modulepath, |
| 2577 | Py_GetExecPrefix(), |
| 2578 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 2579 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 2580 | strcat(modulepath, "\\"); |
| 2581 | strncat(modulepath, |
| 2582 | szConsoleSpawn, |
| 2583 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 2584 | -strlen(modulepath)); |
| 2585 | /* No where else to look - raise an easily identifiable |
| 2586 | error, rather than leaving Windows to report |
| 2587 | "file not found" - as the user is probably blissfully |
| 2588 | unaware this shim EXE is used, and it will confuse them. |
| 2589 | (well, it confused me for a while ;-) |
| 2590 | */ |
| 2591 | if (stat(modulepath, &statinfo) != 0) { |
| 2592 | PyErr_Format(PyExc_RuntimeError, |
| 2593 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2594 | "for popen to work with your shell " |
| 2595 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2596 | szConsoleSpawn); |
| 2597 | return FALSE; |
| 2598 | } |
| 2599 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2600 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
| 2601 | strlen(modulepath) + |
| 2602 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2603 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2604 | s2 = (char *)_alloca(x); |
| 2605 | ZeroMemory(s2, x); |
| 2606 | sprintf( |
| 2607 | s2, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2608 | "%s \"%s%s%s\"", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2609 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2610 | s1, |
| 2611 | s3, |
| 2612 | cmdstring); |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | /* Could be an else here to try cmd.exe / command.com in the path |
| 2617 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2618 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2619 | PyErr_SetString(PyExc_RuntimeError, |
| 2620 | "Cannot locate a COMSPEC environment variable to " |
| 2621 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2622 | return FALSE; |
| 2623 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2624 | |
| 2625 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 2626 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 2627 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 2628 | siStartInfo.hStdInput = hStdin; |
| 2629 | siStartInfo.hStdOutput = hStdout; |
| 2630 | siStartInfo.hStdError = hStderr; |
| 2631 | siStartInfo.wShowWindow = SW_HIDE; |
| 2632 | |
| 2633 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2634 | s2, |
| 2635 | NULL, |
| 2636 | NULL, |
| 2637 | TRUE, |
| 2638 | CREATE_NEW_CONSOLE, |
| 2639 | NULL, |
| 2640 | NULL, |
| 2641 | &siStartInfo, |
| 2642 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2643 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2644 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2645 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2646 | /* Return process handle */ |
| 2647 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2648 | return TRUE; |
| 2649 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 2650 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2651 | return FALSE; |
| 2652 | } |
| 2653 | |
| 2654 | /* The following code is based off of KB: Q190351 */ |
| 2655 | |
| 2656 | static PyObject * |
| 2657 | _PyPopen(char *cmdstring, int mode, int n) |
| 2658 | { |
| 2659 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 2660 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2661 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2662 | |
| 2663 | SECURITY_ATTRIBUTES saAttr; |
| 2664 | BOOL fSuccess; |
| 2665 | int fd1, fd2, fd3; |
| 2666 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2667 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2668 | PyObject *f; |
| 2669 | |
| 2670 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 2671 | saAttr.bInheritHandle = TRUE; |
| 2672 | saAttr.lpSecurityDescriptor = NULL; |
| 2673 | |
| 2674 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 2675 | return win32_error("CreatePipe", NULL); |
| 2676 | |
| 2677 | /* Create new output read handle and the input write handle. Set |
| 2678 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 2679 | * the these handles; resulting in non-closeable handles to the pipes |
| 2680 | * being created. */ |
| 2681 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2682 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 2683 | FALSE, |
| 2684 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2685 | if (!fSuccess) |
| 2686 | return win32_error("DuplicateHandle", NULL); |
| 2687 | |
| 2688 | /* Close the inheritable version of ChildStdin |
| 2689 | that we're using. */ |
| 2690 | CloseHandle(hChildStdinWr); |
| 2691 | |
| 2692 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 2693 | return win32_error("CreatePipe", NULL); |
| 2694 | |
| 2695 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2696 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 2697 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2698 | if (!fSuccess) |
| 2699 | return win32_error("DuplicateHandle", NULL); |
| 2700 | |
| 2701 | /* Close the inheritable version of ChildStdout |
| 2702 | that we're using. */ |
| 2703 | CloseHandle(hChildStdoutRd); |
| 2704 | |
| 2705 | if (n != POPEN_4) { |
| 2706 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 2707 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2708 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 2709 | hChildStderrRd, |
| 2710 | GetCurrentProcess(), |
| 2711 | &hChildStderrRdDup, 0, |
| 2712 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2713 | if (!fSuccess) |
| 2714 | return win32_error("DuplicateHandle", NULL); |
| 2715 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 2716 | CloseHandle(hChildStderrRd); |
| 2717 | } |
| 2718 | |
| 2719 | switch (n) { |
| 2720 | case POPEN_1: |
| 2721 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 2722 | case _O_WRONLY | _O_TEXT: |
| 2723 | /* Case for writing to child Stdin in text mode. */ |
| 2724 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2725 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2726 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2727 | PyFile_SetBufSize(f, 0); |
| 2728 | /* We don't care about these pipes anymore, so close them. */ |
| 2729 | CloseHandle(hChildStdoutRdDup); |
| 2730 | CloseHandle(hChildStderrRdDup); |
| 2731 | break; |
| 2732 | |
| 2733 | case _O_RDONLY | _O_TEXT: |
| 2734 | /* Case for reading from child Stdout in text mode. */ |
| 2735 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2736 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2737 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2738 | PyFile_SetBufSize(f, 0); |
| 2739 | /* We don't care about these pipes anymore, so close them. */ |
| 2740 | CloseHandle(hChildStdinWrDup); |
| 2741 | CloseHandle(hChildStderrRdDup); |
| 2742 | break; |
| 2743 | |
| 2744 | case _O_RDONLY | _O_BINARY: |
| 2745 | /* Case for readinig from child Stdout in binary mode. */ |
| 2746 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2747 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2748 | f = PyFile_FromFile(f1, cmdstring, "rb", _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(hChildStdinWrDup); |
| 2752 | CloseHandle(hChildStderrRdDup); |
| 2753 | break; |
| 2754 | |
| 2755 | case _O_WRONLY | _O_BINARY: |
| 2756 | /* Case for writing to child Stdin in binary mode. */ |
| 2757 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2758 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2759 | f = PyFile_FromFile(f1, cmdstring, "wb", _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(hChildStdoutRdDup); |
| 2763 | CloseHandle(hChildStderrRdDup); |
| 2764 | break; |
| 2765 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2766 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2767 | break; |
| 2768 | |
| 2769 | case POPEN_2: |
| 2770 | case POPEN_4: |
| 2771 | { |
| 2772 | char *m1, *m2; |
| 2773 | PyObject *p1, *p2; |
| 2774 | |
| 2775 | if (mode && _O_TEXT) { |
| 2776 | m1 = "r"; |
| 2777 | m2 = "w"; |
| 2778 | } else { |
| 2779 | m1 = "rb"; |
| 2780 | m2 = "wb"; |
| 2781 | } |
| 2782 | |
| 2783 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2784 | f1 = _fdopen(fd1, m2); |
| 2785 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2786 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2787 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2788 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2789 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2790 | PyFile_SetBufSize(p2, 0); |
| 2791 | |
| 2792 | if (n != 4) |
| 2793 | CloseHandle(hChildStderrRdDup); |
| 2794 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2795 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2796 | Py_XDECREF(p1); |
| 2797 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2798 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2799 | break; |
| 2800 | } |
| 2801 | |
| 2802 | case POPEN_3: |
| 2803 | { |
| 2804 | char *m1, *m2; |
| 2805 | PyObject *p1, *p2, *p3; |
| 2806 | |
| 2807 | if (mode && _O_TEXT) { |
| 2808 | m1 = "r"; |
| 2809 | m2 = "w"; |
| 2810 | } else { |
| 2811 | m1 = "rb"; |
| 2812 | m2 = "wb"; |
| 2813 | } |
| 2814 | |
| 2815 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 2816 | f1 = _fdopen(fd1, m2); |
| 2817 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 2818 | f2 = _fdopen(fd2, m1); |
| 2819 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 2820 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2821 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2822 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 2823 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2824 | PyFile_SetBufSize(p1, 0); |
| 2825 | PyFile_SetBufSize(p2, 0); |
| 2826 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2827 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 2828 | Py_XDECREF(p1); |
| 2829 | Py_XDECREF(p2); |
| 2830 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2831 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2832 | break; |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | if (n == POPEN_4) { |
| 2837 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2838 | hChildStdinRd, |
| 2839 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2840 | hChildStdoutWr, |
| 2841 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2842 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2843 | } |
| 2844 | else { |
| 2845 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 2846 | hChildStdinRd, |
| 2847 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2848 | hChildStderrWr, |
| 2849 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 2850 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2853 | /* |
| 2854 | * Insert the files we've created into the process dictionary |
| 2855 | * all referencing the list with the process handle and the |
| 2856 | * initial number of files (see description below in _PyPclose). |
| 2857 | * Since if _PyPclose later tried to wait on a process when all |
| 2858 | * handles weren't closed, it could create a deadlock with the |
| 2859 | * child, we spend some energy here to try to ensure that we |
| 2860 | * either insert all file handles into the dictionary or none |
| 2861 | * at all. It's a little clumsy with the various popen modes |
| 2862 | * and variable number of files involved. |
| 2863 | */ |
| 2864 | if (!_PyPopenProcs) { |
| 2865 | _PyPopenProcs = PyDict_New(); |
| 2866 | } |
| 2867 | |
| 2868 | if (_PyPopenProcs) { |
| 2869 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 2870 | int ins_rc[3]; |
| 2871 | |
| 2872 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 2873 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 2874 | |
| 2875 | procObj = PyList_New(2); |
| 2876 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 2877 | intObj = PyInt_FromLong(file_count); |
| 2878 | |
| 2879 | if (procObj && hProcessObj && intObj) { |
| 2880 | PyList_SetItem(procObj,0,hProcessObj); |
| 2881 | PyList_SetItem(procObj,1,intObj); |
| 2882 | |
| 2883 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 2884 | if (fileObj[0]) { |
| 2885 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 2886 | fileObj[0], |
| 2887 | procObj); |
| 2888 | } |
| 2889 | if (file_count >= 2) { |
| 2890 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 2891 | if (fileObj[1]) { |
| 2892 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 2893 | fileObj[1], |
| 2894 | procObj); |
| 2895 | } |
| 2896 | } |
| 2897 | if (file_count >= 3) { |
| 2898 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 2899 | if (fileObj[2]) { |
| 2900 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 2901 | fileObj[2], |
| 2902 | procObj); |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 2907 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 2908 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 2909 | /* Something failed - remove any dictionary |
| 2910 | * entries that did make it. |
| 2911 | */ |
| 2912 | if (!ins_rc[0] && fileObj[0]) { |
| 2913 | PyDict_DelItem(_PyPopenProcs, |
| 2914 | fileObj[0]); |
| 2915 | } |
| 2916 | if (!ins_rc[1] && fileObj[1]) { |
| 2917 | PyDict_DelItem(_PyPopenProcs, |
| 2918 | fileObj[1]); |
| 2919 | } |
| 2920 | if (!ins_rc[2] && fileObj[2]) { |
| 2921 | PyDict_DelItem(_PyPopenProcs, |
| 2922 | fileObj[2]); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | /* |
| 2928 | * Clean up our localized references for the dictionary keys |
| 2929 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 2930 | * that got placed in the dictionary. |
| 2931 | */ |
| 2932 | Py_XDECREF(procObj); |
| 2933 | Py_XDECREF(fileObj[0]); |
| 2934 | Py_XDECREF(fileObj[1]); |
| 2935 | Py_XDECREF(fileObj[2]); |
| 2936 | } |
| 2937 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 2938 | /* Child is launched. Close the parents copy of those pipe |
| 2939 | * handles that only the child should have open. You need to |
| 2940 | * make sure that no handles to the write end of the output pipe |
| 2941 | * are maintained in this process or else the pipe will not close |
| 2942 | * when the child process exits and the ReadFile will hang. */ |
| 2943 | |
| 2944 | if (!CloseHandle(hChildStdinRd)) |
| 2945 | return win32_error("CloseHandle", NULL); |
| 2946 | |
| 2947 | if (!CloseHandle(hChildStdoutWr)) |
| 2948 | return win32_error("CloseHandle", NULL); |
| 2949 | |
| 2950 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 2951 | return win32_error("CloseHandle", NULL); |
| 2952 | |
| 2953 | return f; |
| 2954 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2955 | |
| 2956 | /* |
| 2957 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 2958 | * 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] | 2959 | * |
| 2960 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 2961 | * input file pointer to information about the process that was |
| 2962 | * originally created by the popen* call that created the file pointer. |
| 2963 | * The dictionary uses the file pointer as a key (with one entry |
| 2964 | * inserted for each file returned by the original popen* call) and a |
| 2965 | * single list object as the value for all files from a single call. |
| 2966 | * The list object contains the Win32 process handle at [0], and a file |
| 2967 | * count at [1], which is initialized to the total number of file |
| 2968 | * handles using that list. |
| 2969 | * |
| 2970 | * This function closes whichever handle it is passed, and decrements |
| 2971 | * the file count in the dictionary for the process handle pointed to |
| 2972 | * by this file. On the last close (when the file count reaches zero), |
| 2973 | * this function will wait for the child process and then return its |
| 2974 | * exit code as the result of the close() operation. This permits the |
| 2975 | * files to be closed in any order - it is always the close() of the |
| 2976 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2977 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 2978 | |
| 2979 | /* RED_FLAG 31-Aug-2000 Tim |
| 2980 | * This is always called (today!) between a pair of |
| 2981 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 2982 | * macros. So the thread running this has no valid thread state, as |
| 2983 | * far as Python is concerned. However, this calls some Python API |
| 2984 | * functions that cannot be called safely without a valid thread |
| 2985 | * state, in particular PyDict_GetItem. |
| 2986 | * As a temporary hack (although it may last for years ...), we |
| 2987 | * *rely* on not having a valid thread state in this function, in |
| 2988 | * order to create our own "from scratch". |
| 2989 | * This will deadlock if _PyPclose is ever called by a thread |
| 2990 | * holding the global lock. |
| 2991 | */ |
| 2992 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2993 | static int _PyPclose(FILE *file) |
| 2994 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 2995 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 2996 | DWORD exit_code; |
| 2997 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 2998 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 2999 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3000 | #ifdef WITH_THREAD |
| 3001 | PyInterpreterState* pInterpreterState; |
| 3002 | PyThreadState* pThreadState; |
| 3003 | #endif |
| 3004 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3005 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3006 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3007 | */ |
| 3008 | result = fclose(file); |
| 3009 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3010 | #ifdef WITH_THREAD |
| 3011 | /* Bootstrap a valid thread state into existence. */ |
| 3012 | pInterpreterState = PyInterpreterState_New(); |
| 3013 | if (!pInterpreterState) { |
| 3014 | /* Well, we're hosed now! We don't have a thread |
| 3015 | * state, so can't call a nice error routine, or raise |
| 3016 | * an exception. Just die. |
| 3017 | */ |
| 3018 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3019 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3020 | return -1; /* unreachable */ |
| 3021 | } |
| 3022 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3023 | if (!pThreadState) { |
| 3024 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3025 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3026 | return -1; /* unreachable */ |
| 3027 | } |
| 3028 | /* Grab the global lock. Note that this will deadlock if the |
| 3029 | * current thread already has the lock! (see RED_FLAG comments |
| 3030 | * before this function) |
| 3031 | */ |
| 3032 | PyEval_RestoreThread(pThreadState); |
| 3033 | #endif |
| 3034 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3035 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3036 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3037 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3038 | fileObj)) != NULL && |
| 3039 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 3040 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 3041 | |
| 3042 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 3043 | file_count = PyInt_AsLong(intObj); |
| 3044 | |
| 3045 | if (file_count > 1) { |
| 3046 | /* Still other files referencing process */ |
| 3047 | file_count--; |
| 3048 | PyList_SetItem(procObj,1, |
| 3049 | PyInt_FromLong(file_count)); |
| 3050 | } else { |
| 3051 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3052 | if (result != EOF && |
| 3053 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 3054 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3055 | /* Possible truncation here in 16-bit environments, but |
| 3056 | * real exit codes are just the lower byte in any event. |
| 3057 | */ |
| 3058 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3059 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3060 | /* Indicate failure - this will cause the file object |
| 3061 | * to raise an I/O error and translate the last Win32 |
| 3062 | * error code from errno. We do have a problem with |
| 3063 | * last errors that overlap the normal errno table, |
| 3064 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3065 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 3066 | if (result != EOF) { |
| 3067 | /* If the error wasn't from the fclose(), then |
| 3068 | * set errno for the file object error handling. |
| 3069 | */ |
| 3070 | errno = GetLastError(); |
| 3071 | } |
| 3072 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | /* Free up the native handle at this point */ |
| 3076 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3077 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3078 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3079 | /* Remove this file pointer from dictionary */ |
| 3080 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3081 | |
| 3082 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 3083 | Py_DECREF(_PyPopenProcs); |
| 3084 | _PyPopenProcs = NULL; |
| 3085 | } |
| 3086 | |
| 3087 | } /* if object retrieval ok */ |
| 3088 | |
| 3089 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3090 | } /* if _PyPopenProcs */ |
| 3091 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3092 | #ifdef WITH_THREAD |
| 3093 | /* Tear down the thread & interpreter states. |
| 3094 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3095 | * call the thread clear & delete functions, and indeed insist on |
| 3096 | * doing that themselves. The lock must be held during the clear, but |
| 3097 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 3098 | */ |
| 3099 | PyInterpreterState_Clear(pInterpreterState); |
| 3100 | PyEval_ReleaseThread(pThreadState); |
| 3101 | PyInterpreterState_Delete(pInterpreterState); |
| 3102 | #endif |
| 3103 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3104 | return result; |
| 3105 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 3106 | |
| 3107 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3109 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3110 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3111 | char *name; |
| 3112 | char *mode = "r"; |
| 3113 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3114 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3115 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3116 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3117 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3118 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3119 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3120 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3121 | if (fp == NULL) |
| 3122 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3123 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3124 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3125 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3126 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3127 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3128 | #endif |
| 3129 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3130 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3131 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3132 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3133 | #ifdef HAVE_SETUID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3134 | static char posix_setuid__doc__[] = |
| 3135 | "setuid(uid) -> None\n\ |
| 3136 | Set the current process's user id."; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3137 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3138 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3139 | { |
| 3140 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3141 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3142 | return NULL; |
| 3143 | if (setuid(uid) < 0) |
| 3144 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3145 | Py_INCREF(Py_None); |
| 3146 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3147 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3148 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3149 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3150 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 3151 | #ifdef HAVE_SETEUID |
| 3152 | static char posix_seteuid__doc__[] = |
| 3153 | "seteuid(uid) -> None\n\ |
| 3154 | Set the current process's effective user id."; |
| 3155 | static PyObject * |
| 3156 | posix_seteuid (PyObject *self, PyObject *args) |
| 3157 | { |
| 3158 | int euid; |
| 3159 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 3160 | return NULL; |
| 3161 | } else if (seteuid(euid) < 0) { |
| 3162 | return posix_error(); |
| 3163 | } else { |
| 3164 | Py_INCREF(Py_None); |
| 3165 | return Py_None; |
| 3166 | } |
| 3167 | } |
| 3168 | #endif /* HAVE_SETEUID */ |
| 3169 | |
| 3170 | #ifdef HAVE_SETEGID |
| 3171 | static char posix_setegid__doc__[] = |
| 3172 | "setegid(gid) -> None\n\ |
| 3173 | Set the current process's effective group id."; |
| 3174 | static PyObject * |
| 3175 | posix_setegid (PyObject *self, PyObject *args) |
| 3176 | { |
| 3177 | int egid; |
| 3178 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 3179 | return NULL; |
| 3180 | } else if (setegid(egid) < 0) { |
| 3181 | return posix_error(); |
| 3182 | } else { |
| 3183 | Py_INCREF(Py_None); |
| 3184 | return Py_None; |
| 3185 | } |
| 3186 | } |
| 3187 | #endif /* HAVE_SETEGID */ |
| 3188 | |
| 3189 | #ifdef HAVE_SETREUID |
| 3190 | static char posix_setreuid__doc__[] = |
| 3191 | "seteuid(ruid, euid) -> None\n\ |
| 3192 | Set the current process's real and effective user ids."; |
| 3193 | static PyObject * |
| 3194 | posix_setreuid (PyObject *self, PyObject *args) |
| 3195 | { |
| 3196 | int ruid, euid; |
| 3197 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 3198 | return NULL; |
| 3199 | } else if (setreuid(ruid, euid) < 0) { |
| 3200 | return posix_error(); |
| 3201 | } else { |
| 3202 | Py_INCREF(Py_None); |
| 3203 | return Py_None; |
| 3204 | } |
| 3205 | } |
| 3206 | #endif /* HAVE_SETREUID */ |
| 3207 | |
| 3208 | #ifdef HAVE_SETREGID |
| 3209 | static char posix_setregid__doc__[] = |
| 3210 | "setegid(rgid, egid) -> None\n\ |
| 3211 | Set the current process's real and effective group ids."; |
| 3212 | static PyObject * |
| 3213 | posix_setregid (PyObject *self, PyObject *args) |
| 3214 | { |
| 3215 | int rgid, egid; |
| 3216 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 3217 | return NULL; |
| 3218 | } else if (setregid(rgid, egid) < 0) { |
| 3219 | return posix_error(); |
| 3220 | } else { |
| 3221 | Py_INCREF(Py_None); |
| 3222 | return Py_None; |
| 3223 | } |
| 3224 | } |
| 3225 | #endif /* HAVE_SETREGID */ |
| 3226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3227 | #ifdef HAVE_SETGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3228 | static char posix_setgid__doc__[] = |
| 3229 | "setgid(gid) -> None\n\ |
| 3230 | Set the current process's group id."; |
| 3231 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3232 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3233 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3234 | { |
| 3235 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3236 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3237 | return NULL; |
| 3238 | if (setgid(gid) < 0) |
| 3239 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3240 | Py_INCREF(Py_None); |
| 3241 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3242 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3243 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 3244 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 3245 | #ifdef HAVE_SETGROUPS |
| 3246 | static char posix_setgroups__doc__[] = |
| 3247 | "setgroups(list) -> None\n\ |
| 3248 | Set the groups of the current process to list."; |
| 3249 | |
| 3250 | static PyObject * |
| 3251 | posix_setgroups(PyObject *self, PyObject *args) |
| 3252 | { |
| 3253 | PyObject *groups; |
| 3254 | int i, len; |
| 3255 | gid_t grouplist[MAX_GROUPS]; |
| 3256 | |
| 3257 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 3258 | return NULL; |
| 3259 | if (!PySequence_Check(groups)) { |
| 3260 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 3261 | return NULL; |
| 3262 | } |
| 3263 | len = PySequence_Size(groups); |
| 3264 | if (len > MAX_GROUPS) { |
| 3265 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 3266 | return NULL; |
| 3267 | } |
| 3268 | for(i = 0; i < len; i++) { |
| 3269 | PyObject *elem; |
| 3270 | elem = PySequence_GetItem(groups, i); |
| 3271 | if (!elem) |
| 3272 | return NULL; |
| 3273 | if (!PyInt_Check(elem)) { |
| 3274 | PyErr_SetString(PyExc_TypeError, |
| 3275 | "groups must be integers"); |
| 3276 | Py_DECREF(elem); |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | /* XXX: check that value fits into gid_t. */ |
| 3280 | grouplist[i] = PyInt_AsLong(elem); |
| 3281 | Py_DECREF(elem); |
| 3282 | } |
| 3283 | |
| 3284 | if (setgroups(len, grouplist) < 0) |
| 3285 | return posix_error(); |
| 3286 | Py_INCREF(Py_None); |
| 3287 | return Py_None; |
| 3288 | } |
| 3289 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3290 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3291 | #ifdef HAVE_WAITPID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3292 | static char posix_waitpid__doc__[] = |
| 3293 | "waitpid(pid, options) -> (pid, status)\n\ |
Guido van Rossum | f377d57 | 2000-12-12 00:37:58 +0000 | [diff] [blame] | 3294 | Wait for completion of a given child process."; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3295 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3296 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3297 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3298 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3299 | int pid, options; |
| 3300 | #ifdef UNION_WAIT |
| 3301 | union wait status; |
| 3302 | #define status_i (status.w_status) |
| 3303 | #else |
| 3304 | int status; |
| 3305 | #define status_i status |
| 3306 | #endif |
| 3307 | status_i = 0; |
| 3308 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3309 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3310 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3311 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3312 | #ifdef NeXT |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3313 | pid = wait4(pid, &status, options, NULL); |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 3314 | #else |
| 3315 | pid = waitpid(pid, &status, options); |
| 3316 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3317 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3318 | if (pid == -1) |
| 3319 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3320 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3321 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3322 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3323 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3324 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3325 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3326 | #ifdef HAVE_WAIT |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3327 | static char posix_wait__doc__[] = |
| 3328 | "wait() -> (pid, status)\n\ |
| 3329 | Wait for completion of a child process."; |
| 3330 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3331 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3332 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3333 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3334 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3335 | #ifdef UNION_WAIT |
| 3336 | union wait status; |
| 3337 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3338 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3339 | int status; |
| 3340 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3341 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3342 | if (!PyArg_ParseTuple(args, ":wait")) |
| 3343 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3344 | status_i = 0; |
| 3345 | Py_BEGIN_ALLOW_THREADS |
| 3346 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3347 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 3348 | if (pid == -1) |
| 3349 | return posix_error(); |
| 3350 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 3351 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3352 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3353 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3354 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3355 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3356 | |
| 3357 | static char posix_lstat__doc__[] = |
| 3358 | "lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\ |
| 3359 | Like stat(path), but do not follow symbolic links."; |
| 3360 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3361 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3362 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3363 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3364 | #ifdef HAVE_LSTAT |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3365 | return posix_do_stat(self, args, "et:lstat", lstat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3366 | #else /* !HAVE_LSTAT */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3367 | return posix_do_stat(self, args, "et:lstat", STAT); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3368 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3372 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3373 | static char posix_readlink__doc__[] = |
| 3374 | "readlink(path) -> path\n\ |
| 3375 | Return a string representing the path to which the symbolic link points."; |
| 3376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3378 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3379 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3380 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3381 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3382 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3383 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3384 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3385 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 3386 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3387 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3388 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 3389 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3390 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3391 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3392 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3393 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3394 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3395 | #ifdef HAVE_SYMLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3396 | static char posix_symlink__doc__[] = |
| 3397 | "symlink(src, dst) -> None\n\ |
| 3398 | Create a symbolic link."; |
| 3399 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3400 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3401 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3402 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3403 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3404 | } |
| 3405 | #endif /* HAVE_SYMLINK */ |
| 3406 | |
| 3407 | |
| 3408 | #ifdef HAVE_TIMES |
| 3409 | #ifndef HZ |
| 3410 | #define HZ 60 /* Universal constant :-) */ |
| 3411 | #endif /* HZ */ |
| 3412 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3413 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 3414 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3415 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3416 | { |
| 3417 | ULONG value = 0; |
| 3418 | |
| 3419 | Py_BEGIN_ALLOW_THREADS |
| 3420 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 3421 | Py_END_ALLOW_THREADS |
| 3422 | |
| 3423 | return value; |
| 3424 | } |
| 3425 | |
| 3426 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3427 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3428 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3429 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3430 | return NULL; |
| 3431 | |
| 3432 | /* Currently Only Uptime is Provided -- Others Later */ |
| 3433 | return Py_BuildValue("ddddd", |
| 3434 | (double)0 /* t.tms_utime / HZ */, |
| 3435 | (double)0 /* t.tms_stime / HZ */, |
| 3436 | (double)0 /* t.tms_cutime / HZ */, |
| 3437 | (double)0 /* t.tms_cstime / HZ */, |
| 3438 | (double)system_uptime() / 1000); |
| 3439 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3440 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3441 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3442 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3443 | { |
| 3444 | struct tms t; |
| 3445 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3446 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3447 | return NULL; |
| 3448 | errno = 0; |
| 3449 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3450 | if (c == (clock_t) -1) |
| 3451 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3452 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3453 | (double)t.tms_utime / HZ, |
| 3454 | (double)t.tms_stime / HZ, |
| 3455 | (double)t.tms_cutime / HZ, |
| 3456 | (double)t.tms_cstime / HZ, |
| 3457 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3458 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3459 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3460 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3461 | |
| 3462 | |
Guido van Rossum | 87755a2 | 1996-09-07 00:59:43 +0000 | [diff] [blame] | 3463 | #ifdef MS_WIN32 |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3464 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3465 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3466 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3467 | { |
| 3468 | FILETIME create, exit, kernel, user; |
| 3469 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3470 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3471 | return NULL; |
| 3472 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3473 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 3474 | /* The fields of a FILETIME structure are the hi and lo part |
| 3475 | of a 64-bit value expressed in 100 nanosecond units. |
| 3476 | 1e7 is one second in such units; 1e-7 the inverse. |
| 3477 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 3478 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3479 | return Py_BuildValue( |
| 3480 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 3481 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 3482 | kernel.dwLowDateTime*1e-7), |
| 3483 | (double)(user.dwHighDateTime*429.4967296 + |
| 3484 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3485 | (double)0, |
| 3486 | (double)0, |
| 3487 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 3488 | } |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3489 | #endif /* MS_WIN32 */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3490 | |
| 3491 | #ifdef HAVE_TIMES |
Roger E. Masse | 0318fd6 | 1997-06-05 22:07:58 +0000 | [diff] [blame] | 3492 | static char posix_times__doc__[] = |
| 3493 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\ |
| 3494 | Return a tuple of floating point numbers indicating process times."; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 3495 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3496 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3497 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3498 | #ifdef HAVE_SETSID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3499 | static char posix_setsid__doc__[] = |
| 3500 | "setsid() -> None\n\ |
| 3501 | Call the system call setsid()."; |
| 3502 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3503 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3504 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3505 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3506 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3507 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3508 | if (setsid() < 0) |
| 3509 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3510 | Py_INCREF(Py_None); |
| 3511 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3512 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3513 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3514 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3515 | #ifdef HAVE_SETPGID |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3516 | static char posix_setpgid__doc__[] = |
| 3517 | "setpgid(pid, pgrp) -> None\n\ |
| 3518 | Call the system call setpgid()."; |
| 3519 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3520 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3521 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3522 | { |
| 3523 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3524 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3525 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3526 | if (setpgid(pid, pgrp) < 0) |
| 3527 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3528 | Py_INCREF(Py_None); |
| 3529 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3530 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3531 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3532 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3533 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3534 | #ifdef HAVE_TCGETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3535 | static char posix_tcgetpgrp__doc__[] = |
| 3536 | "tcgetpgrp(fd) -> pgid\n\ |
| 3537 | Return the process group associated with the terminal given by a fd."; |
| 3538 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3540 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3541 | { |
| 3542 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3543 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3544 | return NULL; |
| 3545 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3546 | if (pgid < 0) |
| 3547 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3548 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3549 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3550 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3551 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3553 | #ifdef HAVE_TCSETPGRP |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3554 | static char posix_tcsetpgrp__doc__[] = |
| 3555 | "tcsetpgrp(fd, pgid) -> None\n\ |
| 3556 | Set the process group associated with the terminal given by a fd."; |
| 3557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3558 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3559 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3560 | { |
| 3561 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3562 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3563 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3564 | if (tcsetpgrp(fd, pgid) < 0) |
| 3565 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3566 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3567 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 3568 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3569 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3571 | /* Functions acting on file descriptors */ |
| 3572 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3573 | static char posix_open__doc__[] = |
| 3574 | "open(filename, flag [, mode=0777]) -> fd\n\ |
| 3575 | Open a file (for low level IO)."; |
| 3576 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3577 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3578 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3579 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3580 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3581 | int flag; |
| 3582 | int mode = 0777; |
| 3583 | int fd; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3584 | if (!PyArg_ParseTuple(args, "eti|i", |
| 3585 | Py_FileSystemDefaultEncoding, &file, |
| 3586 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3587 | return NULL; |
| 3588 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3589 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3590 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3591 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3592 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3593 | return posix_error_with_allocated_filename(file); |
| 3594 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3595 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3598 | |
| 3599 | static char posix_close__doc__[] = |
| 3600 | "close(fd) -> None\n\ |
| 3601 | Close a file descriptor (for low level IO)."; |
| 3602 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3603 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3604 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3605 | { |
| 3606 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3607 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3608 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3609 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3610 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3611 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3612 | if (res < 0) |
| 3613 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3614 | Py_INCREF(Py_None); |
| 3615 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3618 | |
| 3619 | static char posix_dup__doc__[] = |
| 3620 | "dup(fd) -> fd2\n\ |
| 3621 | Return a duplicate of a file descriptor."; |
| 3622 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3623 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3624 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3625 | { |
| 3626 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3627 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3628 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3629 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3630 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3631 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3632 | if (fd < 0) |
| 3633 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3634 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3637 | |
| 3638 | static char posix_dup2__doc__[] = |
| 3639 | "dup2(fd, fd2) -> None\n\ |
| 3640 | Duplicate file descriptor."; |
| 3641 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3642 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3643 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3644 | { |
| 3645 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3646 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3647 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3648 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3649 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3650 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3651 | if (res < 0) |
| 3652 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3653 | Py_INCREF(Py_None); |
| 3654 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3657 | |
| 3658 | static char posix_lseek__doc__[] = |
| 3659 | "lseek(fd, pos, how) -> newpos\n\ |
| 3660 | Set the current position of a file descriptor."; |
| 3661 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3662 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3663 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3664 | { |
| 3665 | int fd, how; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3666 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3667 | LONG_LONG pos, res; |
| 3668 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3669 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3670 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3671 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3672 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3673 | return NULL; |
| 3674 | #ifdef SEEK_SET |
| 3675 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 3676 | switch (how) { |
| 3677 | case 0: how = SEEK_SET; break; |
| 3678 | case 1: how = SEEK_CUR; break; |
| 3679 | case 2: how = SEEK_END; break; |
| 3680 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3681 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3682 | |
| 3683 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3684 | pos = PyInt_AsLong(posobj); |
| 3685 | #else |
| 3686 | pos = PyLong_Check(posobj) ? |
| 3687 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 3688 | #endif |
| 3689 | if (PyErr_Occurred()) |
| 3690 | return NULL; |
| 3691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3692 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 3693 | #if defined(MS_WIN64) || defined(MS_WIN32) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3694 | res = _lseeki64(fd, pos, how); |
| 3695 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3696 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3697 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3698 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3699 | if (res < 0) |
| 3700 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3701 | |
| 3702 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3703 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3704 | #else |
| 3705 | return PyLong_FromLongLong(res); |
| 3706 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3709 | |
| 3710 | static char posix_read__doc__[] = |
| 3711 | "read(fd, buffersize) -> string\n\ |
| 3712 | Read a file descriptor."; |
| 3713 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3714 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3715 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3716 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3717 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3718 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3719 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3720 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3721 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3722 | if (buffer == NULL) |
| 3723 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3724 | Py_BEGIN_ALLOW_THREADS |
| 3725 | n = read(fd, PyString_AsString(buffer), size); |
| 3726 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3727 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3728 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3729 | return posix_error(); |
| 3730 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 3731 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3732 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3733 | return buffer; |
| 3734 | } |
| 3735 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3736 | |
| 3737 | static char posix_write__doc__[] = |
| 3738 | "write(fd, string) -> byteswritten\n\ |
| 3739 | Write a string to a file descriptor."; |
| 3740 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3741 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3742 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3743 | { |
| 3744 | int fd, size; |
| 3745 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3746 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3747 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3748 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3749 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3750 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3751 | if (size < 0) |
| 3752 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3753 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3756 | |
| 3757 | static char posix_fstat__doc__[]= |
| 3758 | "fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
| 3759 | Like stat(), but for an open file descriptor."; |
| 3760 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3761 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3762 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3763 | { |
| 3764 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3765 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3766 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3767 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3768 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3769 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3770 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3771 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3772 | if (res != 0) |
| 3773 | return posix_error(); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3774 | |
| 3775 | return _pystat_fromstructstat(st); |
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_fdopen__doc__[] = |
| 3780 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\ |
| 3781 | Return an open file object connected to a 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_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3785 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3786 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3787 | char *mode = "r"; |
| 3788 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3789 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3790 | PyObject *f; |
| 3791 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3792 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3794 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3795 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3796 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3797 | if (fp == NULL) |
| 3798 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3799 | f = PyFile_FromFile(fp, "(fdopen)", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3800 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3801 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 3802 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3805 | static char posix_isatty__doc__[] = |
| 3806 | "isatty(fd) -> Boolean\n\ |
| 3807 | 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] | 3808 | connected to the slave end of a terminal."; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3809 | |
| 3810 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 3811 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 3812 | { |
| 3813 | int fd; |
| 3814 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 3815 | return NULL; |
| 3816 | return Py_BuildValue("i", isatty(fd)); |
| 3817 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3818 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3819 | #ifdef HAVE_PIPE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3820 | static char posix_pipe__doc__[] = |
| 3821 | "pipe() -> (read_end, write_end)\n\ |
| 3822 | Create a pipe."; |
| 3823 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3824 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3825 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3826 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3827 | #if defined(PYOS_OS2) |
| 3828 | HFILE read, write; |
| 3829 | APIRET rc; |
| 3830 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3831 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3832 | return NULL; |
| 3833 | |
| 3834 | Py_BEGIN_ALLOW_THREADS |
| 3835 | rc = DosCreatePipe( &read, &write, 4096); |
| 3836 | Py_END_ALLOW_THREADS |
| 3837 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3838 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3839 | |
| 3840 | return Py_BuildValue("(ii)", read, write); |
| 3841 | #else |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3842 | #if !defined(MS_WIN32) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3843 | int fds[2]; |
| 3844 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3845 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3846 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3847 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3848 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3849 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3850 | if (res != 0) |
| 3851 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3852 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3853 | #else /* MS_WIN32 */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3854 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3855 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3856 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3857 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3858 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3859 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3860 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3861 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 3862 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3863 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3864 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 3865 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 3866 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3867 | #endif /* MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3868 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3869 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3870 | #endif /* HAVE_PIPE */ |
| 3871 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3872 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3873 | #ifdef HAVE_MKFIFO |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3874 | static char posix_mkfifo__doc__[] = |
| 3875 | "mkfifo(file, [, mode=0666]) -> None\n\ |
| 3876 | Create a FIFO (a POSIX named pipe)."; |
| 3877 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3878 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3879 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3880 | { |
| 3881 | char *file; |
| 3882 | int mode = 0666; |
| 3883 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3884 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &file, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3885 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3886 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3887 | res = mkfifo(file, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3888 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3889 | if (res < 0) |
| 3890 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3891 | Py_INCREF(Py_None); |
| 3892 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3893 | } |
| 3894 | #endif |
| 3895 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3896 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3897 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3898 | static char posix_ftruncate__doc__[] = |
| 3899 | "ftruncate(fd, length) -> None\n\ |
| 3900 | Truncate a file to a specified length."; |
| 3901 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3902 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3903 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3904 | { |
| 3905 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3906 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3907 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3908 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3909 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3910 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3911 | return NULL; |
| 3912 | |
| 3913 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 3914 | length = PyInt_AsLong(lenobj); |
| 3915 | #else |
| 3916 | length = PyLong_Check(lenobj) ? |
| 3917 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 3918 | #endif |
| 3919 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3920 | return NULL; |
| 3921 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3922 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3923 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3924 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3925 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3926 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3927 | return NULL; |
| 3928 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3929 | Py_INCREF(Py_None); |
| 3930 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3931 | } |
| 3932 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 3933 | |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 3934 | #ifdef NeXT |
| 3935 | #define HAVE_PUTENV |
| 3936 | /* Steve Spicklemire got this putenv from NeXTAnswers */ |
| 3937 | static int |
| 3938 | putenv(char *newval) |
| 3939 | { |
| 3940 | extern char **environ; |
| 3941 | |
| 3942 | static int firstTime = 1; |
| 3943 | char **ep; |
| 3944 | char *cp; |
| 3945 | int esiz; |
| 3946 | char *np; |
| 3947 | |
| 3948 | if (!(np = strchr(newval, '='))) |
| 3949 | return 1; |
| 3950 | *np = '\0'; |
| 3951 | |
| 3952 | /* look it up */ |
| 3953 | for (ep=environ ; *ep ; ep++) |
| 3954 | { |
| 3955 | /* this should always be true... */ |
| 3956 | if (cp = strchr(*ep, '=')) |
| 3957 | { |
| 3958 | *cp = '\0'; |
| 3959 | if (!strcmp(*ep, newval)) |
| 3960 | { |
| 3961 | /* got it! */ |
| 3962 | *cp = '='; |
| 3963 | break; |
| 3964 | } |
| 3965 | *cp = '='; |
| 3966 | } |
| 3967 | else |
| 3968 | { |
| 3969 | *np = '='; |
| 3970 | return 1; |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | *np = '='; |
| 3975 | if (*ep) |
| 3976 | { |
| 3977 | /* the string was already there: |
| 3978 | just replace it with the new one */ |
| 3979 | *ep = newval; |
| 3980 | return 0; |
| 3981 | } |
| 3982 | |
| 3983 | /* expand environ by one */ |
| 3984 | for (esiz=2, ep=environ ; *ep ; ep++) |
| 3985 | esiz++; |
| 3986 | if (firstTime) |
| 3987 | { |
| 3988 | char **epp; |
| 3989 | char **newenv; |
| 3990 | if (!(newenv = malloc(esiz * sizeof(char *)))) |
| 3991 | return 1; |
| 3992 | |
| 3993 | for (ep=environ, epp=newenv ; *ep ;) |
| 3994 | *epp++ = *ep++; |
| 3995 | *epp++ = newval; |
| 3996 | *epp = (char *) 0; |
| 3997 | environ = newenv; |
| 3998 | } |
| 3999 | else |
| 4000 | { |
| 4001 | if (!(environ = realloc(environ, esiz * sizeof(char *)))) |
| 4002 | return 1; |
| 4003 | environ[esiz - 2] = newval; |
| 4004 | environ[esiz - 1] = (char *) 0; |
| 4005 | firstTime = 0; |
| 4006 | } |
| 4007 | |
| 4008 | return 0; |
| 4009 | } |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 4010 | #endif /* NeXT */ |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4011 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4012 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4013 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4014 | static char posix_putenv__doc__[] = |
| 4015 | "putenv(key, value) -> None\n\ |
| 4016 | Change or add an environment variable."; |
| 4017 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4018 | /* Save putenv() parameters as values here, so we can collect them when they |
| 4019 | * get re-set with another call for the same key. */ |
| 4020 | static PyObject *posix_putenv_garbage; |
| 4021 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4023 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4024 | { |
| 4025 | char *s1, *s2; |
| 4026 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4027 | PyObject *newstr; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4028 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4029 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4030 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4031 | |
| 4032 | #if defined(PYOS_OS2) |
| 4033 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 4034 | APIRET rc; |
| 4035 | |
| 4036 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4037 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4038 | |
| 4039 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 4040 | if (rc != NO_ERROR) |
| 4041 | return os2_error(rc); |
| 4042 | |
| 4043 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 4044 | APIRET rc; |
| 4045 | |
| 4046 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 4047 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 4048 | |
| 4049 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 4050 | if (rc != NO_ERROR) |
| 4051 | return os2_error(rc); |
| 4052 | } else { |
| 4053 | #endif |
| 4054 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4055 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 4056 | newstr = PyString_FromStringAndSize(NULL, strlen(s1) + strlen(s2) + 2); |
| 4057 | if (newstr == NULL) |
| 4058 | return PyErr_NoMemory(); |
| 4059 | new = PyString_AS_STRING(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4060 | (void) sprintf(new, "%s=%s", s1, s2); |
| 4061 | if (putenv(new)) { |
| 4062 | posix_error(); |
| 4063 | return NULL; |
| 4064 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 4065 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 4066 | * this will cause previous value to be collected. This has to |
| 4067 | * happen after the real putenv() call because the old value |
| 4068 | * was still accessible until then. */ |
| 4069 | if (PyDict_SetItem(posix_putenv_garbage, |
| 4070 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 4071 | /* really not much we can do; just leak */ |
| 4072 | PyErr_Clear(); |
| 4073 | } |
| 4074 | else { |
| 4075 | Py_DECREF(newstr); |
| 4076 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4077 | |
| 4078 | #if defined(PYOS_OS2) |
| 4079 | } |
| 4080 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4081 | Py_INCREF(Py_None); |
| 4082 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4083 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4084 | #endif /* putenv */ |
| 4085 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 4086 | #ifdef HAVE_UNSETENV |
| 4087 | static char posix_unsetenv__doc__[] = |
| 4088 | "unsetenv(key) -> None\n\ |
| 4089 | Delete an environment variable."; |
| 4090 | |
| 4091 | static PyObject * |
| 4092 | posix_unsetenv(PyObject *self, PyObject *args) |
| 4093 | { |
| 4094 | char *s1; |
| 4095 | |
| 4096 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 4097 | return NULL; |
| 4098 | |
| 4099 | unsetenv(s1); |
| 4100 | |
| 4101 | /* Remove the key from posix_putenv_garbage; |
| 4102 | * this will cause it to be collected. This has to |
| 4103 | * happen after the real unsetenv() call because the |
| 4104 | * old value was still accessible until then. |
| 4105 | */ |
| 4106 | if (PyDict_DelItem(posix_putenv_garbage, |
| 4107 | PyTuple_GET_ITEM(args, 0))) { |
| 4108 | /* really not much we can do; just leak */ |
| 4109 | PyErr_Clear(); |
| 4110 | } |
| 4111 | |
| 4112 | Py_INCREF(Py_None); |
| 4113 | return Py_None; |
| 4114 | } |
| 4115 | #endif /* unsetenv */ |
| 4116 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4117 | #ifdef HAVE_STRERROR |
| 4118 | static char posix_strerror__doc__[] = |
| 4119 | "strerror(code) -> string\n\ |
| 4120 | Translate an error code to a message string."; |
| 4121 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 4122 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4123 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4124 | { |
| 4125 | int code; |
| 4126 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4127 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4128 | return NULL; |
| 4129 | message = strerror(code); |
| 4130 | if (message == NULL) { |
| 4131 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4132 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 4133 | return NULL; |
| 4134 | } |
| 4135 | return PyString_FromString(message); |
| 4136 | } |
| 4137 | #endif /* strerror */ |
| 4138 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 4139 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4140 | #ifdef HAVE_SYS_WAIT_H |
| 4141 | |
| 4142 | #ifdef WIFSTOPPED |
| 4143 | static char posix_WIFSTOPPED__doc__[] = |
| 4144 | "WIFSTOPPED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4145 | Return true if the process returning 'status' was stopped."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4146 | |
| 4147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4148 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4149 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4150 | #ifdef UNION_WAIT |
| 4151 | union wait status; |
| 4152 | #define status_i (status.w_status) |
| 4153 | #else |
| 4154 | int status; |
| 4155 | #define status_i status |
| 4156 | #endif |
| 4157 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4158 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4159 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4160 | { |
| 4161 | return NULL; |
| 4162 | } |
| 4163 | |
| 4164 | return Py_BuildValue("i", WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4165 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4166 | } |
| 4167 | #endif /* WIFSTOPPED */ |
| 4168 | |
| 4169 | #ifdef WIFSIGNALED |
| 4170 | static char posix_WIFSIGNALED__doc__[] = |
| 4171 | "WIFSIGNALED(status) -> Boolean\n\ |
Guido van Rossum | 3366d1c | 1999-02-23 18:34:43 +0000 | [diff] [blame] | 4172 | 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] | 4173 | |
| 4174 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4175 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4176 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4177 | #ifdef UNION_WAIT |
| 4178 | union wait status; |
| 4179 | #define status_i (status.w_status) |
| 4180 | #else |
| 4181 | int status; |
| 4182 | #define status_i status |
| 4183 | #endif |
| 4184 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4185 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4186 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4187 | { |
| 4188 | return NULL; |
| 4189 | } |
| 4190 | |
| 4191 | return Py_BuildValue("i", WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4192 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4193 | } |
| 4194 | #endif /* WIFSIGNALED */ |
| 4195 | |
| 4196 | #ifdef WIFEXITED |
| 4197 | static char posix_WIFEXITED__doc__[] = |
| 4198 | "WIFEXITED(status) -> Boolean\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4199 | Return true if the process returning 'status' exited using the exit()\n\ |
| 4200 | system call."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4201 | |
| 4202 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4203 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4204 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4205 | #ifdef UNION_WAIT |
| 4206 | union wait status; |
| 4207 | #define status_i (status.w_status) |
| 4208 | #else |
| 4209 | int status; |
| 4210 | #define status_i status |
| 4211 | #endif |
| 4212 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4213 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4214 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4215 | { |
| 4216 | return NULL; |
| 4217 | } |
| 4218 | |
| 4219 | return Py_BuildValue("i", WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4220 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4221 | } |
| 4222 | #endif /* WIFEXITED */ |
| 4223 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4224 | #ifdef WEXITSTATUS |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4225 | static char posix_WEXITSTATUS__doc__[] = |
| 4226 | "WEXITSTATUS(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4227 | Return the process return code from 'status'."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4228 | |
| 4229 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4230 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4231 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4232 | #ifdef UNION_WAIT |
| 4233 | union wait status; |
| 4234 | #define status_i (status.w_status) |
| 4235 | #else |
| 4236 | int status; |
| 4237 | #define status_i status |
| 4238 | #endif |
| 4239 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4240 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4241 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4242 | { |
| 4243 | return NULL; |
| 4244 | } |
| 4245 | |
| 4246 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4247 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4248 | } |
| 4249 | #endif /* WEXITSTATUS */ |
| 4250 | |
| 4251 | #ifdef WTERMSIG |
| 4252 | static char posix_WTERMSIG__doc__[] = |
| 4253 | "WTERMSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4254 | Return the signal that terminated the process that provided the 'status'\n\ |
| 4255 | value."; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4256 | |
| 4257 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4258 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4259 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4260 | #ifdef UNION_WAIT |
| 4261 | union wait status; |
| 4262 | #define status_i (status.w_status) |
| 4263 | #else |
| 4264 | int status; |
| 4265 | #define status_i status |
| 4266 | #endif |
| 4267 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4268 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4269 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4270 | { |
| 4271 | return NULL; |
| 4272 | } |
| 4273 | |
| 4274 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4275 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4276 | } |
| 4277 | #endif /* WTERMSIG */ |
| 4278 | |
| 4279 | #ifdef WSTOPSIG |
| 4280 | static char posix_WSTOPSIG__doc__[] = |
| 4281 | "WSTOPSIG(status) -> integer\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 4282 | 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] | 4283 | |
| 4284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4285 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4286 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4287 | #ifdef UNION_WAIT |
| 4288 | union wait status; |
| 4289 | #define status_i (status.w_status) |
| 4290 | #else |
| 4291 | int status; |
| 4292 | #define status_i status |
| 4293 | #endif |
| 4294 | status_i = 0; |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4295 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4296 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4297 | { |
| 4298 | return NULL; |
| 4299 | } |
| 4300 | |
| 4301 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4302 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 4303 | } |
| 4304 | #endif /* WSTOPSIG */ |
| 4305 | |
| 4306 | #endif /* HAVE_SYS_WAIT_H */ |
| 4307 | |
| 4308 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4309 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 4310 | #ifdef _SCO_DS |
| 4311 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 4312 | needed definitions in sys/statvfs.h */ |
| 4313 | #define _SVID3 |
| 4314 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4315 | #include <sys/statvfs.h> |
| 4316 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4317 | static PyObject* |
| 4318 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 4319 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 4320 | if (v == NULL) |
| 4321 | return NULL; |
| 4322 | |
| 4323 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4324 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4325 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4326 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 4327 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 4328 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 4329 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 4330 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 4331 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 4332 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4333 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4334 | #else |
| 4335 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 4336 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 4337 | PyStructSequence_SET_ITEM(v, 2, |
| 4338 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
| 4339 | PyStructSequence_SET_ITEM(v, 3, |
| 4340 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 4341 | PyStructSequence_SET_ITEM(v, 4, |
| 4342 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
| 4343 | PyStructSequence_SET_ITEM(v, 5, |
| 4344 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
| 4345 | PyStructSequence_SET_ITEM(v, 6, |
| 4346 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
| 4347 | PyStructSequence_SET_ITEM(v, 7, |
| 4348 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 4349 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 4350 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 4351 | #endif |
| 4352 | |
| 4353 | return v; |
| 4354 | } |
| 4355 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4356 | static char posix_fstatvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4357 | "fstatvfs(fd) -> \n\ |
| 4358 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4359 | Perform an fstatvfs system call on the given fd."; |
| 4360 | |
| 4361 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4362 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4363 | { |
| 4364 | int fd, res; |
| 4365 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4366 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4367 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4368 | return NULL; |
| 4369 | Py_BEGIN_ALLOW_THREADS |
| 4370 | res = fstatvfs(fd, &st); |
| 4371 | Py_END_ALLOW_THREADS |
| 4372 | if (res != 0) |
| 4373 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4374 | |
| 4375 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4376 | } |
| 4377 | #endif /* HAVE_FSTATVFS */ |
| 4378 | |
| 4379 | |
| 4380 | #if defined(HAVE_STATVFS) |
| 4381 | #include <sys/statvfs.h> |
| 4382 | |
| 4383 | static char posix_statvfs__doc__[] = |
Guido van Rossum | 0c9608c | 1999-02-03 16:32:37 +0000 | [diff] [blame] | 4384 | "statvfs(path) -> \n\ |
| 4385 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4386 | Perform a statvfs system call on the given path."; |
| 4387 | |
| 4388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4389 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4390 | { |
| 4391 | char *path; |
| 4392 | int res; |
| 4393 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4394 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4395 | return NULL; |
| 4396 | Py_BEGIN_ALLOW_THREADS |
| 4397 | res = statvfs(path, &st); |
| 4398 | Py_END_ALLOW_THREADS |
| 4399 | if (res != 0) |
| 4400 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 4401 | |
| 4402 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4403 | } |
| 4404 | #endif /* HAVE_STATVFS */ |
| 4405 | |
| 4406 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4407 | #ifdef HAVE_TEMPNAM |
| 4408 | static char posix_tempnam__doc__[] = "\ |
| 4409 | tempnam([dir[, prefix]]) -> string\n\ |
| 4410 | Return a unique name for a temporary file.\n\ |
| 4411 | The directory and a short may be specified as strings; they may be omitted\n\ |
| 4412 | or None if not needed."; |
| 4413 | |
| 4414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4415 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4416 | { |
| 4417 | PyObject *result = NULL; |
| 4418 | char *dir = NULL; |
| 4419 | char *pfx = NULL; |
| 4420 | char *name; |
| 4421 | |
| 4422 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 4423 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4424 | |
| 4425 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4426 | "tempnam is a potential security risk to your program") < 0) |
| 4427 | return NULL; |
| 4428 | |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4429 | #ifdef MS_WIN32 |
| 4430 | name = _tempnam(dir, pfx); |
| 4431 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4432 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 4433 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4434 | if (name == NULL) |
| 4435 | return PyErr_NoMemory(); |
| 4436 | result = PyString_FromString(name); |
| 4437 | free(name); |
| 4438 | return result; |
| 4439 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 4440 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4441 | |
| 4442 | |
| 4443 | #ifdef HAVE_TMPFILE |
| 4444 | static char posix_tmpfile__doc__[] = "\ |
| 4445 | tmpfile() -> file object\n\ |
| 4446 | Create a temporary file with no directory entries."; |
| 4447 | |
| 4448 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4449 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4450 | { |
| 4451 | FILE *fp; |
| 4452 | |
| 4453 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 4454 | return NULL; |
| 4455 | fp = tmpfile(); |
| 4456 | if (fp == NULL) |
| 4457 | return posix_error(); |
| 4458 | return PyFile_FromFile(fp, "<tmpfile>", "w+", fclose); |
| 4459 | } |
| 4460 | #endif |
| 4461 | |
| 4462 | |
| 4463 | #ifdef HAVE_TMPNAM |
| 4464 | static char posix_tmpnam__doc__[] = "\ |
| 4465 | tmpnam() -> string\n\ |
| 4466 | Return a unique name for a temporary file."; |
| 4467 | |
| 4468 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4469 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4470 | { |
| 4471 | char buffer[L_tmpnam]; |
| 4472 | char *name; |
| 4473 | |
| 4474 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 4475 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 4476 | |
| 4477 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 4478 | "tmpnam is a potential security risk to your program") < 0) |
| 4479 | return NULL; |
| 4480 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4481 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4482 | name = tmpnam_r(buffer); |
| 4483 | #else |
| 4484 | name = tmpnam(buffer); |
| 4485 | #endif |
| 4486 | if (name == NULL) { |
| 4487 | PyErr_SetObject(PyExc_OSError, |
| 4488 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 4489 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4490 | "unexpected NULL from tmpnam_r" |
| 4491 | #else |
| 4492 | "unexpected NULL from tmpnam" |
| 4493 | #endif |
| 4494 | )); |
| 4495 | return NULL; |
| 4496 | } |
| 4497 | return PyString_FromString(buffer); |
| 4498 | } |
| 4499 | #endif |
| 4500 | |
| 4501 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4502 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 4503 | * It maps strings representing configuration variable names to |
| 4504 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 4505 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4506 | * rarely-used constants. There are three separate tables that use |
| 4507 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 4508 | * |
| 4509 | * This code is always included, even if none of the interfaces that |
| 4510 | * need it are included. The #if hackery needed to avoid it would be |
| 4511 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4512 | */ |
| 4513 | struct constdef { |
| 4514 | char *name; |
| 4515 | long value; |
| 4516 | }; |
| 4517 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4518 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4519 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 4520 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4521 | { |
| 4522 | if (PyInt_Check(arg)) { |
| 4523 | *valuep = PyInt_AS_LONG(arg); |
| 4524 | return 1; |
| 4525 | } |
| 4526 | if (PyString_Check(arg)) { |
| 4527 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4528 | size_t lo = 0; |
| 4529 | size_t mid; |
| 4530 | size_t hi = tablesize; |
| 4531 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4532 | char *confname = PyString_AS_STRING(arg); |
| 4533 | while (lo < hi) { |
| 4534 | mid = (lo + hi) / 2; |
| 4535 | cmp = strcmp(confname, table[mid].name); |
| 4536 | if (cmp < 0) |
| 4537 | hi = mid; |
| 4538 | else if (cmp > 0) |
| 4539 | lo = mid + 1; |
| 4540 | else { |
| 4541 | *valuep = table[mid].value; |
| 4542 | return 1; |
| 4543 | } |
| 4544 | } |
| 4545 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 4546 | } |
| 4547 | else |
| 4548 | PyErr_SetString(PyExc_TypeError, |
| 4549 | "configuration names must be strings or integers"); |
| 4550 | return 0; |
| 4551 | } |
| 4552 | |
| 4553 | |
| 4554 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 4555 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4556 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 4557 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 4558 | #endif |
| 4559 | #ifdef _PC_ABI_ASYNC_IO |
| 4560 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 4561 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4562 | #ifdef _PC_ASYNC_IO |
| 4563 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 4564 | #endif |
| 4565 | #ifdef _PC_CHOWN_RESTRICTED |
| 4566 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 4567 | #endif |
| 4568 | #ifdef _PC_FILESIZEBITS |
| 4569 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 4570 | #endif |
| 4571 | #ifdef _PC_LAST |
| 4572 | {"PC_LAST", _PC_LAST}, |
| 4573 | #endif |
| 4574 | #ifdef _PC_LINK_MAX |
| 4575 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 4576 | #endif |
| 4577 | #ifdef _PC_MAX_CANON |
| 4578 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 4579 | #endif |
| 4580 | #ifdef _PC_MAX_INPUT |
| 4581 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 4582 | #endif |
| 4583 | #ifdef _PC_NAME_MAX |
| 4584 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 4585 | #endif |
| 4586 | #ifdef _PC_NO_TRUNC |
| 4587 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 4588 | #endif |
| 4589 | #ifdef _PC_PATH_MAX |
| 4590 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 4591 | #endif |
| 4592 | #ifdef _PC_PIPE_BUF |
| 4593 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 4594 | #endif |
| 4595 | #ifdef _PC_PRIO_IO |
| 4596 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 4597 | #endif |
| 4598 | #ifdef _PC_SOCK_MAXBUF |
| 4599 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 4600 | #endif |
| 4601 | #ifdef _PC_SYNC_IO |
| 4602 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 4603 | #endif |
| 4604 | #ifdef _PC_VDISABLE |
| 4605 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 4606 | #endif |
| 4607 | }; |
| 4608 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4609 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4610 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4611 | { |
| 4612 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 4613 | sizeof(posix_constants_pathconf) |
| 4614 | / sizeof(struct constdef)); |
| 4615 | } |
| 4616 | #endif |
| 4617 | |
| 4618 | #ifdef HAVE_FPATHCONF |
| 4619 | static char posix_fpathconf__doc__[] = "\ |
| 4620 | fpathconf(fd, name) -> integer\n\ |
| 4621 | Return the configuration limit name for the file descriptor fd.\n\ |
| 4622 | If there is no limit, return -1."; |
| 4623 | |
| 4624 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4625 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4626 | { |
| 4627 | PyObject *result = NULL; |
| 4628 | int name, fd; |
| 4629 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4630 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 4631 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4632 | long limit; |
| 4633 | |
| 4634 | errno = 0; |
| 4635 | limit = fpathconf(fd, name); |
| 4636 | if (limit == -1 && errno != 0) |
| 4637 | posix_error(); |
| 4638 | else |
| 4639 | result = PyInt_FromLong(limit); |
| 4640 | } |
| 4641 | return result; |
| 4642 | } |
| 4643 | #endif |
| 4644 | |
| 4645 | |
| 4646 | #ifdef HAVE_PATHCONF |
| 4647 | static char posix_pathconf__doc__[] = "\ |
| 4648 | pathconf(path, name) -> integer\n\ |
| 4649 | Return the configuration limit name for the file or directory path.\n\ |
| 4650 | If there is no limit, return -1."; |
| 4651 | |
| 4652 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4653 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4654 | { |
| 4655 | PyObject *result = NULL; |
| 4656 | int name; |
| 4657 | char *path; |
| 4658 | |
| 4659 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 4660 | conv_path_confname, &name)) { |
| 4661 | long limit; |
| 4662 | |
| 4663 | errno = 0; |
| 4664 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4665 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4666 | if (errno == EINVAL) |
| 4667 | /* could be a path or name problem */ |
| 4668 | posix_error(); |
| 4669 | else |
| 4670 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4671 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4672 | else |
| 4673 | result = PyInt_FromLong(limit); |
| 4674 | } |
| 4675 | return result; |
| 4676 | } |
| 4677 | #endif |
| 4678 | |
| 4679 | #ifdef HAVE_CONFSTR |
| 4680 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4681 | #ifdef _CS_ARCHITECTURE |
| 4682 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 4683 | #endif |
| 4684 | #ifdef _CS_HOSTNAME |
| 4685 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 4686 | #endif |
| 4687 | #ifdef _CS_HW_PROVIDER |
| 4688 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 4689 | #endif |
| 4690 | #ifdef _CS_HW_SERIAL |
| 4691 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 4692 | #endif |
| 4693 | #ifdef _CS_INITTAB_NAME |
| 4694 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 4695 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4696 | #ifdef _CS_LFS64_CFLAGS |
| 4697 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 4698 | #endif |
| 4699 | #ifdef _CS_LFS64_LDFLAGS |
| 4700 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 4701 | #endif |
| 4702 | #ifdef _CS_LFS64_LIBS |
| 4703 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 4704 | #endif |
| 4705 | #ifdef _CS_LFS64_LINTFLAGS |
| 4706 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 4707 | #endif |
| 4708 | #ifdef _CS_LFS_CFLAGS |
| 4709 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 4710 | #endif |
| 4711 | #ifdef _CS_LFS_LDFLAGS |
| 4712 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 4713 | #endif |
| 4714 | #ifdef _CS_LFS_LIBS |
| 4715 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 4716 | #endif |
| 4717 | #ifdef _CS_LFS_LINTFLAGS |
| 4718 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 4719 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4720 | #ifdef _CS_MACHINE |
| 4721 | {"CS_MACHINE", _CS_MACHINE}, |
| 4722 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4723 | #ifdef _CS_PATH |
| 4724 | {"CS_PATH", _CS_PATH}, |
| 4725 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4726 | #ifdef _CS_RELEASE |
| 4727 | {"CS_RELEASE", _CS_RELEASE}, |
| 4728 | #endif |
| 4729 | #ifdef _CS_SRPC_DOMAIN |
| 4730 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 4731 | #endif |
| 4732 | #ifdef _CS_SYSNAME |
| 4733 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 4734 | #endif |
| 4735 | #ifdef _CS_VERSION |
| 4736 | {"CS_VERSION", _CS_VERSION}, |
| 4737 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4738 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 4739 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 4740 | #endif |
| 4741 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 4742 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 4743 | #endif |
| 4744 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 4745 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 4746 | #endif |
| 4747 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 4748 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 4749 | #endif |
| 4750 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 4751 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 4752 | #endif |
| 4753 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 4754 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 4755 | #endif |
| 4756 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 4757 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 4758 | #endif |
| 4759 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 4760 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 4761 | #endif |
| 4762 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 4763 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 4764 | #endif |
| 4765 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 4766 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 4767 | #endif |
| 4768 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 4769 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 4770 | #endif |
| 4771 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 4772 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 4773 | #endif |
| 4774 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 4775 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 4776 | #endif |
| 4777 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 4778 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 4779 | #endif |
| 4780 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 4781 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 4782 | #endif |
| 4783 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 4784 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 4785 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4786 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 4787 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 4788 | #endif |
| 4789 | #ifdef _MIPS_CS_BASE |
| 4790 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 4791 | #endif |
| 4792 | #ifdef _MIPS_CS_HOSTID |
| 4793 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 4794 | #endif |
| 4795 | #ifdef _MIPS_CS_HW_NAME |
| 4796 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 4797 | #endif |
| 4798 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 4799 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 4800 | #endif |
| 4801 | #ifdef _MIPS_CS_OSREL_MAJ |
| 4802 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 4803 | #endif |
| 4804 | #ifdef _MIPS_CS_OSREL_MIN |
| 4805 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 4806 | #endif |
| 4807 | #ifdef _MIPS_CS_OSREL_PATCH |
| 4808 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 4809 | #endif |
| 4810 | #ifdef _MIPS_CS_OS_NAME |
| 4811 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 4812 | #endif |
| 4813 | #ifdef _MIPS_CS_OS_PROVIDER |
| 4814 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 4815 | #endif |
| 4816 | #ifdef _MIPS_CS_PROCESSORS |
| 4817 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 4818 | #endif |
| 4819 | #ifdef _MIPS_CS_SERIAL |
| 4820 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 4821 | #endif |
| 4822 | #ifdef _MIPS_CS_VENDOR |
| 4823 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 4824 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4825 | }; |
| 4826 | |
| 4827 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4828 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4829 | { |
| 4830 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 4831 | sizeof(posix_constants_confstr) |
| 4832 | / sizeof(struct constdef)); |
| 4833 | } |
| 4834 | |
| 4835 | static char posix_confstr__doc__[] = "\ |
| 4836 | confstr(name) -> string\n\ |
| 4837 | Return a string-valued system configuration variable."; |
| 4838 | |
| 4839 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4840 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4841 | { |
| 4842 | PyObject *result = NULL; |
| 4843 | int name; |
| 4844 | char buffer[64]; |
| 4845 | |
| 4846 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 4847 | int len = confstr(name, buffer, sizeof(buffer)); |
| 4848 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4849 | errno = 0; |
| 4850 | if (len == 0) { |
| 4851 | if (errno != 0) |
| 4852 | posix_error(); |
| 4853 | else |
| 4854 | result = PyString_FromString(""); |
| 4855 | } |
| 4856 | else { |
| 4857 | if (len >= sizeof(buffer)) { |
| 4858 | result = PyString_FromStringAndSize(NULL, len); |
| 4859 | if (result != NULL) |
| 4860 | confstr(name, PyString_AS_STRING(result), len+1); |
| 4861 | } |
| 4862 | else |
| 4863 | result = PyString_FromString(buffer); |
| 4864 | } |
| 4865 | } |
| 4866 | return result; |
| 4867 | } |
| 4868 | #endif |
| 4869 | |
| 4870 | |
| 4871 | #ifdef HAVE_SYSCONF |
| 4872 | static struct constdef posix_constants_sysconf[] = { |
| 4873 | #ifdef _SC_2_CHAR_TERM |
| 4874 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 4875 | #endif |
| 4876 | #ifdef _SC_2_C_BIND |
| 4877 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 4878 | #endif |
| 4879 | #ifdef _SC_2_C_DEV |
| 4880 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 4881 | #endif |
| 4882 | #ifdef _SC_2_C_VERSION |
| 4883 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 4884 | #endif |
| 4885 | #ifdef _SC_2_FORT_DEV |
| 4886 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 4887 | #endif |
| 4888 | #ifdef _SC_2_FORT_RUN |
| 4889 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 4890 | #endif |
| 4891 | #ifdef _SC_2_LOCALEDEF |
| 4892 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 4893 | #endif |
| 4894 | #ifdef _SC_2_SW_DEV |
| 4895 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 4896 | #endif |
| 4897 | #ifdef _SC_2_UPE |
| 4898 | {"SC_2_UPE", _SC_2_UPE}, |
| 4899 | #endif |
| 4900 | #ifdef _SC_2_VERSION |
| 4901 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 4902 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4903 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 4904 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 4905 | #endif |
| 4906 | #ifdef _SC_ACL |
| 4907 | {"SC_ACL", _SC_ACL}, |
| 4908 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4909 | #ifdef _SC_AIO_LISTIO_MAX |
| 4910 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 4911 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4912 | #ifdef _SC_AIO_MAX |
| 4913 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 4914 | #endif |
| 4915 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 4916 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 4917 | #endif |
| 4918 | #ifdef _SC_ARG_MAX |
| 4919 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 4920 | #endif |
| 4921 | #ifdef _SC_ASYNCHRONOUS_IO |
| 4922 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 4923 | #endif |
| 4924 | #ifdef _SC_ATEXIT_MAX |
| 4925 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 4926 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4927 | #ifdef _SC_AUDIT |
| 4928 | {"SC_AUDIT", _SC_AUDIT}, |
| 4929 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4930 | #ifdef _SC_AVPHYS_PAGES |
| 4931 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 4932 | #endif |
| 4933 | #ifdef _SC_BC_BASE_MAX |
| 4934 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 4935 | #endif |
| 4936 | #ifdef _SC_BC_DIM_MAX |
| 4937 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 4938 | #endif |
| 4939 | #ifdef _SC_BC_SCALE_MAX |
| 4940 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 4941 | #endif |
| 4942 | #ifdef _SC_BC_STRING_MAX |
| 4943 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 4944 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 4945 | #ifdef _SC_CAP |
| 4946 | {"SC_CAP", _SC_CAP}, |
| 4947 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 4948 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 4949 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 4950 | #endif |
| 4951 | #ifdef _SC_CHAR_BIT |
| 4952 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 4953 | #endif |
| 4954 | #ifdef _SC_CHAR_MAX |
| 4955 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 4956 | #endif |
| 4957 | #ifdef _SC_CHAR_MIN |
| 4958 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 4959 | #endif |
| 4960 | #ifdef _SC_CHILD_MAX |
| 4961 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 4962 | #endif |
| 4963 | #ifdef _SC_CLK_TCK |
| 4964 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 4965 | #endif |
| 4966 | #ifdef _SC_COHER_BLKSZ |
| 4967 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 4968 | #endif |
| 4969 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 4970 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 4971 | #endif |
| 4972 | #ifdef _SC_DCACHE_ASSOC |
| 4973 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 4974 | #endif |
| 4975 | #ifdef _SC_DCACHE_BLKSZ |
| 4976 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 4977 | #endif |
| 4978 | #ifdef _SC_DCACHE_LINESZ |
| 4979 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 4980 | #endif |
| 4981 | #ifdef _SC_DCACHE_SZ |
| 4982 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 4983 | #endif |
| 4984 | #ifdef _SC_DCACHE_TBLKSZ |
| 4985 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 4986 | #endif |
| 4987 | #ifdef _SC_DELAYTIMER_MAX |
| 4988 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 4989 | #endif |
| 4990 | #ifdef _SC_EQUIV_CLASS_MAX |
| 4991 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 4992 | #endif |
| 4993 | #ifdef _SC_EXPR_NEST_MAX |
| 4994 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 4995 | #endif |
| 4996 | #ifdef _SC_FSYNC |
| 4997 | {"SC_FSYNC", _SC_FSYNC}, |
| 4998 | #endif |
| 4999 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 5000 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 5001 | #endif |
| 5002 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 5003 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 5004 | #endif |
| 5005 | #ifdef _SC_ICACHE_ASSOC |
| 5006 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 5007 | #endif |
| 5008 | #ifdef _SC_ICACHE_BLKSZ |
| 5009 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 5010 | #endif |
| 5011 | #ifdef _SC_ICACHE_LINESZ |
| 5012 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 5013 | #endif |
| 5014 | #ifdef _SC_ICACHE_SZ |
| 5015 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 5016 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5017 | #ifdef _SC_INF |
| 5018 | {"SC_INF", _SC_INF}, |
| 5019 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5020 | #ifdef _SC_INT_MAX |
| 5021 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 5022 | #endif |
| 5023 | #ifdef _SC_INT_MIN |
| 5024 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 5025 | #endif |
| 5026 | #ifdef _SC_IOV_MAX |
| 5027 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 5028 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5029 | #ifdef _SC_IP_SECOPTS |
| 5030 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 5031 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5032 | #ifdef _SC_JOB_CONTROL |
| 5033 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 5034 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5035 | #ifdef _SC_KERN_POINTERS |
| 5036 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 5037 | #endif |
| 5038 | #ifdef _SC_KERN_SIM |
| 5039 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 5040 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5041 | #ifdef _SC_LINE_MAX |
| 5042 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 5043 | #endif |
| 5044 | #ifdef _SC_LOGIN_NAME_MAX |
| 5045 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 5046 | #endif |
| 5047 | #ifdef _SC_LOGNAME_MAX |
| 5048 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 5049 | #endif |
| 5050 | #ifdef _SC_LONG_BIT |
| 5051 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 5052 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5053 | #ifdef _SC_MAC |
| 5054 | {"SC_MAC", _SC_MAC}, |
| 5055 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5056 | #ifdef _SC_MAPPED_FILES |
| 5057 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 5058 | #endif |
| 5059 | #ifdef _SC_MAXPID |
| 5060 | {"SC_MAXPID", _SC_MAXPID}, |
| 5061 | #endif |
| 5062 | #ifdef _SC_MB_LEN_MAX |
| 5063 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 5064 | #endif |
| 5065 | #ifdef _SC_MEMLOCK |
| 5066 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 5067 | #endif |
| 5068 | #ifdef _SC_MEMLOCK_RANGE |
| 5069 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 5070 | #endif |
| 5071 | #ifdef _SC_MEMORY_PROTECTION |
| 5072 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 5073 | #endif |
| 5074 | #ifdef _SC_MESSAGE_PASSING |
| 5075 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 5076 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5077 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 5078 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 5079 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5080 | #ifdef _SC_MQ_OPEN_MAX |
| 5081 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 5082 | #endif |
| 5083 | #ifdef _SC_MQ_PRIO_MAX |
| 5084 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 5085 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5086 | #ifdef _SC_NACLS_MAX |
| 5087 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 5088 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5089 | #ifdef _SC_NGROUPS_MAX |
| 5090 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 5091 | #endif |
| 5092 | #ifdef _SC_NL_ARGMAX |
| 5093 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 5094 | #endif |
| 5095 | #ifdef _SC_NL_LANGMAX |
| 5096 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 5097 | #endif |
| 5098 | #ifdef _SC_NL_MSGMAX |
| 5099 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 5100 | #endif |
| 5101 | #ifdef _SC_NL_NMAX |
| 5102 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 5103 | #endif |
| 5104 | #ifdef _SC_NL_SETMAX |
| 5105 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 5106 | #endif |
| 5107 | #ifdef _SC_NL_TEXTMAX |
| 5108 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 5109 | #endif |
| 5110 | #ifdef _SC_NPROCESSORS_CONF |
| 5111 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 5112 | #endif |
| 5113 | #ifdef _SC_NPROCESSORS_ONLN |
| 5114 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 5115 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5116 | #ifdef _SC_NPROC_CONF |
| 5117 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 5118 | #endif |
| 5119 | #ifdef _SC_NPROC_ONLN |
| 5120 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 5121 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5122 | #ifdef _SC_NZERO |
| 5123 | {"SC_NZERO", _SC_NZERO}, |
| 5124 | #endif |
| 5125 | #ifdef _SC_OPEN_MAX |
| 5126 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 5127 | #endif |
| 5128 | #ifdef _SC_PAGESIZE |
| 5129 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 5130 | #endif |
| 5131 | #ifdef _SC_PAGE_SIZE |
| 5132 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 5133 | #endif |
| 5134 | #ifdef _SC_PASS_MAX |
| 5135 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 5136 | #endif |
| 5137 | #ifdef _SC_PHYS_PAGES |
| 5138 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 5139 | #endif |
| 5140 | #ifdef _SC_PII |
| 5141 | {"SC_PII", _SC_PII}, |
| 5142 | #endif |
| 5143 | #ifdef _SC_PII_INTERNET |
| 5144 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 5145 | #endif |
| 5146 | #ifdef _SC_PII_INTERNET_DGRAM |
| 5147 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 5148 | #endif |
| 5149 | #ifdef _SC_PII_INTERNET_STREAM |
| 5150 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 5151 | #endif |
| 5152 | #ifdef _SC_PII_OSI |
| 5153 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 5154 | #endif |
| 5155 | #ifdef _SC_PII_OSI_CLTS |
| 5156 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 5157 | #endif |
| 5158 | #ifdef _SC_PII_OSI_COTS |
| 5159 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 5160 | #endif |
| 5161 | #ifdef _SC_PII_OSI_M |
| 5162 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 5163 | #endif |
| 5164 | #ifdef _SC_PII_SOCKET |
| 5165 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 5166 | #endif |
| 5167 | #ifdef _SC_PII_XTI |
| 5168 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 5169 | #endif |
| 5170 | #ifdef _SC_POLL |
| 5171 | {"SC_POLL", _SC_POLL}, |
| 5172 | #endif |
| 5173 | #ifdef _SC_PRIORITIZED_IO |
| 5174 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 5175 | #endif |
| 5176 | #ifdef _SC_PRIORITY_SCHEDULING |
| 5177 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 5178 | #endif |
| 5179 | #ifdef _SC_REALTIME_SIGNALS |
| 5180 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 5181 | #endif |
| 5182 | #ifdef _SC_RE_DUP_MAX |
| 5183 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 5184 | #endif |
| 5185 | #ifdef _SC_RTSIG_MAX |
| 5186 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 5187 | #endif |
| 5188 | #ifdef _SC_SAVED_IDS |
| 5189 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 5190 | #endif |
| 5191 | #ifdef _SC_SCHAR_MAX |
| 5192 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 5193 | #endif |
| 5194 | #ifdef _SC_SCHAR_MIN |
| 5195 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 5196 | #endif |
| 5197 | #ifdef _SC_SELECT |
| 5198 | {"SC_SELECT", _SC_SELECT}, |
| 5199 | #endif |
| 5200 | #ifdef _SC_SEMAPHORES |
| 5201 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 5202 | #endif |
| 5203 | #ifdef _SC_SEM_NSEMS_MAX |
| 5204 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 5205 | #endif |
| 5206 | #ifdef _SC_SEM_VALUE_MAX |
| 5207 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 5208 | #endif |
| 5209 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 5210 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 5211 | #endif |
| 5212 | #ifdef _SC_SHRT_MAX |
| 5213 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 5214 | #endif |
| 5215 | #ifdef _SC_SHRT_MIN |
| 5216 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 5217 | #endif |
| 5218 | #ifdef _SC_SIGQUEUE_MAX |
| 5219 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 5220 | #endif |
| 5221 | #ifdef _SC_SIGRT_MAX |
| 5222 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 5223 | #endif |
| 5224 | #ifdef _SC_SIGRT_MIN |
| 5225 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 5226 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5227 | #ifdef _SC_SOFTPOWER |
| 5228 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 5229 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5230 | #ifdef _SC_SPLIT_CACHE |
| 5231 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 5232 | #endif |
| 5233 | #ifdef _SC_SSIZE_MAX |
| 5234 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 5235 | #endif |
| 5236 | #ifdef _SC_STACK_PROT |
| 5237 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 5238 | #endif |
| 5239 | #ifdef _SC_STREAM_MAX |
| 5240 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 5241 | #endif |
| 5242 | #ifdef _SC_SYNCHRONIZED_IO |
| 5243 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 5244 | #endif |
| 5245 | #ifdef _SC_THREADS |
| 5246 | {"SC_THREADS", _SC_THREADS}, |
| 5247 | #endif |
| 5248 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 5249 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 5250 | #endif |
| 5251 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 5252 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 5253 | #endif |
| 5254 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 5255 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 5256 | #endif |
| 5257 | #ifdef _SC_THREAD_KEYS_MAX |
| 5258 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 5259 | #endif |
| 5260 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 5261 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 5262 | #endif |
| 5263 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 5264 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 5265 | #endif |
| 5266 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 5267 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 5268 | #endif |
| 5269 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 5270 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 5271 | #endif |
| 5272 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 5273 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 5274 | #endif |
| 5275 | #ifdef _SC_THREAD_STACK_MIN |
| 5276 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 5277 | #endif |
| 5278 | #ifdef _SC_THREAD_THREADS_MAX |
| 5279 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 5280 | #endif |
| 5281 | #ifdef _SC_TIMERS |
| 5282 | {"SC_TIMERS", _SC_TIMERS}, |
| 5283 | #endif |
| 5284 | #ifdef _SC_TIMER_MAX |
| 5285 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 5286 | #endif |
| 5287 | #ifdef _SC_TTY_NAME_MAX |
| 5288 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 5289 | #endif |
| 5290 | #ifdef _SC_TZNAME_MAX |
| 5291 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 5292 | #endif |
| 5293 | #ifdef _SC_T_IOV_MAX |
| 5294 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 5295 | #endif |
| 5296 | #ifdef _SC_UCHAR_MAX |
| 5297 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 5298 | #endif |
| 5299 | #ifdef _SC_UINT_MAX |
| 5300 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 5301 | #endif |
| 5302 | #ifdef _SC_UIO_MAXIOV |
| 5303 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 5304 | #endif |
| 5305 | #ifdef _SC_ULONG_MAX |
| 5306 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 5307 | #endif |
| 5308 | #ifdef _SC_USHRT_MAX |
| 5309 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 5310 | #endif |
| 5311 | #ifdef _SC_VERSION |
| 5312 | {"SC_VERSION", _SC_VERSION}, |
| 5313 | #endif |
| 5314 | #ifdef _SC_WORD_BIT |
| 5315 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 5316 | #endif |
| 5317 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 5318 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 5319 | #endif |
| 5320 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 5321 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 5322 | #endif |
| 5323 | #ifdef _SC_XBS5_LP64_OFF64 |
| 5324 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 5325 | #endif |
| 5326 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 5327 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 5328 | #endif |
| 5329 | #ifdef _SC_XOPEN_CRYPT |
| 5330 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 5331 | #endif |
| 5332 | #ifdef _SC_XOPEN_ENH_I18N |
| 5333 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 5334 | #endif |
| 5335 | #ifdef _SC_XOPEN_LEGACY |
| 5336 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 5337 | #endif |
| 5338 | #ifdef _SC_XOPEN_REALTIME |
| 5339 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 5340 | #endif |
| 5341 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 5342 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 5343 | #endif |
| 5344 | #ifdef _SC_XOPEN_SHM |
| 5345 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 5346 | #endif |
| 5347 | #ifdef _SC_XOPEN_UNIX |
| 5348 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 5349 | #endif |
| 5350 | #ifdef _SC_XOPEN_VERSION |
| 5351 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 5352 | #endif |
| 5353 | #ifdef _SC_XOPEN_XCU_VERSION |
| 5354 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 5355 | #endif |
| 5356 | #ifdef _SC_XOPEN_XPG2 |
| 5357 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 5358 | #endif |
| 5359 | #ifdef _SC_XOPEN_XPG3 |
| 5360 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 5361 | #endif |
| 5362 | #ifdef _SC_XOPEN_XPG4 |
| 5363 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 5364 | #endif |
| 5365 | }; |
| 5366 | |
| 5367 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5368 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5369 | { |
| 5370 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 5371 | sizeof(posix_constants_sysconf) |
| 5372 | / sizeof(struct constdef)); |
| 5373 | } |
| 5374 | |
| 5375 | static char posix_sysconf__doc__[] = "\ |
| 5376 | sysconf(name) -> integer\n\ |
| 5377 | Return an integer-valued system configuration variable."; |
| 5378 | |
| 5379 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5380 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5381 | { |
| 5382 | PyObject *result = NULL; |
| 5383 | int name; |
| 5384 | |
| 5385 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 5386 | int value; |
| 5387 | |
| 5388 | errno = 0; |
| 5389 | value = sysconf(name); |
| 5390 | if (value == -1 && errno != 0) |
| 5391 | posix_error(); |
| 5392 | else |
| 5393 | result = PyInt_FromLong(value); |
| 5394 | } |
| 5395 | return result; |
| 5396 | } |
| 5397 | #endif |
| 5398 | |
| 5399 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5400 | /* This code is used to ensure that the tables of configuration value names |
| 5401 | * are in sorted order as required by conv_confname(), and also to build the |
| 5402 | * the exported dictionaries that are used to publish information about the |
| 5403 | * names available on the host platform. |
| 5404 | * |
| 5405 | * Sorting the table at runtime ensures that the table is properly ordered |
| 5406 | * when used, even for platforms we're not able to test on. It also makes |
| 5407 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5408 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5409 | |
| 5410 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5411 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5412 | { |
| 5413 | const struct constdef *c1 = |
| 5414 | (const struct constdef *) v1; |
| 5415 | const struct constdef *c2 = |
| 5416 | (const struct constdef *) v2; |
| 5417 | |
| 5418 | return strcmp(c1->name, c2->name); |
| 5419 | } |
| 5420 | |
| 5421 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5422 | setup_confname_table(struct constdef *table, size_t tablesize, |
| 5423 | char *tablename, PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5424 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5425 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5426 | size_t i; |
| 5427 | int status; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5428 | |
| 5429 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 5430 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5431 | if (d == NULL) |
| 5432 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5433 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5434 | for (i=0; i < tablesize; ++i) { |
| 5435 | PyObject *o = PyInt_FromLong(table[i].value); |
| 5436 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 5437 | Py_XDECREF(o); |
| 5438 | Py_DECREF(d); |
| 5439 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5440 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5441 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5442 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 5443 | status = PyDict_SetItemString(moddict, tablename, d); |
| 5444 | Py_DECREF(d); |
| 5445 | return status; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5448 | /* Return -1 on failure, 0 on success. */ |
| 5449 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5450 | setup_confname_tables(PyObject *moddict) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5451 | { |
| 5452 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5453 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5454 | sizeof(posix_constants_pathconf) |
| 5455 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5456 | "pathconf_names", moddict)) |
| 5457 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5458 | #endif |
| 5459 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5460 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5461 | sizeof(posix_constants_confstr) |
| 5462 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5463 | "confstr_names", moddict)) |
| 5464 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5465 | #endif |
| 5466 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5467 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5468 | sizeof(posix_constants_sysconf) |
| 5469 | / sizeof(struct constdef), |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5470 | "sysconf_names", moddict)) |
| 5471 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5472 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5473 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5474 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5475 | |
| 5476 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5477 | static char posix_abort__doc__[] = "\ |
| 5478 | abort() -> does not return!\n\ |
| 5479 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
| 5480 | in the hardest way possible on the hosting operating system."; |
| 5481 | |
| 5482 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5483 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5484 | { |
| 5485 | if (!PyArg_ParseTuple(args, ":abort")) |
| 5486 | return NULL; |
| 5487 | abort(); |
| 5488 | /*NOTREACHED*/ |
| 5489 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 5490 | return NULL; |
| 5491 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5492 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5493 | #ifdef MS_WIN32 |
| 5494 | static char win32_startfile__doc__[] = "\ |
| 5495 | startfile(filepath) - Start a file with its associated application.\n\ |
| 5496 | \n\ |
| 5497 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 5498 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 5499 | with whatever application (if any) its extension is associated.\n\ |
| 5500 | \n\ |
| 5501 | startfile returns as soon as the associated application is launched.\n\ |
| 5502 | There is no option to wait for the application to close, and no way\n\ |
| 5503 | to retrieve the application's exit status.\n\ |
| 5504 | \n\ |
| 5505 | The filepath is relative to the current directory. If you want to use\n\ |
| 5506 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
| 5507 | the underlying Win32 ShellExecute function doesn't work if it is."; |
| 5508 | |
| 5509 | static PyObject * |
| 5510 | win32_startfile(PyObject *self, PyObject *args) |
| 5511 | { |
| 5512 | char *filepath; |
| 5513 | HINSTANCE rc; |
| 5514 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 5515 | return NULL; |
| 5516 | Py_BEGIN_ALLOW_THREADS |
| 5517 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 5518 | Py_END_ALLOW_THREADS |
| 5519 | if (rc <= (HINSTANCE)32) |
| 5520 | return win32_error("startfile", filepath); |
| 5521 | Py_INCREF(Py_None); |
| 5522 | return Py_None; |
| 5523 | } |
| 5524 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5525 | |
| 5526 | static PyMethodDef posix_methods[] = { |
| 5527 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 5528 | #ifdef HAVE_TTYNAME |
| 5529 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 5530 | #endif |
| 5531 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 5532 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5533 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5534 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5535 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 5536 | #ifdef HAVE_CHROOT |
| 5537 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 5538 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5539 | #ifdef HAVE_CTERMID |
| 5540 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 5541 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5542 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5543 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 5544 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5545 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5546 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5547 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5548 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 5549 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 5550 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5551 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5552 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5553 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5554 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5555 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5556 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5557 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 5558 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 5559 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5560 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5561 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5562 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5563 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5564 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5565 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5566 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5567 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5568 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5569 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5570 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 5571 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 5572 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5573 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5574 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5575 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5576 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5577 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5578 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 5579 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5580 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5581 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5582 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 5583 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5584 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 5585 | #ifdef HAVE_FORK1 |
| 5586 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 5587 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5588 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5589 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5590 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5591 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5592 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5593 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5594 | #ifdef HAVE_FORKPTY |
| 5595 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 5596 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5597 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5598 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5599 | #endif /* HAVE_GETEGID */ |
| 5600 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5601 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5602 | #endif /* HAVE_GETEUID */ |
| 5603 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5604 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5605 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5606 | #ifdef HAVE_GETGROUPS |
| 5607 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 5608 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5609 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5610 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5611 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5612 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5613 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5614 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5615 | #endif /* HAVE_GETPPID */ |
| 5616 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5617 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5618 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5619 | #ifdef HAVE_GETLOGIN |
| 5620 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 5621 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5622 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5623 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5624 | #endif /* HAVE_KILL */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5625 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5626 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5627 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5628 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5629 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5630 | #ifdef MS_WIN32 |
| 5631 | {"popen2", win32_popen2, METH_VARARGS}, |
| 5632 | {"popen3", win32_popen3, METH_VARARGS}, |
| 5633 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 5634 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5635 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5636 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5637 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5638 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5639 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5640 | #ifdef HAVE_SETEUID |
| 5641 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 5642 | #endif /* HAVE_SETEUID */ |
| 5643 | #ifdef HAVE_SETEGID |
| 5644 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 5645 | #endif /* HAVE_SETEGID */ |
| 5646 | #ifdef HAVE_SETREUID |
| 5647 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 5648 | #endif /* HAVE_SETREUID */ |
| 5649 | #ifdef HAVE_SETREGID |
| 5650 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 5651 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5652 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5653 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5654 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5655 | #ifdef HAVE_SETGROUPS |
| 5656 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 5657 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5658 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5659 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5660 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5661 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5662 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5663 | #endif /* HAVE_WAIT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5664 | #ifdef HAVE_WAITPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5665 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5666 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5667 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5668 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5669 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5670 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5671 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5672 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5673 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5674 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5675 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5676 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5677 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5678 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5679 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 5680 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 5681 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 5682 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 5683 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 5684 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 5685 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 5686 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 5687 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5688 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5689 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5690 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5691 | #endif |
| 5692 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5693 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5694 | #endif |
| 5695 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5696 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5697 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5698 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5699 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5700 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5701 | #ifdef HAVE_UNSETENV |
| 5702 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 5703 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5704 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5705 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5706 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5707 | #ifdef HAVE_FSYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5708 | {"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5709 | #endif |
| 5710 | #ifdef HAVE_FDATASYNC |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5711 | {"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 5712 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5713 | #ifdef HAVE_SYS_WAIT_H |
| 5714 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5715 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5716 | #endif /* WIFSTOPPED */ |
| 5717 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5718 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5719 | #endif /* WIFSIGNALED */ |
| 5720 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5721 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5722 | #endif /* WIFEXITED */ |
| 5723 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5724 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5725 | #endif /* WEXITSTATUS */ |
| 5726 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5727 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5728 | #endif /* WTERMSIG */ |
| 5729 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5730 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5731 | #endif /* WSTOPSIG */ |
| 5732 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5733 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5734 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5735 | #endif |
| 5736 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5737 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5738 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 5739 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5740 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 5741 | #endif |
| 5742 | #ifdef HAVE_TEMPNAM |
| 5743 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 5744 | #endif |
| 5745 | #ifdef HAVE_TMPNAM |
| 5746 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 5747 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5748 | #ifdef HAVE_CONFSTR |
| 5749 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 5750 | #endif |
| 5751 | #ifdef HAVE_SYSCONF |
| 5752 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 5753 | #endif |
| 5754 | #ifdef HAVE_FPATHCONF |
| 5755 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 5756 | #endif |
| 5757 | #ifdef HAVE_PATHCONF |
| 5758 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 5759 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5760 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5761 | #ifdef MS_WIN32 |
| 5762 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 5763 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5764 | {NULL, NULL} /* Sentinel */ |
| 5765 | }; |
| 5766 | |
| 5767 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5768 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5769 | ins(PyObject *d, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5770 | { |
| 5771 | PyObject* v = PyInt_FromLong(value); |
| 5772 | if (!v || PyDict_SetItemString(d, symbol, v) < 0) |
| 5773 | return -1; /* triggers fatal error */ |
| 5774 | |
| 5775 | Py_DECREF(v); |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5779 | #if defined(PYOS_OS2) |
| 5780 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
| 5781 | static int insertvalues(PyObject *d) |
| 5782 | { |
| 5783 | APIRET rc; |
| 5784 | ULONG values[QSV_MAX+1]; |
| 5785 | PyObject *v; |
| 5786 | char *ver, tmp[10]; |
| 5787 | |
| 5788 | Py_BEGIN_ALLOW_THREADS |
| 5789 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 5790 | Py_END_ALLOW_THREADS |
| 5791 | |
| 5792 | if (rc != NO_ERROR) { |
| 5793 | os2_error(rc); |
| 5794 | return -1; |
| 5795 | } |
| 5796 | |
| 5797 | if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 5798 | if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 5799 | if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 5800 | if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 5801 | if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 5802 | if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 5803 | if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1; |
| 5804 | |
| 5805 | switch (values[QSV_VERSION_MINOR]) { |
| 5806 | case 0: ver = "2.00"; break; |
| 5807 | case 10: ver = "2.10"; break; |
| 5808 | case 11: ver = "2.11"; break; |
| 5809 | case 30: ver = "3.00"; break; |
| 5810 | case 40: ver = "4.00"; break; |
| 5811 | case 50: ver = "5.00"; break; |
| 5812 | default: |
| 5813 | sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR], |
| 5814 | values[QSV_VERSION_MINOR]); |
| 5815 | ver = &tmp[0]; |
| 5816 | } |
| 5817 | |
| 5818 | /* Add Indicator of the Version of the Operating System */ |
| 5819 | v = PyString_FromString(ver); |
| 5820 | if (!v || PyDict_SetItemString(d, "version", v) < 0) |
| 5821 | return -1; |
| 5822 | Py_DECREF(v); |
| 5823 | |
| 5824 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 5825 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 5826 | tmp[1] = ':'; |
| 5827 | tmp[2] = '\0'; |
| 5828 | |
| 5829 | v = PyString_FromString(tmp); |
| 5830 | if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0) |
| 5831 | return -1; |
| 5832 | Py_DECREF(v); |
| 5833 | |
| 5834 | return 0; |
| 5835 | } |
| 5836 | #endif |
| 5837 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5838 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5839 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5840 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5841 | #ifdef F_OK |
| 5842 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
| 5843 | #endif |
| 5844 | #ifdef R_OK |
| 5845 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
| 5846 | #endif |
| 5847 | #ifdef W_OK |
| 5848 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
| 5849 | #endif |
| 5850 | #ifdef X_OK |
| 5851 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
| 5852 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5853 | #ifdef NGROUPS_MAX |
| 5854 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 5855 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5856 | #ifdef TMP_MAX |
| 5857 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 5858 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5859 | #ifdef WNOHANG |
| 5860 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
| 5861 | #endif |
| 5862 | #ifdef O_RDONLY |
| 5863 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 5864 | #endif |
| 5865 | #ifdef O_WRONLY |
| 5866 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 5867 | #endif |
| 5868 | #ifdef O_RDWR |
| 5869 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 5870 | #endif |
| 5871 | #ifdef O_NDELAY |
| 5872 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 5873 | #endif |
| 5874 | #ifdef O_NONBLOCK |
| 5875 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 5876 | #endif |
| 5877 | #ifdef O_APPEND |
| 5878 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 5879 | #endif |
| 5880 | #ifdef O_DSYNC |
| 5881 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 5882 | #endif |
| 5883 | #ifdef O_RSYNC |
| 5884 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 5885 | #endif |
| 5886 | #ifdef O_SYNC |
| 5887 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 5888 | #endif |
| 5889 | #ifdef O_NOCTTY |
| 5890 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 5891 | #endif |
| 5892 | #ifdef O_CREAT |
| 5893 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 5894 | #endif |
| 5895 | #ifdef O_EXCL |
| 5896 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 5897 | #endif |
| 5898 | #ifdef O_TRUNC |
| 5899 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 5900 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 5901 | #ifdef O_BINARY |
| 5902 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 5903 | #endif |
| 5904 | #ifdef O_TEXT |
| 5905 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 5906 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 5907 | #ifdef O_LARGEFILE |
| 5908 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 5909 | #endif |
| 5910 | |
| 5911 | /* GNU extensions. */ |
| 5912 | #ifdef O_DIRECT |
| 5913 | /* Direct disk access. */ |
| 5914 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 5915 | #endif |
| 5916 | #ifdef O_DIRECTORY |
| 5917 | /* Must be a directory. */ |
| 5918 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 5919 | #endif |
| 5920 | #ifdef O_NOFOLLOW |
| 5921 | /* Do not follow links. */ |
| 5922 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 5923 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5924 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5925 | #ifdef HAVE_SPAWNV |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 5926 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 5927 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 5928 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 5929 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 5930 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 5931 | #endif |
| 5932 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5933 | #if defined(PYOS_OS2) |
| 5934 | if (insertvalues(d)) return -1; |
| 5935 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5936 | return 0; |
| 5937 | } |
| 5938 | |
| 5939 | |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5940 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5941 | #define INITFUNC initnt |
| 5942 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5943 | |
| 5944 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5945 | #define INITFUNC initos2 |
| 5946 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 5947 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5948 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5949 | #define INITFUNC initposix |
| 5950 | #define MODNAME "posix" |
| 5951 | #endif |
| 5952 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 5953 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5954 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5955 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5956 | PyObject *m, *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5957 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5958 | m = Py_InitModule4(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5959 | posix_methods, |
| 5960 | posix__doc__, |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5961 | (PyObject *)NULL, |
| 5962 | PYTHON_API_VERSION); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5963 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5964 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5965 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5966 | v = convertenviron(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5967 | if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 5968 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5969 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5970 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5971 | if (all_ins(d)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 5972 | return; |
| 5973 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5974 | if (setup_confname_tables(d)) |
| 5975 | return; |
| 5976 | |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 5977 | PyDict_SetItemString(d, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5978 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5979 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 5980 | if (posix_putenv_garbage == NULL) |
| 5981 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 5982 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5983 | |
| 5984 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 5985 | PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType); |
| 5986 | |
| 5987 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 5988 | PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5989 | } |