Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 14 | /* See also ../Dos/dosmodule.c */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structseq.h" |
| 18 | |
| 19 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 20 | "This module provides access to operating system functionality that is\n\ |
| 21 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 22 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 25 | #if defined(PYOS_OS2) |
| 26 | #define INCL_DOS |
| 27 | #define INCL_DOSERRORS |
| 28 | #define INCL_DOSPROCESS |
| 29 | #define INCL_NOPMAPI |
| 30 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 31 | #if defined(PYCC_GCC) |
| 32 | #include <ctype.h> |
| 33 | #include <io.h> |
| 34 | #include <stdio.h> |
| 35 | #include <process.h> |
| 36 | #include "osdefs.h" |
| 37 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 43 | #ifdef HAVE_SYS_WAIT_H |
| 44 | #include <sys/wait.h> /* For WNOHANG */ |
| 45 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SIGNAL_H |
| 48 | #include <signal.h> |
| 49 | #endif |
| 50 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #ifdef HAVE_FCNTL_H |
| 52 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 53 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 55 | #ifdef HAVE_GRP_H |
| 56 | #include <grp.h> |
| 57 | #endif |
| 58 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 59 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 60 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 62 | #include <process.h> |
| 63 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 64 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 65 | #define HAVE_GETCWD 1 |
| 66 | #define HAVE_OPENDIR 1 |
| 67 | #define HAVE_SYSTEM 1 |
| 68 | #if defined(__OS2__) |
| 69 | #define HAVE_EXECV 1 |
| 70 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 71 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 72 | #include <process.h> |
| 73 | #else |
| 74 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 75 | #define HAVE_EXECV 1 |
| 76 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 77 | #define HAVE_OPENDIR 1 |
| 78 | #define HAVE_PIPE 1 |
| 79 | #define HAVE_POPEN 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #define HAVE_WAIT 1 |
| 82 | #else |
| 83 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 84 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 85 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 86 | #define HAVE_EXECV 1 |
| 87 | #define HAVE_PIPE 1 |
| 88 | #define HAVE_POPEN 1 |
| 89 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 90 | #define HAVE_CWAIT 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 91 | #else |
| 92 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 93 | /* Everything needed is defined in PC/os2emx/pyconfig.h */ |
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 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 98 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 99 | #define HAVE_FORK1 1 |
| 100 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 101 | #define HAVE_GETCWD 1 |
| 102 | #define HAVE_GETEGID 1 |
| 103 | #define HAVE_GETEUID 1 |
| 104 | #define HAVE_GETGID 1 |
| 105 | #define HAVE_GETPPID 1 |
| 106 | #define HAVE_GETUID 1 |
| 107 | #define HAVE_KILL 1 |
| 108 | #define HAVE_OPENDIR 1 |
| 109 | #define HAVE_PIPE 1 |
| 110 | #define HAVE_POPEN 1 |
| 111 | #define HAVE_SYSTEM 1 |
| 112 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 113 | #define HAVE_TTYNAME 1 |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 114 | #endif /* PYOS_OS2 && PYCC_GCC */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 115 | #endif /* _MSC_VER */ |
| 116 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 117 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 118 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 120 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 122 | #if defined(sun) && !defined(__SVR4) |
| 123 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 124 | extern int rename(const char *, const char *); |
| 125 | extern int pclose(FILE *); |
| 126 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 127 | extern int fsync(int); |
| 128 | extern int lstat(const char *, struct stat *); |
| 129 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 132 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 133 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 134 | (default) */ |
| 135 | extern char *ctermid_r(char *); |
| 136 | #endif |
| 137 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 138 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 139 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 140 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 141 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 142 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 143 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 145 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 147 | #endif |
| 148 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 149 | extern int chdir(char *); |
| 150 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 151 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 152 | extern int chdir(const char *); |
| 153 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 154 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 155 | #ifdef __BORLANDC__ |
| 156 | extern int chmod(const char *, int); |
| 157 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 158 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 159 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int chown(const char *, uid_t, gid_t); |
| 161 | extern char *getcwd(char *, int); |
| 162 | extern char *strerror(int); |
| 163 | extern int link(const char *, const char *); |
| 164 | extern int rename(const char *, const char *); |
| 165 | extern int stat(const char *, struct stat *); |
| 166 | extern int unlink(const char *); |
| 167 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 170 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 171 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 173 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 176 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | #ifdef HAVE_UTIME_H |
| 179 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 180 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 182 | #ifdef HAVE_SYS_UTIME_H |
| 183 | #include <sys/utime.h> |
| 184 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 185 | #endif /* HAVE_SYS_UTIME_H */ |
| 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #ifdef HAVE_SYS_TIMES_H |
| 188 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 189 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 190 | |
| 191 | #ifdef HAVE_SYS_PARAM_H |
| 192 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 193 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | |
| 195 | #ifdef HAVE_SYS_UTSNAME_H |
| 196 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
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> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 225 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 226 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 228 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 229 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 230 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 233 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 235 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 237 | #ifndef MAXPATHLEN |
| 238 | #define MAXPATHLEN 1024 |
| 239 | #endif /* MAXPATHLEN */ |
| 240 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 241 | #ifdef UNION_WAIT |
| 242 | /* Emulate some macros on systems that have a union instead of macros */ |
| 243 | |
| 244 | #ifndef WIFEXITED |
| 245 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 246 | #endif |
| 247 | |
| 248 | #ifndef WEXITSTATUS |
| 249 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 250 | #endif |
| 251 | |
| 252 | #ifndef WTERMSIG |
| 253 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 254 | #endif |
| 255 | |
| 256 | #endif /* UNION_WAIT */ |
| 257 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 258 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 259 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 260 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 261 | #define USE_CTERMID_R |
| 262 | #endif |
| 263 | |
| 264 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 265 | #define USE_TMPNAM_R |
| 266 | #endif |
| 267 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 268 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 269 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 270 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 271 | # define STAT _stati64 |
| 272 | # define FSTAT _fstati64 |
| 273 | # define STRUCT_STAT struct _stati64 |
| 274 | #else |
| 275 | # define STAT stat |
| 276 | # define FSTAT fstat |
| 277 | # define STRUCT_STAT struct stat |
| 278 | #endif |
| 279 | |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 280 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 281 | #include <sys/mkdev.h> |
| 282 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 284 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 285 | #ifdef WITH_NEXT_FRAMEWORK |
| 286 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 287 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 288 | */ |
| 289 | #include <crt_externs.h> |
| 290 | static char **environ; |
| 291 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 293 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 295 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 296 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 298 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 300 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | if (d == NULL) |
| 302 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 303 | #ifdef WITH_NEXT_FRAMEWORK |
| 304 | if (environ == NULL) |
| 305 | environ = *_NSGetEnviron(); |
| 306 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 307 | if (environ == NULL) |
| 308 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 309 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 311 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 312 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | char *p = strchr(*e, '='); |
| 314 | if (p == NULL) |
| 315 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 316 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 317 | if (k == NULL) { |
| 318 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 320 | } |
| 321 | v = PyString_FromString(p+1); |
| 322 | if (v == NULL) { |
| 323 | PyErr_Clear(); |
| 324 | Py_DECREF(k); |
| 325 | continue; |
| 326 | } |
| 327 | if (PyDict_GetItem(d, k) == NULL) { |
| 328 | if (PyDict_SetItem(d, k, v) != 0) |
| 329 | PyErr_Clear(); |
| 330 | } |
| 331 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 332 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 334 | #if defined(PYOS_OS2) |
| 335 | { |
| 336 | APIRET rc; |
| 337 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 338 | |
| 339 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 340 | 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] | 341 | PyObject *v = PyString_FromString(buffer); |
| 342 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 343 | Py_DECREF(v); |
| 344 | } |
| 345 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 346 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 347 | PyObject *v = PyString_FromString(buffer); |
| 348 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 349 | Py_DECREF(v); |
| 350 | } |
| 351 | } |
| 352 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | return d; |
| 354 | } |
| 355 | |
| 356 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 358 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 359 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 360 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 361 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 362 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 364 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 365 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 366 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 367 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 370 | #ifdef Py_WIN_WIDE_FILENAMES |
| 371 | static PyObject * |
| 372 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 373 | { |
| 374 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 375 | } |
| 376 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 377 | |
| 378 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 379 | static PyObject * |
| 380 | posix_error_with_allocated_filename(char* name) |
| 381 | { |
| 382 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 383 | PyMem_Free(name); |
| 384 | return rc; |
| 385 | } |
| 386 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 387 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 388 | static PyObject * |
| 389 | win32_error(char* function, char* filename) |
| 390 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 391 | /* XXX We should pass the function name along in the future. |
| 392 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 393 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 394 | Windows error object, which is non-trivial. |
| 395 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 396 | errno = GetLastError(); |
| 397 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 398 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 399 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 400 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 401 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 402 | |
| 403 | #ifdef Py_WIN_WIDE_FILENAMES |
| 404 | static PyObject * |
| 405 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 406 | { |
| 407 | /* XXX - see win32_error for comments on 'function' */ |
| 408 | errno = GetLastError(); |
| 409 | if (filename) |
| 410 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 411 | else |
| 412 | return PyErr_SetFromWindowsErr(errno); |
| 413 | } |
| 414 | |
| 415 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 416 | { |
| 417 | /* XXX Perhaps we should make this API an alias of |
| 418 | PyObject_Unicode() instead ?! */ |
| 419 | if (PyUnicode_CheckExact(obj)) { |
| 420 | Py_INCREF(obj); |
| 421 | return obj; |
| 422 | } |
| 423 | if (PyUnicode_Check(obj)) { |
| 424 | /* For a Unicode subtype that's not a Unicode object, |
| 425 | return a true Unicode object with the same data. */ |
| 426 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 427 | PyUnicode_GET_SIZE(obj)); |
| 428 | } |
| 429 | return PyUnicode_FromEncodedObject(obj, |
| 430 | Py_FileSystemDefaultEncoding, |
| 431 | "strict"); |
| 432 | } |
| 433 | |
| 434 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 435 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 436 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 438 | #if defined(PYOS_OS2) |
| 439 | /********************************************************************** |
| 440 | * Helper Function to Trim and Format OS/2 Messages |
| 441 | **********************************************************************/ |
| 442 | static void |
| 443 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 444 | { |
| 445 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 446 | |
| 447 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 448 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 449 | |
| 450 | while (lastc > msgbuf && isspace(*lastc)) |
| 451 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 452 | } |
| 453 | |
| 454 | /* Add Optional Reason Text */ |
| 455 | if (reason) { |
| 456 | strcat(msgbuf, " : "); |
| 457 | strcat(msgbuf, reason); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /********************************************************************** |
| 462 | * Decode an OS/2 Operating System Error Code |
| 463 | * |
| 464 | * A convenience function to lookup an OS/2 error code and return a |
| 465 | * text message we can use to raise a Python exception. |
| 466 | * |
| 467 | * Notes: |
| 468 | * The messages for errors returned from the OS/2 kernel reside in |
| 469 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 470 | * |
| 471 | **********************************************************************/ |
| 472 | static char * |
| 473 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 474 | { |
| 475 | APIRET rc; |
| 476 | ULONG msglen; |
| 477 | |
| 478 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 479 | Py_BEGIN_ALLOW_THREADS |
| 480 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 481 | errorcode, "oso001.msg", &msglen); |
| 482 | Py_END_ALLOW_THREADS |
| 483 | |
| 484 | if (rc == NO_ERROR) |
| 485 | os2_formatmsg(msgbuf, msglen, reason); |
| 486 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 487 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 488 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 489 | |
| 490 | return msgbuf; |
| 491 | } |
| 492 | |
| 493 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 494 | errors are not in a global variable e.g. 'errno' nor are |
| 495 | they congruent with posix error numbers. */ |
| 496 | |
| 497 | static PyObject * os2_error(int code) |
| 498 | { |
| 499 | char text[1024]; |
| 500 | PyObject *v; |
| 501 | |
| 502 | os2_strerror(text, sizeof(text), code, ""); |
| 503 | |
| 504 | v = Py_BuildValue("(is)", code, text); |
| 505 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 506 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 507 | Py_DECREF(v); |
| 508 | } |
| 509 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 510 | } |
| 511 | |
| 512 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 513 | |
| 514 | /* POSIX generic methods */ |
| 515 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 516 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 517 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 518 | { |
| 519 | int fd; |
| 520 | int res; |
| 521 | fd = PyObject_AsFileDescriptor(fdobj); |
| 522 | if (fd < 0) |
| 523 | return NULL; |
| 524 | Py_BEGIN_ALLOW_THREADS |
| 525 | res = (*func)(fd); |
| 526 | Py_END_ALLOW_THREADS |
| 527 | if (res < 0) |
| 528 | return posix_error(); |
| 529 | Py_INCREF(Py_None); |
| 530 | return Py_None; |
| 531 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 532 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 533 | #ifdef Py_WIN_WIDE_FILENAMES |
| 534 | static int |
| 535 | unicode_file_names(void) |
| 536 | { |
| 537 | static int canusewide = -1; |
| 538 | if (canusewide == -1) { |
| 539 | /* As per doc for ::GetVersion(), this is the correct test for |
| 540 | the Windows NT family. */ |
| 541 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 542 | } |
| 543 | return canusewide; |
| 544 | } |
| 545 | #endif |
| 546 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 547 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 548 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 549 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 551 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 552 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 553 | #ifdef Py_WIN_WIDE_FILENAMES |
| 554 | if (unicode_file_names()) { |
| 555 | PyUnicodeObject *po; |
| 556 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 557 | Py_BEGIN_ALLOW_THREADS |
| 558 | /* PyUnicode_AS_UNICODE OK without thread |
| 559 | lock as it is a simple dereference. */ |
| 560 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 561 | Py_END_ALLOW_THREADS |
| 562 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 563 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | Py_INCREF(Py_None); |
| 565 | return Py_None; |
| 566 | } |
| 567 | /* Drop the argument parsing error as narrow |
| 568 | strings are also valid. */ |
| 569 | PyErr_Clear(); |
| 570 | } |
| 571 | #else |
| 572 | /* Platforms that don't support Unicode filenames |
| 573 | shouldn't be passing these extra params */ |
| 574 | assert(wformat==NULL && wfunc == NULL); |
| 575 | #endif |
| 576 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 577 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 578 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 579 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 580 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 581 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 582 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 583 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 584 | return posix_error_with_allocated_filename(path1); |
| 585 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 586 | Py_INCREF(Py_None); |
| 587 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 590 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 591 | posix_2str(PyObject *args, |
| 592 | char *format, |
| 593 | int (*func)(const char *, const char *), |
| 594 | char *wformat, |
| 595 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 596 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 597 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 598 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 599 | #ifdef Py_WIN_WIDE_FILENAMES |
| 600 | if (unicode_file_names()) { |
| 601 | PyObject *po1; |
| 602 | PyObject *po2; |
| 603 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 604 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 605 | PyObject *wpath1; |
| 606 | PyObject *wpath2; |
| 607 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 608 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 609 | if (!wpath1 || !wpath2) { |
| 610 | Py_XDECREF(wpath1); |
| 611 | Py_XDECREF(wpath2); |
| 612 | return NULL; |
| 613 | } |
| 614 | Py_BEGIN_ALLOW_THREADS |
| 615 | /* PyUnicode_AS_UNICODE OK without thread |
| 616 | lock as it is a simple dereference. */ |
| 617 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 618 | PyUnicode_AS_UNICODE(wpath2)); |
| 619 | Py_END_ALLOW_THREADS |
| 620 | Py_XDECREF(wpath1); |
| 621 | Py_XDECREF(wpath2); |
| 622 | if (res != 0) |
| 623 | return posix_error(); |
| 624 | Py_INCREF(Py_None); |
| 625 | return Py_None; |
| 626 | } |
| 627 | /* Else flow through as neither is Unicode. */ |
| 628 | } |
| 629 | /* Drop the argument parsing error as narrow |
| 630 | strings are also valid. */ |
| 631 | PyErr_Clear(); |
| 632 | } |
| 633 | #else |
| 634 | /* Platforms that don't support Unicode filenames |
| 635 | shouldn't be passing these extra params */ |
| 636 | assert(wformat==NULL && wfunc == NULL); |
| 637 | #endif |
| 638 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 639 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 640 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 641 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 642 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 643 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 644 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 645 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 646 | PyMem_Free(path1); |
| 647 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 648 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 649 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 650 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 651 | Py_INCREF(Py_None); |
| 652 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 655 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 656 | "stat_result: Result from stat or lstat.\n\n\ |
| 657 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 658 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 659 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 660 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 661 | Posix/windows: If your platform supports st_blksize, st_blocks, or st_rdev,\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 662 | they are available as attributes only.\n\ |
| 663 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 664 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 665 | |
| 666 | static PyStructSequence_Field stat_result_fields[] = { |
| 667 | {"st_mode", "protection bits"}, |
| 668 | {"st_ino", "inode"}, |
| 669 | {"st_dev", "device"}, |
| 670 | {"st_nlink", "number of hard links"}, |
| 671 | {"st_uid", "user ID of owner"}, |
| 672 | {"st_gid", "group ID of owner"}, |
| 673 | {"st_size", "total size, in bytes"}, |
| 674 | {"st_atime", "time of last access"}, |
| 675 | {"st_mtime", "time of last modification"}, |
| 676 | {"st_ctime", "time of last change"}, |
| 677 | #ifdef HAVE_ST_BLKSIZE |
| 678 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 679 | #endif |
| 680 | #ifdef HAVE_ST_BLOCKS |
| 681 | {"st_blocks", "number of blocks allocated"}, |
| 682 | #endif |
| 683 | #ifdef HAVE_ST_RDEV |
| 684 | {"st_rdev", "device type (if inode device)"}, |
| 685 | #endif |
| 686 | {0} |
| 687 | }; |
| 688 | |
| 689 | #ifdef HAVE_ST_BLKSIZE |
| 690 | #define ST_BLKSIZE_IDX 10 |
| 691 | #else |
| 692 | #define ST_BLKSIZE_IDX 9 |
| 693 | #endif |
| 694 | |
| 695 | #ifdef HAVE_ST_BLOCKS |
| 696 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 697 | #else |
| 698 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 699 | #endif |
| 700 | |
| 701 | #ifdef HAVE_ST_RDEV |
| 702 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 703 | #else |
| 704 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 705 | #endif |
| 706 | |
| 707 | static PyStructSequence_Desc stat_result_desc = { |
| 708 | "stat_result", /* name */ |
| 709 | stat_result__doc__, /* doc */ |
| 710 | stat_result_fields, |
| 711 | 10 |
| 712 | }; |
| 713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 714 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 715 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 716 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 717 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 718 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 719 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 720 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 721 | |
| 722 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 723 | {"f_bsize", }, |
| 724 | {"f_frsize", }, |
| 725 | {"f_blocks", }, |
| 726 | {"f_bfree", }, |
| 727 | {"f_bavail", }, |
| 728 | {"f_files", }, |
| 729 | {"f_ffree", }, |
| 730 | {"f_favail", }, |
| 731 | {"f_flag", }, |
| 732 | {"f_namemax",}, |
| 733 | {0} |
| 734 | }; |
| 735 | |
| 736 | static PyStructSequence_Desc statvfs_result_desc = { |
| 737 | "statvfs_result", /* name */ |
| 738 | statvfs_result__doc__, /* doc */ |
| 739 | statvfs_result_fields, |
| 740 | 10 |
| 741 | }; |
| 742 | |
| 743 | static PyTypeObject StatResultType; |
| 744 | static PyTypeObject StatVFSResultType; |
| 745 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 746 | static void |
| 747 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 748 | { |
| 749 | PyObject *val; |
Martin v. Löwis | a32c994 | 2002-09-09 16:17:47 +0000 | [diff] [blame] | 750 | val = PyFloat_FromDouble(sec + 1e-9*nsec); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 751 | PyStructSequence_SET_ITEM(v, index, val); |
| 752 | } |
| 753 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 754 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 755 | (used by posix_stat() and posix_fstat()) */ |
| 756 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 757 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 758 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 759 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 760 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 761 | if (v == NULL) |
| 762 | return NULL; |
| 763 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 764 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 765 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 766 | PyStructSequence_SET_ITEM(v, 1, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 767 | PyLong_FromLongLong((LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 768 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 769 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 770 | #endif |
| 771 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 772 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 773 | PyLong_FromLongLong((LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 774 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 775 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 776 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 777 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 778 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 779 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 780 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 781 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 782 | PyLong_FromLongLong((LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 783 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 784 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 785 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 786 | |
| 787 | #ifdef HAVE_STAT_TV_NSEC |
| 788 | ansec = st.st_atim.tv_nsec; |
| 789 | mnsec = st.st_mtim.tv_nsec; |
| 790 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 791 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 792 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 793 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 794 | fill_time(v, 7, st.st_atime, ansec); |
| 795 | fill_time(v, 8, st.st_mtime, mnsec); |
| 796 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 797 | |
| 798 | #ifdef HAVE_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 799 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 800 | PyInt_FromLong((long)st.st_blksize)); |
| 801 | #endif |
| 802 | #ifdef HAVE_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 803 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 804 | PyInt_FromLong((long)st.st_blocks)); |
| 805 | #endif |
| 806 | #ifdef HAVE_ST_RDEV |
| 807 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 808 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 809 | #endif |
| 810 | |
| 811 | if (PyErr_Occurred()) { |
| 812 | Py_DECREF(v); |
| 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | return v; |
| 817 | } |
| 818 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 819 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 820 | posix_do_stat(PyObject *self, PyObject *args, |
| 821 | char *format, |
| 822 | int (*statfunc)(const char *, STRUCT_STAT *), |
| 823 | char *wformat, |
| 824 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 825 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 826 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 827 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 828 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 829 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 830 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 831 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 832 | int pathlen; |
| 833 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 834 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 835 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 836 | |
| 837 | #ifdef Py_WIN_WIDE_FILENAMES |
| 838 | /* If on wide-character-capable OS see if argument |
| 839 | is Unicode and if so use wide API. */ |
| 840 | if (unicode_file_names()) { |
| 841 | PyUnicodeObject *po; |
| 842 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 843 | Py_UNICODE wpath[MAX_PATH+1]; |
| 844 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 845 | /* the library call can blow up if the file name is too long! */ |
| 846 | if (pathlen > MAX_PATH) { |
| 847 | errno = ENAMETOOLONG; |
| 848 | return posix_error(); |
| 849 | } |
| 850 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 851 | /* Remove trailing slash or backslash, unless it's the current |
| 852 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 853 | */ |
| 854 | if (pathlen > 0 && |
| 855 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 856 | /* It does end with a slash -- exempt the root drive cases. */ |
| 857 | /* XXX UNC root drives should also be exempted? */ |
| 858 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 859 | /* leave it alone */; |
| 860 | else { |
| 861 | /* nuke the trailing backslash */ |
| 862 | wpath[pathlen-1] = L'\0'; |
| 863 | } |
| 864 | } |
| 865 | Py_BEGIN_ALLOW_THREADS |
| 866 | /* PyUnicode_AS_UNICODE result OK without |
| 867 | thread lock as it is a simple dereference. */ |
| 868 | res = wstatfunc(wpath, &st); |
| 869 | Py_END_ALLOW_THREADS |
| 870 | if (res != 0) |
| 871 | return posix_error_with_unicode_filename(wpath); |
| 872 | return _pystat_fromstructstat(st); |
| 873 | } |
| 874 | /* Drop the argument parsing error as narrow strings |
| 875 | are also valid. */ |
| 876 | PyErr_Clear(); |
| 877 | } |
| 878 | #endif |
| 879 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 880 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 881 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 882 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 883 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 884 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 885 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 886 | pathlen = strlen(path); |
| 887 | /* the library call can blow up if the file name is too long! */ |
| 888 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 889 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 890 | errno = ENAMETOOLONG; |
| 891 | return posix_error(); |
| 892 | } |
| 893 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 894 | /* Remove trailing slash or backslash, unless it's the current |
| 895 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 896 | */ |
| 897 | if (pathlen > 0 && |
| 898 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 899 | /* It does end with a slash -- exempt the root drive cases. */ |
| 900 | /* XXX UNC root drives should also be exempted? */ |
| 901 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 902 | /* leave it alone */; |
| 903 | else { |
| 904 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 905 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 906 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 907 | path = pathcopy; |
| 908 | } |
| 909 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 910 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 911 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 912 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 913 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 914 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 915 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 916 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 917 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 918 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 919 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
| 923 | /* POSIX methods */ |
| 924 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 925 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 926 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 927 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 928 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 929 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 930 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 931 | existence, or the inclusive-OR of R_OK, W_OK, and X_OK."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 932 | |
| 933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 934 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 936 | char *path; |
| 937 | int mode; |
| 938 | int res; |
| 939 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 940 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 941 | return NULL; |
| 942 | Py_BEGIN_ALLOW_THREADS |
| 943 | res = access(path, mode); |
| 944 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 945 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 948 | #ifndef F_OK |
| 949 | #define F_OK 0 |
| 950 | #endif |
| 951 | #ifndef R_OK |
| 952 | #define R_OK 4 |
| 953 | #endif |
| 954 | #ifndef W_OK |
| 955 | #define W_OK 2 |
| 956 | #endif |
| 957 | #ifndef X_OK |
| 958 | #define X_OK 1 |
| 959 | #endif |
| 960 | |
| 961 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 962 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 963 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 964 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 965 | |
| 966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 967 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 968 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 969 | int id; |
| 970 | char *ret; |
| 971 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 972 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 973 | return NULL; |
| 974 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 975 | ret = ttyname(id); |
| 976 | if (ret == NULL) |
| 977 | return(posix_error()); |
| 978 | return(PyString_FromString(ret)); |
| 979 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 980 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 981 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 982 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 983 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 984 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 985 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 986 | |
| 987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 988 | posix_ctermid(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 989 | { |
| 990 | char *ret; |
| 991 | char buffer[L_ctermid]; |
| 992 | |
| 993 | if (!PyArg_ParseTuple(args, ":ctermid")) |
| 994 | return NULL; |
| 995 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 996 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 997 | ret = ctermid_r(buffer); |
| 998 | #else |
| 999 | ret = ctermid(buffer); |
| 1000 | #endif |
| 1001 | if (ret == NULL) |
| 1002 | return(posix_error()); |
| 1003 | return(PyString_FromString(buffer)); |
| 1004 | } |
| 1005 | #endif |
| 1006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1007 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1008 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1009 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1010 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1011 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1012 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1013 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1014 | #ifdef MS_WINDOWS |
| 1015 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1016 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1017 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1018 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1019 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1020 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1023 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1024 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1025 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1026 | Change to the directory of the given file descriptor. fildes must be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1027 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1028 | |
| 1029 | static PyObject * |
| 1030 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1031 | { |
| 1032 | return posix_fildes(fdobj, fchdir); |
| 1033 | } |
| 1034 | #endif /* HAVE_FCHDIR */ |
| 1035 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1037 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1038 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1039 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1040 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1042 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1043 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1044 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1045 | int i; |
| 1046 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1047 | if (!PyArg_ParseTuple(args, "eti", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1048 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1049 | return NULL; |
| 1050 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1051 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1052 | Py_END_ALLOW_THREADS |
| 1053 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1054 | return posix_error_with_allocated_filename(path); |
| 1055 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1056 | Py_INCREF(Py_None); |
| 1057 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1060 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1061 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1062 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1063 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1064 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1065 | |
| 1066 | static PyObject * |
| 1067 | posix_chroot(PyObject *self, PyObject *args) |
| 1068 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1069 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1070 | } |
| 1071 | #endif |
| 1072 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1073 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1074 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1075 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1076 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1077 | |
| 1078 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1079 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1080 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1081 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1082 | } |
| 1083 | #endif /* HAVE_FSYNC */ |
| 1084 | |
| 1085 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1086 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1087 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1088 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1089 | #endif |
| 1090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1091 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1092 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1093 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1094 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1095 | |
| 1096 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1097 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1098 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1099 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1100 | } |
| 1101 | #endif /* HAVE_FDATASYNC */ |
| 1102 | |
| 1103 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1104 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1105 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1106 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1107 | Change the owner and group id of path to the numeric uid and gid."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1110 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1111 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1112 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1113 | int uid, gid; |
| 1114 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1115 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1116 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1117 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1118 | return NULL; |
| 1119 | Py_BEGIN_ALLOW_THREADS |
| 1120 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1121 | Py_END_ALLOW_THREADS |
| 1122 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1123 | return posix_error_with_allocated_filename(path); |
| 1124 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1125 | Py_INCREF(Py_None); |
| 1126 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1127 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1128 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1129 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1130 | #ifdef HAVE_LCHOWN |
| 1131 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1132 | "lchown(path, uid, gid)\n\n\ |
| 1133 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1134 | This function will not follow symbolic links."); |
| 1135 | |
| 1136 | static PyObject * |
| 1137 | posix_lchown(PyObject *self, PyObject *args) |
| 1138 | { |
| 1139 | char *path = NULL; |
| 1140 | int uid, gid; |
| 1141 | int res; |
| 1142 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1143 | Py_FileSystemDefaultEncoding, &path, |
| 1144 | &uid, &gid)) |
| 1145 | return NULL; |
| 1146 | Py_BEGIN_ALLOW_THREADS |
| 1147 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1148 | Py_END_ALLOW_THREADS |
| 1149 | if (res < 0) |
| 1150 | return posix_error_with_allocated_filename(path); |
| 1151 | PyMem_Free(path); |
| 1152 | Py_INCREF(Py_None); |
| 1153 | return Py_None; |
| 1154 | } |
| 1155 | #endif /* HAVE_LCHOWN */ |
| 1156 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1157 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1158 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1159 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1160 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1161 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1162 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1163 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1164 | posix_getcwd(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1165 | { |
| 1166 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1167 | char *res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1168 | if (!PyArg_ParseTuple(args, ":getcwd")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1170 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1171 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1172 | res = _getcwd2(buf, sizeof buf); |
| 1173 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1174 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1175 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1176 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1177 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1178 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1179 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1180 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1181 | |
| 1182 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1183 | "getcwdu() -> path\n\n\ |
| 1184 | Return a unicode string representing the current working directory."); |
| 1185 | |
| 1186 | static PyObject * |
| 1187 | posix_getcwdu(PyObject *self, PyObject *args) |
| 1188 | { |
| 1189 | char buf[1026]; |
| 1190 | char *res; |
| 1191 | if (!PyArg_ParseTuple(args, ":getcwd")) |
| 1192 | return NULL; |
| 1193 | |
| 1194 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1195 | if (unicode_file_names()) { |
| 1196 | wchar_t *wres; |
| 1197 | wchar_t wbuf[1026]; |
| 1198 | Py_BEGIN_ALLOW_THREADS |
| 1199 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1200 | Py_END_ALLOW_THREADS |
| 1201 | if (wres == NULL) |
| 1202 | return posix_error(); |
| 1203 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1204 | } |
| 1205 | #endif |
| 1206 | |
| 1207 | Py_BEGIN_ALLOW_THREADS |
| 1208 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1209 | res = _getcwd2(buf, sizeof buf); |
| 1210 | #else |
| 1211 | res = getcwd(buf, sizeof buf); |
| 1212 | #endif |
| 1213 | Py_END_ALLOW_THREADS |
| 1214 | if (res == NULL) |
| 1215 | return posix_error(); |
| 1216 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1217 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1218 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1221 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1222 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1223 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1224 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1225 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1226 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1227 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1228 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1229 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1230 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1231 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1232 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1234 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1235 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1236 | Return a list containing the names of the entries in the directory.\n\ |
| 1237 | \n\ |
| 1238 | path: path of directory to list\n\ |
| 1239 | \n\ |
| 1240 | The list is in arbitrary order. It does not include the special\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1241 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1243 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1244 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1245 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1246 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1247 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1248 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1249 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1250 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1251 | HANDLE hFindFile; |
| 1252 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1253 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1254 | char namebuf[MAX_PATH*2+5]; |
| 1255 | char *bufptr = namebuf; |
| 1256 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1257 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1258 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1259 | /* If on wide-character-capable OS see if argument |
| 1260 | is Unicode and if so use wide API. */ |
| 1261 | if (unicode_file_names()) { |
| 1262 | PyUnicodeObject *po; |
| 1263 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1264 | WIN32_FIND_DATAW wFileData; |
| 1265 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1266 | Py_UNICODE wch; |
| 1267 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1268 | wnamebuf[MAX_PATH] = L'\0'; |
| 1269 | len = wcslen(wnamebuf); |
| 1270 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1271 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1272 | wnamebuf[len++] = L'/'; |
| 1273 | wcscpy(wnamebuf + len, L"*.*"); |
| 1274 | if ((d = PyList_New(0)) == NULL) |
| 1275 | return NULL; |
| 1276 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1277 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1278 | errno = GetLastError(); |
| 1279 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1280 | return d; |
| 1281 | } |
| 1282 | Py_DECREF(d); |
| 1283 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1284 | } |
| 1285 | do { |
| 1286 | if (wFileData.cFileName[0] == L'.' && |
| 1287 | (wFileData.cFileName[1] == L'\0' || |
| 1288 | wFileData.cFileName[1] == L'.' && |
| 1289 | wFileData.cFileName[2] == L'\0')) |
| 1290 | continue; |
| 1291 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1292 | if (v == NULL) { |
| 1293 | Py_DECREF(d); |
| 1294 | d = NULL; |
| 1295 | break; |
| 1296 | } |
| 1297 | if (PyList_Append(d, v) != 0) { |
| 1298 | Py_DECREF(v); |
| 1299 | Py_DECREF(d); |
| 1300 | d = NULL; |
| 1301 | break; |
| 1302 | } |
| 1303 | Py_DECREF(v); |
| 1304 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1305 | |
| 1306 | if (FindClose(hFindFile) == FALSE) { |
| 1307 | Py_DECREF(d); |
| 1308 | return win32_error_unicode("FindClose", wnamebuf); |
| 1309 | } |
| 1310 | return d; |
| 1311 | } |
| 1312 | /* Drop the argument parsing error as narrow strings |
| 1313 | are also valid. */ |
| 1314 | PyErr_Clear(); |
| 1315 | } |
| 1316 | #endif |
| 1317 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1318 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1319 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1320 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1321 | if (len > 0) { |
| 1322 | char ch = namebuf[len-1]; |
| 1323 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1324 | namebuf[len++] = '/'; |
| 1325 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1326 | strcpy(namebuf + len, "*.*"); |
| 1327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1328 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1329 | return NULL; |
| 1330 | |
| 1331 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1332 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1333 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1334 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1335 | return d; |
| 1336 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1337 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1338 | } |
| 1339 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1340 | if (FileData.cFileName[0] == '.' && |
| 1341 | (FileData.cFileName[1] == '\0' || |
| 1342 | FileData.cFileName[1] == '.' && |
| 1343 | FileData.cFileName[2] == '\0')) |
| 1344 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1345 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1346 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1347 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1348 | d = NULL; |
| 1349 | break; |
| 1350 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1351 | if (PyList_Append(d, v) != 0) { |
| 1352 | Py_DECREF(v); |
| 1353 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1354 | d = NULL; |
| 1355 | break; |
| 1356 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1357 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1358 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1359 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1360 | if (FindClose(hFindFile) == FALSE) { |
| 1361 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1362 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1363 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1364 | |
| 1365 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1366 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1367 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1368 | |
| 1369 | #ifndef MAX_PATH |
| 1370 | #define MAX_PATH CCHMAXPATH |
| 1371 | #endif |
| 1372 | char *name, *pt; |
| 1373 | int len; |
| 1374 | PyObject *d, *v; |
| 1375 | char namebuf[MAX_PATH+5]; |
| 1376 | HDIR hdir = 1; |
| 1377 | ULONG srchcnt = 1; |
| 1378 | FILEFINDBUF3 ep; |
| 1379 | APIRET rc; |
| 1380 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1381 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1382 | return NULL; |
| 1383 | if (len >= MAX_PATH) { |
| 1384 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1385 | return NULL; |
| 1386 | } |
| 1387 | strcpy(namebuf, name); |
| 1388 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1389 | if (*pt == ALTSEP) |
| 1390 | *pt = SEP; |
| 1391 | if (namebuf[len-1] != SEP) |
| 1392 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1393 | strcpy(namebuf + len, "*.*"); |
| 1394 | |
| 1395 | if ((d = PyList_New(0)) == NULL) |
| 1396 | return NULL; |
| 1397 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1398 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1399 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1400 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1401 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1402 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1403 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (rc != NO_ERROR) { |
| 1406 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1407 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1410 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1411 | do { |
| 1412 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1413 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1414 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1415 | |
| 1416 | strcpy(namebuf, ep.achName); |
| 1417 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1418 | /* Leave Case of Name Alone -- In Native Form */ |
| 1419 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1420 | |
| 1421 | v = PyString_FromString(namebuf); |
| 1422 | if (v == NULL) { |
| 1423 | Py_DECREF(d); |
| 1424 | d = NULL; |
| 1425 | break; |
| 1426 | } |
| 1427 | if (PyList_Append(d, v) != 0) { |
| 1428 | Py_DECREF(v); |
| 1429 | Py_DECREF(d); |
| 1430 | d = NULL; |
| 1431 | break; |
| 1432 | } |
| 1433 | Py_DECREF(v); |
| 1434 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1435 | } |
| 1436 | |
| 1437 | return d; |
| 1438 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1439 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1440 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1441 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1442 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1443 | struct dirent *ep; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1444 | if (!PyArg_ParseTuple(args, "s:listdir", &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1445 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1446 | if ((dirp = opendir(name)) == NULL) { |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1447 | return posix_error_with_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1448 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1449 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1450 | closedir(dirp); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1454 | if (ep->d_name[0] == '.' && |
| 1455 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1456 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1457 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1458 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1459 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1460 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1461 | d = NULL; |
| 1462 | break; |
| 1463 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1464 | if (PyList_Append(d, v) != 0) { |
| 1465 | Py_DECREF(v); |
| 1466 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1467 | d = NULL; |
| 1468 | break; |
| 1469 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1470 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1471 | } |
| 1472 | closedir(dirp); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1473 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1474 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1475 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1476 | #endif /* which OS */ |
| 1477 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1478 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1479 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1480 | /* A helper function for abspath on win32 */ |
| 1481 | static PyObject * |
| 1482 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1483 | { |
| 1484 | /* assume encoded strings wont more than double no of chars */ |
| 1485 | char inbuf[MAX_PATH*2]; |
| 1486 | char *inbufp = inbuf; |
| 1487 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1488 | char outbuf[MAX_PATH*2]; |
| 1489 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1490 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1491 | if (unicode_file_names()) { |
| 1492 | PyUnicodeObject *po; |
| 1493 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1494 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1495 | Py_UNICODE *wtemp; |
| 1496 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
| 1497 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1498 | woutbuf, &wtemp)) |
| 1499 | return win32_error("GetFullPathName", ""); |
| 1500 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1501 | } |
| 1502 | /* Drop the argument parsing error as narrow strings |
| 1503 | are also valid. */ |
| 1504 | PyErr_Clear(); |
| 1505 | } |
| 1506 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1507 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1508 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1509 | &insize)) |
| 1510 | return NULL; |
| 1511 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1512 | outbuf, &temp)) |
| 1513 | return win32_error("GetFullPathName", inbuf); |
| 1514 | return PyString_FromString(outbuf); |
| 1515 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1516 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1518 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1519 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1520 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1521 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1522 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1523 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1524 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1525 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1526 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1527 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1528 | |
| 1529 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1530 | if (unicode_file_names()) { |
| 1531 | PyUnicodeObject *po; |
| 1532 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po)) { |
| 1533 | Py_BEGIN_ALLOW_THREADS |
| 1534 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1535 | it is a simple dereference. */ |
| 1536 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1537 | Py_END_ALLOW_THREADS |
| 1538 | if (res < 0) |
| 1539 | return posix_error(); |
| 1540 | Py_INCREF(Py_None); |
| 1541 | return Py_None; |
| 1542 | } |
| 1543 | /* Drop the argument parsing error as narrow strings |
| 1544 | are also valid. */ |
| 1545 | PyErr_Clear(); |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1549 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1550 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1551 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1552 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1553 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1554 | res = mkdir(path); |
| 1555 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1556 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1557 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1558 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1559 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1560 | return posix_error_with_allocated_filename(path); |
| 1561 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1562 | Py_INCREF(Py_None); |
| 1563 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1566 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1567 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1568 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1569 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1570 | #include <sys/resource.h> |
| 1571 | #endif |
| 1572 | #endif |
| 1573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1574 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1575 | "nice(inc) -> new_priority\n\n\ |
| 1576 | Decrease the priority of process by inc and return the new priority."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1577 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1578 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1579 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1580 | { |
| 1581 | int increment, value; |
| 1582 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1583 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1584 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1585 | |
| 1586 | /* There are two flavours of 'nice': one that returns the new |
| 1587 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1588 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1589 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1590 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1591 | If we are of the nice family that returns the new priority, we |
| 1592 | need to clear errno before the call, and check if errno is filled |
| 1593 | before calling posix_error() on a returnvalue of -1, because the |
| 1594 | -1 may be the actual new priority! */ |
| 1595 | |
| 1596 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1597 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1598 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1599 | if (value == 0) |
| 1600 | value = getpriority(PRIO_PROCESS, 0); |
| 1601 | #endif |
| 1602 | if (value == -1 && errno != 0) |
| 1603 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1604 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1605 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1606 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1607 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1608 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1610 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1611 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1612 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1614 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1615 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1616 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1617 | #ifdef MS_WINDOWS |
| 1618 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1619 | #else |
| 1620 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1621 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1625 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1626 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1627 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1628 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1629 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1630 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1631 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1632 | #ifdef MS_WINDOWS |
| 1633 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1634 | #else |
| 1635 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1636 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1639 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1640 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1641 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1642 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1643 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1644 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1645 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1646 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1647 | #ifdef MS_WINDOWS |
| 1648 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1649 | #else |
| 1650 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1651 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1655 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1656 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1657 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1658 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1659 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1660 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1661 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1662 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1663 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1664 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1665 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1666 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1668 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1669 | Py_END_ALLOW_THREADS |
| 1670 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1671 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1672 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1673 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1675 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1676 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1678 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1679 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1680 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1681 | { |
| 1682 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1683 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1684 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1685 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1686 | if (i < 0) |
| 1687 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1688 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1692 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1693 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1694 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1695 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1696 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1697 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1699 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1700 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1701 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1702 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1703 | #ifdef MS_WINDOWS |
| 1704 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1705 | #else |
| 1706 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1707 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1710 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1711 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1712 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1713 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1714 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1715 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1716 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1717 | posix_uname(PyObject *self, PyObject *args) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1718 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1719 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1720 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1721 | if (!PyArg_ParseTuple(args, ":uname")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 1722 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1723 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1724 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1725 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1726 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1727 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1728 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1729 | u.sysname, |
| 1730 | u.nodename, |
| 1731 | u.release, |
| 1732 | u.version, |
| 1733 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1734 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1735 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1736 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1737 | static int |
| 1738 | extract_time(PyObject *t, long* sec, long* usec) |
| 1739 | { |
| 1740 | long intval; |
| 1741 | if (PyFloat_Check(t)) { |
| 1742 | double tval = PyFloat_AsDouble(t); |
| 1743 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1744 | if (!intobj) |
| 1745 | return -1; |
| 1746 | intval = PyInt_AsLong(intobj); |
| 1747 | Py_DECREF(intobj); |
| 1748 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1749 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1750 | if (*usec < 0) |
| 1751 | /* If rounding gave us a negative number, |
| 1752 | truncate. */ |
| 1753 | *usec = 0; |
| 1754 | return 0; |
| 1755 | } |
| 1756 | intval = PyInt_AsLong(t); |
| 1757 | if (intval == -1 && PyErr_Occurred()) |
| 1758 | return -1; |
| 1759 | *sec = intval; |
| 1760 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1761 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1762 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1764 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1765 | "utime(path, (atime, utime))\n\ |
| 1766 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1767 | Set the access and modified time of the file to the given values. If the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1768 | 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] | 1769 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1770 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1771 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1772 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1773 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1774 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1775 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1776 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1777 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1778 | #if defined(HAVE_UTIMES) |
| 1779 | struct timeval buf[2]; |
| 1780 | #define ATIME buf[0].tv_sec |
| 1781 | #define MTIME buf[1].tv_sec |
| 1782 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1783 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1784 | struct utimbuf buf; |
| 1785 | #define ATIME buf.actime |
| 1786 | #define MTIME buf.modtime |
| 1787 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1788 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1789 | time_t buf[2]; |
| 1790 | #define ATIME buf[0] |
| 1791 | #define MTIME buf[1] |
| 1792 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1793 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1794 | |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1795 | if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1796 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1797 | if (arg == Py_None) { |
| 1798 | /* optional time values not given */ |
| 1799 | Py_BEGIN_ALLOW_THREADS |
| 1800 | res = utime(path, NULL); |
| 1801 | Py_END_ALLOW_THREADS |
| 1802 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1803 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1804 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1805 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1806 | return NULL; |
| 1807 | } |
| 1808 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1809 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 1810 | &atime, &ausec) == -1) |
| 1811 | return NULL; |
| 1812 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 1813 | &mtime, &musec) == -1) |
| 1814 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1815 | ATIME = atime; |
| 1816 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1817 | #ifdef HAVE_UTIMES |
| 1818 | buf[0].tv_usec = ausec; |
| 1819 | buf[1].tv_usec = musec; |
| 1820 | Py_BEGIN_ALLOW_THREADS |
| 1821 | res = utimes(path, buf); |
| 1822 | Py_END_ALLOW_THREADS |
| 1823 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1824 | Py_BEGIN_ALLOW_THREADS |
| 1825 | res = utime(path, UTIME_ARG); |
| 1826 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1827 | #endif |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1828 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1829 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1830 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1831 | Py_INCREF(Py_None); |
| 1832 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1833 | #undef UTIME_ARG |
| 1834 | #undef ATIME |
| 1835 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1838 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 1839 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1841 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1842 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1843 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1844 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1845 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1846 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1847 | { |
| 1848 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1849 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1850 | return NULL; |
| 1851 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1852 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1855 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 1856 | static void |
| 1857 | free_string_array(char **array, int count) |
| 1858 | { |
| 1859 | int i; |
| 1860 | for (i = 0; i < count; i++) |
| 1861 | PyMem_Free(array[i]); |
| 1862 | PyMem_DEL(array); |
| 1863 | } |
| 1864 | #endif |
| 1865 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1866 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1867 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1869 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1870 | Execute an executable path with arguments, replacing current process.\n\ |
| 1871 | \n\ |
| 1872 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1873 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1874 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1875 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1876 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1877 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1878 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1879 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1880 | char **argvlist; |
| 1881 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1882 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1883 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 1884 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1885 | argv is a list or tuple of strings. */ |
| 1886 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1887 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 1888 | Py_FileSystemDefaultEncoding, |
| 1889 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1890 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1891 | if (PyList_Check(argv)) { |
| 1892 | argc = PyList_Size(argv); |
| 1893 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1894 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1895 | else if (PyTuple_Check(argv)) { |
| 1896 | argc = PyTuple_Size(argv); |
| 1897 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1898 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1899 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1900 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1901 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1902 | return NULL; |
| 1903 | } |
| 1904 | |
| 1905 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1906 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1907 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1908 | return NULL; |
| 1909 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1910 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1911 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1912 | if (argvlist == NULL) { |
| 1913 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1914 | return NULL; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1915 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1916 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1917 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 1918 | Py_FileSystemDefaultEncoding, |
| 1919 | &argvlist[i])) { |
| 1920 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1921 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1922 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1923 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1924 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1925 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1926 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1927 | } |
| 1928 | argvlist[argc] = NULL; |
| 1929 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1930 | #ifdef BAD_EXEC_PROTOTYPES |
| 1931 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1932 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1933 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1934 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1935 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1936 | /* If we get here it's definitely an error */ |
| 1937 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1938 | free_string_array(argvlist, argc); |
| 1939 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 1940 | return posix_error(); |
| 1941 | } |
| 1942 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1945 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1946 | Execute a path with arguments and environment, replacing current process.\n\ |
| 1947 | \n\ |
| 1948 | path: path of executable file\n\ |
| 1949 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1950 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 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_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1954 | { |
| 1955 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1956 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1957 | char **argvlist; |
| 1958 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1959 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1960 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1961 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1962 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1963 | |
| 1964 | /* execve has three arguments: (path, argv, env), where |
| 1965 | argv is a list or tuple of strings and env is a dictionary |
| 1966 | like posix.environ. */ |
| 1967 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1968 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 1969 | Py_FileSystemDefaultEncoding, |
| 1970 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1971 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1972 | if (PyList_Check(argv)) { |
| 1973 | argc = PyList_Size(argv); |
| 1974 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1975 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1976 | else if (PyTuple_Check(argv)) { |
| 1977 | argc = PyTuple_Size(argv); |
| 1978 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1979 | } |
| 1980 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1981 | PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1982 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1983 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 1984 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1985 | PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1986 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1989 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1990 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1991 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1992 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1995 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1996 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1997 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 1998 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 1999 | } |
| 2000 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2001 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2002 | "et;execve() arg 2 must contain only strings", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2003 | &argvlist[i])) |
| 2004 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2005 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2006 | goto fail_1; |
| 2007 | } |
| 2008 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2009 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2010 | argvlist[argc] = NULL; |
| 2011 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2012 | i = PyMapping_Size(env); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2013 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2014 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2015 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2016 | goto fail_1; |
| 2017 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2018 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2019 | keys = PyMapping_Keys(env); |
| 2020 | vals = PyMapping_Values(env); |
| 2021 | if (!keys || !vals) |
| 2022 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2023 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2024 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2025 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2026 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2027 | |
| 2028 | key = PyList_GetItem(keys, pos); |
| 2029 | val = PyList_GetItem(vals, pos); |
| 2030 | if (!key || !val) |
| 2031 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2032 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2033 | if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || |
| 2034 | !PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2035 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2036 | goto fail_2; |
| 2037 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2038 | |
| 2039 | #if defined(PYOS_OS2) |
| 2040 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2041 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2042 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2043 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2044 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2045 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2046 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2047 | goto fail_2; |
| 2048 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2049 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2050 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2051 | #if defined(PYOS_OS2) |
| 2052 | } |
| 2053 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2054 | } |
| 2055 | envlist[envc] = 0; |
| 2056 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2057 | |
| 2058 | #ifdef BAD_EXEC_PROTOTYPES |
| 2059 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2060 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2061 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2062 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2063 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2064 | /* If we get here it's definitely an error */ |
| 2065 | |
| 2066 | (void) posix_error(); |
| 2067 | |
| 2068 | fail_2: |
| 2069 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2070 | PyMem_DEL(envlist[envc]); |
| 2071 | PyMem_DEL(envlist); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2072 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2073 | free_string_array(argvlist,lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2074 | Py_XDECREF(vals); |
| 2075 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2076 | fail_0: |
| 2077 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2078 | return NULL; |
| 2079 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2080 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2081 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2082 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2083 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2084 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2085 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2086 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2087 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2088 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2089 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2090 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2091 | |
| 2092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2093 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2094 | { |
| 2095 | char *path; |
| 2096 | PyObject *argv; |
| 2097 | char **argvlist; |
| 2098 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2099 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2100 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2101 | |
| 2102 | /* spawnv has three arguments: (mode, path, argv), where |
| 2103 | argv is a list or tuple of strings. */ |
| 2104 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2105 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2106 | Py_FileSystemDefaultEncoding, |
| 2107 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2108 | return NULL; |
| 2109 | if (PyList_Check(argv)) { |
| 2110 | argc = PyList_Size(argv); |
| 2111 | getitem = PyList_GetItem; |
| 2112 | } |
| 2113 | else if (PyTuple_Check(argv)) { |
| 2114 | argc = PyTuple_Size(argv); |
| 2115 | getitem = PyTuple_GetItem; |
| 2116 | } |
| 2117 | else { |
Martin v. Löwis | e75f0e4 | 2001-11-24 09:31:44 +0000 | [diff] [blame] | 2118 | PyErr_SetString(PyExc_TypeError, "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2119 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2120 | return NULL; |
| 2121 | } |
| 2122 | |
| 2123 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2124 | if (argvlist == NULL) { |
| 2125 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2126 | return NULL; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2127 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2128 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2129 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2130 | Py_FileSystemDefaultEncoding, |
| 2131 | &argvlist[i])) { |
| 2132 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2133 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2134 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2135 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2136 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | argvlist[argc] = NULL; |
| 2140 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2141 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2142 | Py_BEGIN_ALLOW_THREADS |
| 2143 | spawnval = spawnv(mode, path, argvlist); |
| 2144 | Py_END_ALLOW_THREADS |
| 2145 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2146 | if (mode == _OLD_P_OVERLAY) |
| 2147 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2148 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2149 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2150 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2151 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2152 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2153 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2154 | free_string_array(argvlist, argc); |
| 2155 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2156 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2157 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2158 | return posix_error(); |
| 2159 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2160 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2161 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2162 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2163 | return Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2164 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2168 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2169 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2170 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2171 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2172 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2173 | path: path of executable file\n\ |
| 2174 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2175 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2176 | |
| 2177 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2178 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2179 | { |
| 2180 | char *path; |
| 2181 | PyObject *argv, *env; |
| 2182 | char **argvlist; |
| 2183 | char **envlist; |
| 2184 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2185 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2186 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2187 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2188 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2189 | |
| 2190 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2191 | argv is a list or tuple of strings and env is a dictionary |
| 2192 | like posix.environ. */ |
| 2193 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2194 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2195 | Py_FileSystemDefaultEncoding, |
| 2196 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2197 | return NULL; |
| 2198 | if (PyList_Check(argv)) { |
| 2199 | argc = PyList_Size(argv); |
| 2200 | getitem = PyList_GetItem; |
| 2201 | } |
| 2202 | else if (PyTuple_Check(argv)) { |
| 2203 | argc = PyTuple_Size(argv); |
| 2204 | getitem = PyTuple_GetItem; |
| 2205 | } |
| 2206 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2207 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2208 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2209 | } |
| 2210 | if (!PyMapping_Check(env)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2211 | PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2212 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | argvlist = PyMem_NEW(char *, argc+1); |
| 2216 | if (argvlist == NULL) { |
| 2217 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2218 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2219 | } |
| 2220 | for (i = 0; i < argc; i++) { |
| 2221 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2222 | "et;spawnve() arg 2 must contain only strings", |
| 2223 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2224 | &argvlist[i])) |
| 2225 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2226 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2227 | goto fail_1; |
| 2228 | } |
| 2229 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2230 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2231 | argvlist[argc] = NULL; |
| 2232 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2233 | i = PyMapping_Size(env); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2234 | envlist = PyMem_NEW(char *, i + 1); |
| 2235 | if (envlist == NULL) { |
| 2236 | PyErr_NoMemory(); |
| 2237 | goto fail_1; |
| 2238 | } |
| 2239 | envc = 0; |
| 2240 | keys = PyMapping_Keys(env); |
| 2241 | vals = PyMapping_Values(env); |
| 2242 | if (!keys || !vals) |
| 2243 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2244 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2245 | for (pos = 0; pos < i; pos++) { |
| 2246 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2247 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2248 | |
| 2249 | key = PyList_GetItem(keys, pos); |
| 2250 | val = PyList_GetItem(vals, pos); |
| 2251 | if (!key || !val) |
| 2252 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2253 | |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2254 | if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || |
| 2255 | !PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2256 | { |
| 2257 | goto fail_2; |
| 2258 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2259 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2260 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2261 | if (p == NULL) { |
| 2262 | PyErr_NoMemory(); |
| 2263 | goto fail_2; |
| 2264 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2265 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2266 | envlist[envc++] = p; |
| 2267 | } |
| 2268 | envlist[envc] = 0; |
| 2269 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2270 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2271 | Py_BEGIN_ALLOW_THREADS |
| 2272 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2273 | Py_END_ALLOW_THREADS |
| 2274 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2275 | if (mode == _OLD_P_OVERLAY) |
| 2276 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2277 | |
| 2278 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2279 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2280 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2281 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2282 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2283 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2284 | (void) posix_error(); |
| 2285 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2286 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2287 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2288 | #else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2289 | res = Py_BuildValue("L", (LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2290 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2291 | |
| 2292 | fail_2: |
| 2293 | while (--envc >= 0) |
| 2294 | PyMem_DEL(envlist[envc]); |
| 2295 | PyMem_DEL(envlist); |
| 2296 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2297 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2298 | Py_XDECREF(vals); |
| 2299 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2300 | fail_0: |
| 2301 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2302 | return res; |
| 2303 | } |
| 2304 | #endif /* HAVE_SPAWNV */ |
| 2305 | |
| 2306 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2307 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2308 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2309 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2310 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2311 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2312 | Return 0 to child process and PID of child to parent process."); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2313 | |
| 2314 | static PyObject * |
| 2315 | posix_fork1(self, args) |
| 2316 | PyObject *self; |
| 2317 | PyObject *args; |
| 2318 | { |
| 2319 | int pid; |
| 2320 | if (!PyArg_ParseTuple(args, ":fork1")) |
| 2321 | return NULL; |
| 2322 | pid = fork1(); |
| 2323 | if (pid == -1) |
| 2324 | return posix_error(); |
| 2325 | PyOS_AfterFork(); |
| 2326 | return PyInt_FromLong((long)pid); |
| 2327 | } |
| 2328 | #endif |
| 2329 | |
| 2330 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2331 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2332 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2333 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2334 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2335 | Return 0 to child process and PID of child to parent process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2336 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2337 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2338 | posix_fork(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2339 | { |
| 2340 | int pid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2341 | if (!PyArg_ParseTuple(args, ":fork")) |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 2342 | return NULL; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2343 | pid = fork(); |
| 2344 | if (pid == -1) |
| 2345 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2346 | if (pid == 0) |
| 2347 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2348 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2349 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2350 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2351 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2352 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 2353 | #ifdef HAVE_PTY_H |
| 2354 | #include <pty.h> |
| 2355 | #else |
| 2356 | #ifdef HAVE_LIBUTIL_H |
| 2357 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2358 | #endif /* HAVE_LIBUTIL_H */ |
| 2359 | #endif /* HAVE_PTY_H */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2360 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2361 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2362 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2363 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2364 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2365 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2366 | |
| 2367 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2368 | posix_openpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2369 | { |
| 2370 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2371 | #ifndef HAVE_OPENPTY |
| 2372 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2373 | #endif |
| 2374 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2375 | if (!PyArg_ParseTuple(args, ":openpty")) |
| 2376 | return NULL; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2377 | |
| 2378 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2379 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2380 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2381 | #else |
| 2382 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2383 | if (slave_name == NULL) |
| 2384 | return posix_error(); |
| 2385 | |
| 2386 | slave_fd = open(slave_name, O_RDWR); |
| 2387 | if (slave_fd < 0) |
| 2388 | return posix_error(); |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2389 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2390 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2391 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2392 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2393 | } |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2394 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2395 | |
| 2396 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2397 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2398 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2399 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2400 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2401 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2402 | |
| 2403 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2404 | posix_forkpty(PyObject *self, PyObject *args) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2405 | { |
| 2406 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2407 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2408 | if (!PyArg_ParseTuple(args, ":forkpty")) |
| 2409 | return NULL; |
| 2410 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2411 | if (pid == -1) |
| 2412 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2413 | if (pid == 0) |
| 2414 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2415 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2416 | } |
| 2417 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2418 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2419 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2420 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2421 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2422 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2423 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2424 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2425 | posix_getegid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2426 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2427 | if (!PyArg_ParseTuple(args, ":getegid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2428 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2429 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2430 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2431 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2432 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2433 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2434 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2435 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2436 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2437 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2438 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2439 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2440 | posix_geteuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2441 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2442 | if (!PyArg_ParseTuple(args, ":geteuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2443 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2444 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2445 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2446 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2447 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2448 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2449 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2450 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2451 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2452 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2454 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2455 | posix_getgid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2456 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2457 | if (!PyArg_ParseTuple(args, ":getgid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2458 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2459 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2460 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2461 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2462 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2464 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2465 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2466 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2467 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2468 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2469 | posix_getpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2470 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2471 | if (!PyArg_ParseTuple(args, ":getpid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2472 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2473 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2476 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2477 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2478 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2479 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2480 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2481 | |
| 2482 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2483 | posix_getgroups(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2484 | { |
| 2485 | PyObject *result = NULL; |
| 2486 | |
| 2487 | if (PyArg_ParseTuple(args, ":getgroups")) { |
| 2488 | #ifdef NGROUPS_MAX |
| 2489 | #define MAX_GROUPS NGROUPS_MAX |
| 2490 | #else |
| 2491 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2492 | #define MAX_GROUPS 64 |
| 2493 | #endif |
| 2494 | gid_t grouplist[MAX_GROUPS]; |
| 2495 | int n; |
| 2496 | |
| 2497 | n = getgroups(MAX_GROUPS, grouplist); |
| 2498 | if (n < 0) |
| 2499 | posix_error(); |
| 2500 | else { |
| 2501 | result = PyList_New(n); |
| 2502 | if (result != NULL) { |
| 2503 | PyObject *o; |
| 2504 | int i; |
| 2505 | for (i = 0; i < n; ++i) { |
| 2506 | o = PyInt_FromLong((long)grouplist[i]); |
| 2507 | if (o == NULL) { |
| 2508 | Py_DECREF(result); |
| 2509 | result = NULL; |
| 2510 | break; |
| 2511 | } |
| 2512 | PyList_SET_ITEM(result, i, o); |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | return result; |
| 2518 | } |
| 2519 | #endif |
| 2520 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2521 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2522 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2523 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2524 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2525 | |
| 2526 | static PyObject * |
| 2527 | posix_getpgid(PyObject *self, PyObject *args) |
| 2528 | { |
| 2529 | int pid, pgid; |
| 2530 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2531 | return NULL; |
| 2532 | pgid = getpgid(pid); |
| 2533 | if (pgid < 0) |
| 2534 | return posix_error(); |
| 2535 | return PyInt_FromLong((long)pgid); |
| 2536 | } |
| 2537 | #endif /* HAVE_GETPGID */ |
| 2538 | |
| 2539 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2540 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2541 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2542 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2543 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2544 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2545 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2546 | posix_getpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2547 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2548 | if (!PyArg_ParseTuple(args, ":getpgrp")) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2549 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2550 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2551 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2552 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2553 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2554 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2555 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2556 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2557 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2558 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2559 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2560 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2561 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2562 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2563 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2564 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2565 | posix_setpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2566 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2567 | if (!PyArg_ParseTuple(args, ":setpgrp")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2568 | return NULL; |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2569 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2570 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2571 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2572 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2573 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2574 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2575 | Py_INCREF(Py_None); |
| 2576 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2579 | #endif /* HAVE_SETPGRP */ |
| 2580 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2581 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2582 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2583 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2584 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2586 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2587 | posix_getppid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2588 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2589 | if (!PyArg_ParseTuple(args, ":getppid")) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2590 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2591 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2592 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2593 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2594 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2595 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2596 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2597 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2598 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2599 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2600 | |
| 2601 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2602 | posix_getlogin(PyObject *self, PyObject *args) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2603 | { |
| 2604 | PyObject *result = NULL; |
| 2605 | |
| 2606 | if (PyArg_ParseTuple(args, ":getlogin")) { |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2607 | char *name; |
| 2608 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2609 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2610 | errno = 0; |
| 2611 | name = getlogin(); |
| 2612 | if (name == NULL) { |
| 2613 | if (errno) |
| 2614 | posix_error(); |
| 2615 | else |
| 2616 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2617 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2618 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2619 | else |
| 2620 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2621 | errno = old_errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2622 | } |
| 2623 | return result; |
| 2624 | } |
| 2625 | #endif |
| 2626 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2627 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2628 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2629 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2630 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2631 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2632 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2633 | posix_getuid(PyObject *self, PyObject *args) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2634 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2635 | if (!PyArg_ParseTuple(args, ":getuid")) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2636 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2637 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2638 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2639 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2640 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2641 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2642 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2643 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2644 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2645 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2646 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2647 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2648 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2649 | { |
| 2650 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2651 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2652 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2653 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2654 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2655 | APIRET rc; |
| 2656 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2657 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2658 | |
| 2659 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2660 | APIRET rc; |
| 2661 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2662 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2663 | |
| 2664 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2665 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2666 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2667 | if (kill(pid, sig) == -1) |
| 2668 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2669 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2670 | Py_INCREF(Py_None); |
| 2671 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2672 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2673 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2674 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2675 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2676 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2677 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2678 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2679 | |
| 2680 | static PyObject * |
| 2681 | posix_killpg(PyObject *self, PyObject *args) |
| 2682 | { |
| 2683 | int pgid, sig; |
| 2684 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2685 | return NULL; |
| 2686 | if (killpg(pgid, sig) == -1) |
| 2687 | return posix_error(); |
| 2688 | Py_INCREF(Py_None); |
| 2689 | return Py_None; |
| 2690 | } |
| 2691 | #endif |
| 2692 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2693 | #ifdef HAVE_PLOCK |
| 2694 | |
| 2695 | #ifdef HAVE_SYS_LOCK_H |
| 2696 | #include <sys/lock.h> |
| 2697 | #endif |
| 2698 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2699 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2700 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2701 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2702 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2703 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2704 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2705 | { |
| 2706 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2707 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2708 | return NULL; |
| 2709 | if (plock(op) == -1) |
| 2710 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2711 | Py_INCREF(Py_None); |
| 2712 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2713 | } |
| 2714 | #endif |
| 2715 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2716 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2717 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2718 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2719 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2720 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2721 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2722 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2723 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2724 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2725 | async_system(const char *command) |
| 2726 | { |
| 2727 | char *p, errormsg[256], args[1024]; |
| 2728 | RESULTCODES rcodes; |
| 2729 | APIRET rc; |
| 2730 | char *shell = getenv("COMSPEC"); |
| 2731 | if (!shell) |
| 2732 | shell = "cmd"; |
| 2733 | |
| 2734 | strcpy(args, shell); |
| 2735 | p = &args[ strlen(args)+1 ]; |
| 2736 | strcpy(p, "/c "); |
| 2737 | strcat(p, command); |
| 2738 | p += strlen(p) + 1; |
| 2739 | *p = '\0'; |
| 2740 | |
| 2741 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2742 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2743 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2744 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2745 | &rcodes, shell); |
| 2746 | return rc; |
| 2747 | } |
| 2748 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2749 | static FILE * |
| 2750 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2751 | { |
| 2752 | HFILE rhan, whan; |
| 2753 | FILE *retfd = NULL; |
| 2754 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2755 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2756 | if (rc != NO_ERROR) { |
| 2757 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2758 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2759 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2760 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2761 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2762 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2763 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2764 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2765 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2766 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2767 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 2768 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2769 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2770 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2773 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 2774 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2775 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2776 | if (rc == NO_ERROR) |
| 2777 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 2778 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2779 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 2780 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2781 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2782 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 2783 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2784 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2785 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 2786 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2787 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2788 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 2789 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2790 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2791 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2794 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 2795 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2796 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 2797 | if (rc == NO_ERROR) |
| 2798 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 2799 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2800 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 2801 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2802 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2803 | } else { |
| 2804 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2805 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2806 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2810 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2811 | { |
| 2812 | char *name; |
| 2813 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2814 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2815 | FILE *fp; |
| 2816 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2817 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2818 | return NULL; |
| 2819 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2820 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2821 | Py_END_ALLOW_THREADS |
| 2822 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2823 | return os2_error(err); |
| 2824 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2825 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 2826 | if (f != NULL) |
| 2827 | PyFile_SetBufSize(f, bufsize); |
| 2828 | return f; |
| 2829 | } |
| 2830 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2831 | #elif defined(PYCC_GCC) |
| 2832 | |
| 2833 | /* standard posix version of popen() support */ |
| 2834 | static PyObject * |
| 2835 | posix_popen(PyObject *self, PyObject *args) |
| 2836 | { |
| 2837 | char *name; |
| 2838 | char *mode = "r"; |
| 2839 | int bufsize = -1; |
| 2840 | FILE *fp; |
| 2841 | PyObject *f; |
| 2842 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 2843 | return NULL; |
| 2844 | Py_BEGIN_ALLOW_THREADS |
| 2845 | fp = popen(name, mode); |
| 2846 | Py_END_ALLOW_THREADS |
| 2847 | if (fp == NULL) |
| 2848 | return posix_error(); |
| 2849 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 2850 | if (f != NULL) |
| 2851 | PyFile_SetBufSize(f, bufsize); |
| 2852 | return f; |
| 2853 | } |
| 2854 | |
| 2855 | /* fork() under OS/2 has lots'o'warts |
| 2856 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 2857 | * most of this code is a ripoff of the win32 code, but using the |
| 2858 | * capabilities of EMX's C library routines |
| 2859 | */ |
| 2860 | |
| 2861 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 2862 | #define POPEN_1 1 |
| 2863 | #define POPEN_2 2 |
| 2864 | #define POPEN_3 3 |
| 2865 | #define POPEN_4 4 |
| 2866 | |
| 2867 | static PyObject *_PyPopen(char *, int, int, int); |
| 2868 | static int _PyPclose(FILE *file); |
| 2869 | |
| 2870 | /* |
| 2871 | * Internal dictionary mapping popen* file pointers to process handles, |
| 2872 | * for use when retrieving the process exit code. See _PyPclose() below |
| 2873 | * for more information on this dictionary's use. |
| 2874 | */ |
| 2875 | static PyObject *_PyPopenProcs = NULL; |
| 2876 | |
| 2877 | /* os2emx version of popen2() |
| 2878 | * |
| 2879 | * The result of this function is a pipe (file) connected to the |
| 2880 | * process's stdin, and a pipe connected to the process's stdout. |
| 2881 | */ |
| 2882 | |
| 2883 | static PyObject * |
| 2884 | os2emx_popen2(PyObject *self, PyObject *args) |
| 2885 | { |
| 2886 | PyObject *f; |
| 2887 | int tm=0; |
| 2888 | |
| 2889 | char *cmdstring; |
| 2890 | char *mode = "t"; |
| 2891 | int bufsize = -1; |
| 2892 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 2893 | return NULL; |
| 2894 | |
| 2895 | if (*mode == 't') |
| 2896 | tm = O_TEXT; |
| 2897 | else if (*mode != 'b') { |
| 2898 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2899 | return NULL; |
| 2900 | } else |
| 2901 | tm = O_BINARY; |
| 2902 | |
| 2903 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 2904 | |
| 2905 | return f; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * Variation on os2emx.popen2 |
| 2910 | * |
| 2911 | * The result of this function is 3 pipes - the process's stdin, |
| 2912 | * stdout and stderr |
| 2913 | */ |
| 2914 | |
| 2915 | static PyObject * |
| 2916 | os2emx_popen3(PyObject *self, PyObject *args) |
| 2917 | { |
| 2918 | PyObject *f; |
| 2919 | int tm = 0; |
| 2920 | |
| 2921 | char *cmdstring; |
| 2922 | char *mode = "t"; |
| 2923 | int bufsize = -1; |
| 2924 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 2925 | return NULL; |
| 2926 | |
| 2927 | if (*mode == 't') |
| 2928 | tm = O_TEXT; |
| 2929 | else if (*mode != 'b') { |
| 2930 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2931 | return NULL; |
| 2932 | } else |
| 2933 | tm = O_BINARY; |
| 2934 | |
| 2935 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 2936 | |
| 2937 | return f; |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * Variation on os2emx.popen2 |
| 2942 | * |
| 2943 | * The result of this function is 2 pipes - the processes stdin, |
| 2944 | * and stdout+stderr combined as a single pipe. |
| 2945 | */ |
| 2946 | |
| 2947 | static PyObject * |
| 2948 | os2emx_popen4(PyObject *self, PyObject *args) |
| 2949 | { |
| 2950 | PyObject *f; |
| 2951 | int tm = 0; |
| 2952 | |
| 2953 | char *cmdstring; |
| 2954 | char *mode = "t"; |
| 2955 | int bufsize = -1; |
| 2956 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 2957 | return NULL; |
| 2958 | |
| 2959 | if (*mode == 't') |
| 2960 | tm = O_TEXT; |
| 2961 | else if (*mode != 'b') { |
| 2962 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 2963 | return NULL; |
| 2964 | } else |
| 2965 | tm = O_BINARY; |
| 2966 | |
| 2967 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 2968 | |
| 2969 | return f; |
| 2970 | } |
| 2971 | |
| 2972 | /* a couple of structures for convenient handling of multiple |
| 2973 | * file handles and pipes |
| 2974 | */ |
| 2975 | struct file_ref |
| 2976 | { |
| 2977 | int handle; |
| 2978 | int flags; |
| 2979 | }; |
| 2980 | |
| 2981 | struct pipe_ref |
| 2982 | { |
| 2983 | int rd; |
| 2984 | int wr; |
| 2985 | }; |
| 2986 | |
| 2987 | /* The following code is derived from the win32 code */ |
| 2988 | |
| 2989 | static PyObject * |
| 2990 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 2991 | { |
| 2992 | struct file_ref stdio[3]; |
| 2993 | struct pipe_ref p_fd[3]; |
| 2994 | FILE *p_s[3]; |
| 2995 | int file_count, i, pipe_err, pipe_pid; |
| 2996 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 2997 | PyObject *f, *p_f[3]; |
| 2998 | |
| 2999 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3000 | if (mode == O_TEXT) |
| 3001 | { |
| 3002 | rd_mode = "rt"; |
| 3003 | wr_mode = "wt"; |
| 3004 | } |
| 3005 | else |
| 3006 | { |
| 3007 | rd_mode = "rb"; |
| 3008 | wr_mode = "wb"; |
| 3009 | } |
| 3010 | |
| 3011 | /* prepare shell references */ |
| 3012 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3013 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3014 | { |
| 3015 | errno = ENOENT; |
| 3016 | return posix_error(); |
| 3017 | } |
| 3018 | |
| 3019 | sh_name = _getname(shell); |
| 3020 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3021 | opt = "/c"; |
| 3022 | else |
| 3023 | opt = "-c"; |
| 3024 | |
| 3025 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3026 | i = pipe_err = 0; |
| 3027 | while (pipe_err >= 0 && i < 3) |
| 3028 | { |
| 3029 | pipe_err = stdio[i].handle = dup(i); |
| 3030 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3031 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3032 | i++; |
| 3033 | } |
| 3034 | if (pipe_err < 0) |
| 3035 | { |
| 3036 | /* didn't get them all saved - clean up and bail out */ |
| 3037 | int saved_err = errno; |
| 3038 | while (i-- > 0) |
| 3039 | { |
| 3040 | close(stdio[i].handle); |
| 3041 | } |
| 3042 | errno = saved_err; |
| 3043 | return posix_error(); |
| 3044 | } |
| 3045 | |
| 3046 | /* create pipe ends */ |
| 3047 | file_count = 2; |
| 3048 | if (n == POPEN_3) |
| 3049 | file_count = 3; |
| 3050 | i = pipe_err = 0; |
| 3051 | while ((pipe_err == 0) && (i < file_count)) |
| 3052 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3053 | if (pipe_err < 0) |
| 3054 | { |
| 3055 | /* didn't get them all made - clean up and bail out */ |
| 3056 | while (i-- > 0) |
| 3057 | { |
| 3058 | close(p_fd[i].wr); |
| 3059 | close(p_fd[i].rd); |
| 3060 | } |
| 3061 | errno = EPIPE; |
| 3062 | return posix_error(); |
| 3063 | } |
| 3064 | |
| 3065 | /* change the actual standard IO streams over temporarily, |
| 3066 | * making the retained pipe ends non-inheritable |
| 3067 | */ |
| 3068 | pipe_err = 0; |
| 3069 | |
| 3070 | /* - stdin */ |
| 3071 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3072 | { |
| 3073 | close(p_fd[0].rd); |
| 3074 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3075 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3076 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3077 | { |
| 3078 | close(p_fd[0].wr); |
| 3079 | pipe_err = -1; |
| 3080 | } |
| 3081 | } |
| 3082 | else |
| 3083 | { |
| 3084 | pipe_err = -1; |
| 3085 | } |
| 3086 | |
| 3087 | /* - stdout */ |
| 3088 | if (pipe_err == 0) |
| 3089 | { |
| 3090 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3091 | { |
| 3092 | close(p_fd[1].wr); |
| 3093 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3094 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3095 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3096 | { |
| 3097 | close(p_fd[1].rd); |
| 3098 | pipe_err = -1; |
| 3099 | } |
| 3100 | } |
| 3101 | else |
| 3102 | { |
| 3103 | pipe_err = -1; |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | /* - stderr, as required */ |
| 3108 | if (pipe_err == 0) |
| 3109 | switch (n) |
| 3110 | { |
| 3111 | case POPEN_3: |
| 3112 | { |
| 3113 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3114 | { |
| 3115 | close(p_fd[2].wr); |
| 3116 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3117 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3118 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3119 | { |
| 3120 | close(p_fd[2].rd); |
| 3121 | pipe_err = -1; |
| 3122 | } |
| 3123 | } |
| 3124 | else |
| 3125 | { |
| 3126 | pipe_err = -1; |
| 3127 | } |
| 3128 | break; |
| 3129 | } |
| 3130 | |
| 3131 | case POPEN_4: |
| 3132 | { |
| 3133 | if (dup2(1, 2) != 2) |
| 3134 | { |
| 3135 | pipe_err = -1; |
| 3136 | } |
| 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | /* spawn the child process */ |
| 3142 | if (pipe_err == 0) |
| 3143 | { |
| 3144 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3145 | if (pipe_pid == -1) |
| 3146 | { |
| 3147 | pipe_err = -1; |
| 3148 | } |
| 3149 | else |
| 3150 | { |
| 3151 | /* save the PID into the FILE structure |
| 3152 | * NOTE: this implementation doesn't actually |
| 3153 | * take advantage of this, but do it for |
| 3154 | * completeness - AIM Apr01 |
| 3155 | */ |
| 3156 | for (i = 0; i < file_count; i++) |
| 3157 | p_s[i]->_pid = pipe_pid; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | /* reset standard IO to normal */ |
| 3162 | for (i = 0; i < 3; i++) |
| 3163 | { |
| 3164 | dup2(stdio[i].handle, i); |
| 3165 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3166 | close(stdio[i].handle); |
| 3167 | } |
| 3168 | |
| 3169 | /* if any remnant problems, clean up and bail out */ |
| 3170 | if (pipe_err < 0) |
| 3171 | { |
| 3172 | for (i = 0; i < 3; i++) |
| 3173 | { |
| 3174 | close(p_fd[i].rd); |
| 3175 | close(p_fd[i].wr); |
| 3176 | } |
| 3177 | errno = EPIPE; |
| 3178 | return posix_error_with_filename(cmdstring); |
| 3179 | } |
| 3180 | |
| 3181 | /* build tuple of file objects to return */ |
| 3182 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3183 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3184 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3185 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3186 | if (n == POPEN_3) |
| 3187 | { |
| 3188 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3189 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3190 | f = Py_BuildValue("OOO", p_f[0], p_f[1], p_f[2]); |
| 3191 | } |
| 3192 | else |
| 3193 | f = Py_BuildValue("OO", p_f[0], p_f[1]); |
| 3194 | |
| 3195 | /* |
| 3196 | * Insert the files we've created into the process dictionary |
| 3197 | * all referencing the list with the process handle and the |
| 3198 | * initial number of files (see description below in _PyPclose). |
| 3199 | * Since if _PyPclose later tried to wait on a process when all |
| 3200 | * handles weren't closed, it could create a deadlock with the |
| 3201 | * child, we spend some energy here to try to ensure that we |
| 3202 | * either insert all file handles into the dictionary or none |
| 3203 | * at all. It's a little clumsy with the various popen modes |
| 3204 | * and variable number of files involved. |
| 3205 | */ |
| 3206 | if (!_PyPopenProcs) |
| 3207 | { |
| 3208 | _PyPopenProcs = PyDict_New(); |
| 3209 | } |
| 3210 | |
| 3211 | if (_PyPopenProcs) |
| 3212 | { |
| 3213 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3214 | int ins_rc[3]; |
| 3215 | |
| 3216 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3217 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3218 | |
| 3219 | procObj = PyList_New(2); |
| 3220 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3221 | intObj = PyInt_FromLong((long) file_count); |
| 3222 | |
| 3223 | if (procObj && pidObj && intObj) |
| 3224 | { |
| 3225 | PyList_SetItem(procObj, 0, pidObj); |
| 3226 | PyList_SetItem(procObj, 1, intObj); |
| 3227 | |
| 3228 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3229 | if (fileObj[0]) |
| 3230 | { |
| 3231 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3232 | fileObj[0], |
| 3233 | procObj); |
| 3234 | } |
| 3235 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3236 | if (fileObj[1]) |
| 3237 | { |
| 3238 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3239 | fileObj[1], |
| 3240 | procObj); |
| 3241 | } |
| 3242 | if (file_count >= 3) |
| 3243 | { |
| 3244 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3245 | if (fileObj[2]) |
| 3246 | { |
| 3247 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3248 | fileObj[2], |
| 3249 | procObj); |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3254 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3255 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3256 | { |
| 3257 | /* Something failed - remove any dictionary |
| 3258 | * entries that did make it. |
| 3259 | */ |
| 3260 | if (!ins_rc[0] && fileObj[0]) |
| 3261 | { |
| 3262 | PyDict_DelItem(_PyPopenProcs, |
| 3263 | fileObj[0]); |
| 3264 | } |
| 3265 | if (!ins_rc[1] && fileObj[1]) |
| 3266 | { |
| 3267 | PyDict_DelItem(_PyPopenProcs, |
| 3268 | fileObj[1]); |
| 3269 | } |
| 3270 | if (!ins_rc[2] && fileObj[2]) |
| 3271 | { |
| 3272 | PyDict_DelItem(_PyPopenProcs, |
| 3273 | fileObj[2]); |
| 3274 | } |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * Clean up our localized references for the dictionary keys |
| 3280 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3281 | * that got placed in the dictionary. |
| 3282 | */ |
| 3283 | Py_XDECREF(procObj); |
| 3284 | Py_XDECREF(fileObj[0]); |
| 3285 | Py_XDECREF(fileObj[1]); |
| 3286 | Py_XDECREF(fileObj[2]); |
| 3287 | } |
| 3288 | |
| 3289 | /* Child is launched. */ |
| 3290 | return f; |
| 3291 | } |
| 3292 | |
| 3293 | /* |
| 3294 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3295 | * exit code for the child process and return as a result of the close. |
| 3296 | * |
| 3297 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3298 | * input file pointer to information about the process that was |
| 3299 | * originally created by the popen* call that created the file pointer. |
| 3300 | * The dictionary uses the file pointer as a key (with one entry |
| 3301 | * inserted for each file returned by the original popen* call) and a |
| 3302 | * single list object as the value for all files from a single call. |
| 3303 | * The list object contains the Win32 process handle at [0], and a file |
| 3304 | * count at [1], which is initialized to the total number of file |
| 3305 | * handles using that list. |
| 3306 | * |
| 3307 | * This function closes whichever handle it is passed, and decrements |
| 3308 | * the file count in the dictionary for the process handle pointed to |
| 3309 | * by this file. On the last close (when the file count reaches zero), |
| 3310 | * this function will wait for the child process and then return its |
| 3311 | * exit code as the result of the close() operation. This permits the |
| 3312 | * files to be closed in any order - it is always the close() of the |
| 3313 | * final handle that will return the exit code. |
| 3314 | */ |
| 3315 | |
| 3316 | /* RED_FLAG 31-Aug-2000 Tim |
| 3317 | * This is always called (today!) between a pair of |
| 3318 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 3319 | * macros. So the thread running this has no valid thread state, as |
| 3320 | * far as Python is concerned. However, this calls some Python API |
| 3321 | * functions that cannot be called safely without a valid thread |
| 3322 | * state, in particular PyDict_GetItem. |
| 3323 | * As a temporary hack (although it may last for years ...), we |
| 3324 | * *rely* on not having a valid thread state in this function, in |
| 3325 | * order to create our own "from scratch". |
| 3326 | * This will deadlock if _PyPclose is ever called by a thread |
| 3327 | * holding the global lock. |
| 3328 | * (The OS/2 EMX thread support appears to cover the case where the |
| 3329 | * lock is already held - AIM Apr01) |
| 3330 | */ |
| 3331 | |
| 3332 | static int _PyPclose(FILE *file) |
| 3333 | { |
| 3334 | int result; |
| 3335 | int exit_code; |
| 3336 | int pipe_pid; |
| 3337 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3338 | int file_count; |
| 3339 | #ifdef WITH_THREAD |
| 3340 | PyInterpreterState* pInterpreterState; |
| 3341 | PyThreadState* pThreadState; |
| 3342 | #endif |
| 3343 | |
| 3344 | /* Close the file handle first, to ensure it can't block the |
| 3345 | * child from exiting if it's the last handle. |
| 3346 | */ |
| 3347 | result = fclose(file); |
| 3348 | |
| 3349 | #ifdef WITH_THREAD |
| 3350 | /* Bootstrap a valid thread state into existence. */ |
| 3351 | pInterpreterState = PyInterpreterState_New(); |
| 3352 | if (!pInterpreterState) { |
| 3353 | /* Well, we're hosed now! We don't have a thread |
| 3354 | * state, so can't call a nice error routine, or raise |
| 3355 | * an exception. Just die. |
| 3356 | */ |
| 3357 | Py_FatalError("unable to allocate interpreter state " |
| 3358 | "when closing popen object."); |
| 3359 | return -1; /* unreachable */ |
| 3360 | } |
| 3361 | pThreadState = PyThreadState_New(pInterpreterState); |
| 3362 | if (!pThreadState) { |
| 3363 | Py_FatalError("unable to allocate thread state " |
| 3364 | "when closing popen object."); |
| 3365 | return -1; /* unreachable */ |
| 3366 | } |
| 3367 | /* Grab the global lock. Note that this will deadlock if the |
| 3368 | * current thread already has the lock! (see RED_FLAG comments |
| 3369 | * before this function) |
| 3370 | */ |
| 3371 | PyEval_RestoreThread(pThreadState); |
| 3372 | #endif |
| 3373 | |
| 3374 | if (_PyPopenProcs) |
| 3375 | { |
| 3376 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3377 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3378 | fileObj)) != NULL && |
| 3379 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3380 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3381 | { |
| 3382 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3383 | file_count = (int) PyInt_AsLong(intObj); |
| 3384 | |
| 3385 | if (file_count > 1) |
| 3386 | { |
| 3387 | /* Still other files referencing process */ |
| 3388 | file_count--; |
| 3389 | PyList_SetItem(procObj,1, |
| 3390 | PyInt_FromLong((long) file_count)); |
| 3391 | } |
| 3392 | else |
| 3393 | { |
| 3394 | /* Last file for this process */ |
| 3395 | if (result != EOF && |
| 3396 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3397 | { |
| 3398 | /* extract exit status */ |
| 3399 | if (WIFEXITED(exit_code)) |
| 3400 | { |
| 3401 | result = WEXITSTATUS(exit_code); |
| 3402 | } |
| 3403 | else |
| 3404 | { |
| 3405 | errno = EPIPE; |
| 3406 | result = -1; |
| 3407 | } |
| 3408 | } |
| 3409 | else |
| 3410 | { |
| 3411 | /* Indicate failure - this will cause the file object |
| 3412 | * to raise an I/O error and translate the last |
| 3413 | * error code from errno. We do have a problem with |
| 3414 | * last errors that overlap the normal errno table, |
| 3415 | * but that's a consistent problem with the file object. |
| 3416 | */ |
| 3417 | result = -1; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | /* Remove this file pointer from dictionary */ |
| 3422 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3423 | |
| 3424 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3425 | { |
| 3426 | Py_DECREF(_PyPopenProcs); |
| 3427 | _PyPopenProcs = NULL; |
| 3428 | } |
| 3429 | |
| 3430 | } /* if object retrieval ok */ |
| 3431 | |
| 3432 | Py_XDECREF(fileObj); |
| 3433 | } /* if _PyPopenProcs */ |
| 3434 | |
| 3435 | #ifdef WITH_THREAD |
| 3436 | /* Tear down the thread & interpreter states. |
| 3437 | * Note that interpreter state clear & delete functions automatically |
| 3438 | * call the thread clear & delete functions, and indeed insist on |
| 3439 | * doing that themselves. The lock must be held during the clear, but |
| 3440 | * need not be held during the delete. |
| 3441 | */ |
| 3442 | PyInterpreterState_Clear(pInterpreterState); |
| 3443 | PyEval_ReleaseThread(pThreadState); |
| 3444 | PyInterpreterState_Delete(pInterpreterState); |
| 3445 | #endif |
| 3446 | |
| 3447 | return result; |
| 3448 | } |
| 3449 | |
| 3450 | #endif /* PYCC_??? */ |
| 3451 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3452 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * Portable 'popen' replacement for Win32. |
| 3456 | * |
| 3457 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3458 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3459 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3460 | */ |
| 3461 | |
| 3462 | #include <malloc.h> |
| 3463 | #include <io.h> |
| 3464 | #include <fcntl.h> |
| 3465 | |
| 3466 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3467 | #define POPEN_1 1 |
| 3468 | #define POPEN_2 2 |
| 3469 | #define POPEN_3 3 |
| 3470 | #define POPEN_4 4 |
| 3471 | |
| 3472 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3473 | static int _PyPclose(FILE *file); |
| 3474 | |
| 3475 | /* |
| 3476 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3477 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3478 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3479 | */ |
| 3480 | static PyObject *_PyPopenProcs = NULL; |
| 3481 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3482 | |
| 3483 | /* popen that works from a GUI. |
| 3484 | * |
| 3485 | * The result of this function is a pipe (file) connected to the |
| 3486 | * processes stdin or stdout, depending on the requested mode. |
| 3487 | */ |
| 3488 | |
| 3489 | static PyObject * |
| 3490 | posix_popen(PyObject *self, PyObject *args) |
| 3491 | { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3492 | PyObject *f, *s; |
| 3493 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3494 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3495 | char *cmdstring; |
| 3496 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3497 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3498 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3499 | return NULL; |
| 3500 | |
| 3501 | s = PyTuple_New(0); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3502 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3503 | if (*mode == 'r') |
| 3504 | tm = _O_RDONLY; |
| 3505 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3506 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3507 | return NULL; |
| 3508 | } else |
| 3509 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3510 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3511 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3512 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3513 | return NULL; |
| 3514 | } |
| 3515 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3516 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3517 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3518 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3519 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3520 | else |
| 3521 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3522 | |
| 3523 | return f; |
| 3524 | } |
| 3525 | |
| 3526 | /* Variation on win32pipe.popen |
| 3527 | * |
| 3528 | * The result of this function is a pipe (file) connected to the |
| 3529 | * process's stdin, and a pipe connected to the process's stdout. |
| 3530 | */ |
| 3531 | |
| 3532 | static PyObject * |
| 3533 | win32_popen2(PyObject *self, PyObject *args) |
| 3534 | { |
| 3535 | PyObject *f; |
| 3536 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3537 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3538 | char *cmdstring; |
| 3539 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3540 | int bufsize = -1; |
| 3541 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3542 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3543 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3544 | if (*mode == 't') |
| 3545 | tm = _O_TEXT; |
| 3546 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3547 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3548 | return NULL; |
| 3549 | } else |
| 3550 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3551 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3552 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3553 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3554 | return NULL; |
| 3555 | } |
| 3556 | |
| 3557 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3558 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3559 | return f; |
| 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3564 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3565 | * The result of this function is 3 pipes - the process's stdin, |
| 3566 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3567 | */ |
| 3568 | |
| 3569 | static PyObject * |
| 3570 | win32_popen3(PyObject *self, PyObject *args) |
| 3571 | { |
| 3572 | PyObject *f; |
| 3573 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3574 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3575 | char *cmdstring; |
| 3576 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3577 | int bufsize = -1; |
| 3578 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3579 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3580 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3581 | if (*mode == 't') |
| 3582 | tm = _O_TEXT; |
| 3583 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3584 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3585 | return NULL; |
| 3586 | } else |
| 3587 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3588 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3589 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3590 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3591 | return NULL; |
| 3592 | } |
| 3593 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3594 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3595 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3596 | return f; |
| 3597 | } |
| 3598 | |
| 3599 | /* |
| 3600 | * Variation on win32pipe.popen |
| 3601 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3602 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3603 | * and stdout+stderr combined as a single pipe. |
| 3604 | */ |
| 3605 | |
| 3606 | static PyObject * |
| 3607 | win32_popen4(PyObject *self, PyObject *args) |
| 3608 | { |
| 3609 | PyObject *f; |
| 3610 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3611 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3612 | char *cmdstring; |
| 3613 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3614 | int bufsize = -1; |
| 3615 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3616 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3617 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3618 | if (*mode == 't') |
| 3619 | tm = _O_TEXT; |
| 3620 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3621 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3622 | return NULL; |
| 3623 | } else |
| 3624 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3625 | |
| 3626 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3627 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3628 | return NULL; |
| 3629 | } |
| 3630 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3631 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3632 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3633 | return f; |
| 3634 | } |
| 3635 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3636 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3637 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3638 | HANDLE hStdin, |
| 3639 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3640 | HANDLE hStderr, |
| 3641 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3642 | { |
| 3643 | PROCESS_INFORMATION piProcInfo; |
| 3644 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3645 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3646 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3647 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3648 | int i; |
| 3649 | int x; |
| 3650 | |
| 3651 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3652 | char *comshell; |
| 3653 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3654 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3655 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3656 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3657 | |
| 3658 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3659 | * then use the w9xpopen hack. |
| 3660 | */ |
| 3661 | comshell = s1 + x; |
| 3662 | while (comshell >= s1 && *comshell != '\\') |
| 3663 | --comshell; |
| 3664 | ++comshell; |
| 3665 | |
| 3666 | if (GetVersion() < 0x80000000 && |
| 3667 | _stricmp(comshell, "command.com") != 0) { |
| 3668 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3669 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3670 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3671 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3672 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3673 | } |
| 3674 | else { |
| 3675 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3676 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3677 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3678 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3679 | char modulepath[_MAX_PATH]; |
| 3680 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3681 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3682 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3683 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3684 | x = i+1; |
| 3685 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3686 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3687 | strncat(modulepath, |
| 3688 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3689 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3690 | -strlen(modulepath)); |
| 3691 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3692 | /* Eeek - file-not-found - possibly an embedding |
| 3693 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3694 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3695 | strncpy(modulepath, |
| 3696 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3697 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3698 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3699 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3700 | strncat(modulepath, |
| 3701 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3702 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3703 | -strlen(modulepath)); |
| 3704 | /* No where else to look - raise an easily identifiable |
| 3705 | error, rather than leaving Windows to report |
| 3706 | "file not found" - as the user is probably blissfully |
| 3707 | unaware this shim EXE is used, and it will confuse them. |
| 3708 | (well, it confused me for a while ;-) |
| 3709 | */ |
| 3710 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3711 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3712 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3713 | "for popen to work with your shell " |
| 3714 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3715 | szConsoleSpawn); |
| 3716 | return FALSE; |
| 3717 | } |
| 3718 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3719 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3720 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3721 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3722 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3723 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3724 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3725 | /* To maintain correct argument passing semantics, |
| 3726 | we pass the command-line as it stands, and allow |
| 3727 | quoting to be applied. w9xpopen.exe will then |
| 3728 | use its argv vector, and re-quote the necessary |
| 3729 | args for the ultimate child process. |
| 3730 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3731 | PyOS_snprintf( |
| 3732 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3733 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3734 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3735 | s1, |
| 3736 | s3, |
| 3737 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3738 | /* Not passing CREATE_NEW_CONSOLE has been known to |
| 3739 | cause random failures on win9x. Specifically a |
| 3740 | dialog: |
| 3741 | "Your program accessed mem currently in use at xxx" |
| 3742 | and a hopeful warning about the stability of your |
| 3743 | system. |
| 3744 | Cost is Ctrl+C wont kill children, but anyone |
| 3745 | who cares can have a go! |
| 3746 | */ |
| 3747 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3752 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3753 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3754 | PyErr_SetString(PyExc_RuntimeError, |
| 3755 | "Cannot locate a COMSPEC environment variable to " |
| 3756 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3757 | return FALSE; |
| 3758 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3759 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3760 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3761 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3762 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3763 | siStartInfo.hStdInput = hStdin; |
| 3764 | siStartInfo.hStdOutput = hStdout; |
| 3765 | siStartInfo.hStdError = hStderr; |
| 3766 | siStartInfo.wShowWindow = SW_HIDE; |
| 3767 | |
| 3768 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3769 | s2, |
| 3770 | NULL, |
| 3771 | NULL, |
| 3772 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3773 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3774 | NULL, |
| 3775 | NULL, |
| 3776 | &siStartInfo, |
| 3777 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3778 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3779 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3780 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3781 | /* Return process handle */ |
| 3782 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3783 | return TRUE; |
| 3784 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3785 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3786 | return FALSE; |
| 3787 | } |
| 3788 | |
| 3789 | /* The following code is based off of KB: Q190351 */ |
| 3790 | |
| 3791 | static PyObject * |
| 3792 | _PyPopen(char *cmdstring, int mode, int n) |
| 3793 | { |
| 3794 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3795 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3796 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3797 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3798 | SECURITY_ATTRIBUTES saAttr; |
| 3799 | BOOL fSuccess; |
| 3800 | int fd1, fd2, fd3; |
| 3801 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3802 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3803 | PyObject *f; |
| 3804 | |
| 3805 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3806 | saAttr.bInheritHandle = TRUE; |
| 3807 | saAttr.lpSecurityDescriptor = NULL; |
| 3808 | |
| 3809 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 3810 | return win32_error("CreatePipe", NULL); |
| 3811 | |
| 3812 | /* Create new output read handle and the input write handle. Set |
| 3813 | * the inheritance properties to FALSE. Otherwise, the child inherits |
| 3814 | * the these handles; resulting in non-closeable handles to the pipes |
| 3815 | * being created. */ |
| 3816 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3817 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 3818 | FALSE, |
| 3819 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3820 | if (!fSuccess) |
| 3821 | return win32_error("DuplicateHandle", NULL); |
| 3822 | |
| 3823 | /* Close the inheritable version of ChildStdin |
| 3824 | that we're using. */ |
| 3825 | CloseHandle(hChildStdinWr); |
| 3826 | |
| 3827 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 3828 | return win32_error("CreatePipe", NULL); |
| 3829 | |
| 3830 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3831 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 3832 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3833 | if (!fSuccess) |
| 3834 | return win32_error("DuplicateHandle", NULL); |
| 3835 | |
| 3836 | /* Close the inheritable version of ChildStdout |
| 3837 | that we're using. */ |
| 3838 | CloseHandle(hChildStdoutRd); |
| 3839 | |
| 3840 | if (n != POPEN_4) { |
| 3841 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 3842 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3843 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 3844 | hChildStderrRd, |
| 3845 | GetCurrentProcess(), |
| 3846 | &hChildStderrRdDup, 0, |
| 3847 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3848 | if (!fSuccess) |
| 3849 | return win32_error("DuplicateHandle", NULL); |
| 3850 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 3851 | CloseHandle(hChildStderrRd); |
| 3852 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3853 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3854 | switch (n) { |
| 3855 | case POPEN_1: |
| 3856 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 3857 | case _O_WRONLY | _O_TEXT: |
| 3858 | /* Case for writing to child Stdin in text mode. */ |
| 3859 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3860 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3861 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3862 | PyFile_SetBufSize(f, 0); |
| 3863 | /* We don't care about these pipes anymore, so close them. */ |
| 3864 | CloseHandle(hChildStdoutRdDup); |
| 3865 | CloseHandle(hChildStderrRdDup); |
| 3866 | break; |
| 3867 | |
| 3868 | case _O_RDONLY | _O_TEXT: |
| 3869 | /* Case for reading from child Stdout in text mode. */ |
| 3870 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3871 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3872 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3873 | PyFile_SetBufSize(f, 0); |
| 3874 | /* We don't care about these pipes anymore, so close them. */ |
| 3875 | CloseHandle(hChildStdinWrDup); |
| 3876 | CloseHandle(hChildStderrRdDup); |
| 3877 | break; |
| 3878 | |
| 3879 | case _O_RDONLY | _O_BINARY: |
| 3880 | /* Case for readinig from child Stdout in binary mode. */ |
| 3881 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3882 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3883 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3884 | PyFile_SetBufSize(f, 0); |
| 3885 | /* We don't care about these pipes anymore, so close them. */ |
| 3886 | CloseHandle(hChildStdinWrDup); |
| 3887 | CloseHandle(hChildStderrRdDup); |
| 3888 | break; |
| 3889 | |
| 3890 | case _O_WRONLY | _O_BINARY: |
| 3891 | /* Case for writing to child Stdin in binary mode. */ |
| 3892 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3893 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3894 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3895 | PyFile_SetBufSize(f, 0); |
| 3896 | /* We don't care about these pipes anymore, so close them. */ |
| 3897 | CloseHandle(hChildStdoutRdDup); |
| 3898 | CloseHandle(hChildStderrRdDup); |
| 3899 | break; |
| 3900 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3901 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3902 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3903 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3904 | case POPEN_2: |
| 3905 | case POPEN_4: |
| 3906 | { |
| 3907 | char *m1, *m2; |
| 3908 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3909 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 3910 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3911 | m1 = "r"; |
| 3912 | m2 = "w"; |
| 3913 | } else { |
| 3914 | m1 = "rb"; |
| 3915 | m2 = "wb"; |
| 3916 | } |
| 3917 | |
| 3918 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3919 | f1 = _fdopen(fd1, m2); |
| 3920 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3921 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3922 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3923 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3924 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3925 | PyFile_SetBufSize(p2, 0); |
| 3926 | |
| 3927 | if (n != 4) |
| 3928 | CloseHandle(hChildStderrRdDup); |
| 3929 | |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3930 | f = Py_BuildValue("OO",p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3931 | Py_XDECREF(p1); |
| 3932 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3933 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3934 | break; |
| 3935 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3936 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3937 | case POPEN_3: |
| 3938 | { |
| 3939 | char *m1, *m2; |
| 3940 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3941 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 3942 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3943 | m1 = "r"; |
| 3944 | m2 = "w"; |
| 3945 | } else { |
| 3946 | m1 = "rb"; |
| 3947 | m2 = "wb"; |
| 3948 | } |
| 3949 | |
| 3950 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 3951 | f1 = _fdopen(fd1, m2); |
| 3952 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 3953 | f2 = _fdopen(fd2, m1); |
| 3954 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 3955 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3956 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3957 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 3958 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3959 | PyFile_SetBufSize(p1, 0); |
| 3960 | PyFile_SetBufSize(p2, 0); |
| 3961 | PyFile_SetBufSize(p3, 0); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3962 | f = Py_BuildValue("OOO",p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 3963 | Py_XDECREF(p1); |
| 3964 | Py_XDECREF(p2); |
| 3965 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3966 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3967 | break; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | if (n == POPEN_4) { |
| 3972 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3973 | hChildStdinRd, |
| 3974 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3975 | hChildStdoutWr, |
| 3976 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3977 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3978 | } |
| 3979 | else { |
| 3980 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3981 | hChildStdinRd, |
| 3982 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3983 | hChildStderrWr, |
| 3984 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3985 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3988 | /* |
| 3989 | * Insert the files we've created into the process dictionary |
| 3990 | * all referencing the list with the process handle and the |
| 3991 | * initial number of files (see description below in _PyPclose). |
| 3992 | * Since if _PyPclose later tried to wait on a process when all |
| 3993 | * handles weren't closed, it could create a deadlock with the |
| 3994 | * child, we spend some energy here to try to ensure that we |
| 3995 | * either insert all file handles into the dictionary or none |
| 3996 | * at all. It's a little clumsy with the various popen modes |
| 3997 | * and variable number of files involved. |
| 3998 | */ |
| 3999 | if (!_PyPopenProcs) { |
| 4000 | _PyPopenProcs = PyDict_New(); |
| 4001 | } |
| 4002 | |
| 4003 | if (_PyPopenProcs) { |
| 4004 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4005 | int ins_rc[3]; |
| 4006 | |
| 4007 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4008 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4009 | |
| 4010 | procObj = PyList_New(2); |
| 4011 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4012 | intObj = PyInt_FromLong(file_count); |
| 4013 | |
| 4014 | if (procObj && hProcessObj && intObj) { |
| 4015 | PyList_SetItem(procObj,0,hProcessObj); |
| 4016 | PyList_SetItem(procObj,1,intObj); |
| 4017 | |
| 4018 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4019 | if (fileObj[0]) { |
| 4020 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4021 | fileObj[0], |
| 4022 | procObj); |
| 4023 | } |
| 4024 | if (file_count >= 2) { |
| 4025 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4026 | if (fileObj[1]) { |
| 4027 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4028 | fileObj[1], |
| 4029 | procObj); |
| 4030 | } |
| 4031 | } |
| 4032 | if (file_count >= 3) { |
| 4033 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4034 | if (fileObj[2]) { |
| 4035 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4036 | fileObj[2], |
| 4037 | procObj); |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4042 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4043 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4044 | /* Something failed - remove any dictionary |
| 4045 | * entries that did make it. |
| 4046 | */ |
| 4047 | if (!ins_rc[0] && fileObj[0]) { |
| 4048 | PyDict_DelItem(_PyPopenProcs, |
| 4049 | fileObj[0]); |
| 4050 | } |
| 4051 | if (!ins_rc[1] && fileObj[1]) { |
| 4052 | PyDict_DelItem(_PyPopenProcs, |
| 4053 | fileObj[1]); |
| 4054 | } |
| 4055 | if (!ins_rc[2] && fileObj[2]) { |
| 4056 | PyDict_DelItem(_PyPopenProcs, |
| 4057 | fileObj[2]); |
| 4058 | } |
| 4059 | } |
| 4060 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4061 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4062 | /* |
| 4063 | * Clean up our localized references for the dictionary keys |
| 4064 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4065 | * that got placed in the dictionary. |
| 4066 | */ |
| 4067 | Py_XDECREF(procObj); |
| 4068 | Py_XDECREF(fileObj[0]); |
| 4069 | Py_XDECREF(fileObj[1]); |
| 4070 | Py_XDECREF(fileObj[2]); |
| 4071 | } |
| 4072 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4073 | /* Child is launched. Close the parents copy of those pipe |
| 4074 | * handles that only the child should have open. You need to |
| 4075 | * make sure that no handles to the write end of the output pipe |
| 4076 | * are maintained in this process or else the pipe will not close |
| 4077 | * when the child process exits and the ReadFile will hang. */ |
| 4078 | |
| 4079 | if (!CloseHandle(hChildStdinRd)) |
| 4080 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4081 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4082 | if (!CloseHandle(hChildStdoutWr)) |
| 4083 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4084 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4085 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4086 | return win32_error("CloseHandle", NULL); |
| 4087 | |
| 4088 | return f; |
| 4089 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4090 | |
| 4091 | /* |
| 4092 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4093 | * exit code for the child process and return as a result of the close. |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4094 | * |
| 4095 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4096 | * input file pointer to information about the process that was |
| 4097 | * originally created by the popen* call that created the file pointer. |
| 4098 | * The dictionary uses the file pointer as a key (with one entry |
| 4099 | * inserted for each file returned by the original popen* call) and a |
| 4100 | * single list object as the value for all files from a single call. |
| 4101 | * The list object contains the Win32 process handle at [0], and a file |
| 4102 | * count at [1], which is initialized to the total number of file |
| 4103 | * handles using that list. |
| 4104 | * |
| 4105 | * This function closes whichever handle it is passed, and decrements |
| 4106 | * the file count in the dictionary for the process handle pointed to |
| 4107 | * by this file. On the last close (when the file count reaches zero), |
| 4108 | * this function will wait for the child process and then return its |
| 4109 | * exit code as the result of the close() operation. This permits the |
| 4110 | * files to be closed in any order - it is always the close() of the |
| 4111 | * final handle that will return the exit code. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4112 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4113 | |
| 4114 | /* RED_FLAG 31-Aug-2000 Tim |
| 4115 | * This is always called (today!) between a pair of |
| 4116 | * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS |
| 4117 | * macros. So the thread running this has no valid thread state, as |
| 4118 | * far as Python is concerned. However, this calls some Python API |
| 4119 | * functions that cannot be called safely without a valid thread |
| 4120 | * state, in particular PyDict_GetItem. |
| 4121 | * As a temporary hack (although it may last for years ...), we |
| 4122 | * *rely* on not having a valid thread state in this function, in |
| 4123 | * order to create our own "from scratch". |
| 4124 | * This will deadlock if _PyPclose is ever called by a thread |
| 4125 | * holding the global lock. |
| 4126 | */ |
| 4127 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4128 | static int _PyPclose(FILE *file) |
| 4129 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4130 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4131 | DWORD exit_code; |
| 4132 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4133 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4134 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4135 | #ifdef WITH_THREAD |
| 4136 | PyInterpreterState* pInterpreterState; |
| 4137 | PyThreadState* pThreadState; |
| 4138 | #endif |
| 4139 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4140 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4141 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4142 | */ |
| 4143 | result = fclose(file); |
| 4144 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4145 | #ifdef WITH_THREAD |
| 4146 | /* Bootstrap a valid thread state into existence. */ |
| 4147 | pInterpreterState = PyInterpreterState_New(); |
| 4148 | if (!pInterpreterState) { |
| 4149 | /* Well, we're hosed now! We don't have a thread |
| 4150 | * state, so can't call a nice error routine, or raise |
| 4151 | * an exception. Just die. |
| 4152 | */ |
| 4153 | Py_FatalError("unable to allocate interpreter state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4154 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4155 | return -1; /* unreachable */ |
| 4156 | } |
| 4157 | pThreadState = PyThreadState_New(pInterpreterState); |
| 4158 | if (!pThreadState) { |
| 4159 | Py_FatalError("unable to allocate thread state " |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4160 | "when closing popen object"); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4161 | return -1; /* unreachable */ |
| 4162 | } |
| 4163 | /* Grab the global lock. Note that this will deadlock if the |
| 4164 | * current thread already has the lock! (see RED_FLAG comments |
| 4165 | * before this function) |
| 4166 | */ |
| 4167 | PyEval_RestoreThread(pThreadState); |
| 4168 | #endif |
| 4169 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4170 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4171 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4172 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4173 | fileObj)) != NULL && |
| 4174 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4175 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4176 | |
| 4177 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4178 | file_count = PyInt_AsLong(intObj); |
| 4179 | |
| 4180 | if (file_count > 1) { |
| 4181 | /* Still other files referencing process */ |
| 4182 | file_count--; |
| 4183 | PyList_SetItem(procObj,1, |
| 4184 | PyInt_FromLong(file_count)); |
| 4185 | } else { |
| 4186 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4187 | if (result != EOF && |
| 4188 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4189 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4190 | /* Possible truncation here in 16-bit environments, but |
| 4191 | * real exit codes are just the lower byte in any event. |
| 4192 | */ |
| 4193 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4194 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4195 | /* Indicate failure - this will cause the file object |
| 4196 | * to raise an I/O error and translate the last Win32 |
| 4197 | * error code from errno. We do have a problem with |
| 4198 | * last errors that overlap the normal errno table, |
| 4199 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4200 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4201 | if (result != EOF) { |
| 4202 | /* If the error wasn't from the fclose(), then |
| 4203 | * set errno for the file object error handling. |
| 4204 | */ |
| 4205 | errno = GetLastError(); |
| 4206 | } |
| 4207 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /* Free up the native handle at this point */ |
| 4211 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4212 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4213 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4214 | /* Remove this file pointer from dictionary */ |
| 4215 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4216 | |
| 4217 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4218 | Py_DECREF(_PyPopenProcs); |
| 4219 | _PyPopenProcs = NULL; |
| 4220 | } |
| 4221 | |
| 4222 | } /* if object retrieval ok */ |
| 4223 | |
| 4224 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4225 | } /* if _PyPopenProcs */ |
| 4226 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4227 | #ifdef WITH_THREAD |
| 4228 | /* Tear down the thread & interpreter states. |
| 4229 | * Note that interpreter state clear & delete functions automatically |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4230 | * call the thread clear & delete functions, and indeed insist on |
| 4231 | * doing that themselves. The lock must be held during the clear, but |
| 4232 | * need not be held during the delete. |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4233 | */ |
| 4234 | PyInterpreterState_Clear(pInterpreterState); |
| 4235 | PyEval_ReleaseThread(pThreadState); |
| 4236 | PyInterpreterState_Delete(pInterpreterState); |
| 4237 | #endif |
| 4238 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4239 | return result; |
| 4240 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4241 | |
| 4242 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4243 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4244 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4245 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4246 | char *name; |
| 4247 | char *mode = "r"; |
| 4248 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4249 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4250 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4251 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4252 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4253 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4254 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4255 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4256 | if (fp == NULL) |
| 4257 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4258 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4259 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4260 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4261 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4262 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4263 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4264 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4265 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4266 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4267 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4268 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4269 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4270 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4271 | Set the current process's user id."); |
| 4272 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4274 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4275 | { |
| 4276 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4277 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4278 | return NULL; |
| 4279 | if (setuid(uid) < 0) |
| 4280 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4281 | Py_INCREF(Py_None); |
| 4282 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4283 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4284 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4285 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4286 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4287 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4288 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4289 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4290 | Set the current process's effective user id."); |
| 4291 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4292 | static PyObject * |
| 4293 | posix_seteuid (PyObject *self, PyObject *args) |
| 4294 | { |
| 4295 | int euid; |
| 4296 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4297 | return NULL; |
| 4298 | } else if (seteuid(euid) < 0) { |
| 4299 | return posix_error(); |
| 4300 | } else { |
| 4301 | Py_INCREF(Py_None); |
| 4302 | return Py_None; |
| 4303 | } |
| 4304 | } |
| 4305 | #endif /* HAVE_SETEUID */ |
| 4306 | |
| 4307 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4308 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4309 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4310 | Set the current process's effective group id."); |
| 4311 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4312 | static PyObject * |
| 4313 | posix_setegid (PyObject *self, PyObject *args) |
| 4314 | { |
| 4315 | int egid; |
| 4316 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4317 | return NULL; |
| 4318 | } else if (setegid(egid) < 0) { |
| 4319 | return posix_error(); |
| 4320 | } else { |
| 4321 | Py_INCREF(Py_None); |
| 4322 | return Py_None; |
| 4323 | } |
| 4324 | } |
| 4325 | #endif /* HAVE_SETEGID */ |
| 4326 | |
| 4327 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4328 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4329 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4330 | Set the current process's real and effective user ids."); |
| 4331 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4332 | static PyObject * |
| 4333 | posix_setreuid (PyObject *self, PyObject *args) |
| 4334 | { |
| 4335 | int ruid, euid; |
| 4336 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4337 | return NULL; |
| 4338 | } else if (setreuid(ruid, euid) < 0) { |
| 4339 | return posix_error(); |
| 4340 | } else { |
| 4341 | Py_INCREF(Py_None); |
| 4342 | return Py_None; |
| 4343 | } |
| 4344 | } |
| 4345 | #endif /* HAVE_SETREUID */ |
| 4346 | |
| 4347 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4348 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4349 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4350 | Set the current process's real and effective group ids."); |
| 4351 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4352 | static PyObject * |
| 4353 | posix_setregid (PyObject *self, PyObject *args) |
| 4354 | { |
| 4355 | int rgid, egid; |
| 4356 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4357 | return NULL; |
| 4358 | } else if (setregid(rgid, egid) < 0) { |
| 4359 | return posix_error(); |
| 4360 | } else { |
| 4361 | Py_INCREF(Py_None); |
| 4362 | return Py_None; |
| 4363 | } |
| 4364 | } |
| 4365 | #endif /* HAVE_SETREGID */ |
| 4366 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4367 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4368 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4369 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4370 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4371 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4373 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4374 | { |
| 4375 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4376 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4377 | return NULL; |
| 4378 | if (setgid(gid) < 0) |
| 4379 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4380 | Py_INCREF(Py_None); |
| 4381 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4382 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4383 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4384 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4385 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4386 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4387 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4388 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4389 | |
| 4390 | static PyObject * |
| 4391 | posix_setgroups(PyObject *self, PyObject *args) |
| 4392 | { |
| 4393 | PyObject *groups; |
| 4394 | int i, len; |
| 4395 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4396 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4397 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4398 | return NULL; |
| 4399 | if (!PySequence_Check(groups)) { |
| 4400 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4401 | return NULL; |
| 4402 | } |
| 4403 | len = PySequence_Size(groups); |
| 4404 | if (len > MAX_GROUPS) { |
| 4405 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4406 | return NULL; |
| 4407 | } |
| 4408 | for(i = 0; i < len; i++) { |
| 4409 | PyObject *elem; |
| 4410 | elem = PySequence_GetItem(groups, i); |
| 4411 | if (!elem) |
| 4412 | return NULL; |
| 4413 | if (!PyInt_Check(elem)) { |
| 4414 | PyErr_SetString(PyExc_TypeError, |
| 4415 | "groups must be integers"); |
| 4416 | Py_DECREF(elem); |
| 4417 | return NULL; |
| 4418 | } |
| 4419 | /* XXX: check that value fits into gid_t. */ |
| 4420 | grouplist[i] = PyInt_AsLong(elem); |
| 4421 | Py_DECREF(elem); |
| 4422 | } |
| 4423 | |
| 4424 | if (setgroups(len, grouplist) < 0) |
| 4425 | return posix_error(); |
| 4426 | Py_INCREF(Py_None); |
| 4427 | return Py_None; |
| 4428 | } |
| 4429 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4430 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4431 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4432 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4433 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4434 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4435 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4436 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4437 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4438 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4439 | int pid, options; |
| 4440 | #ifdef UNION_WAIT |
| 4441 | union wait status; |
| 4442 | #define status_i (status.w_status) |
| 4443 | #else |
| 4444 | int status; |
| 4445 | #define status_i status |
| 4446 | #endif |
| 4447 | status_i = 0; |
| 4448 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4449 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4450 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4451 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4452 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4453 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4454 | if (pid == -1) |
| 4455 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4456 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4457 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4460 | #elif defined(HAVE_CWAIT) |
| 4461 | |
| 4462 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4463 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4464 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4465 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4466 | |
| 4467 | static PyObject * |
| 4468 | posix_waitpid(PyObject *self, PyObject *args) |
| 4469 | { |
| 4470 | int pid, options; |
| 4471 | int status; |
| 4472 | |
| 4473 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4474 | return NULL; |
| 4475 | Py_BEGIN_ALLOW_THREADS |
| 4476 | pid = _cwait(&status, pid, options); |
| 4477 | Py_END_ALLOW_THREADS |
| 4478 | if (pid == -1) |
| 4479 | return posix_error(); |
| 4480 | else |
| 4481 | /* shift the status left a byte so this is more like the |
| 4482 | POSIX waitpid */ |
| 4483 | return Py_BuildValue("ii", pid, status << 8); |
| 4484 | } |
| 4485 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4486 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4487 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4488 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4489 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4490 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4491 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4492 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4493 | posix_wait(PyObject *self, PyObject *args) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4494 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4495 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4496 | #ifdef UNION_WAIT |
| 4497 | union wait status; |
| 4498 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4499 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4500 | int status; |
| 4501 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4502 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4503 | if (!PyArg_ParseTuple(args, ":wait")) |
| 4504 | return NULL; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4505 | status_i = 0; |
| 4506 | Py_BEGIN_ALLOW_THREADS |
| 4507 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4508 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4509 | if (pid == -1) |
| 4510 | return posix_error(); |
| 4511 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4512 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4513 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4514 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4515 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4516 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4518 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4519 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4520 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4521 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4522 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4523 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4524 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4525 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4526 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4527 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4528 | #ifdef MS_WINDOWS |
| 4529 | return posix_do_stat(self, args, "et:lstat", STAT, "u:lstat", _wstati64); |
| 4530 | #else |
| 4531 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4532 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4533 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4536 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4537 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4538 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4539 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4540 | Return a string representing the path to which the symbolic link points."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4543 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4544 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4545 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4546 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4547 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4548 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4549 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4550 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4551 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4552 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4553 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4554 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4555 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4556 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4557 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4558 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4559 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4560 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4561 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4562 | "symlink(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4563 | Create a symbolic link."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4564 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4565 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4566 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4567 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4568 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4569 | } |
| 4570 | #endif /* HAVE_SYMLINK */ |
| 4571 | |
| 4572 | |
| 4573 | #ifdef HAVE_TIMES |
| 4574 | #ifndef HZ |
| 4575 | #define HZ 60 /* Universal constant :-) */ |
| 4576 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4577 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4578 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4579 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4580 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4581 | { |
| 4582 | ULONG value = 0; |
| 4583 | |
| 4584 | Py_BEGIN_ALLOW_THREADS |
| 4585 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4586 | Py_END_ALLOW_THREADS |
| 4587 | |
| 4588 | return value; |
| 4589 | } |
| 4590 | |
| 4591 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4592 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4593 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4594 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4595 | return NULL; |
| 4596 | |
| 4597 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4598 | return Py_BuildValue("ddddd", |
| 4599 | (double)0 /* t.tms_utime / HZ */, |
| 4600 | (double)0 /* t.tms_stime / HZ */, |
| 4601 | (double)0 /* t.tms_cutime / HZ */, |
| 4602 | (double)0 /* t.tms_cstime / HZ */, |
| 4603 | (double)system_uptime() / 1000); |
| 4604 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4605 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4606 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4607 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4608 | { |
| 4609 | struct tms t; |
| 4610 | clock_t c; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4611 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4612 | return NULL; |
| 4613 | errno = 0; |
| 4614 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4615 | if (c == (clock_t) -1) |
| 4616 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4617 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4618 | (double)t.tms_utime / HZ, |
| 4619 | (double)t.tms_stime / HZ, |
| 4620 | (double)t.tms_cutime / HZ, |
| 4621 | (double)t.tms_cstime / HZ, |
| 4622 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4623 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4624 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4625 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4626 | |
| 4627 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4628 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4629 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4630 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4631 | posix_times(PyObject *self, PyObject *args) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4632 | { |
| 4633 | FILETIME create, exit, kernel, user; |
| 4634 | HANDLE hProc; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4635 | if (!PyArg_ParseTuple(args, ":times")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4636 | return NULL; |
| 4637 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4638 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4639 | /* The fields of a FILETIME structure are the hi and lo part |
| 4640 | of a 64-bit value expressed in 100 nanosecond units. |
| 4641 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4642 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4643 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4644 | return Py_BuildValue( |
| 4645 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4646 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4647 | kernel.dwLowDateTime*1e-7), |
| 4648 | (double)(user.dwHighDateTime*429.4967296 + |
| 4649 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4650 | (double)0, |
| 4651 | (double)0, |
| 4652 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4653 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4654 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4655 | |
| 4656 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4657 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4658 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4659 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4660 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4661 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4662 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4663 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4664 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4665 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4666 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4668 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4669 | posix_setsid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4670 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4671 | if (!PyArg_ParseTuple(args, ":setsid")) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4672 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4673 | if (setsid() < 0) |
| 4674 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4675 | Py_INCREF(Py_None); |
| 4676 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4677 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4678 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4679 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4680 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4681 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4682 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4683 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4686 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4687 | { |
| 4688 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4689 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4690 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4691 | if (setpgid(pid, pgrp) < 0) |
| 4692 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4693 | Py_INCREF(Py_None); |
| 4694 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4695 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4696 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4697 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4698 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4699 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4700 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4701 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4702 | Return the process group associated with the terminal given by a fd."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4703 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4704 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4705 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4706 | { |
| 4707 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4708 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4709 | return NULL; |
| 4710 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4711 | if (pgid < 0) |
| 4712 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4713 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4714 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4715 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4716 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4717 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4718 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4719 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4720 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4721 | Set the process group associated with the terminal given by a fd."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4722 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4723 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4724 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4725 | { |
| 4726 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4727 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4728 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4729 | if (tcsetpgrp(fd, pgid) < 0) |
| 4730 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4731 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4732 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4733 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4734 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4735 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4736 | /* Functions acting on file descriptors */ |
| 4737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4738 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4739 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4740 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4742 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4743 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4744 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4745 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4746 | int flag; |
| 4747 | int mode = 0777; |
| 4748 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4749 | |
| 4750 | #ifdef MS_WINDOWS |
| 4751 | if (unicode_file_names()) { |
| 4752 | PyUnicodeObject *po; |
| 4753 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4754 | Py_BEGIN_ALLOW_THREADS |
| 4755 | /* PyUnicode_AS_UNICODE OK without thread |
| 4756 | lock as it is a simple dereference. */ |
| 4757 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4758 | Py_END_ALLOW_THREADS |
| 4759 | if (fd < 0) |
| 4760 | return posix_error(); |
| 4761 | return PyInt_FromLong((long)fd); |
| 4762 | } |
| 4763 | /* Drop the argument parsing error as narrow strings |
| 4764 | are also valid. */ |
| 4765 | PyErr_Clear(); |
| 4766 | } |
| 4767 | #endif |
| 4768 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4769 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4770 | Py_FileSystemDefaultEncoding, &file, |
| 4771 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4772 | return NULL; |
| 4773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4774 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4775 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4776 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4777 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4778 | return posix_error_with_allocated_filename(file); |
| 4779 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4780 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4784 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4785 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4786 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4787 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4788 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4789 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4790 | { |
| 4791 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4792 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4793 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4794 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4795 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4796 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4797 | if (res < 0) |
| 4798 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4799 | Py_INCREF(Py_None); |
| 4800 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4804 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4805 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4806 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4809 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4810 | { |
| 4811 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4812 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4813 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4814 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4815 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4816 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4817 | if (fd < 0) |
| 4818 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4819 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4822 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4823 | PyDoc_STRVAR(posix_dup2__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4824 | "dup2(fd, fd2)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4825 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4826 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4827 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4828 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4829 | { |
| 4830 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4831 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4832 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4833 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4834 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4835 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4836 | if (res < 0) |
| 4837 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4838 | Py_INCREF(Py_None); |
| 4839 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4843 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4844 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4845 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4846 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4847 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4848 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4849 | { |
| 4850 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4851 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4852 | LONG_LONG pos, res; |
| 4853 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4854 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4855 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4856 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4857 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4858 | return NULL; |
| 4859 | #ifdef SEEK_SET |
| 4860 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 4861 | switch (how) { |
| 4862 | case 0: how = SEEK_SET; break; |
| 4863 | case 1: how = SEEK_CUR; break; |
| 4864 | case 2: how = SEEK_END; break; |
| 4865 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4866 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4867 | |
| 4868 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 4869 | pos = PyInt_AsLong(posobj); |
| 4870 | #else |
| 4871 | pos = PyLong_Check(posobj) ? |
| 4872 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 4873 | #endif |
| 4874 | if (PyErr_Occurred()) |
| 4875 | return NULL; |
| 4876 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4877 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4878 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4879 | res = _lseeki64(fd, pos, how); |
| 4880 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4881 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4882 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4883 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4884 | if (res < 0) |
| 4885 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4886 | |
| 4887 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4888 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 4889 | #else |
| 4890 | return PyLong_FromLongLong(res); |
| 4891 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4895 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4896 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4897 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4898 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4899 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4900 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4901 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4902 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4903 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4904 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4905 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4906 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4907 | if (buffer == NULL) |
| 4908 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4909 | Py_BEGIN_ALLOW_THREADS |
| 4910 | n = read(fd, PyString_AsString(buffer), size); |
| 4911 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4912 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4913 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4914 | return posix_error(); |
| 4915 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 4916 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4917 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4918 | return buffer; |
| 4919 | } |
| 4920 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4921 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4922 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4923 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4924 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4925 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4926 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4927 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4928 | { |
| 4929 | int fd, size; |
| 4930 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4931 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4932 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4933 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4934 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4935 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4936 | if (size < 0) |
| 4937 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4938 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4941 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4942 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4943 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4944 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4945 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4946 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4947 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4948 | { |
| 4949 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4950 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4951 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4952 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4953 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4954 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4955 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4956 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4957 | if (res != 0) |
| 4958 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4959 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4960 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | PyDoc_STRVAR(posix_fdopen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4965 | "fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4968 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4969 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4970 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4971 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4972 | char *mode = "r"; |
| 4973 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4974 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4975 | PyObject *f; |
| 4976 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4977 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4978 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4979 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4980 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4981 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4982 | if (fp == NULL) |
| 4983 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 4984 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4985 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4986 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4987 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4990 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4991 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 4992 | Return True if the file descriptor 'fd' is an open file descriptor\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4993 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4994 | |
| 4995 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 4996 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 4997 | { |
| 4998 | int fd; |
| 4999 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5000 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5001 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5002 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5003 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5004 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5005 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5006 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5007 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5009 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5010 | posix_pipe(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5011 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5012 | #if defined(PYOS_OS2) |
| 5013 | HFILE read, write; |
| 5014 | APIRET rc; |
| 5015 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5016 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5017 | return NULL; |
| 5018 | |
| 5019 | Py_BEGIN_ALLOW_THREADS |
| 5020 | rc = DosCreatePipe( &read, &write, 4096); |
| 5021 | Py_END_ALLOW_THREADS |
| 5022 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5023 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5024 | |
| 5025 | return Py_BuildValue("(ii)", read, write); |
| 5026 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5027 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5028 | int fds[2]; |
| 5029 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5030 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5031 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5032 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5033 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5034 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5035 | if (res != 0) |
| 5036 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5037 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5038 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5039 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5040 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5041 | BOOL ok; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5042 | if (!PyArg_ParseTuple(args, ":pipe")) |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5043 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5044 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5045 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5046 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5047 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5048 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5049 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5050 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5051 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5052 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5053 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5054 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5055 | #endif /* HAVE_PIPE */ |
| 5056 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5057 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5058 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5059 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5060 | "mkfifo(filename, [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5061 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5062 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5063 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5064 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5065 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5066 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5067 | int mode = 0666; |
| 5068 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5069 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5070 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5071 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5072 | res = mkfifo(filename, mode); |
| 5073 | Py_END_ALLOW_THREADS |
| 5074 | if (res < 0) |
| 5075 | return posix_error(); |
| 5076 | Py_INCREF(Py_None); |
| 5077 | return Py_None; |
| 5078 | } |
| 5079 | #endif |
| 5080 | |
| 5081 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5082 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5083 | PyDoc_STRVAR(posix_mknod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5084 | "mknod(filename, [, mode=0600, major, minor])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5085 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5086 | named filename. mode specifies both the permissions to use and the\n\ |
| 5087 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5088 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\ |
| 5089 | major and minor define the newly created device special file, otherwise\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5090 | they are ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5091 | |
| 5092 | |
| 5093 | static PyObject * |
| 5094 | posix_mknod(PyObject *self, PyObject *args) |
| 5095 | { |
| 5096 | char *filename; |
| 5097 | int mode = 0600; |
| 5098 | int major = 0; |
| 5099 | int minor = 0; |
| 5100 | int res; |
| 5101 | if (!PyArg_ParseTuple(args, "s|iii:mknod", &filename, |
| 5102 | &mode, &major, &minor)) |
| 5103 | return NULL; |
| 5104 | Py_BEGIN_ALLOW_THREADS |
| 5105 | res = mknod(filename, mode, makedev(major, minor)); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5106 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5107 | if (res < 0) |
| 5108 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5109 | Py_INCREF(Py_None); |
| 5110 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5111 | } |
| 5112 | #endif |
| 5113 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5114 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5115 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5116 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5117 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5118 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5119 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5120 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5121 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5122 | { |
| 5123 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5124 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5125 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5126 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5127 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5128 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5129 | return NULL; |
| 5130 | |
| 5131 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5132 | length = PyInt_AsLong(lenobj); |
| 5133 | #else |
| 5134 | length = PyLong_Check(lenobj) ? |
| 5135 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5136 | #endif |
| 5137 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5138 | return NULL; |
| 5139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5140 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5141 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5142 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5143 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5144 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5145 | return NULL; |
| 5146 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5147 | Py_INCREF(Py_None); |
| 5148 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5149 | } |
| 5150 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5151 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5152 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5153 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5154 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5155 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5156 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5157 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5158 | * get re-set with another call for the same key. */ |
| 5159 | static PyObject *posix_putenv_garbage; |
| 5160 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5161 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5162 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5163 | { |
| 5164 | char *s1, *s2; |
| 5165 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5166 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5167 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5168 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5169 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5170 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5171 | |
| 5172 | #if defined(PYOS_OS2) |
| 5173 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5174 | APIRET rc; |
| 5175 | |
| 5176 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5177 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5178 | |
| 5179 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5180 | if (rc != NO_ERROR) |
| 5181 | return os2_error(rc); |
| 5182 | |
| 5183 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5184 | APIRET rc; |
| 5185 | |
| 5186 | if (strlen(s2) == 0) /* If New Value is an Empty String */ |
| 5187 | s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */ |
| 5188 | |
| 5189 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5190 | if (rc != NO_ERROR) |
| 5191 | return os2_error(rc); |
| 5192 | } else { |
| 5193 | #endif |
| 5194 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5195 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5196 | len = strlen(s1) + strlen(s2) + 2; |
| 5197 | /* len includes space for a trailing \0; the size arg to |
| 5198 | PyString_FromStringAndSize does not count that */ |
| 5199 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5200 | if (newstr == NULL) |
| 5201 | return PyErr_NoMemory(); |
| 5202 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5203 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5204 | if (putenv(new)) { |
| 5205 | posix_error(); |
| 5206 | return NULL; |
| 5207 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5208 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5209 | * this will cause previous value to be collected. This has to |
| 5210 | * happen after the real putenv() call because the old value |
| 5211 | * was still accessible until then. */ |
| 5212 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5213 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5214 | /* really not much we can do; just leak */ |
| 5215 | PyErr_Clear(); |
| 5216 | } |
| 5217 | else { |
| 5218 | Py_DECREF(newstr); |
| 5219 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5220 | |
| 5221 | #if defined(PYOS_OS2) |
| 5222 | } |
| 5223 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5224 | Py_INCREF(Py_None); |
| 5225 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5226 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5227 | #endif /* putenv */ |
| 5228 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5229 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5230 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5231 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5232 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5233 | |
| 5234 | static PyObject * |
| 5235 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5236 | { |
| 5237 | char *s1; |
| 5238 | |
| 5239 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5240 | return NULL; |
| 5241 | |
| 5242 | unsetenv(s1); |
| 5243 | |
| 5244 | /* Remove the key from posix_putenv_garbage; |
| 5245 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5246 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5247 | * old value was still accessible until then. |
| 5248 | */ |
| 5249 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5250 | PyTuple_GET_ITEM(args, 0))) { |
| 5251 | /* really not much we can do; just leak */ |
| 5252 | PyErr_Clear(); |
| 5253 | } |
| 5254 | |
| 5255 | Py_INCREF(Py_None); |
| 5256 | return Py_None; |
| 5257 | } |
| 5258 | #endif /* unsetenv */ |
| 5259 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5260 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5261 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5262 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5263 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5264 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5265 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5266 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5267 | { |
| 5268 | int code; |
| 5269 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5270 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5271 | return NULL; |
| 5272 | message = strerror(code); |
| 5273 | if (message == NULL) { |
| 5274 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5275 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5276 | return NULL; |
| 5277 | } |
| 5278 | return PyString_FromString(message); |
| 5279 | } |
| 5280 | #endif /* strerror */ |
| 5281 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5282 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5283 | #ifdef HAVE_SYS_WAIT_H |
| 5284 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5285 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5286 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5287 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5288 | Return True if the process returning 'status' was dumped to a core file."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5289 | |
| 5290 | static PyObject * |
| 5291 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5292 | { |
| 5293 | #ifdef UNION_WAIT |
| 5294 | union wait status; |
| 5295 | #define status_i (status.w_status) |
| 5296 | #else |
| 5297 | int status; |
| 5298 | #define status_i status |
| 5299 | #endif |
| 5300 | status_i = 0; |
| 5301 | |
| 5302 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5303 | { |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | |
| 5307 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5308 | #undef status_i |
| 5309 | } |
| 5310 | #endif /* WCOREDUMP */ |
| 5311 | |
| 5312 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5313 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5314 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5315 | Return True if the process returning 'status' was continued from a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5316 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5317 | |
| 5318 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5319 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5320 | { |
| 5321 | #ifdef UNION_WAIT |
| 5322 | union wait status; |
| 5323 | #define status_i (status.w_status) |
| 5324 | #else |
| 5325 | int status; |
| 5326 | #define status_i status |
| 5327 | #endif |
| 5328 | status_i = 0; |
| 5329 | |
| 5330 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5331 | { |
| 5332 | return NULL; |
| 5333 | } |
| 5334 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5335 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5336 | #undef status_i |
| 5337 | } |
| 5338 | #endif /* WIFCONTINUED */ |
| 5339 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5340 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5341 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5342 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5343 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5344 | |
| 5345 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5346 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5347 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5348 | #ifdef UNION_WAIT |
| 5349 | union wait status; |
| 5350 | #define status_i (status.w_status) |
| 5351 | #else |
| 5352 | int status; |
| 5353 | #define status_i status |
| 5354 | #endif |
| 5355 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5356 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5357 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5358 | { |
| 5359 | return NULL; |
| 5360 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5361 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5362 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5363 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5364 | } |
| 5365 | #endif /* WIFSTOPPED */ |
| 5366 | |
| 5367 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5368 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5369 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5370 | 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] | 5371 | |
| 5372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5373 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5374 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5375 | #ifdef UNION_WAIT |
| 5376 | union wait status; |
| 5377 | #define status_i (status.w_status) |
| 5378 | #else |
| 5379 | int status; |
| 5380 | #define status_i status |
| 5381 | #endif |
| 5382 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5383 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5384 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5385 | { |
| 5386 | return NULL; |
| 5387 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5388 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5389 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5390 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5391 | } |
| 5392 | #endif /* WIFSIGNALED */ |
| 5393 | |
| 5394 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5396 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5397 | Return true if the process returning 'status' exited using the exit()\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5398 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5399 | |
| 5400 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5401 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5402 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5403 | #ifdef UNION_WAIT |
| 5404 | union wait status; |
| 5405 | #define status_i (status.w_status) |
| 5406 | #else |
| 5407 | int status; |
| 5408 | #define status_i status |
| 5409 | #endif |
| 5410 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5411 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5412 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5413 | { |
| 5414 | return NULL; |
| 5415 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5416 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5417 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5418 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5419 | } |
| 5420 | #endif /* WIFEXITED */ |
| 5421 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5422 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5423 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5424 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5425 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5426 | |
| 5427 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5428 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5429 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5430 | #ifdef UNION_WAIT |
| 5431 | union wait status; |
| 5432 | #define status_i (status.w_status) |
| 5433 | #else |
| 5434 | int status; |
| 5435 | #define status_i status |
| 5436 | #endif |
| 5437 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5438 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5439 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5440 | { |
| 5441 | return NULL; |
| 5442 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5443 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5444 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5445 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5446 | } |
| 5447 | #endif /* WEXITSTATUS */ |
| 5448 | |
| 5449 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5450 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5451 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5452 | Return the signal that terminated the process that provided the 'status'\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5453 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5454 | |
| 5455 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5456 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5457 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5458 | #ifdef UNION_WAIT |
| 5459 | union wait status; |
| 5460 | #define status_i (status.w_status) |
| 5461 | #else |
| 5462 | int status; |
| 5463 | #define status_i status |
| 5464 | #endif |
| 5465 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5466 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5467 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5468 | { |
| 5469 | return NULL; |
| 5470 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5471 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5472 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5473 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5474 | } |
| 5475 | #endif /* WTERMSIG */ |
| 5476 | |
| 5477 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5478 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5479 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5480 | Return the signal that stopped the process that provided\n\ |
| 5481 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5482 | |
| 5483 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5484 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5485 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5486 | #ifdef UNION_WAIT |
| 5487 | union wait status; |
| 5488 | #define status_i (status.w_status) |
| 5489 | #else |
| 5490 | int status; |
| 5491 | #define status_i status |
| 5492 | #endif |
| 5493 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5494 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5495 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5496 | { |
| 5497 | return NULL; |
| 5498 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5499 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5500 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5501 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5502 | } |
| 5503 | #endif /* WSTOPSIG */ |
| 5504 | |
| 5505 | #endif /* HAVE_SYS_WAIT_H */ |
| 5506 | |
| 5507 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5508 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5509 | #ifdef _SCO_DS |
| 5510 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5511 | needed definitions in sys/statvfs.h */ |
| 5512 | #define _SVID3 |
| 5513 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5514 | #include <sys/statvfs.h> |
| 5515 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5516 | static PyObject* |
| 5517 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5518 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5519 | if (v == NULL) |
| 5520 | return NULL; |
| 5521 | |
| 5522 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5523 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5524 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5525 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5526 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5527 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5528 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5529 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5530 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5531 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5532 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5533 | #else |
| 5534 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5535 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5536 | PyStructSequence_SET_ITEM(v, 2, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5537 | PyLong_FromLongLong((LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5538 | PyStructSequence_SET_ITEM(v, 3, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5539 | PyLong_FromLongLong((LONG_LONG) st.f_bfree)); |
| 5540 | PyStructSequence_SET_ITEM(v, 4, |
| 5541 | PyLong_FromLongLong((LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5542 | PyStructSequence_SET_ITEM(v, 5, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5543 | PyLong_FromLongLong((LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5544 | PyStructSequence_SET_ITEM(v, 6, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5545 | PyLong_FromLongLong((LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5546 | PyStructSequence_SET_ITEM(v, 7, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5547 | PyLong_FromLongLong((LONG_LONG) st.f_favail)); |
| 5548 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5549 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5550 | #endif |
| 5551 | |
| 5552 | return v; |
| 5553 | } |
| 5554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5555 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5556 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5558 | |
| 5559 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5560 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5561 | { |
| 5562 | int fd, res; |
| 5563 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5564 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5565 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5566 | return NULL; |
| 5567 | Py_BEGIN_ALLOW_THREADS |
| 5568 | res = fstatvfs(fd, &st); |
| 5569 | Py_END_ALLOW_THREADS |
| 5570 | if (res != 0) |
| 5571 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5572 | |
| 5573 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5574 | } |
| 5575 | #endif /* HAVE_FSTATVFS */ |
| 5576 | |
| 5577 | |
| 5578 | #if defined(HAVE_STATVFS) |
| 5579 | #include <sys/statvfs.h> |
| 5580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5581 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5582 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5583 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5584 | |
| 5585 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5586 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5587 | { |
| 5588 | char *path; |
| 5589 | int res; |
| 5590 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5591 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5592 | return NULL; |
| 5593 | Py_BEGIN_ALLOW_THREADS |
| 5594 | res = statvfs(path, &st); |
| 5595 | Py_END_ALLOW_THREADS |
| 5596 | if (res != 0) |
| 5597 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5598 | |
| 5599 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5600 | } |
| 5601 | #endif /* HAVE_STATVFS */ |
| 5602 | |
| 5603 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5604 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5605 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5606 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5607 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5608 | The directory and a prefix may be specified as strings; they may be omitted\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5610 | |
| 5611 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5612 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5613 | { |
| 5614 | PyObject *result = NULL; |
| 5615 | char *dir = NULL; |
| 5616 | char *pfx = NULL; |
| 5617 | char *name; |
| 5618 | |
| 5619 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5620 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5621 | |
| 5622 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5623 | "tempnam is a potential security risk to your program") < 0) |
| 5624 | return NULL; |
| 5625 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5626 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5627 | name = _tempnam(dir, pfx); |
| 5628 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5629 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5630 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5631 | if (name == NULL) |
| 5632 | return PyErr_NoMemory(); |
| 5633 | result = PyString_FromString(name); |
| 5634 | free(name); |
| 5635 | return result; |
| 5636 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5637 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5638 | |
| 5639 | |
| 5640 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5641 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5642 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5643 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5644 | |
| 5645 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5646 | posix_tmpfile(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5647 | { |
| 5648 | FILE *fp; |
| 5649 | |
| 5650 | if (!PyArg_ParseTuple(args, ":tmpfile")) |
| 5651 | return NULL; |
| 5652 | fp = tmpfile(); |
| 5653 | if (fp == NULL) |
| 5654 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5655 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5656 | } |
| 5657 | #endif |
| 5658 | |
| 5659 | |
| 5660 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5661 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5662 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5663 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5664 | |
| 5665 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5666 | posix_tmpnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5667 | { |
| 5668 | char buffer[L_tmpnam]; |
| 5669 | char *name; |
| 5670 | |
| 5671 | if (!PyArg_ParseTuple(args, ":tmpnam")) |
| 5672 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5673 | |
| 5674 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5675 | "tmpnam is a potential security risk to your program") < 0) |
| 5676 | return NULL; |
| 5677 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5678 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5679 | name = tmpnam_r(buffer); |
| 5680 | #else |
| 5681 | name = tmpnam(buffer); |
| 5682 | #endif |
| 5683 | if (name == NULL) { |
| 5684 | PyErr_SetObject(PyExc_OSError, |
| 5685 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5686 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5687 | "unexpected NULL from tmpnam_r" |
| 5688 | #else |
| 5689 | "unexpected NULL from tmpnam" |
| 5690 | #endif |
| 5691 | )); |
| 5692 | return NULL; |
| 5693 | } |
| 5694 | return PyString_FromString(buffer); |
| 5695 | } |
| 5696 | #endif |
| 5697 | |
| 5698 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5699 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5700 | * It maps strings representing configuration variable names to |
| 5701 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5702 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5703 | * rarely-used constants. There are three separate tables that use |
| 5704 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5705 | * |
| 5706 | * This code is always included, even if none of the interfaces that |
| 5707 | * need it are included. The #if hackery needed to avoid it would be |
| 5708 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5709 | */ |
| 5710 | struct constdef { |
| 5711 | char *name; |
| 5712 | long value; |
| 5713 | }; |
| 5714 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5715 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5716 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5717 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5718 | { |
| 5719 | if (PyInt_Check(arg)) { |
| 5720 | *valuep = PyInt_AS_LONG(arg); |
| 5721 | return 1; |
| 5722 | } |
| 5723 | if (PyString_Check(arg)) { |
| 5724 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5725 | size_t lo = 0; |
| 5726 | size_t mid; |
| 5727 | size_t hi = tablesize; |
| 5728 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5729 | char *confname = PyString_AS_STRING(arg); |
| 5730 | while (lo < hi) { |
| 5731 | mid = (lo + hi) / 2; |
| 5732 | cmp = strcmp(confname, table[mid].name); |
| 5733 | if (cmp < 0) |
| 5734 | hi = mid; |
| 5735 | else if (cmp > 0) |
| 5736 | lo = mid + 1; |
| 5737 | else { |
| 5738 | *valuep = table[mid].value; |
| 5739 | return 1; |
| 5740 | } |
| 5741 | } |
| 5742 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5743 | } |
| 5744 | else |
| 5745 | PyErr_SetString(PyExc_TypeError, |
| 5746 | "configuration names must be strings or integers"); |
| 5747 | return 0; |
| 5748 | } |
| 5749 | |
| 5750 | |
| 5751 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5752 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5753 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5754 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5755 | #endif |
| 5756 | #ifdef _PC_ABI_ASYNC_IO |
| 5757 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5759 | #ifdef _PC_ASYNC_IO |
| 5760 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5761 | #endif |
| 5762 | #ifdef _PC_CHOWN_RESTRICTED |
| 5763 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5764 | #endif |
| 5765 | #ifdef _PC_FILESIZEBITS |
| 5766 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5767 | #endif |
| 5768 | #ifdef _PC_LAST |
| 5769 | {"PC_LAST", _PC_LAST}, |
| 5770 | #endif |
| 5771 | #ifdef _PC_LINK_MAX |
| 5772 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5773 | #endif |
| 5774 | #ifdef _PC_MAX_CANON |
| 5775 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5776 | #endif |
| 5777 | #ifdef _PC_MAX_INPUT |
| 5778 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5779 | #endif |
| 5780 | #ifdef _PC_NAME_MAX |
| 5781 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5782 | #endif |
| 5783 | #ifdef _PC_NO_TRUNC |
| 5784 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5785 | #endif |
| 5786 | #ifdef _PC_PATH_MAX |
| 5787 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5788 | #endif |
| 5789 | #ifdef _PC_PIPE_BUF |
| 5790 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5791 | #endif |
| 5792 | #ifdef _PC_PRIO_IO |
| 5793 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5794 | #endif |
| 5795 | #ifdef _PC_SOCK_MAXBUF |
| 5796 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5797 | #endif |
| 5798 | #ifdef _PC_SYNC_IO |
| 5799 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5800 | #endif |
| 5801 | #ifdef _PC_VDISABLE |
| 5802 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5803 | #endif |
| 5804 | }; |
| 5805 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5806 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5807 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5808 | { |
| 5809 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5810 | sizeof(posix_constants_pathconf) |
| 5811 | / sizeof(struct constdef)); |
| 5812 | } |
| 5813 | #endif |
| 5814 | |
| 5815 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5816 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5817 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5818 | Return the configuration limit name for the file descriptor fd.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5819 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5820 | |
| 5821 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5822 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5823 | { |
| 5824 | PyObject *result = NULL; |
| 5825 | int name, fd; |
| 5826 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5827 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5828 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5829 | long limit; |
| 5830 | |
| 5831 | errno = 0; |
| 5832 | limit = fpathconf(fd, name); |
| 5833 | if (limit == -1 && errno != 0) |
| 5834 | posix_error(); |
| 5835 | else |
| 5836 | result = PyInt_FromLong(limit); |
| 5837 | } |
| 5838 | return result; |
| 5839 | } |
| 5840 | #endif |
| 5841 | |
| 5842 | |
| 5843 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5844 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5845 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5846 | Return the configuration limit name for the file or directory path.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5847 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5848 | |
| 5849 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5850 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5851 | { |
| 5852 | PyObject *result = NULL; |
| 5853 | int name; |
| 5854 | char *path; |
| 5855 | |
| 5856 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5857 | conv_path_confname, &name)) { |
| 5858 | long limit; |
| 5859 | |
| 5860 | errno = 0; |
| 5861 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5862 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5863 | if (errno == EINVAL) |
| 5864 | /* could be a path or name problem */ |
| 5865 | posix_error(); |
| 5866 | else |
| 5867 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5868 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5869 | else |
| 5870 | result = PyInt_FromLong(limit); |
| 5871 | } |
| 5872 | return result; |
| 5873 | } |
| 5874 | #endif |
| 5875 | |
| 5876 | #ifdef HAVE_CONFSTR |
| 5877 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5878 | #ifdef _CS_ARCHITECTURE |
| 5879 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5880 | #endif |
| 5881 | #ifdef _CS_HOSTNAME |
| 5882 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5883 | #endif |
| 5884 | #ifdef _CS_HW_PROVIDER |
| 5885 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5886 | #endif |
| 5887 | #ifdef _CS_HW_SERIAL |
| 5888 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5889 | #endif |
| 5890 | #ifdef _CS_INITTAB_NAME |
| 5891 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5892 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5893 | #ifdef _CS_LFS64_CFLAGS |
| 5894 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5895 | #endif |
| 5896 | #ifdef _CS_LFS64_LDFLAGS |
| 5897 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 5898 | #endif |
| 5899 | #ifdef _CS_LFS64_LIBS |
| 5900 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 5901 | #endif |
| 5902 | #ifdef _CS_LFS64_LINTFLAGS |
| 5903 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 5904 | #endif |
| 5905 | #ifdef _CS_LFS_CFLAGS |
| 5906 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 5907 | #endif |
| 5908 | #ifdef _CS_LFS_LDFLAGS |
| 5909 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 5910 | #endif |
| 5911 | #ifdef _CS_LFS_LIBS |
| 5912 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 5913 | #endif |
| 5914 | #ifdef _CS_LFS_LINTFLAGS |
| 5915 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 5916 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5917 | #ifdef _CS_MACHINE |
| 5918 | {"CS_MACHINE", _CS_MACHINE}, |
| 5919 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5920 | #ifdef _CS_PATH |
| 5921 | {"CS_PATH", _CS_PATH}, |
| 5922 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5923 | #ifdef _CS_RELEASE |
| 5924 | {"CS_RELEASE", _CS_RELEASE}, |
| 5925 | #endif |
| 5926 | #ifdef _CS_SRPC_DOMAIN |
| 5927 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 5928 | #endif |
| 5929 | #ifdef _CS_SYSNAME |
| 5930 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 5931 | #endif |
| 5932 | #ifdef _CS_VERSION |
| 5933 | {"CS_VERSION", _CS_VERSION}, |
| 5934 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5935 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 5936 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 5937 | #endif |
| 5938 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 5939 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 5940 | #endif |
| 5941 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 5942 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 5943 | #endif |
| 5944 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 5945 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 5946 | #endif |
| 5947 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 5948 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 5949 | #endif |
| 5950 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 5951 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 5952 | #endif |
| 5953 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 5954 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 5955 | #endif |
| 5956 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 5957 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 5958 | #endif |
| 5959 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 5960 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 5961 | #endif |
| 5962 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 5963 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 5964 | #endif |
| 5965 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 5966 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 5967 | #endif |
| 5968 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 5969 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 5970 | #endif |
| 5971 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 5972 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 5973 | #endif |
| 5974 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 5975 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 5976 | #endif |
| 5977 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 5978 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 5979 | #endif |
| 5980 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 5981 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 5982 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5983 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 5984 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 5985 | #endif |
| 5986 | #ifdef _MIPS_CS_BASE |
| 5987 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 5988 | #endif |
| 5989 | #ifdef _MIPS_CS_HOSTID |
| 5990 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 5991 | #endif |
| 5992 | #ifdef _MIPS_CS_HW_NAME |
| 5993 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 5994 | #endif |
| 5995 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 5996 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 5997 | #endif |
| 5998 | #ifdef _MIPS_CS_OSREL_MAJ |
| 5999 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6000 | #endif |
| 6001 | #ifdef _MIPS_CS_OSREL_MIN |
| 6002 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6003 | #endif |
| 6004 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6005 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6006 | #endif |
| 6007 | #ifdef _MIPS_CS_OS_NAME |
| 6008 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6009 | #endif |
| 6010 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6011 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6012 | #endif |
| 6013 | #ifdef _MIPS_CS_PROCESSORS |
| 6014 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6015 | #endif |
| 6016 | #ifdef _MIPS_CS_SERIAL |
| 6017 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6018 | #endif |
| 6019 | #ifdef _MIPS_CS_VENDOR |
| 6020 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6021 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6022 | }; |
| 6023 | |
| 6024 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6025 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6026 | { |
| 6027 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6028 | sizeof(posix_constants_confstr) |
| 6029 | / sizeof(struct constdef)); |
| 6030 | } |
| 6031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6032 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6033 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6034 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6035 | |
| 6036 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6037 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6038 | { |
| 6039 | PyObject *result = NULL; |
| 6040 | int name; |
| 6041 | char buffer[64]; |
| 6042 | |
| 6043 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6044 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6045 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6046 | errno = 0; |
| 6047 | if (len == 0) { |
| 6048 | if (errno != 0) |
| 6049 | posix_error(); |
| 6050 | else |
| 6051 | result = PyString_FromString(""); |
| 6052 | } |
| 6053 | else { |
| 6054 | if (len >= sizeof(buffer)) { |
| 6055 | result = PyString_FromStringAndSize(NULL, len); |
| 6056 | if (result != NULL) |
| 6057 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6058 | } |
| 6059 | else |
| 6060 | result = PyString_FromString(buffer); |
| 6061 | } |
| 6062 | } |
| 6063 | return result; |
| 6064 | } |
| 6065 | #endif |
| 6066 | |
| 6067 | |
| 6068 | #ifdef HAVE_SYSCONF |
| 6069 | static struct constdef posix_constants_sysconf[] = { |
| 6070 | #ifdef _SC_2_CHAR_TERM |
| 6071 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6072 | #endif |
| 6073 | #ifdef _SC_2_C_BIND |
| 6074 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6075 | #endif |
| 6076 | #ifdef _SC_2_C_DEV |
| 6077 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6078 | #endif |
| 6079 | #ifdef _SC_2_C_VERSION |
| 6080 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6081 | #endif |
| 6082 | #ifdef _SC_2_FORT_DEV |
| 6083 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6084 | #endif |
| 6085 | #ifdef _SC_2_FORT_RUN |
| 6086 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6087 | #endif |
| 6088 | #ifdef _SC_2_LOCALEDEF |
| 6089 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6090 | #endif |
| 6091 | #ifdef _SC_2_SW_DEV |
| 6092 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6093 | #endif |
| 6094 | #ifdef _SC_2_UPE |
| 6095 | {"SC_2_UPE", _SC_2_UPE}, |
| 6096 | #endif |
| 6097 | #ifdef _SC_2_VERSION |
| 6098 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6099 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6100 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6101 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6102 | #endif |
| 6103 | #ifdef _SC_ACL |
| 6104 | {"SC_ACL", _SC_ACL}, |
| 6105 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6106 | #ifdef _SC_AIO_LISTIO_MAX |
| 6107 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6108 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6109 | #ifdef _SC_AIO_MAX |
| 6110 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6111 | #endif |
| 6112 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6113 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6114 | #endif |
| 6115 | #ifdef _SC_ARG_MAX |
| 6116 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6117 | #endif |
| 6118 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6119 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6120 | #endif |
| 6121 | #ifdef _SC_ATEXIT_MAX |
| 6122 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6123 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6124 | #ifdef _SC_AUDIT |
| 6125 | {"SC_AUDIT", _SC_AUDIT}, |
| 6126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6127 | #ifdef _SC_AVPHYS_PAGES |
| 6128 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6129 | #endif |
| 6130 | #ifdef _SC_BC_BASE_MAX |
| 6131 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6132 | #endif |
| 6133 | #ifdef _SC_BC_DIM_MAX |
| 6134 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6135 | #endif |
| 6136 | #ifdef _SC_BC_SCALE_MAX |
| 6137 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6138 | #endif |
| 6139 | #ifdef _SC_BC_STRING_MAX |
| 6140 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6141 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6142 | #ifdef _SC_CAP |
| 6143 | {"SC_CAP", _SC_CAP}, |
| 6144 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6145 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6146 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6147 | #endif |
| 6148 | #ifdef _SC_CHAR_BIT |
| 6149 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6150 | #endif |
| 6151 | #ifdef _SC_CHAR_MAX |
| 6152 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6153 | #endif |
| 6154 | #ifdef _SC_CHAR_MIN |
| 6155 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6156 | #endif |
| 6157 | #ifdef _SC_CHILD_MAX |
| 6158 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6159 | #endif |
| 6160 | #ifdef _SC_CLK_TCK |
| 6161 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6162 | #endif |
| 6163 | #ifdef _SC_COHER_BLKSZ |
| 6164 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6165 | #endif |
| 6166 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6167 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6168 | #endif |
| 6169 | #ifdef _SC_DCACHE_ASSOC |
| 6170 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6171 | #endif |
| 6172 | #ifdef _SC_DCACHE_BLKSZ |
| 6173 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6174 | #endif |
| 6175 | #ifdef _SC_DCACHE_LINESZ |
| 6176 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6177 | #endif |
| 6178 | #ifdef _SC_DCACHE_SZ |
| 6179 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6180 | #endif |
| 6181 | #ifdef _SC_DCACHE_TBLKSZ |
| 6182 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6183 | #endif |
| 6184 | #ifdef _SC_DELAYTIMER_MAX |
| 6185 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6186 | #endif |
| 6187 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6188 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6189 | #endif |
| 6190 | #ifdef _SC_EXPR_NEST_MAX |
| 6191 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6192 | #endif |
| 6193 | #ifdef _SC_FSYNC |
| 6194 | {"SC_FSYNC", _SC_FSYNC}, |
| 6195 | #endif |
| 6196 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6197 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6198 | #endif |
| 6199 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6200 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6201 | #endif |
| 6202 | #ifdef _SC_ICACHE_ASSOC |
| 6203 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6204 | #endif |
| 6205 | #ifdef _SC_ICACHE_BLKSZ |
| 6206 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6207 | #endif |
| 6208 | #ifdef _SC_ICACHE_LINESZ |
| 6209 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6210 | #endif |
| 6211 | #ifdef _SC_ICACHE_SZ |
| 6212 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6213 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6214 | #ifdef _SC_INF |
| 6215 | {"SC_INF", _SC_INF}, |
| 6216 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6217 | #ifdef _SC_INT_MAX |
| 6218 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6219 | #endif |
| 6220 | #ifdef _SC_INT_MIN |
| 6221 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6222 | #endif |
| 6223 | #ifdef _SC_IOV_MAX |
| 6224 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6225 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6226 | #ifdef _SC_IP_SECOPTS |
| 6227 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6228 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6229 | #ifdef _SC_JOB_CONTROL |
| 6230 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6231 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6232 | #ifdef _SC_KERN_POINTERS |
| 6233 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6234 | #endif |
| 6235 | #ifdef _SC_KERN_SIM |
| 6236 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6237 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | #ifdef _SC_LINE_MAX |
| 6239 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6240 | #endif |
| 6241 | #ifdef _SC_LOGIN_NAME_MAX |
| 6242 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6243 | #endif |
| 6244 | #ifdef _SC_LOGNAME_MAX |
| 6245 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6246 | #endif |
| 6247 | #ifdef _SC_LONG_BIT |
| 6248 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6249 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6250 | #ifdef _SC_MAC |
| 6251 | {"SC_MAC", _SC_MAC}, |
| 6252 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6253 | #ifdef _SC_MAPPED_FILES |
| 6254 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6255 | #endif |
| 6256 | #ifdef _SC_MAXPID |
| 6257 | {"SC_MAXPID", _SC_MAXPID}, |
| 6258 | #endif |
| 6259 | #ifdef _SC_MB_LEN_MAX |
| 6260 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6261 | #endif |
| 6262 | #ifdef _SC_MEMLOCK |
| 6263 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_MEMLOCK_RANGE |
| 6266 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_MEMORY_PROTECTION |
| 6269 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6270 | #endif |
| 6271 | #ifdef _SC_MESSAGE_PASSING |
| 6272 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6273 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6274 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6275 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6276 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6277 | #ifdef _SC_MQ_OPEN_MAX |
| 6278 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6279 | #endif |
| 6280 | #ifdef _SC_MQ_PRIO_MAX |
| 6281 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6282 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6283 | #ifdef _SC_NACLS_MAX |
| 6284 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6285 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6286 | #ifdef _SC_NGROUPS_MAX |
| 6287 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6288 | #endif |
| 6289 | #ifdef _SC_NL_ARGMAX |
| 6290 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6291 | #endif |
| 6292 | #ifdef _SC_NL_LANGMAX |
| 6293 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6294 | #endif |
| 6295 | #ifdef _SC_NL_MSGMAX |
| 6296 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6297 | #endif |
| 6298 | #ifdef _SC_NL_NMAX |
| 6299 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6300 | #endif |
| 6301 | #ifdef _SC_NL_SETMAX |
| 6302 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6303 | #endif |
| 6304 | #ifdef _SC_NL_TEXTMAX |
| 6305 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6306 | #endif |
| 6307 | #ifdef _SC_NPROCESSORS_CONF |
| 6308 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6309 | #endif |
| 6310 | #ifdef _SC_NPROCESSORS_ONLN |
| 6311 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6312 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6313 | #ifdef _SC_NPROC_CONF |
| 6314 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6315 | #endif |
| 6316 | #ifdef _SC_NPROC_ONLN |
| 6317 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6318 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6319 | #ifdef _SC_NZERO |
| 6320 | {"SC_NZERO", _SC_NZERO}, |
| 6321 | #endif |
| 6322 | #ifdef _SC_OPEN_MAX |
| 6323 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6324 | #endif |
| 6325 | #ifdef _SC_PAGESIZE |
| 6326 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6327 | #endif |
| 6328 | #ifdef _SC_PAGE_SIZE |
| 6329 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6330 | #endif |
| 6331 | #ifdef _SC_PASS_MAX |
| 6332 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6333 | #endif |
| 6334 | #ifdef _SC_PHYS_PAGES |
| 6335 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6336 | #endif |
| 6337 | #ifdef _SC_PII |
| 6338 | {"SC_PII", _SC_PII}, |
| 6339 | #endif |
| 6340 | #ifdef _SC_PII_INTERNET |
| 6341 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6342 | #endif |
| 6343 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6344 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6345 | #endif |
| 6346 | #ifdef _SC_PII_INTERNET_STREAM |
| 6347 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6348 | #endif |
| 6349 | #ifdef _SC_PII_OSI |
| 6350 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6351 | #endif |
| 6352 | #ifdef _SC_PII_OSI_CLTS |
| 6353 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6354 | #endif |
| 6355 | #ifdef _SC_PII_OSI_COTS |
| 6356 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6357 | #endif |
| 6358 | #ifdef _SC_PII_OSI_M |
| 6359 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6360 | #endif |
| 6361 | #ifdef _SC_PII_SOCKET |
| 6362 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6363 | #endif |
| 6364 | #ifdef _SC_PII_XTI |
| 6365 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6366 | #endif |
| 6367 | #ifdef _SC_POLL |
| 6368 | {"SC_POLL", _SC_POLL}, |
| 6369 | #endif |
| 6370 | #ifdef _SC_PRIORITIZED_IO |
| 6371 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6372 | #endif |
| 6373 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6374 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6375 | #endif |
| 6376 | #ifdef _SC_REALTIME_SIGNALS |
| 6377 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6378 | #endif |
| 6379 | #ifdef _SC_RE_DUP_MAX |
| 6380 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6381 | #endif |
| 6382 | #ifdef _SC_RTSIG_MAX |
| 6383 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6384 | #endif |
| 6385 | #ifdef _SC_SAVED_IDS |
| 6386 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6387 | #endif |
| 6388 | #ifdef _SC_SCHAR_MAX |
| 6389 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6390 | #endif |
| 6391 | #ifdef _SC_SCHAR_MIN |
| 6392 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6393 | #endif |
| 6394 | #ifdef _SC_SELECT |
| 6395 | {"SC_SELECT", _SC_SELECT}, |
| 6396 | #endif |
| 6397 | #ifdef _SC_SEMAPHORES |
| 6398 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6399 | #endif |
| 6400 | #ifdef _SC_SEM_NSEMS_MAX |
| 6401 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6402 | #endif |
| 6403 | #ifdef _SC_SEM_VALUE_MAX |
| 6404 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6405 | #endif |
| 6406 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6407 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6408 | #endif |
| 6409 | #ifdef _SC_SHRT_MAX |
| 6410 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6411 | #endif |
| 6412 | #ifdef _SC_SHRT_MIN |
| 6413 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6414 | #endif |
| 6415 | #ifdef _SC_SIGQUEUE_MAX |
| 6416 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6417 | #endif |
| 6418 | #ifdef _SC_SIGRT_MAX |
| 6419 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6420 | #endif |
| 6421 | #ifdef _SC_SIGRT_MIN |
| 6422 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6423 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6424 | #ifdef _SC_SOFTPOWER |
| 6425 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6426 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6427 | #ifdef _SC_SPLIT_CACHE |
| 6428 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6429 | #endif |
| 6430 | #ifdef _SC_SSIZE_MAX |
| 6431 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6432 | #endif |
| 6433 | #ifdef _SC_STACK_PROT |
| 6434 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6435 | #endif |
| 6436 | #ifdef _SC_STREAM_MAX |
| 6437 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6438 | #endif |
| 6439 | #ifdef _SC_SYNCHRONIZED_IO |
| 6440 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6441 | #endif |
| 6442 | #ifdef _SC_THREADS |
| 6443 | {"SC_THREADS", _SC_THREADS}, |
| 6444 | #endif |
| 6445 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6446 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6447 | #endif |
| 6448 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6449 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6450 | #endif |
| 6451 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6452 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6453 | #endif |
| 6454 | #ifdef _SC_THREAD_KEYS_MAX |
| 6455 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6456 | #endif |
| 6457 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6458 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6459 | #endif |
| 6460 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6461 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6462 | #endif |
| 6463 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6464 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6465 | #endif |
| 6466 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6467 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6468 | #endif |
| 6469 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6470 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6471 | #endif |
| 6472 | #ifdef _SC_THREAD_STACK_MIN |
| 6473 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6474 | #endif |
| 6475 | #ifdef _SC_THREAD_THREADS_MAX |
| 6476 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6477 | #endif |
| 6478 | #ifdef _SC_TIMERS |
| 6479 | {"SC_TIMERS", _SC_TIMERS}, |
| 6480 | #endif |
| 6481 | #ifdef _SC_TIMER_MAX |
| 6482 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6483 | #endif |
| 6484 | #ifdef _SC_TTY_NAME_MAX |
| 6485 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6486 | #endif |
| 6487 | #ifdef _SC_TZNAME_MAX |
| 6488 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6489 | #endif |
| 6490 | #ifdef _SC_T_IOV_MAX |
| 6491 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6492 | #endif |
| 6493 | #ifdef _SC_UCHAR_MAX |
| 6494 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6495 | #endif |
| 6496 | #ifdef _SC_UINT_MAX |
| 6497 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6498 | #endif |
| 6499 | #ifdef _SC_UIO_MAXIOV |
| 6500 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6501 | #endif |
| 6502 | #ifdef _SC_ULONG_MAX |
| 6503 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6504 | #endif |
| 6505 | #ifdef _SC_USHRT_MAX |
| 6506 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6507 | #endif |
| 6508 | #ifdef _SC_VERSION |
| 6509 | {"SC_VERSION", _SC_VERSION}, |
| 6510 | #endif |
| 6511 | #ifdef _SC_WORD_BIT |
| 6512 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6513 | #endif |
| 6514 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6515 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6516 | #endif |
| 6517 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6518 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6519 | #endif |
| 6520 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6521 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6522 | #endif |
| 6523 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6524 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6525 | #endif |
| 6526 | #ifdef _SC_XOPEN_CRYPT |
| 6527 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6528 | #endif |
| 6529 | #ifdef _SC_XOPEN_ENH_I18N |
| 6530 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6531 | #endif |
| 6532 | #ifdef _SC_XOPEN_LEGACY |
| 6533 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6534 | #endif |
| 6535 | #ifdef _SC_XOPEN_REALTIME |
| 6536 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6537 | #endif |
| 6538 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6539 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6540 | #endif |
| 6541 | #ifdef _SC_XOPEN_SHM |
| 6542 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6543 | #endif |
| 6544 | #ifdef _SC_XOPEN_UNIX |
| 6545 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6546 | #endif |
| 6547 | #ifdef _SC_XOPEN_VERSION |
| 6548 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6549 | #endif |
| 6550 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6551 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6552 | #endif |
| 6553 | #ifdef _SC_XOPEN_XPG2 |
| 6554 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_XOPEN_XPG3 |
| 6557 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6558 | #endif |
| 6559 | #ifdef _SC_XOPEN_XPG4 |
| 6560 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6561 | #endif |
| 6562 | }; |
| 6563 | |
| 6564 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6565 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6566 | { |
| 6567 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6568 | sizeof(posix_constants_sysconf) |
| 6569 | / sizeof(struct constdef)); |
| 6570 | } |
| 6571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6572 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6573 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6574 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6575 | |
| 6576 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6577 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6578 | { |
| 6579 | PyObject *result = NULL; |
| 6580 | int name; |
| 6581 | |
| 6582 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6583 | int value; |
| 6584 | |
| 6585 | errno = 0; |
| 6586 | value = sysconf(name); |
| 6587 | if (value == -1 && errno != 0) |
| 6588 | posix_error(); |
| 6589 | else |
| 6590 | result = PyInt_FromLong(value); |
| 6591 | } |
| 6592 | return result; |
| 6593 | } |
| 6594 | #endif |
| 6595 | |
| 6596 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6597 | /* This code is used to ensure that the tables of configuration value names |
| 6598 | * are in sorted order as required by conv_confname(), and also to build the |
| 6599 | * the exported dictionaries that are used to publish information about the |
| 6600 | * names available on the host platform. |
| 6601 | * |
| 6602 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6603 | * when used, even for platforms we're not able to test on. It also makes |
| 6604 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6605 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6606 | |
| 6607 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6608 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6609 | { |
| 6610 | const struct constdef *c1 = |
| 6611 | (const struct constdef *) v1; |
| 6612 | const struct constdef *c2 = |
| 6613 | (const struct constdef *) v2; |
| 6614 | |
| 6615 | return strcmp(c1->name, c2->name); |
| 6616 | } |
| 6617 | |
| 6618 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6619 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6620 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6621 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6622 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6623 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6624 | |
| 6625 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6626 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6627 | if (d == NULL) |
| 6628 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6629 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6630 | for (i=0; i < tablesize; ++i) { |
| 6631 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6632 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6633 | Py_XDECREF(o); |
| 6634 | Py_DECREF(d); |
| 6635 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6636 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6637 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6638 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6639 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6642 | /* Return -1 on failure, 0 on success. */ |
| 6643 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6644 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6645 | { |
| 6646 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6647 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6648 | sizeof(posix_constants_pathconf) |
| 6649 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6650 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6651 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6652 | #endif |
| 6653 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6654 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6655 | sizeof(posix_constants_confstr) |
| 6656 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6657 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6658 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6659 | #endif |
| 6660 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6661 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6662 | sizeof(posix_constants_sysconf) |
| 6663 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6664 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6665 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6666 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6667 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6668 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6669 | |
| 6670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6671 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6672 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6673 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6674 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6675 | |
| 6676 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6677 | posix_abort(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6678 | { |
| 6679 | if (!PyArg_ParseTuple(args, ":abort")) |
| 6680 | return NULL; |
| 6681 | abort(); |
| 6682 | /*NOTREACHED*/ |
| 6683 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6684 | return NULL; |
| 6685 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6686 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6687 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6688 | PyDoc_STRVAR(win32_startfile__doc__, |
| 6689 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6690 | \n\ |
| 6691 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6692 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6693 | with whatever application (if any) its extension is associated.\n\ |
| 6694 | \n\ |
| 6695 | startfile returns as soon as the associated application is launched.\n\ |
| 6696 | There is no option to wait for the application to close, and no way\n\ |
| 6697 | to retrieve the application's exit status.\n\ |
| 6698 | \n\ |
| 6699 | The filepath is relative to the current directory. If you want to use\n\ |
| 6700 | an absolute path, make sure the first character is not a slash (\"/\");\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6701 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6702 | |
| 6703 | static PyObject * |
| 6704 | win32_startfile(PyObject *self, PyObject *args) |
| 6705 | { |
| 6706 | char *filepath; |
| 6707 | HINSTANCE rc; |
| 6708 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6709 | return NULL; |
| 6710 | Py_BEGIN_ALLOW_THREADS |
| 6711 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6712 | Py_END_ALLOW_THREADS |
| 6713 | if (rc <= (HINSTANCE)32) |
| 6714 | return win32_error("startfile", filepath); |
| 6715 | Py_INCREF(Py_None); |
| 6716 | return Py_None; |
| 6717 | } |
| 6718 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6719 | |
| 6720 | static PyMethodDef posix_methods[] = { |
| 6721 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6722 | #ifdef HAVE_TTYNAME |
| 6723 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6724 | #endif |
| 6725 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6726 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6727 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6728 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6729 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6730 | #ifdef HAVE_LCHOWN |
| 6731 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6732 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6733 | #ifdef HAVE_CHROOT |
| 6734 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6735 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6736 | #ifdef HAVE_CTERMID |
| 6737 | {"ctermid", posix_ctermid, METH_VARARGS, posix_ctermid__doc__}, |
| 6738 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6739 | #ifdef HAVE_GETCWD |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6740 | {"getcwd", posix_getcwd, METH_VARARGS, posix_getcwd__doc__}, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6741 | {"getcwdu", posix_getcwdu, METH_VARARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6742 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6743 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6744 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6745 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6746 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6747 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6748 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6749 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6750 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6751 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6752 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6753 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6754 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6755 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6756 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6757 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6758 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6759 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6760 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6761 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6762 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6763 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6764 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6765 | #ifdef HAVE_UNAME |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6766 | {"uname", posix_uname, METH_VARARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6767 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6768 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6769 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6770 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6771 | #ifdef HAVE_TIMES |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6772 | {"times", posix_times, METH_VARARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6773 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6774 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6775 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6776 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6777 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6778 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6779 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6780 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6781 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6782 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6783 | #ifdef HAVE_FORK1 |
| 6784 | {"fork1", posix_fork1, METH_VARARGS, posix_fork1__doc__}, |
| 6785 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6786 | #ifdef HAVE_FORK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6787 | {"fork", posix_fork, METH_VARARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6788 | #endif /* HAVE_FORK */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6789 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6790 | {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__}, |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6791 | #endif /* HAVE_OPENPTY || HAVE__GETPTY */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6792 | #ifdef HAVE_FORKPTY |
| 6793 | {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__}, |
| 6794 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6795 | #ifdef HAVE_GETEGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6796 | {"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6797 | #endif /* HAVE_GETEGID */ |
| 6798 | #ifdef HAVE_GETEUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6799 | {"geteuid", posix_geteuid, METH_VARARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6800 | #endif /* HAVE_GETEUID */ |
| 6801 | #ifdef HAVE_GETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6802 | {"getgid", posix_getgid, METH_VARARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6803 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6804 | #ifdef HAVE_GETGROUPS |
| 6805 | {"getgroups", posix_getgroups, METH_VARARGS, posix_getgroups__doc__}, |
| 6806 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6807 | {"getpid", posix_getpid, METH_VARARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6808 | #ifdef HAVE_GETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6809 | {"getpgrp", posix_getpgrp, METH_VARARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6810 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6811 | #ifdef HAVE_GETPPID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6812 | {"getppid", posix_getppid, METH_VARARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6813 | #endif /* HAVE_GETPPID */ |
| 6814 | #ifdef HAVE_GETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6815 | {"getuid", posix_getuid, METH_VARARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6816 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6817 | #ifdef HAVE_GETLOGIN |
| 6818 | {"getlogin", posix_getlogin, METH_VARARGS, posix_getlogin__doc__}, |
| 6819 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6820 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6821 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6822 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 6823 | #ifdef HAVE_KILLPG |
| 6824 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 6825 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6826 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6827 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 6828 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6829 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6830 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6831 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6832 | {"popen2", win32_popen2, METH_VARARGS}, |
| 6833 | {"popen3", win32_popen3, METH_VARARGS}, |
| 6834 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6835 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6836 | #else |
| 6837 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 6838 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 6839 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 6840 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 6841 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6842 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6843 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6844 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6845 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6846 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6847 | #ifdef HAVE_SETEUID |
| 6848 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 6849 | #endif /* HAVE_SETEUID */ |
| 6850 | #ifdef HAVE_SETEGID |
| 6851 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 6852 | #endif /* HAVE_SETEGID */ |
| 6853 | #ifdef HAVE_SETREUID |
| 6854 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 6855 | #endif /* HAVE_SETREUID */ |
| 6856 | #ifdef HAVE_SETREGID |
| 6857 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 6858 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6859 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6860 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6861 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6862 | #ifdef HAVE_SETGROUPS |
| 6863 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 6864 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 6865 | #ifdef HAVE_GETPGID |
| 6866 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 6867 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6868 | #ifdef HAVE_SETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6869 | {"setpgrp", posix_setpgrp, METH_VARARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6870 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6871 | #ifdef HAVE_WAIT |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6872 | {"wait", posix_wait, METH_VARARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6873 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6874 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6875 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6876 | #endif /* HAVE_WAITPID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6877 | #ifdef HAVE_SETSID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6878 | {"setsid", posix_setsid, METH_VARARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6879 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6880 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6881 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6882 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6883 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6884 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6885 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6886 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6887 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6888 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6889 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 6890 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 6891 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 6892 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 6893 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 6894 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 6895 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 6896 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 6897 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6898 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6899 | #ifdef HAVE_PIPE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6900 | {"pipe", posix_pipe, METH_VARARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6901 | #endif |
| 6902 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6903 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6904 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6905 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6906 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 6907 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6908 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6910 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6911 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6912 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6913 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6914 | #ifdef HAVE_UNSETENV |
| 6915 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 6916 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6917 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6918 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6919 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6920 | #ifdef HAVE_FCHDIR |
| 6921 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 6922 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6923 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6924 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6925 | #endif |
| 6926 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6927 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 6928 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6929 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6930 | #ifdef WCOREDUMP |
| 6931 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 6932 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6933 | #ifdef WIFCONTINUED |
| 6934 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 6935 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6936 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6937 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6938 | #endif /* WIFSTOPPED */ |
| 6939 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6940 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6941 | #endif /* WIFSIGNALED */ |
| 6942 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6943 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6944 | #endif /* WIFEXITED */ |
| 6945 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6946 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6947 | #endif /* WEXITSTATUS */ |
| 6948 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6949 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6950 | #endif /* WTERMSIG */ |
| 6951 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6952 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6953 | #endif /* WSTOPSIG */ |
| 6954 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6955 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6956 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6957 | #endif |
| 6958 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6959 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6960 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 6961 | #ifdef HAVE_TMPFILE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6962 | {"tmpfile", posix_tmpfile, METH_VARARGS, posix_tmpfile__doc__}, |
| 6963 | #endif |
| 6964 | #ifdef HAVE_TEMPNAM |
| 6965 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 6966 | #endif |
| 6967 | #ifdef HAVE_TMPNAM |
| 6968 | {"tmpnam", posix_tmpnam, METH_VARARGS, posix_tmpnam__doc__}, |
| 6969 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6970 | #ifdef HAVE_CONFSTR |
| 6971 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 6972 | #endif |
| 6973 | #ifdef HAVE_SYSCONF |
| 6974 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 6975 | #endif |
| 6976 | #ifdef HAVE_FPATHCONF |
| 6977 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 6978 | #endif |
| 6979 | #ifdef HAVE_PATHCONF |
| 6980 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 6981 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6982 | {"abort", posix_abort, METH_VARARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6983 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 6984 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 6985 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6986 | {NULL, NULL} /* Sentinel */ |
| 6987 | }; |
| 6988 | |
| 6989 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6990 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6991 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6992 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6993 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6996 | #if defined(PYOS_OS2) |
| 6997 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6998 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6999 | { |
| 7000 | APIRET rc; |
| 7001 | ULONG values[QSV_MAX+1]; |
| 7002 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7003 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7004 | |
| 7005 | Py_BEGIN_ALLOW_THREADS |
| 7006 | rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values)); |
| 7007 | Py_END_ALLOW_THREADS |
| 7008 | |
| 7009 | if (rc != NO_ERROR) { |
| 7010 | os2_error(rc); |
| 7011 | return -1; |
| 7012 | } |
| 7013 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7014 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7015 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7016 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7017 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7018 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7019 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7020 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7021 | |
| 7022 | switch (values[QSV_VERSION_MINOR]) { |
| 7023 | case 0: ver = "2.00"; break; |
| 7024 | case 10: ver = "2.10"; break; |
| 7025 | case 11: ver = "2.11"; break; |
| 7026 | case 30: ver = "3.00"; break; |
| 7027 | case 40: ver = "4.00"; break; |
| 7028 | case 50: ver = "5.00"; break; |
| 7029 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7030 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7031 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7032 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7033 | ver = &tmp[0]; |
| 7034 | } |
| 7035 | |
| 7036 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7037 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7038 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7039 | |
| 7040 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7041 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7042 | tmp[1] = ':'; |
| 7043 | tmp[2] = '\0'; |
| 7044 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7045 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7046 | } |
| 7047 | #endif |
| 7048 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7049 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7050 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7051 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7052 | #ifdef F_OK |
| 7053 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7054 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7055 | #ifdef R_OK |
| 7056 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7057 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7058 | #ifdef W_OK |
| 7059 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7060 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7061 | #ifdef X_OK |
| 7062 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7063 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7064 | #ifdef NGROUPS_MAX |
| 7065 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7066 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7067 | #ifdef TMP_MAX |
| 7068 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7069 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7070 | #ifdef WCONTINUED |
| 7071 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7072 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7073 | #ifdef WNOHANG |
| 7074 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7075 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7076 | #ifdef WUNTRACED |
| 7077 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7078 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7079 | #ifdef O_RDONLY |
| 7080 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7081 | #endif |
| 7082 | #ifdef O_WRONLY |
| 7083 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7084 | #endif |
| 7085 | #ifdef O_RDWR |
| 7086 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7087 | #endif |
| 7088 | #ifdef O_NDELAY |
| 7089 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7090 | #endif |
| 7091 | #ifdef O_NONBLOCK |
| 7092 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7093 | #endif |
| 7094 | #ifdef O_APPEND |
| 7095 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7096 | #endif |
| 7097 | #ifdef O_DSYNC |
| 7098 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7099 | #endif |
| 7100 | #ifdef O_RSYNC |
| 7101 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7102 | #endif |
| 7103 | #ifdef O_SYNC |
| 7104 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7105 | #endif |
| 7106 | #ifdef O_NOCTTY |
| 7107 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7108 | #endif |
| 7109 | #ifdef O_CREAT |
| 7110 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7111 | #endif |
| 7112 | #ifdef O_EXCL |
| 7113 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7114 | #endif |
| 7115 | #ifdef O_TRUNC |
| 7116 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7117 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7118 | #ifdef O_BINARY |
| 7119 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7120 | #endif |
| 7121 | #ifdef O_TEXT |
| 7122 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7123 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7124 | #ifdef O_LARGEFILE |
| 7125 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7126 | #endif |
| 7127 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7128 | /* MS Windows */ |
| 7129 | #ifdef O_NOINHERIT |
| 7130 | /* Don't inherit in child processes. */ |
| 7131 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7132 | #endif |
| 7133 | #ifdef _O_SHORT_LIVED |
| 7134 | /* Optimize for short life (keep in memory). */ |
| 7135 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7136 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7137 | #endif |
| 7138 | #ifdef O_TEMPORARY |
| 7139 | /* Automatically delete when last handle is closed. */ |
| 7140 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7141 | #endif |
| 7142 | #ifdef O_RANDOM |
| 7143 | /* Optimize for random access. */ |
| 7144 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7145 | #endif |
| 7146 | #ifdef O_SEQUENTIAL |
| 7147 | /* Optimize for sequential access. */ |
| 7148 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7149 | #endif |
| 7150 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7151 | /* GNU extensions. */ |
| 7152 | #ifdef O_DIRECT |
| 7153 | /* Direct disk access. */ |
| 7154 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7155 | #endif |
| 7156 | #ifdef O_DIRECTORY |
| 7157 | /* Must be a directory. */ |
| 7158 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7159 | #endif |
| 7160 | #ifdef O_NOFOLLOW |
| 7161 | /* Do not follow links. */ |
| 7162 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7163 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7164 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7165 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7166 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7167 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7168 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7169 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7170 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7171 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7172 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7173 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7174 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7175 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7176 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7177 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7178 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7179 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7180 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7181 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7182 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7183 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7184 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7185 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7186 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7187 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7188 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7189 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7190 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7191 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7192 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7193 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7194 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7195 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7196 | #if defined(PYOS_OS2) |
| 7197 | if (insertvalues(d)) return -1; |
| 7198 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7199 | return 0; |
| 7200 | } |
| 7201 | |
| 7202 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7203 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7204 | #define INITFUNC initnt |
| 7205 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7206 | |
| 7207 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7208 | #define INITFUNC initos2 |
| 7209 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7210 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7211 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7212 | #define INITFUNC initposix |
| 7213 | #define MODNAME "posix" |
| 7214 | #endif |
| 7215 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7216 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7217 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7218 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7219 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7220 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7221 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7222 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7223 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7224 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7225 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7226 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7227 | Py_XINCREF(v); |
| 7228 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7229 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7230 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7231 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7232 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7233 | return; |
| 7234 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7235 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7236 | return; |
| 7237 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7238 | Py_INCREF(PyExc_OSError); |
| 7239 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7240 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7241 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7242 | if (posix_putenv_garbage == NULL) |
| 7243 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7244 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7245 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7246 | stat_result_desc.name = MODNAME ".stat_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7247 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7248 | Py_INCREF((PyObject*) &StatResultType); |
| 7249 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7250 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7251 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7252 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7253 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7254 | PyModule_AddObject(m, "statvfs_result", |
| 7255 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7256 | } |