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