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 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 19 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 20 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 21 | #endif /* defined(__VMS) */ |
| 22 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 23 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 24 | "This module provides access to operating system functionality that is\n\ |
| 25 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 26 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 29 | #ifndef Py_USING_UNICODE |
| 30 | /* This is used in signatures of functions. */ |
| 31 | #define Py_UNICODE void |
| 32 | #endif |
| 33 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) |
| 35 | #define INCL_DOS |
| 36 | #define INCL_DOSERRORS |
| 37 | #define INCL_DOSPROCESS |
| 38 | #define INCL_NOPMAPI |
| 39 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 40 | #if defined(PYCC_GCC) |
| 41 | #include <ctype.h> |
| 42 | #include <io.h> |
| 43 | #include <stdio.h> |
| 44 | #include <process.h> |
| 45 | #include "osdefs.h" |
| 46 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 52 | #ifdef HAVE_SYS_WAIT_H |
| 53 | #include <sys/wait.h> /* For WNOHANG */ |
| 54 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 56 | #ifdef HAVE_SIGNAL_H |
| 57 | #include <signal.h> |
| 58 | #endif |
| 59 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | #ifdef HAVE_FCNTL_H |
| 61 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 62 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 64 | #ifdef HAVE_GRP_H |
| 65 | #include <grp.h> |
| 66 | #endif |
| 67 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYSEXITS_H |
| 69 | #include <sysexits.h> |
| 70 | #endif /* HAVE_SYSEXITS_H */ |
| 71 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 72 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 73 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 74 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 75 | #include <process.h> |
| 76 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 77 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 78 | #define HAVE_GETCWD 1 |
| 79 | #define HAVE_OPENDIR 1 |
| 80 | #define HAVE_SYSTEM 1 |
| 81 | #if defined(__OS2__) |
| 82 | #define HAVE_EXECV 1 |
| 83 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 85 | #include <process.h> |
| 86 | #else |
| 87 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 88 | #define HAVE_EXECV 1 |
| 89 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 90 | #define HAVE_OPENDIR 1 |
| 91 | #define HAVE_PIPE 1 |
| 92 | #define HAVE_POPEN 1 |
| 93 | #define HAVE_SYSTEM 1 |
| 94 | #define HAVE_WAIT 1 |
| 95 | #else |
| 96 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 97 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 98 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | #define HAVE_EXECV 1 |
| 100 | #define HAVE_PIPE 1 |
| 101 | #define HAVE_POPEN 1 |
| 102 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 103 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 104 | #define HAVE_FSYNC 1 |
| 105 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 106 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 107 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 108 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 109 | #else /* all other compilers */ |
| 110 | /* Unix functions that the configure script doesn't check for */ |
| 111 | #define HAVE_EXECV 1 |
| 112 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 113 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 114 | #define HAVE_FORK1 1 |
| 115 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 116 | #define HAVE_GETCWD 1 |
| 117 | #define HAVE_GETEGID 1 |
| 118 | #define HAVE_GETEUID 1 |
| 119 | #define HAVE_GETGID 1 |
| 120 | #define HAVE_GETPPID 1 |
| 121 | #define HAVE_GETUID 1 |
| 122 | #define HAVE_KILL 1 |
| 123 | #define HAVE_OPENDIR 1 |
| 124 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 125 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 127 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | #define HAVE_SYSTEM 1 |
| 129 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 130 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 131 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 132 | #endif /* _MSC_VER */ |
| 133 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 134 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 135 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 139 | #if defined(sun) && !defined(__SVR4) |
| 140 | /* SunOS 4.1.4 doesn't have prototypes for these: */ |
| 141 | extern int rename(const char *, const char *); |
| 142 | extern int pclose(FILE *); |
| 143 | extern int fclose(FILE *); |
Thomas Wouters | 0f954a4 | 2001-02-15 08:46:56 +0000 | [diff] [blame] | 144 | extern int fsync(int); |
| 145 | extern int lstat(const char *, struct stat *); |
| 146 | extern int symlink(const char *, const char *); |
Guido van Rossum | b00adfb | 2000-09-25 13:22:00 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 149 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 150 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 151 | (default) */ |
| 152 | extern char *ctermid_r(char *); |
| 153 | #endif |
| 154 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 155 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 156 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 157 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 158 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 159 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 160 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 161 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 162 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 163 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 164 | #endif |
| 165 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 166 | extern int chdir(char *); |
| 167 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int chdir(const char *); |
| 170 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 171 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 172 | #ifdef __BORLANDC__ |
| 173 | extern int chmod(const char *, int); |
| 174 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 175 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 176 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int chown(const char *, uid_t, gid_t); |
| 178 | extern char *getcwd(char *, int); |
| 179 | extern char *strerror(int); |
| 180 | extern int link(const char *, const char *); |
| 181 | extern int rename(const char *, const char *); |
| 182 | extern int stat(const char *, struct stat *); |
| 183 | extern int unlink(const char *); |
| 184 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 187 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 189 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 190 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | #ifdef HAVE_UTIME_H |
| 196 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 197 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 199 | #ifdef HAVE_SYS_UTIME_H |
| 200 | #include <sys/utime.h> |
| 201 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 202 | #endif /* HAVE_SYS_UTIME_H */ |
| 203 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | #ifdef HAVE_SYS_TIMES_H |
| 205 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 206 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | |
| 208 | #ifdef HAVE_SYS_PARAM_H |
| 209 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 210 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | |
| 212 | #ifdef HAVE_SYS_UTSNAME_H |
| 213 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 216 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 218 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 219 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 220 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 221 | #include <direct.h> |
| 222 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 223 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 225 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 226 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 227 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 229 | #endif |
| 230 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 232 | #endif |
| 233 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #endif |
| 236 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #ifdef _MSC_VER |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #include <direct.h> |
| 240 | #include <io.h> |
| 241 | #include <process.h> |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 242 | #include "osdefs.h" |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 243 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 245 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 246 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 247 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 248 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 250 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 252 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 254 | #ifndef MAXPATHLEN |
| 255 | #define MAXPATHLEN 1024 |
| 256 | #endif /* MAXPATHLEN */ |
| 257 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 258 | #ifdef UNION_WAIT |
| 259 | /* Emulate some macros on systems that have a union instead of macros */ |
| 260 | |
| 261 | #ifndef WIFEXITED |
| 262 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 263 | #endif |
| 264 | |
| 265 | #ifndef WEXITSTATUS |
| 266 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 267 | #endif |
| 268 | |
| 269 | #ifndef WTERMSIG |
| 270 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 271 | #endif |
| 272 | |
| 273 | #endif /* UNION_WAIT */ |
| 274 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 275 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 276 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 277 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 278 | #define USE_CTERMID_R |
| 279 | #endif |
| 280 | |
| 281 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 282 | #define USE_TMPNAM_R |
| 283 | #endif |
| 284 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 285 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 286 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 287 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 288 | # define STAT _stati64 |
| 289 | # define FSTAT _fstati64 |
| 290 | # define STRUCT_STAT struct _stati64 |
| 291 | #else |
| 292 | # define STAT stat |
| 293 | # define FSTAT fstat |
| 294 | # define STRUCT_STAT struct stat |
| 295 | #endif |
| 296 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 297 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 298 | #include <sys/mkdev.h> |
| 299 | #else |
| 300 | #if defined(MAJOR_IN_SYSMACROS) |
| 301 | #include <sys/sysmacros.h> |
| 302 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 303 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 304 | #include <sys/mkdev.h> |
| 305 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 306 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 309 | #ifdef WITH_NEXT_FRAMEWORK |
| 310 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 311 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 312 | */ |
| 313 | #include <crt_externs.h> |
| 314 | static char **environ; |
| 315 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 317 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 319 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 320 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 322 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 324 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | if (d == NULL) |
| 326 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 327 | #ifdef WITH_NEXT_FRAMEWORK |
| 328 | if (environ == NULL) |
| 329 | environ = *_NSGetEnviron(); |
| 330 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | if (environ == NULL) |
| 332 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 333 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 335 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 336 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | char *p = strchr(*e, '='); |
| 338 | if (p == NULL) |
| 339 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 340 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 341 | if (k == NULL) { |
| 342 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 344 | } |
| 345 | v = PyString_FromString(p+1); |
| 346 | if (v == NULL) { |
| 347 | PyErr_Clear(); |
| 348 | Py_DECREF(k); |
| 349 | continue; |
| 350 | } |
| 351 | if (PyDict_GetItem(d, k) == NULL) { |
| 352 | if (PyDict_SetItem(d, k, v) != 0) |
| 353 | PyErr_Clear(); |
| 354 | } |
| 355 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 356 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 358 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 359 | { |
| 360 | APIRET rc; |
| 361 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 362 | |
| 363 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 364 | 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] | 365 | PyObject *v = PyString_FromString(buffer); |
| 366 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 367 | Py_DECREF(v); |
| 368 | } |
| 369 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 370 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 371 | PyObject *v = PyString_FromString(buffer); |
| 372 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 373 | Py_DECREF(v); |
| 374 | } |
| 375 | } |
| 376 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | return d; |
| 378 | } |
| 379 | |
| 380 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 381 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 382 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 383 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 384 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 385 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 386 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 388 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 389 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 390 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 391 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 394 | #ifdef Py_WIN_WIDE_FILENAMES |
| 395 | static PyObject * |
| 396 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 397 | { |
| 398 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 399 | } |
| 400 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 401 | |
| 402 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 403 | static PyObject * |
| 404 | posix_error_with_allocated_filename(char* name) |
| 405 | { |
| 406 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 407 | PyMem_Free(name); |
| 408 | return rc; |
| 409 | } |
| 410 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 411 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 412 | static PyObject * |
| 413 | win32_error(char* function, char* filename) |
| 414 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 415 | /* XXX We should pass the function name along in the future. |
| 416 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 417 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 418 | Windows error object, which is non-trivial. |
| 419 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 420 | errno = GetLastError(); |
| 421 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 422 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 423 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 424 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 425 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 426 | |
| 427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 428 | static PyObject * |
| 429 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 430 | { |
| 431 | /* XXX - see win32_error for comments on 'function' */ |
| 432 | errno = GetLastError(); |
| 433 | if (filename) |
| 434 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 435 | else |
| 436 | return PyErr_SetFromWindowsErr(errno); |
| 437 | } |
| 438 | |
| 439 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 440 | { |
| 441 | /* XXX Perhaps we should make this API an alias of |
| 442 | PyObject_Unicode() instead ?! */ |
| 443 | if (PyUnicode_CheckExact(obj)) { |
| 444 | Py_INCREF(obj); |
| 445 | return obj; |
| 446 | } |
| 447 | if (PyUnicode_Check(obj)) { |
| 448 | /* For a Unicode subtype that's not a Unicode object, |
| 449 | return a true Unicode object with the same data. */ |
| 450 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 451 | PyUnicode_GET_SIZE(obj)); |
| 452 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 453 | return PyUnicode_FromEncodedObject(obj, |
| 454 | Py_FileSystemDefaultEncoding, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 455 | "strict"); |
| 456 | } |
| 457 | |
| 458 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 459 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 460 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 462 | #if defined(PYOS_OS2) |
| 463 | /********************************************************************** |
| 464 | * Helper Function to Trim and Format OS/2 Messages |
| 465 | **********************************************************************/ |
| 466 | static void |
| 467 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 468 | { |
| 469 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 470 | |
| 471 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 472 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 473 | |
| 474 | while (lastc > msgbuf && isspace(*lastc)) |
| 475 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 476 | } |
| 477 | |
| 478 | /* Add Optional Reason Text */ |
| 479 | if (reason) { |
| 480 | strcat(msgbuf, " : "); |
| 481 | strcat(msgbuf, reason); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /********************************************************************** |
| 486 | * Decode an OS/2 Operating System Error Code |
| 487 | * |
| 488 | * A convenience function to lookup an OS/2 error code and return a |
| 489 | * text message we can use to raise a Python exception. |
| 490 | * |
| 491 | * Notes: |
| 492 | * The messages for errors returned from the OS/2 kernel reside in |
| 493 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 494 | * |
| 495 | **********************************************************************/ |
| 496 | static char * |
| 497 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 498 | { |
| 499 | APIRET rc; |
| 500 | ULONG msglen; |
| 501 | |
| 502 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 503 | Py_BEGIN_ALLOW_THREADS |
| 504 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 505 | errorcode, "oso001.msg", &msglen); |
| 506 | Py_END_ALLOW_THREADS |
| 507 | |
| 508 | if (rc == NO_ERROR) |
| 509 | os2_formatmsg(msgbuf, msglen, reason); |
| 510 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 511 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 512 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 513 | |
| 514 | return msgbuf; |
| 515 | } |
| 516 | |
| 517 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 518 | errors are not in a global variable e.g. 'errno' nor are |
| 519 | they congruent with posix error numbers. */ |
| 520 | |
| 521 | static PyObject * os2_error(int code) |
| 522 | { |
| 523 | char text[1024]; |
| 524 | PyObject *v; |
| 525 | |
| 526 | os2_strerror(text, sizeof(text), code, ""); |
| 527 | |
| 528 | v = Py_BuildValue("(is)", code, text); |
| 529 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 530 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 531 | Py_DECREF(v); |
| 532 | } |
| 533 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 534 | } |
| 535 | |
| 536 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | |
| 538 | /* POSIX generic methods */ |
| 539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 540 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 541 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 542 | { |
| 543 | int fd; |
| 544 | int res; |
| 545 | fd = PyObject_AsFileDescriptor(fdobj); |
| 546 | if (fd < 0) |
| 547 | return NULL; |
| 548 | Py_BEGIN_ALLOW_THREADS |
| 549 | res = (*func)(fd); |
| 550 | Py_END_ALLOW_THREADS |
| 551 | if (res < 0) |
| 552 | return posix_error(); |
| 553 | Py_INCREF(Py_None); |
| 554 | return Py_None; |
| 555 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 556 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 557 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 558 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 559 | unicode_file_names(void) |
| 560 | { |
| 561 | static int canusewide = -1; |
| 562 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 563 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 564 | the Windows NT family. */ |
| 565 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 566 | } |
| 567 | return canusewide; |
| 568 | } |
| 569 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 571 | static PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 572 | posix_1str(PyObject *args, char *format, int (*func)(const char*), |
| 573 | char *wformat, int (*wfunc)(const Py_UNICODE*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 574 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 575 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 576 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 577 | #ifdef Py_WIN_WIDE_FILENAMES |
| 578 | if (unicode_file_names()) { |
| 579 | PyUnicodeObject *po; |
| 580 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 581 | Py_BEGIN_ALLOW_THREADS |
| 582 | /* PyUnicode_AS_UNICODE OK without thread |
| 583 | lock as it is a simple dereference. */ |
| 584 | res = (*wfunc)(PyUnicode_AS_UNICODE(po)); |
| 585 | Py_END_ALLOW_THREADS |
| 586 | if (res < 0) |
Mark Hammond | d389036 | 2002-10-03 07:24:48 +0000 | [diff] [blame] | 587 | return posix_error_with_unicode_filename(PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 588 | Py_INCREF(Py_None); |
| 589 | return Py_None; |
| 590 | } |
| 591 | /* Drop the argument parsing error as narrow |
| 592 | strings are also valid. */ |
| 593 | PyErr_Clear(); |
| 594 | } |
| 595 | #else |
| 596 | /* Platforms that don't support Unicode filenames |
| 597 | shouldn't be passing these extra params */ |
| 598 | assert(wformat==NULL && wfunc == NULL); |
| 599 | #endif |
| 600 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 601 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 602 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 604 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 605 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 606 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 607 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 608 | return posix_error_with_allocated_filename(path1); |
| 609 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 614 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 615 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 616 | char *format, |
| 617 | int (*func)(const char *, const char *), |
| 618 | char *wformat, |
| 619 | int (*wfunc)(const Py_UNICODE *, const Py_UNICODE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 621 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 622 | int res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 623 | #ifdef Py_WIN_WIDE_FILENAMES |
| 624 | if (unicode_file_names()) { |
| 625 | PyObject *po1; |
| 626 | PyObject *po2; |
| 627 | if (PyArg_ParseTuple(args, wformat, &po1, &po2)) { |
| 628 | if (PyUnicode_Check(po1) || PyUnicode_Check(po2)) { |
| 629 | PyObject *wpath1; |
| 630 | PyObject *wpath2; |
| 631 | wpath1 = _PyUnicode_FromFileSystemEncodedObject(po1); |
| 632 | wpath2 = _PyUnicode_FromFileSystemEncodedObject(po2); |
| 633 | if (!wpath1 || !wpath2) { |
| 634 | Py_XDECREF(wpath1); |
| 635 | Py_XDECREF(wpath2); |
| 636 | return NULL; |
| 637 | } |
| 638 | Py_BEGIN_ALLOW_THREADS |
| 639 | /* PyUnicode_AS_UNICODE OK without thread |
| 640 | lock as it is a simple dereference. */ |
| 641 | res = (*wfunc)(PyUnicode_AS_UNICODE(wpath1), |
| 642 | PyUnicode_AS_UNICODE(wpath2)); |
| 643 | Py_END_ALLOW_THREADS |
| 644 | Py_XDECREF(wpath1); |
| 645 | Py_XDECREF(wpath2); |
| 646 | if (res != 0) |
| 647 | return posix_error(); |
| 648 | Py_INCREF(Py_None); |
| 649 | return Py_None; |
| 650 | } |
| 651 | /* Else flow through as neither is Unicode. */ |
| 652 | } |
| 653 | /* Drop the argument parsing error as narrow |
| 654 | strings are also valid. */ |
| 655 | PyErr_Clear(); |
| 656 | } |
| 657 | #else |
| 658 | /* Platforms that don't support Unicode filenames |
| 659 | shouldn't be passing these extra params */ |
| 660 | assert(wformat==NULL && wfunc == NULL); |
| 661 | #endif |
| 662 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 663 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 664 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 665 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 667 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 668 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 669 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 670 | PyMem_Free(path1); |
| 671 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 672 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 673 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 675 | Py_INCREF(Py_None); |
| 676 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 679 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 680 | "stat_result: Result from stat or lstat.\n\n\ |
| 681 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 682 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 683 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 684 | \n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 685 | 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] | 686 | they are available as attributes only.\n\ |
| 687 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 688 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 689 | |
| 690 | static PyStructSequence_Field stat_result_fields[] = { |
| 691 | {"st_mode", "protection bits"}, |
| 692 | {"st_ino", "inode"}, |
| 693 | {"st_dev", "device"}, |
| 694 | {"st_nlink", "number of hard links"}, |
| 695 | {"st_uid", "user ID of owner"}, |
| 696 | {"st_gid", "group ID of owner"}, |
| 697 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 698 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 699 | {NULL, "integer time of last access"}, |
| 700 | {NULL, "integer time of last modification"}, |
| 701 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 702 | {"st_atime", "time of last access"}, |
| 703 | {"st_mtime", "time of last modification"}, |
| 704 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 705 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 706 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 707 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 708 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 709 | {"st_blocks", "number of blocks allocated"}, |
| 710 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 711 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 712 | {"st_rdev", "device type (if inode device)"}, |
| 713 | #endif |
| 714 | {0} |
| 715 | }; |
| 716 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 717 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 718 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 719 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 720 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 721 | #endif |
| 722 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 723 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 724 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 725 | #else |
| 726 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 727 | #endif |
| 728 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 729 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 730 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 731 | #else |
| 732 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 733 | #endif |
| 734 | |
| 735 | static PyStructSequence_Desc stat_result_desc = { |
| 736 | "stat_result", /* name */ |
| 737 | stat_result__doc__, /* doc */ |
| 738 | stat_result_fields, |
| 739 | 10 |
| 740 | }; |
| 741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 742 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 743 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 744 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 745 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 746 | 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] | 747 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 748 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 749 | |
| 750 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 751 | {"f_bsize", }, |
| 752 | {"f_frsize", }, |
| 753 | {"f_blocks", }, |
| 754 | {"f_bfree", }, |
| 755 | {"f_bavail", }, |
| 756 | {"f_files", }, |
| 757 | {"f_ffree", }, |
| 758 | {"f_favail", }, |
| 759 | {"f_flag", }, |
| 760 | {"f_namemax",}, |
| 761 | {0} |
| 762 | }; |
| 763 | |
| 764 | static PyStructSequence_Desc statvfs_result_desc = { |
| 765 | "statvfs_result", /* name */ |
| 766 | statvfs_result__doc__, /* doc */ |
| 767 | statvfs_result_fields, |
| 768 | 10 |
| 769 | }; |
| 770 | |
| 771 | static PyTypeObject StatResultType; |
| 772 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 773 | static newfunc structseq_new; |
| 774 | |
| 775 | static PyObject * |
| 776 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 777 | { |
| 778 | PyStructSequence *result; |
| 779 | int i; |
| 780 | |
| 781 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 782 | if (!result) |
| 783 | return NULL; |
| 784 | /* If we have been initialized from a tuple, |
| 785 | st_?time might be set to None. Initialize it |
| 786 | from the int slots. */ |
| 787 | for (i = 7; i <= 9; i++) { |
| 788 | if (result->ob_item[i+3] == Py_None) { |
| 789 | Py_DECREF(Py_None); |
| 790 | Py_INCREF(result->ob_item[i]); |
| 791 | result->ob_item[i+3] = result->ob_item[i]; |
| 792 | } |
| 793 | } |
| 794 | return (PyObject*)result; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | /* If true, st_?time is float. */ |
| 800 | static int _stat_float_times = 0; |
| 801 | |
| 802 | PyDoc_STRVAR(stat_float_times__doc__, |
| 803 | "stat_float_times([newval]) -> oldval\n\n\ |
| 804 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 805 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 806 | future calls return ints. \n\ |
| 807 | If newval is omitted, return the current setting.\n"); |
| 808 | |
| 809 | static PyObject* |
| 810 | stat_float_times(PyObject* self, PyObject *args) |
| 811 | { |
| 812 | int newval = -1; |
| 813 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 814 | return NULL; |
| 815 | if (newval == -1) |
| 816 | /* Return old value */ |
| 817 | return PyBool_FromLong(_stat_float_times); |
| 818 | _stat_float_times = newval; |
| 819 | Py_INCREF(Py_None); |
| 820 | return Py_None; |
| 821 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 822 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 823 | static void |
| 824 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 825 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 826 | PyObject *fval,*ival; |
| 827 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 828 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 829 | #else |
| 830 | ival = PyInt_FromLong((long)sec); |
| 831 | #endif |
| 832 | if (_stat_float_times) { |
| 833 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 834 | } else { |
| 835 | fval = ival; |
| 836 | Py_INCREF(fval); |
| 837 | } |
| 838 | PyStructSequence_SET_ITEM(v, index, ival); |
| 839 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 842 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 843 | (used by posix_stat() and posix_fstat()) */ |
| 844 | static PyObject* |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 845 | _pystat_fromstructstat(STRUCT_STAT st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 846 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 847 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 848 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 849 | if (v == NULL) |
| 850 | return NULL; |
| 851 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 852 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st.st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 853 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 854 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 855 | PyLong_FromLongLong((PY_LONG_LONG)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 856 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 857 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st.st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 858 | #endif |
| 859 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 860 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 861 | PyLong_FromLongLong((PY_LONG_LONG)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 862 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 863 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st.st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 864 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 865 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st.st_nlink)); |
| 866 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st.st_uid)); |
| 867 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st.st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 868 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 869 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 870 | PyLong_FromLongLong((PY_LONG_LONG)st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 871 | #else |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 872 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st.st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 873 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 874 | |
| 875 | #ifdef HAVE_STAT_TV_NSEC |
| 876 | ansec = st.st_atim.tv_nsec; |
| 877 | mnsec = st.st_mtim.tv_nsec; |
| 878 | cnsec = st.st_ctim.tv_nsec; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 879 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 880 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 881 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 882 | fill_time(v, 7, st.st_atime, ansec); |
| 883 | fill_time(v, 8, st.st_mtime, mnsec); |
| 884 | fill_time(v, 9, st.st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 885 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 886 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 887 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 888 | PyInt_FromLong((long)st.st_blksize)); |
| 889 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 890 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 891 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 892 | PyInt_FromLong((long)st.st_blocks)); |
| 893 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 894 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 895 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 896 | PyInt_FromLong((long)st.st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 897 | #endif |
| 898 | |
| 899 | if (PyErr_Occurred()) { |
| 900 | Py_DECREF(v); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | return v; |
| 905 | } |
| 906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 907 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 908 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 909 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 910 | #ifdef __VMS |
| 911 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 912 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 913 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 914 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 915 | char *wformat, |
| 916 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 917 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 918 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 919 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 920 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 921 | int res; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 922 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 923 | #ifdef MS_WINDOWS |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 924 | int pathlen; |
| 925 | char pathcopy[MAX_PATH]; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 926 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 927 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 928 | |
| 929 | #ifdef Py_WIN_WIDE_FILENAMES |
| 930 | /* If on wide-character-capable OS see if argument |
| 931 | is Unicode and if so use wide API. */ |
| 932 | if (unicode_file_names()) { |
| 933 | PyUnicodeObject *po; |
| 934 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 935 | Py_UNICODE wpath[MAX_PATH+1]; |
| 936 | pathlen = wcslen(PyUnicode_AS_UNICODE(po)); |
| 937 | /* the library call can blow up if the file name is too long! */ |
| 938 | if (pathlen > MAX_PATH) { |
| 939 | errno = ENAMETOOLONG; |
| 940 | return posix_error(); |
| 941 | } |
| 942 | wcscpy(wpath, PyUnicode_AS_UNICODE(po)); |
| 943 | /* Remove trailing slash or backslash, unless it's the current |
| 944 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 945 | */ |
| 946 | if (pathlen > 0 && |
| 947 | (wpath[pathlen-1]== L'\\' || wpath[pathlen-1] == L'/')) { |
| 948 | /* It does end with a slash -- exempt the root drive cases. */ |
| 949 | /* XXX UNC root drives should also be exempted? */ |
| 950 | if (pathlen == 1 || (pathlen == 3 && wpath[1] == L':')) |
| 951 | /* leave it alone */; |
| 952 | else { |
| 953 | /* nuke the trailing backslash */ |
| 954 | wpath[pathlen-1] = L'\0'; |
| 955 | } |
| 956 | } |
| 957 | Py_BEGIN_ALLOW_THREADS |
| 958 | /* PyUnicode_AS_UNICODE result OK without |
| 959 | thread lock as it is a simple dereference. */ |
| 960 | res = wstatfunc(wpath, &st); |
| 961 | Py_END_ALLOW_THREADS |
| 962 | if (res != 0) |
| 963 | return posix_error_with_unicode_filename(wpath); |
| 964 | return _pystat_fromstructstat(st); |
| 965 | } |
| 966 | /* Drop the argument parsing error as narrow strings |
| 967 | are also valid. */ |
| 968 | PyErr_Clear(); |
| 969 | } |
| 970 | #endif |
| 971 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 972 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 973 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 974 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 975 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 976 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 977 | #ifdef MS_WINDOWS |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 978 | pathlen = strlen(path); |
| 979 | /* the library call can blow up if the file name is too long! */ |
| 980 | if (pathlen > MAX_PATH) { |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 981 | PyMem_Free(pathfree); |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 982 | errno = ENAMETOOLONG; |
| 983 | return posix_error(); |
| 984 | } |
| 985 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 986 | /* Remove trailing slash or backslash, unless it's the current |
| 987 | drive root (/ or \) or a specific drive's root (like c:\ or c:/). |
| 988 | */ |
| 989 | if (pathlen > 0 && |
| 990 | (path[pathlen-1]== '\\' || path[pathlen-1] == '/')) { |
| 991 | /* It does end with a slash -- exempt the root drive cases. */ |
| 992 | /* XXX UNC root drives should also be exempted? */ |
| 993 | if (pathlen == 1 || (pathlen == 3 && path[1] == ':')) |
| 994 | /* leave it alone */; |
| 995 | else { |
| 996 | /* nuke the trailing backslash */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 997 | strncpy(pathcopy, path, pathlen); |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 998 | pathcopy[pathlen-1] = '\0'; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 999 | path = pathcopy; |
| 1000 | } |
| 1001 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1002 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1003 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1004 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1005 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1006 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1007 | if (res != 0) |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1008 | return posix_error_with_allocated_filename(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1009 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1010 | PyMem_Free(pathfree); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1011 | return _pystat_fromstructstat(st); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | /* POSIX methods */ |
| 1016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1017 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1018 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1019 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1020 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1021 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1022 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1023 | 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] | 1024 | |
| 1025 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1026 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1027 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1028 | char *path; |
| 1029 | int mode; |
| 1030 | int res; |
| 1031 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1032 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1033 | if (unicode_file_names()) { |
| 1034 | PyUnicodeObject *po; |
| 1035 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1036 | Py_BEGIN_ALLOW_THREADS |
| 1037 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1038 | it is a simple dereference. */ |
| 1039 | res = _waccess(PyUnicode_AS_UNICODE(po), mode); |
| 1040 | Py_END_ALLOW_THREADS |
| 1041 | return(PyBool_FromLong(res == 0)); |
| 1042 | } |
| 1043 | /* Drop the argument parsing error as narrow strings |
| 1044 | are also valid. */ |
| 1045 | PyErr_Clear(); |
| 1046 | } |
| 1047 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1048 | if (!PyArg_ParseTuple(args, "si:access", &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1049 | return NULL; |
| 1050 | Py_BEGIN_ALLOW_THREADS |
| 1051 | res = access(path, mode); |
| 1052 | Py_END_ALLOW_THREADS |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 1053 | return(PyBool_FromLong(res == 0)); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1056 | #ifndef F_OK |
| 1057 | #define F_OK 0 |
| 1058 | #endif |
| 1059 | #ifndef R_OK |
| 1060 | #define R_OK 4 |
| 1061 | #endif |
| 1062 | #ifndef W_OK |
| 1063 | #define W_OK 2 |
| 1064 | #endif |
| 1065 | #ifndef X_OK |
| 1066 | #define X_OK 1 |
| 1067 | #endif |
| 1068 | |
| 1069 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1070 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1071 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1072 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1073 | |
| 1074 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1075 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1076 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1077 | int id; |
| 1078 | char *ret; |
| 1079 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1080 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1081 | return NULL; |
| 1082 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1083 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1084 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1085 | if (id == 0) { |
| 1086 | ret = ttyname(); |
| 1087 | } |
| 1088 | else { |
| 1089 | ret = NULL; |
| 1090 | } |
| 1091 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1092 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1093 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1094 | if (ret == NULL) |
| 1095 | return(posix_error()); |
| 1096 | return(PyString_FromString(ret)); |
| 1097 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1098 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1099 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1100 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1101 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1102 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1103 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1104 | |
| 1105 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1106 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1107 | { |
| 1108 | char *ret; |
| 1109 | char buffer[L_ctermid]; |
| 1110 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1111 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1112 | ret = ctermid_r(buffer); |
| 1113 | #else |
| 1114 | ret = ctermid(buffer); |
| 1115 | #endif |
| 1116 | if (ret == NULL) |
| 1117 | return(posix_error()); |
| 1118 | return(PyString_FromString(buffer)); |
| 1119 | } |
| 1120 | #endif |
| 1121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1122 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1123 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1124 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1125 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1126 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1127 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1128 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1129 | #ifdef MS_WINDOWS |
| 1130 | return posix_1str(args, "et:chdir", chdir, "U:chdir", _wchdir); |
| 1131 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1132 | return posix_1str(args, "et:chdir", _chdir2, NULL, NULL); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1133 | #elif defined(__VMS) |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1134 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1135 | NULL, NULL); |
| 1136 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1137 | return posix_1str(args, "et:chdir", chdir, NULL, NULL); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1138 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1141 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1142 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1143 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1144 | 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] | 1145 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1146 | |
| 1147 | static PyObject * |
| 1148 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1149 | { |
| 1150 | return posix_fildes(fdobj, fchdir); |
| 1151 | } |
| 1152 | #endif /* HAVE_FCHDIR */ |
| 1153 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1155 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1156 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1157 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1158 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1159 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1160 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1161 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1162 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1163 | int i; |
| 1164 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1165 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1166 | if (unicode_file_names()) { |
| 1167 | PyUnicodeObject *po; |
| 1168 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1169 | Py_BEGIN_ALLOW_THREADS |
| 1170 | res = _wchmod(PyUnicode_AS_UNICODE(po), i); |
| 1171 | Py_END_ALLOW_THREADS |
| 1172 | if (res < 0) |
| 1173 | return posix_error_with_unicode_filename( |
| 1174 | PyUnicode_AS_UNICODE(po)); |
| 1175 | Py_INCREF(Py_None); |
| 1176 | return Py_None; |
| 1177 | } |
| 1178 | /* Drop the argument parsing error as narrow strings |
| 1179 | are also valid. */ |
| 1180 | PyErr_Clear(); |
| 1181 | } |
| 1182 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1183 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1184 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1185 | return NULL; |
| 1186 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1187 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1188 | Py_END_ALLOW_THREADS |
| 1189 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1190 | return posix_error_with_allocated_filename(path); |
| 1191 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1192 | Py_INCREF(Py_None); |
| 1193 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1196 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1197 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1198 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1199 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1200 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1201 | |
| 1202 | static PyObject * |
| 1203 | posix_chroot(PyObject *self, PyObject *args) |
| 1204 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1205 | return posix_1str(args, "et:chroot", chroot, NULL, NULL); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1206 | } |
| 1207 | #endif |
| 1208 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1209 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1210 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1211 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1212 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1213 | |
| 1214 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1215 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1216 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1217 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1218 | } |
| 1219 | #endif /* HAVE_FSYNC */ |
| 1220 | |
| 1221 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1223 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1224 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1225 | #endif |
| 1226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1227 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1228 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1229 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1230 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1231 | |
| 1232 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1233 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1234 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1235 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1236 | } |
| 1237 | #endif /* HAVE_FDATASYNC */ |
| 1238 | |
| 1239 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1240 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1241 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1242 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1243 | 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] | 1244 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1245 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1246 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1247 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1248 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1249 | int uid, gid; |
| 1250 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1251 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1252 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1253 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1254 | return NULL; |
| 1255 | Py_BEGIN_ALLOW_THREADS |
| 1256 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1257 | Py_END_ALLOW_THREADS |
| 1258 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1259 | return posix_error_with_allocated_filename(path); |
| 1260 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1261 | Py_INCREF(Py_None); |
| 1262 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1263 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1264 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1265 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1266 | #ifdef HAVE_LCHOWN |
| 1267 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1268 | "lchown(path, uid, gid)\n\n\ |
| 1269 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1270 | This function will not follow symbolic links."); |
| 1271 | |
| 1272 | static PyObject * |
| 1273 | posix_lchown(PyObject *self, PyObject *args) |
| 1274 | { |
| 1275 | char *path = NULL; |
| 1276 | int uid, gid; |
| 1277 | int res; |
| 1278 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1279 | Py_FileSystemDefaultEncoding, &path, |
| 1280 | &uid, &gid)) |
| 1281 | return NULL; |
| 1282 | Py_BEGIN_ALLOW_THREADS |
| 1283 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1284 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1285 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1286 | return posix_error_with_allocated_filename(path); |
| 1287 | PyMem_Free(path); |
| 1288 | Py_INCREF(Py_None); |
| 1289 | return Py_None; |
| 1290 | } |
| 1291 | #endif /* HAVE_LCHOWN */ |
| 1292 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1293 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1294 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1295 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1296 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1297 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1298 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1299 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1300 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1301 | { |
| 1302 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1303 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1304 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1305 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1306 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1307 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1308 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1309 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1310 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1311 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1312 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1314 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1315 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1316 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1317 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1318 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1319 | "getcwdu() -> path\n\n\ |
| 1320 | Return a unicode string representing the current working directory."); |
| 1321 | |
| 1322 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1323 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1324 | { |
| 1325 | char buf[1026]; |
| 1326 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1327 | |
| 1328 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1329 | if (unicode_file_names()) { |
| 1330 | wchar_t *wres; |
| 1331 | wchar_t wbuf[1026]; |
| 1332 | Py_BEGIN_ALLOW_THREADS |
| 1333 | wres = _wgetcwd(wbuf, sizeof wbuf/ sizeof wbuf[0]); |
| 1334 | Py_END_ALLOW_THREADS |
| 1335 | if (wres == NULL) |
| 1336 | return posix_error(); |
| 1337 | return PyUnicode_FromWideChar(wbuf, wcslen(wbuf)); |
| 1338 | } |
| 1339 | #endif |
| 1340 | |
| 1341 | Py_BEGIN_ALLOW_THREADS |
| 1342 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1343 | res = _getcwd2(buf, sizeof buf); |
| 1344 | #else |
| 1345 | res = getcwd(buf, sizeof buf); |
| 1346 | #endif |
| 1347 | Py_END_ALLOW_THREADS |
| 1348 | if (res == NULL) |
| 1349 | return posix_error(); |
| 1350 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1351 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1352 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1353 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1354 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1356 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1357 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1358 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1359 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1360 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1361 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1362 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1363 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1364 | return posix_2str(args, "etet:link", link, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1365 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1366 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1367 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1369 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1370 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1371 | Return a list containing the names of the entries in the directory.\n\ |
| 1372 | \n\ |
| 1373 | path: path of directory to list\n\ |
| 1374 | \n\ |
| 1375 | 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] | 1376 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1377 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1378 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1379 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1380 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1381 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1382 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1383 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1384 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1385 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1386 | HANDLE hFindFile; |
| 1387 | WIN32_FIND_DATA FileData; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1388 | /* MAX_PATH characters could mean a bigger encoded string */ |
| 1389 | char namebuf[MAX_PATH*2+5]; |
| 1390 | char *bufptr = namebuf; |
| 1391 | int len = sizeof(namebuf)/sizeof(namebuf[0]); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1392 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1393 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1394 | /* If on wide-character-capable OS see if argument |
| 1395 | is Unicode and if so use wide API. */ |
| 1396 | if (unicode_file_names()) { |
| 1397 | PyUnicodeObject *po; |
| 1398 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1399 | WIN32_FIND_DATAW wFileData; |
| 1400 | Py_UNICODE wnamebuf[MAX_PATH*2+5]; |
| 1401 | Py_UNICODE wch; |
| 1402 | wcsncpy(wnamebuf, PyUnicode_AS_UNICODE(po), MAX_PATH); |
| 1403 | wnamebuf[MAX_PATH] = L'\0'; |
| 1404 | len = wcslen(wnamebuf); |
| 1405 | wch = (len > 0) ? wnamebuf[len-1] : L'\0'; |
| 1406 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1407 | wnamebuf[len++] = L'/'; |
| 1408 | wcscpy(wnamebuf + len, L"*.*"); |
| 1409 | if ((d = PyList_New(0)) == NULL) |
| 1410 | return NULL; |
| 1411 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1412 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1413 | errno = GetLastError(); |
| 1414 | if (errno == ERROR_FILE_NOT_FOUND) { |
| 1415 | return d; |
| 1416 | } |
| 1417 | Py_DECREF(d); |
| 1418 | return win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1419 | } |
| 1420 | do { |
| 1421 | if (wFileData.cFileName[0] == L'.' && |
| 1422 | (wFileData.cFileName[1] == L'\0' || |
| 1423 | wFileData.cFileName[1] == L'.' && |
| 1424 | wFileData.cFileName[2] == L'\0')) |
| 1425 | continue; |
| 1426 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1427 | if (v == NULL) { |
| 1428 | Py_DECREF(d); |
| 1429 | d = NULL; |
| 1430 | break; |
| 1431 | } |
| 1432 | if (PyList_Append(d, v) != 0) { |
| 1433 | Py_DECREF(v); |
| 1434 | Py_DECREF(d); |
| 1435 | d = NULL; |
| 1436 | break; |
| 1437 | } |
| 1438 | Py_DECREF(v); |
| 1439 | } while (FindNextFileW(hFindFile, &wFileData) == TRUE); |
| 1440 | |
| 1441 | if (FindClose(hFindFile) == FALSE) { |
| 1442 | Py_DECREF(d); |
| 1443 | return win32_error_unicode("FindClose", wnamebuf); |
| 1444 | } |
| 1445 | return d; |
| 1446 | } |
| 1447 | /* Drop the argument parsing error as narrow strings |
| 1448 | are also valid. */ |
| 1449 | PyErr_Clear(); |
| 1450 | } |
| 1451 | #endif |
| 1452 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1453 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1454 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1455 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1456 | if (len > 0) { |
| 1457 | char ch = namebuf[len-1]; |
| 1458 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1459 | namebuf[len++] = '/'; |
| 1460 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1461 | strcpy(namebuf + len, "*.*"); |
| 1462 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1463 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1464 | return NULL; |
| 1465 | |
| 1466 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1467 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 1468 | errno = GetLastError(); |
Guido van Rossum | 617bc19 | 1998-08-06 03:23:32 +0000 | [diff] [blame] | 1469 | if (errno == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1470 | return d; |
| 1471 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1472 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1473 | } |
| 1474 | do { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1475 | if (FileData.cFileName[0] == '.' && |
| 1476 | (FileData.cFileName[1] == '\0' || |
| 1477 | FileData.cFileName[1] == '.' && |
| 1478 | FileData.cFileName[2] == '\0')) |
| 1479 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1480 | v = PyString_FromString(FileData.cFileName); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1481 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1482 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1483 | d = NULL; |
| 1484 | break; |
| 1485 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1486 | if (PyList_Append(d, v) != 0) { |
| 1487 | Py_DECREF(v); |
| 1488 | Py_DECREF(d); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1489 | d = NULL; |
| 1490 | break; |
| 1491 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1492 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1493 | } while (FindNextFile(hFindFile, &FileData) == TRUE); |
| 1494 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1495 | if (FindClose(hFindFile) == FALSE) { |
| 1496 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1497 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1498 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1499 | |
| 1500 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1501 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1502 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1503 | |
| 1504 | #ifndef MAX_PATH |
| 1505 | #define MAX_PATH CCHMAXPATH |
| 1506 | #endif |
| 1507 | char *name, *pt; |
| 1508 | int len; |
| 1509 | PyObject *d, *v; |
| 1510 | char namebuf[MAX_PATH+5]; |
| 1511 | HDIR hdir = 1; |
| 1512 | ULONG srchcnt = 1; |
| 1513 | FILEFINDBUF3 ep; |
| 1514 | APIRET rc; |
| 1515 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1516 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1517 | return NULL; |
| 1518 | if (len >= MAX_PATH) { |
| 1519 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | strcpy(namebuf, name); |
| 1523 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1524 | if (*pt == ALTSEP) |
| 1525 | *pt = SEP; |
| 1526 | if (namebuf[len-1] != SEP) |
| 1527 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1528 | strcpy(namebuf + len, "*.*"); |
| 1529 | |
| 1530 | if ((d = PyList_New(0)) == NULL) |
| 1531 | return NULL; |
| 1532 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1533 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1534 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1535 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1536 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1537 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1538 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1539 | |
| 1540 | if (rc != NO_ERROR) { |
| 1541 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1542 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1545 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1546 | do { |
| 1547 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1548 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1549 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1550 | |
| 1551 | strcpy(namebuf, ep.achName); |
| 1552 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1553 | /* Leave Case of Name Alone -- In Native Form */ |
| 1554 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1555 | |
| 1556 | v = PyString_FromString(namebuf); |
| 1557 | if (v == NULL) { |
| 1558 | Py_DECREF(d); |
| 1559 | d = NULL; |
| 1560 | break; |
| 1561 | } |
| 1562 | if (PyList_Append(d, v) != 0) { |
| 1563 | Py_DECREF(v); |
| 1564 | Py_DECREF(d); |
| 1565 | d = NULL; |
| 1566 | break; |
| 1567 | } |
| 1568 | Py_DECREF(v); |
| 1569 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 1570 | } |
| 1571 | |
| 1572 | return d; |
| 1573 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1574 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1575 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1576 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1577 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1578 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1579 | int arg_is_unicode = 1; |
| 1580 | |
| 1581 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 1582 | arg_is_unicode = 0; |
| 1583 | PyErr_Clear(); |
| 1584 | } |
| 1585 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1586 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1587 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1588 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1589 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1590 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1591 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1592 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1593 | return NULL; |
| 1594 | } |
| 1595 | while ((ep = readdir(dirp)) != NULL) { |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1596 | if (ep->d_name[0] == '.' && |
| 1597 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 1598 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 1599 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1600 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1601 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1602 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1603 | d = NULL; |
| 1604 | break; |
| 1605 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1606 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 1607 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1608 | PyObject *w; |
| 1609 | |
| 1610 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1611 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1612 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 1613 | if (w != NULL) { |
| 1614 | Py_DECREF(v); |
| 1615 | v = w; |
| 1616 | } |
| 1617 | else { |
| 1618 | /* fall back to the original byte string, as |
| 1619 | discussed in patch #683592 */ |
| 1620 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1621 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 1622 | } |
| 1623 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1624 | if (PyList_Append(d, v) != 0) { |
| 1625 | Py_DECREF(v); |
| 1626 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1627 | d = NULL; |
| 1628 | break; |
| 1629 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1630 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1631 | } |
| 1632 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 1633 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 1634 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1635 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1636 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1637 | #endif /* which OS */ |
| 1638 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1639 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1640 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1641 | /* A helper function for abspath on win32 */ |
| 1642 | static PyObject * |
| 1643 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 1644 | { |
| 1645 | /* assume encoded strings wont more than double no of chars */ |
| 1646 | char inbuf[MAX_PATH*2]; |
| 1647 | char *inbufp = inbuf; |
| 1648 | int insize = sizeof(inbuf)/sizeof(inbuf[0]); |
| 1649 | char outbuf[MAX_PATH*2]; |
| 1650 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1651 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1652 | if (unicode_file_names()) { |
| 1653 | PyUnicodeObject *po; |
| 1654 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 1655 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 1656 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1657 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1658 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 1659 | woutbuf, &wtemp)) |
| 1660 | return win32_error("GetFullPathName", ""); |
| 1661 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 1662 | } |
| 1663 | /* Drop the argument parsing error as narrow strings |
| 1664 | are also valid. */ |
| 1665 | PyErr_Clear(); |
| 1666 | } |
| 1667 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1668 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 1669 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1670 | &insize)) |
| 1671 | return NULL; |
| 1672 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 1673 | outbuf, &temp)) |
| 1674 | return win32_error("GetFullPathName", inbuf); |
| 1675 | return PyString_FromString(outbuf); |
| 1676 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1677 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1679 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1680 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1681 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1684 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1685 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1686 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1687 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1688 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1689 | |
| 1690 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1691 | if (unicode_file_names()) { |
| 1692 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 1693 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1694 | Py_BEGIN_ALLOW_THREADS |
| 1695 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1696 | it is a simple dereference. */ |
| 1697 | res = _wmkdir(PyUnicode_AS_UNICODE(po)); |
| 1698 | Py_END_ALLOW_THREADS |
| 1699 | if (res < 0) |
| 1700 | return posix_error(); |
| 1701 | Py_INCREF(Py_None); |
| 1702 | return Py_None; |
| 1703 | } |
| 1704 | /* Drop the argument parsing error as narrow strings |
| 1705 | are also valid. */ |
| 1706 | PyErr_Clear(); |
| 1707 | } |
| 1708 | #endif |
| 1709 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1710 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1711 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1712 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1713 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1714 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1715 | res = mkdir(path); |
| 1716 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1717 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1718 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1719 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 1720 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1721 | return posix_error_with_allocated_filename(path); |
| 1722 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1723 | Py_INCREF(Py_None); |
| 1724 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1727 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1728 | #ifdef HAVE_NICE |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1729 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_SYS_RESOURCE_H) |
| 1730 | #if defined(HAVE_GETPRIORITY) && !defined(PRIO_PROCESS) |
| 1731 | #include <sys/resource.h> |
| 1732 | #endif |
| 1733 | #endif |
| 1734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1735 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1736 | "nice(inc) -> new_priority\n\n\ |
| 1737 | 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] | 1738 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1739 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1740 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1741 | { |
| 1742 | int increment, value; |
| 1743 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1744 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1745 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1746 | |
| 1747 | /* There are two flavours of 'nice': one that returns the new |
| 1748 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1749 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 1750 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1751 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1752 | If we are of the nice family that returns the new priority, we |
| 1753 | need to clear errno before the call, and check if errno is filled |
| 1754 | before calling posix_error() on a returnvalue of -1, because the |
| 1755 | -1 may be the actual new priority! */ |
| 1756 | |
| 1757 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1758 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 1759 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 1760 | if (value == 0) |
| 1761 | value = getpriority(PRIO_PROCESS, 0); |
| 1762 | #endif |
| 1763 | if (value == -1 && errno != 0) |
| 1764 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1765 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1766 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 1767 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1768 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1769 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1771 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1772 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1773 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1774 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1775 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1776 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1777 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1778 | #ifdef MS_WINDOWS |
| 1779 | return posix_2str(args, "etet:rename", rename, "OO:rename", _wrename); |
| 1780 | #else |
| 1781 | return posix_2str(args, "etet:rename", rename, NULL, NULL); |
| 1782 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1785 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1786 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1787 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1788 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1789 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1790 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1791 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1792 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1793 | #ifdef MS_WINDOWS |
| 1794 | return posix_1str(args, "et:rmdir", rmdir, "U:rmdir", _wrmdir); |
| 1795 | #else |
| 1796 | return posix_1str(args, "et:rmdir", rmdir, NULL, NULL); |
| 1797 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1801 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1802 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1803 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1804 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1805 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1806 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1807 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1808 | #ifdef MS_WINDOWS |
| 1809 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", _wstati64); |
| 1810 | #else |
| 1811 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 1812 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1815 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1816 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1818 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1819 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1820 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1821 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1822 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1823 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1824 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1825 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1826 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1827 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1828 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1829 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1830 | Py_END_ALLOW_THREADS |
| 1831 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1832 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1833 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1834 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1836 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1837 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1838 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1839 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1840 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1841 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1842 | { |
| 1843 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1844 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1845 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 1846 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1847 | if (i < 0) |
| 1848 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1849 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1853 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1854 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1855 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1857 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1858 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1859 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1860 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1861 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1862 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1863 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1864 | #ifdef MS_WINDOWS |
| 1865 | return posix_1str(args, "et:remove", unlink, "U:remove", _wunlink); |
| 1866 | #else |
| 1867 | return posix_1str(args, "et:remove", unlink, NULL, NULL); |
| 1868 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1872 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1873 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1874 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1875 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1876 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1877 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1878 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1879 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1880 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1881 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1883 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1884 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1885 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1886 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1887 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1888 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 1889 | u.sysname, |
| 1890 | u.nodename, |
| 1891 | u.release, |
| 1892 | u.version, |
| 1893 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 1894 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1895 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1896 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1897 | static int |
| 1898 | extract_time(PyObject *t, long* sec, long* usec) |
| 1899 | { |
| 1900 | long intval; |
| 1901 | if (PyFloat_Check(t)) { |
| 1902 | double tval = PyFloat_AsDouble(t); |
| 1903 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 1904 | if (!intobj) |
| 1905 | return -1; |
| 1906 | intval = PyInt_AsLong(intobj); |
| 1907 | Py_DECREF(intobj); |
| 1908 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 1909 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1910 | if (*usec < 0) |
| 1911 | /* If rounding gave us a negative number, |
| 1912 | truncate. */ |
| 1913 | *usec = 0; |
| 1914 | return 0; |
| 1915 | } |
| 1916 | intval = PyInt_AsLong(t); |
| 1917 | if (intval == -1 && PyErr_Occurred()) |
| 1918 | return -1; |
| 1919 | *sec = intval; |
| 1920 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 1921 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1922 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | PyDoc_STRVAR(posix_utime__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | "utime(path, (atime, utime))\n\ |
| 1926 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1927 | 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] | 1928 | 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] | 1929 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1930 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1931 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1932 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 1933 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1934 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1935 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1936 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1937 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1938 | #if defined(HAVE_UTIMES) |
| 1939 | struct timeval buf[2]; |
| 1940 | #define ATIME buf[0].tv_sec |
| 1941 | #define MTIME buf[1].tv_sec |
| 1942 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1943 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1944 | struct utimbuf buf; |
| 1945 | #define ATIME buf.actime |
| 1946 | #define MTIME buf.modtime |
| 1947 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1948 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1949 | time_t buf[2]; |
| 1950 | #define ATIME buf[0] |
| 1951 | #define MTIME buf[1] |
| 1952 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1953 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 1954 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1955 | int have_unicode_filename = 0; |
| 1956 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1957 | PyUnicodeObject *obwpath; |
| 1958 | wchar_t *wpath; |
| 1959 | if (unicode_file_names()) { |
| 1960 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 1961 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 1962 | have_unicode_filename = 1; |
| 1963 | } else |
| 1964 | /* Drop the argument parsing error as narrow strings |
| 1965 | are also valid. */ |
| 1966 | PyErr_Clear(); |
| 1967 | } |
| 1968 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1969 | |
| 1970 | if (!have_unicode_filename && \ |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 1971 | !PyArg_ParseTuple(args, "etO:utime", |
| 1972 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1973 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1974 | if (arg == Py_None) { |
| 1975 | /* optional time values not given */ |
| 1976 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1977 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1978 | if (have_unicode_filename) |
| 1979 | res = _wutime(wpath, NULL); |
| 1980 | else |
| 1981 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1982 | res = utime(path, NULL); |
| 1983 | Py_END_ALLOW_THREADS |
| 1984 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1985 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1986 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1987 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1988 | return NULL; |
| 1989 | } |
| 1990 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1991 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 1992 | &atime, &ausec) == -1) |
| 1993 | return NULL; |
| 1994 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 1995 | &mtime, &musec) == -1) |
| 1996 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 1997 | ATIME = atime; |
| 1998 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 1999 | #ifdef HAVE_UTIMES |
| 2000 | buf[0].tv_usec = ausec; |
| 2001 | buf[1].tv_usec = musec; |
| 2002 | Py_BEGIN_ALLOW_THREADS |
| 2003 | res = utimes(path, buf); |
| 2004 | Py_END_ALLOW_THREADS |
| 2005 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2006 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2007 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2008 | if (have_unicode_filename) |
| 2009 | /* utime is OK with utimbuf, but _wutime insists |
| 2010 | on _utimbuf (the msvc headers assert the |
| 2011 | underscore version is ansi) */ |
| 2012 | res = _wutime(wpath, (struct _utimbuf *)UTIME_ARG); |
| 2013 | else |
| 2014 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2015 | res = utime(path, UTIME_ARG); |
| 2016 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2017 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2018 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2019 | if (res < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 2020 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2021 | Py_INCREF(Py_None); |
| 2022 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2023 | #undef UTIME_ARG |
| 2024 | #undef ATIME |
| 2025 | #undef MTIME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2029 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2032 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2033 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2034 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2035 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2036 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2037 | { |
| 2038 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2039 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2040 | return NULL; |
| 2041 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2042 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2045 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2046 | static void |
| 2047 | free_string_array(char **array, int count) |
| 2048 | { |
| 2049 | int i; |
| 2050 | for (i = 0; i < count; i++) |
| 2051 | PyMem_Free(array[i]); |
| 2052 | PyMem_DEL(array); |
| 2053 | } |
| 2054 | #endif |
| 2055 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2057 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2058 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2059 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2060 | Execute an executable path with arguments, replacing current process.\n\ |
| 2061 | \n\ |
| 2062 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2063 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2064 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2065 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2066 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2067 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2068 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2069 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2070 | char **argvlist; |
| 2071 | int i, argc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2072 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2073 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2074 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2075 | argv is a list or tuple of strings. */ |
| 2076 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2077 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2078 | Py_FileSystemDefaultEncoding, |
| 2079 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2080 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2081 | if (PyList_Check(argv)) { |
| 2082 | argc = PyList_Size(argv); |
| 2083 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2084 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2085 | else if (PyTuple_Check(argv)) { |
| 2086 | argc = PyTuple_Size(argv); |
| 2087 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2088 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2089 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2090 | 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] | 2091 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2092 | return NULL; |
| 2093 | } |
| 2094 | |
| 2095 | if (argc == 0) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2096 | 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] | 2097 | PyMem_Free(path); |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2098 | return NULL; |
| 2099 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2100 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2101 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2102 | if (argvlist == NULL) { |
| 2103 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2104 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2105 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2106 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2107 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2108 | Py_FileSystemDefaultEncoding, |
| 2109 | &argvlist[i])) { |
| 2110 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2111 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2112 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2113 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2114 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2115 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2116 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2117 | } |
| 2118 | argvlist[argc] = NULL; |
| 2119 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2120 | #ifdef BAD_EXEC_PROTOTYPES |
| 2121 | execv(path, (const char **) argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2122 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2123 | execv(path, argvlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2124 | #endif /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2125 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2126 | /* If we get here it's definitely an error */ |
| 2127 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2128 | free_string_array(argvlist, argc); |
| 2129 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2130 | return posix_error(); |
| 2131 | } |
| 2132 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2134 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2135 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2136 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2137 | \n\ |
| 2138 | path: path of executable file\n\ |
| 2139 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2140 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2141 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2142 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2143 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2144 | { |
| 2145 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2146 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2147 | char **argvlist; |
| 2148 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2149 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2150 | int i, pos, argc, envc; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2151 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2152 | int lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* execve has three arguments: (path, argv, env), where |
| 2155 | argv is a list or tuple of strings and env is a dictionary |
| 2156 | like posix.environ. */ |
| 2157 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2158 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2159 | Py_FileSystemDefaultEncoding, |
| 2160 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2161 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2162 | if (PyList_Check(argv)) { |
| 2163 | argc = PyList_Size(argv); |
| 2164 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2165 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2166 | else if (PyTuple_Check(argv)) { |
| 2167 | argc = PyTuple_Size(argv); |
| 2168 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2169 | } |
| 2170 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2171 | PyErr_SetString(PyExc_TypeError, |
| 2172 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2173 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2174 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2175 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2176 | PyErr_SetString(PyExc_TypeError, |
| 2177 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2178 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2181 | if (argc == 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2182 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2183 | "execve() arg 2 must not be empty"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2184 | goto fail_0; |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2187 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2188 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2189 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2190 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2191 | } |
| 2192 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2193 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2194 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2195 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2196 | &argvlist[i])) |
| 2197 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2198 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2199 | goto fail_1; |
| 2200 | } |
| 2201 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2202 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2203 | argvlist[argc] = NULL; |
| 2204 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2205 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2206 | if (i < 0) |
| 2207 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2208 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2209 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2210 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2211 | goto fail_1; |
| 2212 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2213 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2214 | keys = PyMapping_Keys(env); |
| 2215 | vals = PyMapping_Values(env); |
| 2216 | if (!keys || !vals) |
| 2217 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2218 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2219 | PyErr_SetString(PyExc_TypeError, |
| 2220 | "execve(): env.keys() or env.values() is not a list"); |
| 2221 | goto fail_2; |
| 2222 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2223 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2224 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2225 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2226 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2227 | |
| 2228 | key = PyList_GetItem(keys, pos); |
| 2229 | val = PyList_GetItem(vals, pos); |
| 2230 | if (!key || !val) |
| 2231 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2232 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2233 | if (!PyArg_Parse( |
| 2234 | key, |
| 2235 | "s;execve() arg 3 contains a non-string key", |
| 2236 | &k) || |
| 2237 | !PyArg_Parse( |
| 2238 | val, |
| 2239 | "s;execve() arg 3 contains a non-string value", |
| 2240 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2241 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2242 | goto fail_2; |
| 2243 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2244 | |
| 2245 | #if defined(PYOS_OS2) |
| 2246 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2247 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2248 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2249 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2250 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2251 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2252 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2253 | goto fail_2; |
| 2254 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2255 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2256 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2257 | #if defined(PYOS_OS2) |
| 2258 | } |
| 2259 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2260 | } |
| 2261 | envlist[envc] = 0; |
| 2262 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2263 | |
| 2264 | #ifdef BAD_EXEC_PROTOTYPES |
| 2265 | execve(path, (const char **)argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2266 | #else /* BAD_EXEC_PROTOTYPES */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2267 | execve(path, argvlist, envlist); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2268 | #endif /* BAD_EXEC_PROTOTYPES */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2270 | /* If we get here it's definitely an error */ |
| 2271 | |
| 2272 | (void) posix_error(); |
| 2273 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2274 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2275 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2276 | PyMem_DEL(envlist[envc]); |
| 2277 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2278 | fail_1: |
| 2279 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2280 | Py_XDECREF(vals); |
| 2281 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2282 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2283 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2284 | return NULL; |
| 2285 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2286 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2287 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2288 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2289 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2290 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2291 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2292 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2293 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2294 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2295 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2296 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2297 | |
| 2298 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2299 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2300 | { |
| 2301 | char *path; |
| 2302 | PyObject *argv; |
| 2303 | char **argvlist; |
| 2304 | int mode, i, argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2305 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2306 | PyObject *(*getitem)(PyObject *, int); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2307 | |
| 2308 | /* spawnv has three arguments: (mode, path, argv), where |
| 2309 | argv is a list or tuple of strings. */ |
| 2310 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2311 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2312 | Py_FileSystemDefaultEncoding, |
| 2313 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2314 | return NULL; |
| 2315 | if (PyList_Check(argv)) { |
| 2316 | argc = PyList_Size(argv); |
| 2317 | getitem = PyList_GetItem; |
| 2318 | } |
| 2319 | else if (PyTuple_Check(argv)) { |
| 2320 | argc = PyTuple_Size(argv); |
| 2321 | getitem = PyTuple_GetItem; |
| 2322 | } |
| 2323 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2324 | PyErr_SetString(PyExc_TypeError, |
| 2325 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2326 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2327 | return NULL; |
| 2328 | } |
| 2329 | |
| 2330 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2331 | if (argvlist == NULL) { |
| 2332 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2333 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2334 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2335 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2336 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2337 | Py_FileSystemDefaultEncoding, |
| 2338 | &argvlist[i])) { |
| 2339 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2340 | PyErr_SetString( |
| 2341 | PyExc_TypeError, |
| 2342 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2343 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2344 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | argvlist[argc] = NULL; |
| 2348 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2349 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2350 | Py_BEGIN_ALLOW_THREADS |
| 2351 | spawnval = spawnv(mode, path, argvlist); |
| 2352 | Py_END_ALLOW_THREADS |
| 2353 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2354 | if (mode == _OLD_P_OVERLAY) |
| 2355 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2356 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2357 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2358 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2359 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2360 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2361 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2362 | free_string_array(argvlist, argc); |
| 2363 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2364 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2365 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2366 | return posix_error(); |
| 2367 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2368 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2369 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2370 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2371 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2372 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2376 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2377 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2378 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2379 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2380 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2381 | path: path of executable file\n\ |
| 2382 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2383 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2384 | |
| 2385 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2386 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2387 | { |
| 2388 | char *path; |
| 2389 | PyObject *argv, *env; |
| 2390 | char **argvlist; |
| 2391 | char **envlist; |
| 2392 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 2393 | int mode, i, pos, argc, envc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2394 | Py_intptr_t spawnval; |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2395 | PyObject *(*getitem)(PyObject *, int); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2396 | int lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2397 | |
| 2398 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2399 | argv is a list or tuple of strings and env is a dictionary |
| 2400 | like posix.environ. */ |
| 2401 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2402 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2403 | Py_FileSystemDefaultEncoding, |
| 2404 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2405 | return NULL; |
| 2406 | if (PyList_Check(argv)) { |
| 2407 | argc = PyList_Size(argv); |
| 2408 | getitem = PyList_GetItem; |
| 2409 | } |
| 2410 | else if (PyTuple_Check(argv)) { |
| 2411 | argc = PyTuple_Size(argv); |
| 2412 | getitem = PyTuple_GetItem; |
| 2413 | } |
| 2414 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2415 | PyErr_SetString(PyExc_TypeError, |
| 2416 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2417 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2418 | } |
| 2419 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2420 | PyErr_SetString(PyExc_TypeError, |
| 2421 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2422 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | argvlist = PyMem_NEW(char *, argc+1); |
| 2426 | if (argvlist == NULL) { |
| 2427 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2428 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2429 | } |
| 2430 | for (i = 0; i < argc; i++) { |
| 2431 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2432 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2433 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2434 | &argvlist[i])) |
| 2435 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2436 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2437 | goto fail_1; |
| 2438 | } |
| 2439 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2440 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2441 | argvlist[argc] = NULL; |
| 2442 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2443 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2444 | if (i < 0) |
| 2445 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2446 | envlist = PyMem_NEW(char *, i + 1); |
| 2447 | if (envlist == NULL) { |
| 2448 | PyErr_NoMemory(); |
| 2449 | goto fail_1; |
| 2450 | } |
| 2451 | envc = 0; |
| 2452 | keys = PyMapping_Keys(env); |
| 2453 | vals = PyMapping_Values(env); |
| 2454 | if (!keys || !vals) |
| 2455 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2456 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2457 | PyErr_SetString(PyExc_TypeError, |
| 2458 | "spawnve(): env.keys() or env.values() is not a list"); |
| 2459 | goto fail_2; |
| 2460 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2461 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2462 | for (pos = 0; pos < i; pos++) { |
| 2463 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2464 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2465 | |
| 2466 | key = PyList_GetItem(keys, pos); |
| 2467 | val = PyList_GetItem(vals, pos); |
| 2468 | if (!key || !val) |
| 2469 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2470 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2471 | if (!PyArg_Parse( |
| 2472 | key, |
| 2473 | "s;spawnve() arg 3 contains a non-string key", |
| 2474 | &k) || |
| 2475 | !PyArg_Parse( |
| 2476 | val, |
| 2477 | "s;spawnve() arg 3 contains a non-string value", |
| 2478 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2479 | { |
| 2480 | goto fail_2; |
| 2481 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2482 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2483 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2484 | if (p == NULL) { |
| 2485 | PyErr_NoMemory(); |
| 2486 | goto fail_2; |
| 2487 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2488 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2489 | envlist[envc++] = p; |
| 2490 | } |
| 2491 | envlist[envc] = 0; |
| 2492 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2493 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2494 | Py_BEGIN_ALLOW_THREADS |
| 2495 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 2496 | Py_END_ALLOW_THREADS |
| 2497 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2498 | if (mode == _OLD_P_OVERLAY) |
| 2499 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2500 | |
| 2501 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2502 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2503 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2504 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2505 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2506 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2507 | (void) posix_error(); |
| 2508 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2509 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2510 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2511 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2512 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2513 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2514 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2515 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2516 | while (--envc >= 0) |
| 2517 | PyMem_DEL(envlist[envc]); |
| 2518 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2519 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2520 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2521 | Py_XDECREF(vals); |
| 2522 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2523 | fail_0: |
| 2524 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2525 | return res; |
| 2526 | } |
| 2527 | #endif /* HAVE_SPAWNV */ |
| 2528 | |
| 2529 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2530 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2531 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2532 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2533 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 2534 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2535 | 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] | 2536 | |
| 2537 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2538 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2539 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2540 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 2541 | if (pid == -1) |
| 2542 | return posix_error(); |
| 2543 | PyOS_AfterFork(); |
| 2544 | return PyInt_FromLong((long)pid); |
| 2545 | } |
| 2546 | #endif |
| 2547 | |
| 2548 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2549 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2550 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2551 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2552 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2553 | 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] | 2554 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2555 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2556 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2557 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2558 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2559 | if (pid == -1) |
| 2560 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2561 | if (pid == 0) |
| 2562 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2563 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2564 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2565 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2566 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2567 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 2568 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 2569 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2570 | #define DEV_PTY_FILE "/dev/ptc" |
| 2571 | #define HAVE_DEV_PTMX |
| 2572 | #else |
| 2573 | #define DEV_PTY_FILE "/dev/ptmx" |
| 2574 | #endif |
| 2575 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2576 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2577 | #ifdef HAVE_PTY_H |
| 2578 | #include <pty.h> |
| 2579 | #else |
| 2580 | #ifdef HAVE_LIBUTIL_H |
| 2581 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2582 | #endif /* HAVE_LIBUTIL_H */ |
| 2583 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 2584 | #ifdef HAVE_STROPTS_H |
| 2585 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2586 | #endif |
| 2587 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2588 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2589 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2590 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2591 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2592 | 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] | 2593 | |
| 2594 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2595 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2596 | { |
| 2597 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2598 | #ifndef HAVE_OPENPTY |
| 2599 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2600 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2601 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2602 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2603 | #ifdef sun |
| 2604 | extern char *ptsname(); |
| 2605 | #endif |
| 2606 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2607 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2608 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2609 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 2610 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2611 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2612 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 2613 | if (slave_name == NULL) |
| 2614 | return posix_error(); |
| 2615 | |
| 2616 | slave_fd = open(slave_name, O_RDWR); |
| 2617 | if (slave_fd < 0) |
| 2618 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2619 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2620 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2621 | if (master_fd < 0) |
| 2622 | return posix_error(); |
| 2623 | sig_saved = signal(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2624 | /* change permission of slave */ |
| 2625 | if (grantpt(master_fd) < 0) { |
| 2626 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2627 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2628 | } |
| 2629 | /* unlock slave */ |
| 2630 | if (unlockpt(master_fd) < 0) { |
| 2631 | signal(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2632 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 2633 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2634 | signal(SIGCHLD, sig_saved); |
| 2635 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 2636 | if (slave_name == NULL) |
| 2637 | return posix_error(); |
| 2638 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 2639 | if (slave_fd < 0) |
| 2640 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 2641 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2642 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 2643 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2644 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2645 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 2646 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2647 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 2648 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2649 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2650 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 2651 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2652 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 2653 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2654 | |
| 2655 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2656 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2657 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2658 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 2659 | 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] | 2660 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2661 | |
| 2662 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2663 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2664 | { |
| 2665 | int master_fd, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2666 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2667 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 2668 | if (pid == -1) |
| 2669 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 2670 | if (pid == 0) |
| 2671 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 2672 | return Py_BuildValue("(ii)", pid, master_fd); |
| 2673 | } |
| 2674 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2675 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2676 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2677 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2678 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2680 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2681 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2682 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2683 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2684 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2685 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2686 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2687 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2688 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2689 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2690 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2691 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2692 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2693 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2694 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2695 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2696 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2697 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2698 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2699 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2700 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2701 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2702 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2703 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2704 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2705 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2706 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2707 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2708 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2709 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2710 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2711 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2712 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2713 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2715 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2716 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2717 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2718 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2719 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2720 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2721 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2722 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2725 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2726 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2728 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2729 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2730 | |
| 2731 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2732 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2733 | { |
| 2734 | PyObject *result = NULL; |
| 2735 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2736 | #ifdef NGROUPS_MAX |
| 2737 | #define MAX_GROUPS NGROUPS_MAX |
| 2738 | #else |
| 2739 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 2740 | #define MAX_GROUPS 64 |
| 2741 | #endif |
| 2742 | gid_t grouplist[MAX_GROUPS]; |
| 2743 | int n; |
| 2744 | |
| 2745 | n = getgroups(MAX_GROUPS, grouplist); |
| 2746 | if (n < 0) |
| 2747 | posix_error(); |
| 2748 | else { |
| 2749 | result = PyList_New(n); |
| 2750 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2751 | int i; |
| 2752 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2753 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2754 | if (o == NULL) { |
| 2755 | Py_DECREF(result); |
| 2756 | result = NULL; |
| 2757 | break; |
| 2758 | } |
| 2759 | PyList_SET_ITEM(result, i, o); |
| 2760 | } |
| 2761 | } |
| 2762 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2763 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 2764 | return result; |
| 2765 | } |
| 2766 | #endif |
| 2767 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2768 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2769 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2770 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 2771 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 2772 | |
| 2773 | static PyObject * |
| 2774 | posix_getpgid(PyObject *self, PyObject *args) |
| 2775 | { |
| 2776 | int pid, pgid; |
| 2777 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 2778 | return NULL; |
| 2779 | pgid = getpgid(pid); |
| 2780 | if (pgid < 0) |
| 2781 | return posix_error(); |
| 2782 | return PyInt_FromLong((long)pgid); |
| 2783 | } |
| 2784 | #endif /* HAVE_GETPGID */ |
| 2785 | |
| 2786 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2787 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2789 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2790 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2791 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2792 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2793 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2794 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2795 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2796 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2797 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2798 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2799 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2800 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2801 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 2802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2803 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2804 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2805 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2806 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2807 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2808 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2809 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2810 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2812 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2813 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2814 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2815 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 2816 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 2817 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2818 | Py_INCREF(Py_None); |
| 2819 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2822 | #endif /* HAVE_SETPGRP */ |
| 2823 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2824 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2825 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2826 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2827 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2829 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2830 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2831 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2832 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2833 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2834 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2835 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2836 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2837 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2838 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2839 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2840 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2841 | |
| 2842 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2843 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2844 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2845 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2846 | char *name; |
| 2847 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2848 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2849 | errno = 0; |
| 2850 | name = getlogin(); |
| 2851 | if (name == NULL) { |
| 2852 | if (errno) |
| 2853 | posix_error(); |
| 2854 | else |
| 2855 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 2856 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2857 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2858 | else |
| 2859 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 2860 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2861 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 2862 | return result; |
| 2863 | } |
| 2864 | #endif |
| 2865 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2866 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2867 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2868 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2869 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2870 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2871 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2872 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2873 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2874 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2875 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2876 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 2877 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2878 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2879 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2880 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2881 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2882 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2883 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2884 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2885 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2886 | { |
| 2887 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2888 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2889 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2890 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2891 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 2892 | APIRET rc; |
| 2893 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2894 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2895 | |
| 2896 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 2897 | APIRET rc; |
| 2898 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2899 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2900 | |
| 2901 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2902 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2903 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2904 | if (kill(pid, sig) == -1) |
| 2905 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2906 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2907 | Py_INCREF(Py_None); |
| 2908 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2909 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 2910 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2911 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2912 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2913 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2914 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2915 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 2916 | |
| 2917 | static PyObject * |
| 2918 | posix_killpg(PyObject *self, PyObject *args) |
| 2919 | { |
| 2920 | int pgid, sig; |
| 2921 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 2922 | return NULL; |
| 2923 | if (killpg(pgid, sig) == -1) |
| 2924 | return posix_error(); |
| 2925 | Py_INCREF(Py_None); |
| 2926 | return Py_None; |
| 2927 | } |
| 2928 | #endif |
| 2929 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2930 | #ifdef HAVE_PLOCK |
| 2931 | |
| 2932 | #ifdef HAVE_SYS_LOCK_H |
| 2933 | #include <sys/lock.h> |
| 2934 | #endif |
| 2935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2936 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2937 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2938 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2939 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2940 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2941 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2942 | { |
| 2943 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2944 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2945 | return NULL; |
| 2946 | if (plock(op) == -1) |
| 2947 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2948 | Py_INCREF(Py_None); |
| 2949 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 2950 | } |
| 2951 | #endif |
| 2952 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2953 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2954 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2955 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2956 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2957 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2958 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2959 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2960 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2961 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2962 | async_system(const char *command) |
| 2963 | { |
| 2964 | char *p, errormsg[256], args[1024]; |
| 2965 | RESULTCODES rcodes; |
| 2966 | APIRET rc; |
| 2967 | char *shell = getenv("COMSPEC"); |
| 2968 | if (!shell) |
| 2969 | shell = "cmd"; |
| 2970 | |
| 2971 | strcpy(args, shell); |
| 2972 | p = &args[ strlen(args)+1 ]; |
| 2973 | strcpy(p, "/c "); |
| 2974 | strcat(p, command); |
| 2975 | p += strlen(p) + 1; |
| 2976 | *p = '\0'; |
| 2977 | |
| 2978 | rc = DosExecPgm(errormsg, sizeof(errormsg), |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2979 | EXEC_ASYNC, /* Execute Async w/o Wait for Results */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2980 | args, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2981 | NULL, /* Inherit Parent's Environment */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2982 | &rcodes, shell); |
| 2983 | return rc; |
| 2984 | } |
| 2985 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2986 | static FILE * |
| 2987 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2988 | { |
| 2989 | HFILE rhan, whan; |
| 2990 | FILE *retfd = NULL; |
| 2991 | APIRET rc = DosCreatePipe(&rhan, &whan, pipesize); |
| 2992 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2993 | if (rc != NO_ERROR) { |
| 2994 | *err = rc; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2995 | return NULL; /* ERROR - Unable to Create Anon Pipe */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2996 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2998 | if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */ |
| 2999 | int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3000 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3001 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3002 | close(1); /* Make STDOUT Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3003 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3004 | if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */ |
| 3005 | DosClose(whan); /* Close Now-Unused Pipe Write Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3006 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3007 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3010 | dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */ |
| 3011 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3012 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3013 | if (rc == NO_ERROR) |
| 3014 | retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */ |
| 3015 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3016 | close(oldfd); /* And Close Saved STDOUT Handle */ |
| 3017 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3018 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3019 | } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */ |
| 3020 | int oldfd = dup(0); /* Save STDIN Handle in Another Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3021 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3022 | DosEnterCritSec(); /* Stop Other Threads While Changing Handles */ |
| 3023 | close(0); /* Make STDIN Available for Reallocation */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3024 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3025 | if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */ |
| 3026 | DosClose(rhan); /* Close Now-Unused Pipe Read Handle */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3027 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3028 | rc = async_system(command); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3031 | dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */ |
| 3032 | DosExitCritSec(); /* Now Allow Other Threads to Run */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3033 | |
Martin v. Löwis | dedbe25 | 2001-11-02 23:59:11 +0000 | [diff] [blame] | 3034 | if (rc == NO_ERROR) |
| 3035 | retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */ |
| 3036 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3037 | close(oldfd); /* And Close Saved STDIN Handle */ |
| 3038 | return retfd; /* Return fd of Pipe or NULL if Error */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3039 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3040 | } else { |
| 3041 | *err = ERROR_INVALID_ACCESS; |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3042 | return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */ |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3043 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3047 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3048 | { |
| 3049 | char *name; |
| 3050 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3051 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3052 | FILE *fp; |
| 3053 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3054 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3055 | return NULL; |
| 3056 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3057 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3058 | Py_END_ALLOW_THREADS |
| 3059 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3060 | return os2_error(err); |
| 3061 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3062 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3063 | if (f != NULL) |
| 3064 | PyFile_SetBufSize(f, bufsize); |
| 3065 | return f; |
| 3066 | } |
| 3067 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3068 | #elif defined(PYCC_GCC) |
| 3069 | |
| 3070 | /* standard posix version of popen() support */ |
| 3071 | static PyObject * |
| 3072 | posix_popen(PyObject *self, PyObject *args) |
| 3073 | { |
| 3074 | char *name; |
| 3075 | char *mode = "r"; |
| 3076 | int bufsize = -1; |
| 3077 | FILE *fp; |
| 3078 | PyObject *f; |
| 3079 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3080 | return NULL; |
| 3081 | Py_BEGIN_ALLOW_THREADS |
| 3082 | fp = popen(name, mode); |
| 3083 | Py_END_ALLOW_THREADS |
| 3084 | if (fp == NULL) |
| 3085 | return posix_error(); |
| 3086 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3087 | if (f != NULL) |
| 3088 | PyFile_SetBufSize(f, bufsize); |
| 3089 | return f; |
| 3090 | } |
| 3091 | |
| 3092 | /* fork() under OS/2 has lots'o'warts |
| 3093 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3094 | * most of this code is a ripoff of the win32 code, but using the |
| 3095 | * capabilities of EMX's C library routines |
| 3096 | */ |
| 3097 | |
| 3098 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3099 | #define POPEN_1 1 |
| 3100 | #define POPEN_2 2 |
| 3101 | #define POPEN_3 3 |
| 3102 | #define POPEN_4 4 |
| 3103 | |
| 3104 | static PyObject *_PyPopen(char *, int, int, int); |
| 3105 | static int _PyPclose(FILE *file); |
| 3106 | |
| 3107 | /* |
| 3108 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3109 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3110 | * for more information on this dictionary's use. |
| 3111 | */ |
| 3112 | static PyObject *_PyPopenProcs = NULL; |
| 3113 | |
| 3114 | /* os2emx version of popen2() |
| 3115 | * |
| 3116 | * The result of this function is a pipe (file) connected to the |
| 3117 | * process's stdin, and a pipe connected to the process's stdout. |
| 3118 | */ |
| 3119 | |
| 3120 | static PyObject * |
| 3121 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3122 | { |
| 3123 | PyObject *f; |
| 3124 | int tm=0; |
| 3125 | |
| 3126 | char *cmdstring; |
| 3127 | char *mode = "t"; |
| 3128 | int bufsize = -1; |
| 3129 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3130 | return NULL; |
| 3131 | |
| 3132 | if (*mode == 't') |
| 3133 | tm = O_TEXT; |
| 3134 | else if (*mode != 'b') { |
| 3135 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3136 | return NULL; |
| 3137 | } else |
| 3138 | tm = O_BINARY; |
| 3139 | |
| 3140 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3141 | |
| 3142 | return f; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
| 3146 | * Variation on os2emx.popen2 |
| 3147 | * |
| 3148 | * The result of this function is 3 pipes - the process's stdin, |
| 3149 | * stdout and stderr |
| 3150 | */ |
| 3151 | |
| 3152 | static PyObject * |
| 3153 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3154 | { |
| 3155 | PyObject *f; |
| 3156 | int tm = 0; |
| 3157 | |
| 3158 | char *cmdstring; |
| 3159 | char *mode = "t"; |
| 3160 | int bufsize = -1; |
| 3161 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3162 | return NULL; |
| 3163 | |
| 3164 | if (*mode == 't') |
| 3165 | tm = O_TEXT; |
| 3166 | else if (*mode != 'b') { |
| 3167 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3168 | return NULL; |
| 3169 | } else |
| 3170 | tm = O_BINARY; |
| 3171 | |
| 3172 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3173 | |
| 3174 | return f; |
| 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * Variation on os2emx.popen2 |
| 3179 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3180 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3181 | * and stdout+stderr combined as a single pipe. |
| 3182 | */ |
| 3183 | |
| 3184 | static PyObject * |
| 3185 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3186 | { |
| 3187 | PyObject *f; |
| 3188 | int tm = 0; |
| 3189 | |
| 3190 | char *cmdstring; |
| 3191 | char *mode = "t"; |
| 3192 | int bufsize = -1; |
| 3193 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3194 | return NULL; |
| 3195 | |
| 3196 | if (*mode == 't') |
| 3197 | tm = O_TEXT; |
| 3198 | else if (*mode != 'b') { |
| 3199 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3200 | return NULL; |
| 3201 | } else |
| 3202 | tm = O_BINARY; |
| 3203 | |
| 3204 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3205 | |
| 3206 | return f; |
| 3207 | } |
| 3208 | |
| 3209 | /* a couple of structures for convenient handling of multiple |
| 3210 | * file handles and pipes |
| 3211 | */ |
| 3212 | struct file_ref |
| 3213 | { |
| 3214 | int handle; |
| 3215 | int flags; |
| 3216 | }; |
| 3217 | |
| 3218 | struct pipe_ref |
| 3219 | { |
| 3220 | int rd; |
| 3221 | int wr; |
| 3222 | }; |
| 3223 | |
| 3224 | /* The following code is derived from the win32 code */ |
| 3225 | |
| 3226 | static PyObject * |
| 3227 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 3228 | { |
| 3229 | struct file_ref stdio[3]; |
| 3230 | struct pipe_ref p_fd[3]; |
| 3231 | FILE *p_s[3]; |
| 3232 | int file_count, i, pipe_err, pipe_pid; |
| 3233 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 3234 | PyObject *f, *p_f[3]; |
| 3235 | |
| 3236 | /* file modes for subsequent fdopen's on pipe handles */ |
| 3237 | if (mode == O_TEXT) |
| 3238 | { |
| 3239 | rd_mode = "rt"; |
| 3240 | wr_mode = "wt"; |
| 3241 | } |
| 3242 | else |
| 3243 | { |
| 3244 | rd_mode = "rb"; |
| 3245 | wr_mode = "wb"; |
| 3246 | } |
| 3247 | |
| 3248 | /* prepare shell references */ |
| 3249 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 3250 | if ((shell = getenv("COMSPEC")) == NULL) |
| 3251 | { |
| 3252 | errno = ENOENT; |
| 3253 | return posix_error(); |
| 3254 | } |
| 3255 | |
| 3256 | sh_name = _getname(shell); |
| 3257 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 3258 | opt = "/c"; |
| 3259 | else |
| 3260 | opt = "-c"; |
| 3261 | |
| 3262 | /* save current stdio fds + their flags, and set not inheritable */ |
| 3263 | i = pipe_err = 0; |
| 3264 | while (pipe_err >= 0 && i < 3) |
| 3265 | { |
| 3266 | pipe_err = stdio[i].handle = dup(i); |
| 3267 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 3268 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 3269 | i++; |
| 3270 | } |
| 3271 | if (pipe_err < 0) |
| 3272 | { |
| 3273 | /* didn't get them all saved - clean up and bail out */ |
| 3274 | int saved_err = errno; |
| 3275 | while (i-- > 0) |
| 3276 | { |
| 3277 | close(stdio[i].handle); |
| 3278 | } |
| 3279 | errno = saved_err; |
| 3280 | return posix_error(); |
| 3281 | } |
| 3282 | |
| 3283 | /* create pipe ends */ |
| 3284 | file_count = 2; |
| 3285 | if (n == POPEN_3) |
| 3286 | file_count = 3; |
| 3287 | i = pipe_err = 0; |
| 3288 | while ((pipe_err == 0) && (i < file_count)) |
| 3289 | pipe_err = pipe((int *)&p_fd[i++]); |
| 3290 | if (pipe_err < 0) |
| 3291 | { |
| 3292 | /* didn't get them all made - clean up and bail out */ |
| 3293 | while (i-- > 0) |
| 3294 | { |
| 3295 | close(p_fd[i].wr); |
| 3296 | close(p_fd[i].rd); |
| 3297 | } |
| 3298 | errno = EPIPE; |
| 3299 | return posix_error(); |
| 3300 | } |
| 3301 | |
| 3302 | /* change the actual standard IO streams over temporarily, |
| 3303 | * making the retained pipe ends non-inheritable |
| 3304 | */ |
| 3305 | pipe_err = 0; |
| 3306 | |
| 3307 | /* - stdin */ |
| 3308 | if (dup2(p_fd[0].rd, 0) == 0) |
| 3309 | { |
| 3310 | close(p_fd[0].rd); |
| 3311 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 3312 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 3313 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 3314 | { |
| 3315 | close(p_fd[0].wr); |
| 3316 | pipe_err = -1; |
| 3317 | } |
| 3318 | } |
| 3319 | else |
| 3320 | { |
| 3321 | pipe_err = -1; |
| 3322 | } |
| 3323 | |
| 3324 | /* - stdout */ |
| 3325 | if (pipe_err == 0) |
| 3326 | { |
| 3327 | if (dup2(p_fd[1].wr, 1) == 1) |
| 3328 | { |
| 3329 | close(p_fd[1].wr); |
| 3330 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 3331 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 3332 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 3333 | { |
| 3334 | close(p_fd[1].rd); |
| 3335 | pipe_err = -1; |
| 3336 | } |
| 3337 | } |
| 3338 | else |
| 3339 | { |
| 3340 | pipe_err = -1; |
| 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | /* - stderr, as required */ |
| 3345 | if (pipe_err == 0) |
| 3346 | switch (n) |
| 3347 | { |
| 3348 | case POPEN_3: |
| 3349 | { |
| 3350 | if (dup2(p_fd[2].wr, 2) == 2) |
| 3351 | { |
| 3352 | close(p_fd[2].wr); |
| 3353 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 3354 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 3355 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 3356 | { |
| 3357 | close(p_fd[2].rd); |
| 3358 | pipe_err = -1; |
| 3359 | } |
| 3360 | } |
| 3361 | else |
| 3362 | { |
| 3363 | pipe_err = -1; |
| 3364 | } |
| 3365 | break; |
| 3366 | } |
| 3367 | |
| 3368 | case POPEN_4: |
| 3369 | { |
| 3370 | if (dup2(1, 2) != 2) |
| 3371 | { |
| 3372 | pipe_err = -1; |
| 3373 | } |
| 3374 | break; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | /* spawn the child process */ |
| 3379 | if (pipe_err == 0) |
| 3380 | { |
| 3381 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 3382 | if (pipe_pid == -1) |
| 3383 | { |
| 3384 | pipe_err = -1; |
| 3385 | } |
| 3386 | else |
| 3387 | { |
| 3388 | /* save the PID into the FILE structure |
| 3389 | * NOTE: this implementation doesn't actually |
| 3390 | * take advantage of this, but do it for |
| 3391 | * completeness - AIM Apr01 |
| 3392 | */ |
| 3393 | for (i = 0; i < file_count; i++) |
| 3394 | p_s[i]->_pid = pipe_pid; |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | /* reset standard IO to normal */ |
| 3399 | for (i = 0; i < 3; i++) |
| 3400 | { |
| 3401 | dup2(stdio[i].handle, i); |
| 3402 | fcntl(i, F_SETFD, stdio[i].flags); |
| 3403 | close(stdio[i].handle); |
| 3404 | } |
| 3405 | |
| 3406 | /* if any remnant problems, clean up and bail out */ |
| 3407 | if (pipe_err < 0) |
| 3408 | { |
| 3409 | for (i = 0; i < 3; i++) |
| 3410 | { |
| 3411 | close(p_fd[i].rd); |
| 3412 | close(p_fd[i].wr); |
| 3413 | } |
| 3414 | errno = EPIPE; |
| 3415 | return posix_error_with_filename(cmdstring); |
| 3416 | } |
| 3417 | |
| 3418 | /* build tuple of file objects to return */ |
| 3419 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 3420 | PyFile_SetBufSize(p_f[0], bufsize); |
| 3421 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3422 | PyFile_SetBufSize(p_f[1], bufsize); |
| 3423 | if (n == POPEN_3) |
| 3424 | { |
| 3425 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 3426 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3427 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3428 | } |
| 3429 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3430 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3431 | |
| 3432 | /* |
| 3433 | * Insert the files we've created into the process dictionary |
| 3434 | * all referencing the list with the process handle and the |
| 3435 | * initial number of files (see description below in _PyPclose). |
| 3436 | * Since if _PyPclose later tried to wait on a process when all |
| 3437 | * handles weren't closed, it could create a deadlock with the |
| 3438 | * child, we spend some energy here to try to ensure that we |
| 3439 | * either insert all file handles into the dictionary or none |
| 3440 | * at all. It's a little clumsy with the various popen modes |
| 3441 | * and variable number of files involved. |
| 3442 | */ |
| 3443 | if (!_PyPopenProcs) |
| 3444 | { |
| 3445 | _PyPopenProcs = PyDict_New(); |
| 3446 | } |
| 3447 | |
| 3448 | if (_PyPopenProcs) |
| 3449 | { |
| 3450 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 3451 | int ins_rc[3]; |
| 3452 | |
| 3453 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 3454 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 3455 | |
| 3456 | procObj = PyList_New(2); |
| 3457 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 3458 | intObj = PyInt_FromLong((long) file_count); |
| 3459 | |
| 3460 | if (procObj && pidObj && intObj) |
| 3461 | { |
| 3462 | PyList_SetItem(procObj, 0, pidObj); |
| 3463 | PyList_SetItem(procObj, 1, intObj); |
| 3464 | |
| 3465 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 3466 | if (fileObj[0]) |
| 3467 | { |
| 3468 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 3469 | fileObj[0], |
| 3470 | procObj); |
| 3471 | } |
| 3472 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 3473 | if (fileObj[1]) |
| 3474 | { |
| 3475 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 3476 | fileObj[1], |
| 3477 | procObj); |
| 3478 | } |
| 3479 | if (file_count >= 3) |
| 3480 | { |
| 3481 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 3482 | if (fileObj[2]) |
| 3483 | { |
| 3484 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 3485 | fileObj[2], |
| 3486 | procObj); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 3491 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 3492 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 3493 | { |
| 3494 | /* Something failed - remove any dictionary |
| 3495 | * entries that did make it. |
| 3496 | */ |
| 3497 | if (!ins_rc[0] && fileObj[0]) |
| 3498 | { |
| 3499 | PyDict_DelItem(_PyPopenProcs, |
| 3500 | fileObj[0]); |
| 3501 | } |
| 3502 | if (!ins_rc[1] && fileObj[1]) |
| 3503 | { |
| 3504 | PyDict_DelItem(_PyPopenProcs, |
| 3505 | fileObj[1]); |
| 3506 | } |
| 3507 | if (!ins_rc[2] && fileObj[2]) |
| 3508 | { |
| 3509 | PyDict_DelItem(_PyPopenProcs, |
| 3510 | fileObj[2]); |
| 3511 | } |
| 3512 | } |
| 3513 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3514 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3515 | /* |
| 3516 | * Clean up our localized references for the dictionary keys |
| 3517 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 3518 | * that got placed in the dictionary. |
| 3519 | */ |
| 3520 | Py_XDECREF(procObj); |
| 3521 | Py_XDECREF(fileObj[0]); |
| 3522 | Py_XDECREF(fileObj[1]); |
| 3523 | Py_XDECREF(fileObj[2]); |
| 3524 | } |
| 3525 | |
| 3526 | /* Child is launched. */ |
| 3527 | return f; |
| 3528 | } |
| 3529 | |
| 3530 | /* |
| 3531 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 3532 | * exit code for the child process and return as a result of the close. |
| 3533 | * |
| 3534 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 3535 | * input file pointer to information about the process that was |
| 3536 | * originally created by the popen* call that created the file pointer. |
| 3537 | * The dictionary uses the file pointer as a key (with one entry |
| 3538 | * inserted for each file returned by the original popen* call) and a |
| 3539 | * single list object as the value for all files from a single call. |
| 3540 | * The list object contains the Win32 process handle at [0], and a file |
| 3541 | * count at [1], which is initialized to the total number of file |
| 3542 | * handles using that list. |
| 3543 | * |
| 3544 | * This function closes whichever handle it is passed, and decrements |
| 3545 | * the file count in the dictionary for the process handle pointed to |
| 3546 | * by this file. On the last close (when the file count reaches zero), |
| 3547 | * this function will wait for the child process and then return its |
| 3548 | * exit code as the result of the close() operation. This permits the |
| 3549 | * files to be closed in any order - it is always the close() of the |
| 3550 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3551 | * |
| 3552 | * NOTE: This function is currently called with the GIL released. |
| 3553 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3554 | */ |
| 3555 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3556 | static int _PyPclose(FILE *file) |
| 3557 | { |
| 3558 | int result; |
| 3559 | int exit_code; |
| 3560 | int pipe_pid; |
| 3561 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 3562 | int file_count; |
| 3563 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3564 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3565 | #endif |
| 3566 | |
| 3567 | /* Close the file handle first, to ensure it can't block the |
| 3568 | * child from exiting if it's the last handle. |
| 3569 | */ |
| 3570 | result = fclose(file); |
| 3571 | |
| 3572 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3573 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3574 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3575 | if (_PyPopenProcs) |
| 3576 | { |
| 3577 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 3578 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 3579 | fileObj)) != NULL && |
| 3580 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 3581 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 3582 | { |
| 3583 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 3584 | file_count = (int) PyInt_AsLong(intObj); |
| 3585 | |
| 3586 | if (file_count > 1) |
| 3587 | { |
| 3588 | /* Still other files referencing process */ |
| 3589 | file_count--; |
| 3590 | PyList_SetItem(procObj,1, |
| 3591 | PyInt_FromLong((long) file_count)); |
| 3592 | } |
| 3593 | else |
| 3594 | { |
| 3595 | /* Last file for this process */ |
| 3596 | if (result != EOF && |
| 3597 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 3598 | { |
| 3599 | /* extract exit status */ |
| 3600 | if (WIFEXITED(exit_code)) |
| 3601 | { |
| 3602 | result = WEXITSTATUS(exit_code); |
| 3603 | } |
| 3604 | else |
| 3605 | { |
| 3606 | errno = EPIPE; |
| 3607 | result = -1; |
| 3608 | } |
| 3609 | } |
| 3610 | else |
| 3611 | { |
| 3612 | /* Indicate failure - this will cause the file object |
| 3613 | * to raise an I/O error and translate the last |
| 3614 | * error code from errno. We do have a problem with |
| 3615 | * last errors that overlap the normal errno table, |
| 3616 | * but that's a consistent problem with the file object. |
| 3617 | */ |
| 3618 | result = -1; |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | /* Remove this file pointer from dictionary */ |
| 3623 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 3624 | |
| 3625 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 3626 | { |
| 3627 | Py_DECREF(_PyPopenProcs); |
| 3628 | _PyPopenProcs = NULL; |
| 3629 | } |
| 3630 | |
| 3631 | } /* if object retrieval ok */ |
| 3632 | |
| 3633 | Py_XDECREF(fileObj); |
| 3634 | } /* if _PyPopenProcs */ |
| 3635 | |
| 3636 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 3637 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3638 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3639 | return result; |
| 3640 | } |
| 3641 | |
| 3642 | #endif /* PYCC_??? */ |
| 3643 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3644 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* |
| 3647 | * Portable 'popen' replacement for Win32. |
| 3648 | * |
| 3649 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 3650 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3651 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3652 | */ |
| 3653 | |
| 3654 | #include <malloc.h> |
| 3655 | #include <io.h> |
| 3656 | #include <fcntl.h> |
| 3657 | |
| 3658 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 3659 | #define POPEN_1 1 |
| 3660 | #define POPEN_2 2 |
| 3661 | #define POPEN_3 3 |
| 3662 | #define POPEN_4 4 |
| 3663 | |
| 3664 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3665 | static int _PyPclose(FILE *file); |
| 3666 | |
| 3667 | /* |
| 3668 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3669 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3670 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3671 | */ |
| 3672 | static PyObject *_PyPopenProcs = NULL; |
| 3673 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3674 | |
| 3675 | /* popen that works from a GUI. |
| 3676 | * |
| 3677 | * The result of this function is a pipe (file) connected to the |
| 3678 | * processes stdin or stdout, depending on the requested mode. |
| 3679 | */ |
| 3680 | |
| 3681 | static PyObject * |
| 3682 | posix_popen(PyObject *self, PyObject *args) |
| 3683 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 3684 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3685 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3686 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3687 | char *cmdstring; |
| 3688 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3689 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3690 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3691 | return NULL; |
| 3692 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3693 | if (*mode == 'r') |
| 3694 | tm = _O_RDONLY; |
| 3695 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3696 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3697 | return NULL; |
| 3698 | } else |
| 3699 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3700 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3701 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3702 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3703 | return NULL; |
| 3704 | } |
| 3705 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3706 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3707 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3708 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3709 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3710 | else |
| 3711 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 3712 | |
| 3713 | return f; |
| 3714 | } |
| 3715 | |
| 3716 | /* Variation on win32pipe.popen |
| 3717 | * |
| 3718 | * The result of this function is a pipe (file) connected to the |
| 3719 | * process's stdin, and a pipe connected to the process's stdout. |
| 3720 | */ |
| 3721 | |
| 3722 | static PyObject * |
| 3723 | win32_popen2(PyObject *self, PyObject *args) |
| 3724 | { |
| 3725 | PyObject *f; |
| 3726 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3727 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3728 | char *cmdstring; |
| 3729 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3730 | int bufsize = -1; |
| 3731 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3732 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3733 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3734 | if (*mode == 't') |
| 3735 | tm = _O_TEXT; |
| 3736 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3737 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3738 | return NULL; |
| 3739 | } else |
| 3740 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3741 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3742 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3743 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3744 | return NULL; |
| 3745 | } |
| 3746 | |
| 3747 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3748 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3749 | return f; |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3754 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3755 | * The result of this function is 3 pipes - the process's stdin, |
| 3756 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3757 | */ |
| 3758 | |
| 3759 | static PyObject * |
| 3760 | win32_popen3(PyObject *self, PyObject *args) |
| 3761 | { |
| 3762 | PyObject *f; |
| 3763 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3764 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3765 | char *cmdstring; |
| 3766 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3767 | int bufsize = -1; |
| 3768 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3769 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3770 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3771 | if (*mode == 't') |
| 3772 | tm = _O_TEXT; |
| 3773 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3774 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3775 | return NULL; |
| 3776 | } else |
| 3777 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3778 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3779 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3780 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3781 | return NULL; |
| 3782 | } |
| 3783 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3784 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3785 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3786 | return f; |
| 3787 | } |
| 3788 | |
| 3789 | /* |
| 3790 | * Variation on win32pipe.popen |
| 3791 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3792 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3793 | * and stdout+stderr combined as a single pipe. |
| 3794 | */ |
| 3795 | |
| 3796 | static PyObject * |
| 3797 | win32_popen4(PyObject *self, PyObject *args) |
| 3798 | { |
| 3799 | PyObject *f; |
| 3800 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3801 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3802 | char *cmdstring; |
| 3803 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3804 | int bufsize = -1; |
| 3805 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3806 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3807 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3808 | if (*mode == 't') |
| 3809 | tm = _O_TEXT; |
| 3810 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3811 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3812 | return NULL; |
| 3813 | } else |
| 3814 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3815 | |
| 3816 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3817 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3818 | return NULL; |
| 3819 | } |
| 3820 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 3821 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 3822 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3823 | return f; |
| 3824 | } |
| 3825 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3826 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3827 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3828 | HANDLE hStdin, |
| 3829 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3830 | HANDLE hStderr, |
| 3831 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3832 | { |
| 3833 | PROCESS_INFORMATION piProcInfo; |
| 3834 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3835 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3836 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3837 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3838 | int i; |
| 3839 | int x; |
| 3840 | |
| 3841 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3842 | char *comshell; |
| 3843 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3844 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3845 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
| 3846 | return x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3847 | |
| 3848 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 3849 | * then use the w9xpopen hack. |
| 3850 | */ |
| 3851 | comshell = s1 + x; |
| 3852 | while (comshell >= s1 && *comshell != '\\') |
| 3853 | --comshell; |
| 3854 | ++comshell; |
| 3855 | |
| 3856 | if (GetVersion() < 0x80000000 && |
| 3857 | _stricmp(comshell, "command.com") != 0) { |
| 3858 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3859 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3860 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3861 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3862 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3863 | } |
| 3864 | else { |
| 3865 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3866 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 3867 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3868 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3869 | char modulepath[_MAX_PATH]; |
| 3870 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3871 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
| 3872 | for (i = x = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3873 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3874 | x = i+1; |
| 3875 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3876 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3877 | strncat(modulepath, |
| 3878 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3879 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3880 | -strlen(modulepath)); |
| 3881 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3882 | /* Eeek - file-not-found - possibly an embedding |
| 3883 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3884 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3885 | strncpy(modulepath, |
| 3886 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3887 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 3888 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 3889 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3890 | strncat(modulepath, |
| 3891 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3892 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 3893 | -strlen(modulepath)); |
| 3894 | /* No where else to look - raise an easily identifiable |
| 3895 | error, rather than leaving Windows to report |
| 3896 | "file not found" - as the user is probably blissfully |
| 3897 | unaware this shim EXE is used, and it will confuse them. |
| 3898 | (well, it confused me for a while ;-) |
| 3899 | */ |
| 3900 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3901 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3902 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3903 | "for popen to work with your shell " |
| 3904 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3905 | szConsoleSpawn); |
| 3906 | return FALSE; |
| 3907 | } |
| 3908 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3909 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3910 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3911 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3912 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 3913 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3914 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3915 | /* To maintain correct argument passing semantics, |
| 3916 | we pass the command-line as it stands, and allow |
| 3917 | quoting to be applied. w9xpopen.exe will then |
| 3918 | use its argv vector, and re-quote the necessary |
| 3919 | args for the ultimate child process. |
| 3920 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 3921 | PyOS_snprintf( |
| 3922 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 3923 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3924 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3925 | s1, |
| 3926 | s3, |
| 3927 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3928 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3929 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3930 | dialog: |
| 3931 | "Your program accessed mem currently in use at xxx" |
| 3932 | and a hopeful warning about the stability of your |
| 3933 | system. |
| 3934 | Cost is Ctrl+C wont kill children, but anyone |
| 3935 | who cares can have a go! |
| 3936 | */ |
| 3937 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | /* Could be an else here to try cmd.exe / command.com in the path |
| 3942 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3943 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3944 | PyErr_SetString(PyExc_RuntimeError, |
| 3945 | "Cannot locate a COMSPEC environment variable to " |
| 3946 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 3947 | return FALSE; |
| 3948 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3949 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3950 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 3951 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 3952 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 3953 | siStartInfo.hStdInput = hStdin; |
| 3954 | siStartInfo.hStdOutput = hStdout; |
| 3955 | siStartInfo.hStdError = hStderr; |
| 3956 | siStartInfo.wShowWindow = SW_HIDE; |
| 3957 | |
| 3958 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3959 | s2, |
| 3960 | NULL, |
| 3961 | NULL, |
| 3962 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3963 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 3964 | NULL, |
| 3965 | NULL, |
| 3966 | &siStartInfo, |
| 3967 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3968 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3969 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 3970 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3971 | /* Return process handle */ |
| 3972 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3973 | return TRUE; |
| 3974 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 3975 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3976 | return FALSE; |
| 3977 | } |
| 3978 | |
| 3979 | /* The following code is based off of KB: Q190351 */ |
| 3980 | |
| 3981 | static PyObject * |
| 3982 | _PyPopen(char *cmdstring, int mode, int n) |
| 3983 | { |
| 3984 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 3985 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3986 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3987 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3988 | SECURITY_ATTRIBUTES saAttr; |
| 3989 | BOOL fSuccess; |
| 3990 | int fd1, fd2, fd3; |
| 3991 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 3992 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 3993 | PyObject *f; |
| 3994 | |
| 3995 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 3996 | saAttr.bInheritHandle = TRUE; |
| 3997 | saAttr.lpSecurityDescriptor = NULL; |
| 3998 | |
| 3999 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4000 | return win32_error("CreatePipe", NULL); |
| 4001 | |
| 4002 | /* Create new output read handle and the input write handle. Set |
| 4003 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4004 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4005 | * being created. */ |
| 4006 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4007 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4008 | FALSE, |
| 4009 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4010 | if (!fSuccess) |
| 4011 | return win32_error("DuplicateHandle", NULL); |
| 4012 | |
| 4013 | /* Close the inheritable version of ChildStdin |
| 4014 | that we're using. */ |
| 4015 | CloseHandle(hChildStdinWr); |
| 4016 | |
| 4017 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4018 | return win32_error("CreatePipe", NULL); |
| 4019 | |
| 4020 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4021 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4022 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4023 | if (!fSuccess) |
| 4024 | return win32_error("DuplicateHandle", NULL); |
| 4025 | |
| 4026 | /* Close the inheritable version of ChildStdout |
| 4027 | that we're using. */ |
| 4028 | CloseHandle(hChildStdoutRd); |
| 4029 | |
| 4030 | if (n != POPEN_4) { |
| 4031 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4032 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4033 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4034 | hChildStderrRd, |
| 4035 | GetCurrentProcess(), |
| 4036 | &hChildStderrRdDup, 0, |
| 4037 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4038 | if (!fSuccess) |
| 4039 | return win32_error("DuplicateHandle", NULL); |
| 4040 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4041 | CloseHandle(hChildStderrRd); |
| 4042 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4043 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4044 | switch (n) { |
| 4045 | case POPEN_1: |
| 4046 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4047 | case _O_WRONLY | _O_TEXT: |
| 4048 | /* Case for writing to child Stdin in text mode. */ |
| 4049 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4050 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4051 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4052 | PyFile_SetBufSize(f, 0); |
| 4053 | /* We don't care about these pipes anymore, so close them. */ |
| 4054 | CloseHandle(hChildStdoutRdDup); |
| 4055 | CloseHandle(hChildStderrRdDup); |
| 4056 | break; |
| 4057 | |
| 4058 | case _O_RDONLY | _O_TEXT: |
| 4059 | /* Case for reading from child Stdout in text mode. */ |
| 4060 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4061 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4062 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4063 | PyFile_SetBufSize(f, 0); |
| 4064 | /* We don't care about these pipes anymore, so close them. */ |
| 4065 | CloseHandle(hChildStdinWrDup); |
| 4066 | CloseHandle(hChildStderrRdDup); |
| 4067 | break; |
| 4068 | |
| 4069 | case _O_RDONLY | _O_BINARY: |
| 4070 | /* Case for readinig from child Stdout in binary mode. */ |
| 4071 | fd1 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4072 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4073 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4074 | PyFile_SetBufSize(f, 0); |
| 4075 | /* We don't care about these pipes anymore, so close them. */ |
| 4076 | CloseHandle(hChildStdinWrDup); |
| 4077 | CloseHandle(hChildStderrRdDup); |
| 4078 | break; |
| 4079 | |
| 4080 | case _O_WRONLY | _O_BINARY: |
| 4081 | /* Case for writing to child Stdin in binary mode. */ |
| 4082 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4083 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4084 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4085 | PyFile_SetBufSize(f, 0); |
| 4086 | /* We don't care about these pipes anymore, so close them. */ |
| 4087 | CloseHandle(hChildStdoutRdDup); |
| 4088 | CloseHandle(hChildStderrRdDup); |
| 4089 | break; |
| 4090 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4091 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4092 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4093 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4094 | case POPEN_2: |
| 4095 | case POPEN_4: |
| 4096 | { |
| 4097 | char *m1, *m2; |
| 4098 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4099 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4100 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4101 | m1 = "r"; |
| 4102 | m2 = "w"; |
| 4103 | } else { |
| 4104 | m1 = "rb"; |
| 4105 | m2 = "wb"; |
| 4106 | } |
| 4107 | |
| 4108 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4109 | f1 = _fdopen(fd1, m2); |
| 4110 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4111 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4112 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4113 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4114 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4115 | PyFile_SetBufSize(p2, 0); |
| 4116 | |
| 4117 | if (n != 4) |
| 4118 | CloseHandle(hChildStderrRdDup); |
| 4119 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4120 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4121 | Py_XDECREF(p1); |
| 4122 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4123 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4124 | break; |
| 4125 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4126 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4127 | case POPEN_3: |
| 4128 | { |
| 4129 | char *m1, *m2; |
| 4130 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4131 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4132 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4133 | m1 = "r"; |
| 4134 | m2 = "w"; |
| 4135 | } else { |
| 4136 | m1 = "rb"; |
| 4137 | m2 = "wb"; |
| 4138 | } |
| 4139 | |
| 4140 | fd1 = _open_osfhandle((long)hChildStdinWrDup, mode); |
| 4141 | f1 = _fdopen(fd1, m2); |
| 4142 | fd2 = _open_osfhandle((long)hChildStdoutRdDup, mode); |
| 4143 | f2 = _fdopen(fd2, m1); |
| 4144 | fd3 = _open_osfhandle((long)hChildStderrRdDup, mode); |
| 4145 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4146 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4147 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4148 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4149 | PyFile_SetBufSize(p1, 0); |
| 4150 | PyFile_SetBufSize(p2, 0); |
| 4151 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4152 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4153 | Py_XDECREF(p1); |
| 4154 | Py_XDECREF(p2); |
| 4155 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4156 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4157 | break; |
| 4158 | } |
| 4159 | } |
| 4160 | |
| 4161 | if (n == POPEN_4) { |
| 4162 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4163 | hChildStdinRd, |
| 4164 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4165 | hChildStdoutWr, |
| 4166 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4167 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4168 | } |
| 4169 | else { |
| 4170 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4171 | hChildStdinRd, |
| 4172 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4173 | hChildStderrWr, |
| 4174 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4175 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4178 | /* |
| 4179 | * Insert the files we've created into the process dictionary |
| 4180 | * all referencing the list with the process handle and the |
| 4181 | * initial number of files (see description below in _PyPclose). |
| 4182 | * Since if _PyPclose later tried to wait on a process when all |
| 4183 | * handles weren't closed, it could create a deadlock with the |
| 4184 | * child, we spend some energy here to try to ensure that we |
| 4185 | * either insert all file handles into the dictionary or none |
| 4186 | * at all. It's a little clumsy with the various popen modes |
| 4187 | * and variable number of files involved. |
| 4188 | */ |
| 4189 | if (!_PyPopenProcs) { |
| 4190 | _PyPopenProcs = PyDict_New(); |
| 4191 | } |
| 4192 | |
| 4193 | if (_PyPopenProcs) { |
| 4194 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4195 | int ins_rc[3]; |
| 4196 | |
| 4197 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4198 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4199 | |
| 4200 | procObj = PyList_New(2); |
| 4201 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4202 | intObj = PyInt_FromLong(file_count); |
| 4203 | |
| 4204 | if (procObj && hProcessObj && intObj) { |
| 4205 | PyList_SetItem(procObj,0,hProcessObj); |
| 4206 | PyList_SetItem(procObj,1,intObj); |
| 4207 | |
| 4208 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4209 | if (fileObj[0]) { |
| 4210 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4211 | fileObj[0], |
| 4212 | procObj); |
| 4213 | } |
| 4214 | if (file_count >= 2) { |
| 4215 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4216 | if (fileObj[1]) { |
| 4217 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4218 | fileObj[1], |
| 4219 | procObj); |
| 4220 | } |
| 4221 | } |
| 4222 | if (file_count >= 3) { |
| 4223 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 4224 | if (fileObj[2]) { |
| 4225 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4226 | fileObj[2], |
| 4227 | procObj); |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4232 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4233 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 4234 | /* Something failed - remove any dictionary |
| 4235 | * entries that did make it. |
| 4236 | */ |
| 4237 | if (!ins_rc[0] && fileObj[0]) { |
| 4238 | PyDict_DelItem(_PyPopenProcs, |
| 4239 | fileObj[0]); |
| 4240 | } |
| 4241 | if (!ins_rc[1] && fileObj[1]) { |
| 4242 | PyDict_DelItem(_PyPopenProcs, |
| 4243 | fileObj[1]); |
| 4244 | } |
| 4245 | if (!ins_rc[2] && fileObj[2]) { |
| 4246 | PyDict_DelItem(_PyPopenProcs, |
| 4247 | fileObj[2]); |
| 4248 | } |
| 4249 | } |
| 4250 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4251 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4252 | /* |
| 4253 | * Clean up our localized references for the dictionary keys |
| 4254 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4255 | * that got placed in the dictionary. |
| 4256 | */ |
| 4257 | Py_XDECREF(procObj); |
| 4258 | Py_XDECREF(fileObj[0]); |
| 4259 | Py_XDECREF(fileObj[1]); |
| 4260 | Py_XDECREF(fileObj[2]); |
| 4261 | } |
| 4262 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4263 | /* Child is launched. Close the parents copy of those pipe |
| 4264 | * handles that only the child should have open. You need to |
| 4265 | * make sure that no handles to the write end of the output pipe |
| 4266 | * are maintained in this process or else the pipe will not close |
| 4267 | * when the child process exits and the ReadFile will hang. */ |
| 4268 | |
| 4269 | if (!CloseHandle(hChildStdinRd)) |
| 4270 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4271 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4272 | if (!CloseHandle(hChildStdoutWr)) |
| 4273 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4274 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4275 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 4276 | return win32_error("CloseHandle", NULL); |
| 4277 | |
| 4278 | return f; |
| 4279 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4280 | |
| 4281 | /* |
| 4282 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4283 | * 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] | 4284 | * |
| 4285 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4286 | * input file pointer to information about the process that was |
| 4287 | * originally created by the popen* call that created the file pointer. |
| 4288 | * The dictionary uses the file pointer as a key (with one entry |
| 4289 | * inserted for each file returned by the original popen* call) and a |
| 4290 | * single list object as the value for all files from a single call. |
| 4291 | * The list object contains the Win32 process handle at [0], and a file |
| 4292 | * count at [1], which is initialized to the total number of file |
| 4293 | * handles using that list. |
| 4294 | * |
| 4295 | * This function closes whichever handle it is passed, and decrements |
| 4296 | * the file count in the dictionary for the process handle pointed to |
| 4297 | * by this file. On the last close (when the file count reaches zero), |
| 4298 | * this function will wait for the child process and then return its |
| 4299 | * exit code as the result of the close() operation. This permits the |
| 4300 | * files to be closed in any order - it is always the close() of the |
| 4301 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4302 | * |
| 4303 | * NOTE: This function is currently called with the GIL released. |
| 4304 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4305 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4306 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4307 | static int _PyPclose(FILE *file) |
| 4308 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4309 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4310 | DWORD exit_code; |
| 4311 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4312 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 4313 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4314 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4315 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4316 | #endif |
| 4317 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4318 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4319 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4320 | */ |
| 4321 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4322 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4323 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4324 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4325 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4326 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4327 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4328 | fileObj)) != NULL && |
| 4329 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 4330 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 4331 | |
| 4332 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 4333 | file_count = PyInt_AsLong(intObj); |
| 4334 | |
| 4335 | if (file_count > 1) { |
| 4336 | /* Still other files referencing process */ |
| 4337 | file_count--; |
| 4338 | PyList_SetItem(procObj,1, |
| 4339 | PyInt_FromLong(file_count)); |
| 4340 | } else { |
| 4341 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4342 | if (result != EOF && |
| 4343 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 4344 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4345 | /* Possible truncation here in 16-bit environments, but |
| 4346 | * real exit codes are just the lower byte in any event. |
| 4347 | */ |
| 4348 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4349 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4350 | /* Indicate failure - this will cause the file object |
| 4351 | * to raise an I/O error and translate the last Win32 |
| 4352 | * error code from errno. We do have a problem with |
| 4353 | * last errors that overlap the normal errno table, |
| 4354 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4355 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 4356 | if (result != EOF) { |
| 4357 | /* If the error wasn't from the fclose(), then |
| 4358 | * set errno for the file object error handling. |
| 4359 | */ |
| 4360 | errno = GetLastError(); |
| 4361 | } |
| 4362 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | /* Free up the native handle at this point */ |
| 4366 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4367 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4368 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4369 | /* Remove this file pointer from dictionary */ |
| 4370 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4371 | |
| 4372 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 4373 | Py_DECREF(_PyPopenProcs); |
| 4374 | _PyPopenProcs = NULL; |
| 4375 | } |
| 4376 | |
| 4377 | } /* if object retrieval ok */ |
| 4378 | |
| 4379 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4380 | } /* if _PyPopenProcs */ |
| 4381 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4382 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4383 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 4384 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4385 | return result; |
| 4386 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 4387 | |
| 4388 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4389 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4390 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4391 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4392 | char *name; |
| 4393 | char *mode = "r"; |
| 4394 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4395 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4396 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4397 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4398 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 4399 | /* Strip mode of binary or text modifiers */ |
| 4400 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 4401 | mode = "r"; |
| 4402 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 4403 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4404 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4405 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4406 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4407 | if (fp == NULL) |
| 4408 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4409 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4410 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4411 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 4412 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4413 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4414 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4415 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4416 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4418 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4419 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4420 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4421 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4422 | Set the current process's user id."); |
| 4423 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4424 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4425 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4426 | { |
| 4427 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4428 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4429 | return NULL; |
| 4430 | if (setuid(uid) < 0) |
| 4431 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4432 | Py_INCREF(Py_None); |
| 4433 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4434 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4435 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4436 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4437 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4438 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4439 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4440 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4441 | Set the current process's effective user id."); |
| 4442 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4443 | static PyObject * |
| 4444 | posix_seteuid (PyObject *self, PyObject *args) |
| 4445 | { |
| 4446 | int euid; |
| 4447 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 4448 | return NULL; |
| 4449 | } else if (seteuid(euid) < 0) { |
| 4450 | return posix_error(); |
| 4451 | } else { |
| 4452 | Py_INCREF(Py_None); |
| 4453 | return Py_None; |
| 4454 | } |
| 4455 | } |
| 4456 | #endif /* HAVE_SETEUID */ |
| 4457 | |
| 4458 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4459 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4460 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4461 | Set the current process's effective group id."); |
| 4462 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4463 | static PyObject * |
| 4464 | posix_setegid (PyObject *self, PyObject *args) |
| 4465 | { |
| 4466 | int egid; |
| 4467 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 4468 | return NULL; |
| 4469 | } else if (setegid(egid) < 0) { |
| 4470 | return posix_error(); |
| 4471 | } else { |
| 4472 | Py_INCREF(Py_None); |
| 4473 | return Py_None; |
| 4474 | } |
| 4475 | } |
| 4476 | #endif /* HAVE_SETEGID */ |
| 4477 | |
| 4478 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4479 | PyDoc_STRVAR(posix_setreuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4480 | "seteuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4481 | Set the current process's real and effective user ids."); |
| 4482 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4483 | static PyObject * |
| 4484 | posix_setreuid (PyObject *self, PyObject *args) |
| 4485 | { |
| 4486 | int ruid, euid; |
| 4487 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 4488 | return NULL; |
| 4489 | } else if (setreuid(ruid, euid) < 0) { |
| 4490 | return posix_error(); |
| 4491 | } else { |
| 4492 | Py_INCREF(Py_None); |
| 4493 | return Py_None; |
| 4494 | } |
| 4495 | } |
| 4496 | #endif /* HAVE_SETREUID */ |
| 4497 | |
| 4498 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4499 | PyDoc_STRVAR(posix_setregid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4500 | "setegid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4501 | Set the current process's real and effective group ids."); |
| 4502 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4503 | static PyObject * |
| 4504 | posix_setregid (PyObject *self, PyObject *args) |
| 4505 | { |
| 4506 | int rgid, egid; |
| 4507 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 4508 | return NULL; |
| 4509 | } else if (setregid(rgid, egid) < 0) { |
| 4510 | return posix_error(); |
| 4511 | } else { |
| 4512 | Py_INCREF(Py_None); |
| 4513 | return Py_None; |
| 4514 | } |
| 4515 | } |
| 4516 | #endif /* HAVE_SETREGID */ |
| 4517 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4518 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4519 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4520 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4521 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4522 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4523 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4524 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4525 | { |
| 4526 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4527 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4528 | return NULL; |
| 4529 | if (setgid(gid) < 0) |
| 4530 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4531 | Py_INCREF(Py_None); |
| 4532 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4533 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4534 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4535 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4536 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4537 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4538 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4539 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4540 | |
| 4541 | static PyObject * |
| 4542 | posix_setgroups(PyObject *self, PyObject *args) |
| 4543 | { |
| 4544 | PyObject *groups; |
| 4545 | int i, len; |
| 4546 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4547 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4548 | if (!PyArg_ParseTuple(args, "O:setgid", &groups)) |
| 4549 | return NULL; |
| 4550 | if (!PySequence_Check(groups)) { |
| 4551 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4552 | return NULL; |
| 4553 | } |
| 4554 | len = PySequence_Size(groups); |
| 4555 | if (len > MAX_GROUPS) { |
| 4556 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4557 | return NULL; |
| 4558 | } |
| 4559 | for(i = 0; i < len; i++) { |
| 4560 | PyObject *elem; |
| 4561 | elem = PySequence_GetItem(groups, i); |
| 4562 | if (!elem) |
| 4563 | return NULL; |
| 4564 | if (!PyInt_Check(elem)) { |
| 4565 | PyErr_SetString(PyExc_TypeError, |
| 4566 | "groups must be integers"); |
| 4567 | Py_DECREF(elem); |
| 4568 | return NULL; |
| 4569 | } |
| 4570 | /* XXX: check that value fits into gid_t. */ |
| 4571 | grouplist[i] = PyInt_AsLong(elem); |
| 4572 | Py_DECREF(elem); |
| 4573 | } |
| 4574 | |
| 4575 | if (setgroups(len, grouplist) < 0) |
| 4576 | return posix_error(); |
| 4577 | Py_INCREF(Py_None); |
| 4578 | return Py_None; |
| 4579 | } |
| 4580 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4581 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4582 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4583 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4584 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4585 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4586 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4587 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4588 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4589 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4590 | int pid, options; |
| 4591 | #ifdef UNION_WAIT |
| 4592 | union wait status; |
| 4593 | #define status_i (status.w_status) |
| 4594 | #else |
| 4595 | int status; |
| 4596 | #define status_i status |
| 4597 | #endif |
| 4598 | status_i = 0; |
| 4599 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4600 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4601 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4602 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4603 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4604 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4605 | if (pid == -1) |
| 4606 | return posix_error(); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4607 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4608 | return Py_BuildValue("ii", pid, status_i); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4611 | #elif defined(HAVE_CWAIT) |
| 4612 | |
| 4613 | /* 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] | 4614 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4615 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4616 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4617 | |
| 4618 | static PyObject * |
| 4619 | posix_waitpid(PyObject *self, PyObject *args) |
| 4620 | { |
| 4621 | int pid, options; |
| 4622 | int status; |
| 4623 | |
| 4624 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 4625 | return NULL; |
| 4626 | Py_BEGIN_ALLOW_THREADS |
| 4627 | pid = _cwait(&status, pid, options); |
| 4628 | Py_END_ALLOW_THREADS |
| 4629 | if (pid == -1) |
| 4630 | return posix_error(); |
| 4631 | else |
| 4632 | /* shift the status left a byte so this is more like the |
| 4633 | POSIX waitpid */ |
| 4634 | return Py_BuildValue("ii", pid, status << 8); |
| 4635 | } |
| 4636 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4637 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4638 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4639 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4640 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4641 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4643 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4644 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4645 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4646 | int pid; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4647 | #ifdef UNION_WAIT |
| 4648 | union wait status; |
| 4649 | #define status_i (status.w_status) |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4650 | #else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4651 | int status; |
| 4652 | #define status_i status |
Guido van Rossum | b9f866c | 1997-05-22 15:12:39 +0000 | [diff] [blame] | 4653 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4654 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4655 | status_i = 0; |
| 4656 | Py_BEGIN_ALLOW_THREADS |
| 4657 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4658 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4659 | if (pid == -1) |
| 4660 | return posix_error(); |
| 4661 | else |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4662 | return Py_BuildValue("ii", pid, status_i); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4663 | #undef status_i |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4664 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4665 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4666 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4668 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4669 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4670 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4671 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4672 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4673 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4674 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4675 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4676 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4677 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4678 | #ifdef MS_WINDOWS |
Mark Hammond | 7edd0a9 | 2003-08-06 02:46:58 +0000 | [diff] [blame] | 4679 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", _wstati64); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4680 | #else |
| 4681 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 4682 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4683 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4686 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4687 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4688 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4689 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4690 | 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] | 4691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4692 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4693 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4694 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4695 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4696 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4697 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4698 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4699 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4700 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4701 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4702 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4703 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 4704 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4705 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4706 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4707 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4708 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4709 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4710 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4711 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4712 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4713 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4714 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4715 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4716 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4717 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4718 | return posix_2str(args, "etet:symlink", symlink, NULL, NULL); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4719 | } |
| 4720 | #endif /* HAVE_SYMLINK */ |
| 4721 | |
| 4722 | |
| 4723 | #ifdef HAVE_TIMES |
| 4724 | #ifndef HZ |
| 4725 | #define HZ 60 /* Universal constant :-) */ |
| 4726 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4727 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4728 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4729 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4730 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4731 | { |
| 4732 | ULONG value = 0; |
| 4733 | |
| 4734 | Py_BEGIN_ALLOW_THREADS |
| 4735 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4736 | Py_END_ALLOW_THREADS |
| 4737 | |
| 4738 | return value; |
| 4739 | } |
| 4740 | |
| 4741 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4742 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4743 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4744 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4745 | return Py_BuildValue("ddddd", |
| 4746 | (double)0 /* t.tms_utime / HZ */, |
| 4747 | (double)0 /* t.tms_stime / HZ */, |
| 4748 | (double)0 /* t.tms_cutime / HZ */, |
| 4749 | (double)0 /* t.tms_cstime / HZ */, |
| 4750 | (double)system_uptime() / 1000); |
| 4751 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4752 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4753 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4754 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4755 | { |
| 4756 | struct tms t; |
| 4757 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4758 | errno = 0; |
| 4759 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4760 | if (c == (clock_t) -1) |
| 4761 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4762 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4763 | (double)t.tms_utime / HZ, |
| 4764 | (double)t.tms_stime / HZ, |
| 4765 | (double)t.tms_cutime / HZ, |
| 4766 | (double)t.tms_cstime / HZ, |
| 4767 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4768 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4769 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4770 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4771 | |
| 4772 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4773 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4774 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4775 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4776 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4777 | { |
| 4778 | FILETIME create, exit, kernel, user; |
| 4779 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4780 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4781 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4782 | /* The fields of a FILETIME structure are the hi and lo part |
| 4783 | of a 64-bit value expressed in 100 nanosecond units. |
| 4784 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4785 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4786 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4787 | return Py_BuildValue( |
| 4788 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4789 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4790 | kernel.dwLowDateTime*1e-7), |
| 4791 | (double)(user.dwHighDateTime*429.4967296 + |
| 4792 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4793 | (double)0, |
| 4794 | (double)0, |
| 4795 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4796 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4797 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4798 | |
| 4799 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4800 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4801 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4802 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4803 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4805 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4806 | #ifdef HAVE_GETSID |
| 4807 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4808 | "getsid(pid) -> sid\n\n\ |
| 4809 | Call the system call getsid()."); |
| 4810 | |
| 4811 | static PyObject * |
| 4812 | posix_getsid(PyObject *self, PyObject *args) |
| 4813 | { |
| 4814 | int pid, sid; |
| 4815 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 4816 | return NULL; |
| 4817 | sid = getsid(pid); |
| 4818 | if (sid < 0) |
| 4819 | return posix_error(); |
| 4820 | return PyInt_FromLong((long)sid); |
| 4821 | } |
| 4822 | #endif /* HAVE_GETSID */ |
| 4823 | |
| 4824 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4825 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4826 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4827 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4828 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4829 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4830 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4831 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4832 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4833 | if (setsid() < 0) |
| 4834 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4835 | Py_INCREF(Py_None); |
| 4836 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4837 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4838 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4839 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4840 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4841 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4842 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4843 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4844 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4845 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4846 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4847 | { |
| 4848 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4849 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4850 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4851 | if (setpgid(pid, pgrp) < 0) |
| 4852 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4853 | Py_INCREF(Py_None); |
| 4854 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4855 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4856 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4857 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4858 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4859 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4860 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4861 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4862 | 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] | 4863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4865 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4866 | { |
| 4867 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4868 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4869 | return NULL; |
| 4870 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4871 | if (pgid < 0) |
| 4872 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4873 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4874 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4875 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4876 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4877 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4878 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4879 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4880 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4881 | 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] | 4882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4883 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4884 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4885 | { |
| 4886 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4887 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4888 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4889 | if (tcsetpgrp(fd, pgid) < 0) |
| 4890 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4891 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4892 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4893 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4894 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4895 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4896 | /* Functions acting on file descriptors */ |
| 4897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4898 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4899 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4900 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4901 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4902 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4903 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4904 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4905 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4906 | int flag; |
| 4907 | int mode = 0777; |
| 4908 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4909 | |
| 4910 | #ifdef MS_WINDOWS |
| 4911 | if (unicode_file_names()) { |
| 4912 | PyUnicodeObject *po; |
| 4913 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4914 | Py_BEGIN_ALLOW_THREADS |
| 4915 | /* PyUnicode_AS_UNICODE OK without thread |
| 4916 | lock as it is a simple dereference. */ |
| 4917 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4918 | Py_END_ALLOW_THREADS |
| 4919 | if (fd < 0) |
| 4920 | return posix_error(); |
| 4921 | return PyInt_FromLong((long)fd); |
| 4922 | } |
| 4923 | /* Drop the argument parsing error as narrow strings |
| 4924 | are also valid. */ |
| 4925 | PyErr_Clear(); |
| 4926 | } |
| 4927 | #endif |
| 4928 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4929 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4930 | Py_FileSystemDefaultEncoding, &file, |
| 4931 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4932 | return NULL; |
| 4933 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4934 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4935 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4936 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4937 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4938 | return posix_error_with_allocated_filename(file); |
| 4939 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4940 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4944 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4945 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4946 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4947 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4949 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4950 | { |
| 4951 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4952 | if (!PyArg_ParseTuple(args, "i:close", &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 |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4955 | res = close(fd); |
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(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4959 | Py_INCREF(Py_None); |
| 4960 | return Py_None; |
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_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4965 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | Return a duplicate of 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_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4970 | { |
| 4971 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4972 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4973 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4974 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4975 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4976 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4977 | if (fd < 0) |
| 4978 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4979 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4983 | PyDoc_STRVAR(posix_dup2__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4984 | "dup2(fd, fd2)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4985 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4988 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4989 | { |
| 4990 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4991 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4992 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4993 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4994 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4995 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4996 | if (res < 0) |
| 4997 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4998 | Py_INCREF(Py_None); |
| 4999 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5003 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5004 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5005 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5006 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5007 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5008 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5009 | { |
| 5010 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5011 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5012 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5013 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5014 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5015 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5016 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5017 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5018 | return NULL; |
| 5019 | #ifdef SEEK_SET |
| 5020 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5021 | switch (how) { |
| 5022 | case 0: how = SEEK_SET; break; |
| 5023 | case 1: how = SEEK_CUR; break; |
| 5024 | case 2: how = SEEK_END; break; |
| 5025 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5026 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5027 | |
| 5028 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5029 | pos = PyInt_AsLong(posobj); |
| 5030 | #else |
| 5031 | pos = PyLong_Check(posobj) ? |
| 5032 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5033 | #endif |
| 5034 | if (PyErr_Occurred()) |
| 5035 | return NULL; |
| 5036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5037 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5038 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5039 | res = _lseeki64(fd, pos, how); |
| 5040 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5041 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5042 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5043 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5044 | if (res < 0) |
| 5045 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5046 | |
| 5047 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5048 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5049 | #else |
| 5050 | return PyLong_FromLongLong(res); |
| 5051 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5055 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5056 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5057 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5058 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5059 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5060 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5061 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5062 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5063 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5064 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5065 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5066 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5067 | if (buffer == NULL) |
| 5068 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5069 | Py_BEGIN_ALLOW_THREADS |
| 5070 | n = read(fd, PyString_AsString(buffer), size); |
| 5071 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5072 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5073 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5074 | return posix_error(); |
| 5075 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5076 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5077 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5078 | return buffer; |
| 5079 | } |
| 5080 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5081 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5082 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5083 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5084 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5085 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5086 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5087 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5088 | { |
| 5089 | int fd, size; |
| 5090 | char *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5091 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5092 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5093 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5094 | size = write(fd, buffer, size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5095 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5096 | if (size < 0) |
| 5097 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5098 | return PyInt_FromLong((long)size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5102 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5103 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5104 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5105 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5107 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5108 | { |
| 5109 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5110 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5111 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5112 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5113 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5114 | #ifdef __VMS |
| 5115 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5116 | fsync(fd); |
| 5117 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5118 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5119 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5120 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5121 | if (res != 0) |
| 5122 | return posix_error(); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5123 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5124 | return _pystat_fromstructstat(st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5128 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5129 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5130 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5131 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5132 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5133 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5134 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5135 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5136 | char *mode = "r"; |
| 5137 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5138 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5139 | PyObject *f; |
| 5140 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5141 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5142 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 5143 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 5144 | PyErr_Format(PyExc_ValueError, |
| 5145 | "invalid file mode '%s'", mode); |
| 5146 | return NULL; |
| 5147 | } |
| 5148 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5149 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5150 | fp = fdopen(fd, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5151 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5152 | if (fp == NULL) |
| 5153 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 5154 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5155 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5156 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5157 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5160 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5161 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5162 | 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] | 5163 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5164 | |
| 5165 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5166 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5167 | { |
| 5168 | int fd; |
| 5169 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5170 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5171 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5172 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5173 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5174 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5175 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5176 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5177 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5178 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5179 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5180 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5181 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5182 | #if defined(PYOS_OS2) |
| 5183 | HFILE read, write; |
| 5184 | APIRET rc; |
| 5185 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5186 | Py_BEGIN_ALLOW_THREADS |
| 5187 | rc = DosCreatePipe( &read, &write, 4096); |
| 5188 | Py_END_ALLOW_THREADS |
| 5189 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5190 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5191 | |
| 5192 | return Py_BuildValue("(ii)", read, write); |
| 5193 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5194 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5195 | int fds[2]; |
| 5196 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5197 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5198 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5199 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5200 | if (res != 0) |
| 5201 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5202 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5203 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5204 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5205 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5206 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5207 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5208 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5209 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5210 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5211 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5212 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5213 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5214 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5215 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5216 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5217 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5218 | #endif /* HAVE_PIPE */ |
| 5219 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5220 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5221 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5222 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5223 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5224 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5225 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5226 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5227 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5228 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5229 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5230 | int mode = 0666; |
| 5231 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5232 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5233 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5234 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5235 | res = mkfifo(filename, mode); |
| 5236 | Py_END_ALLOW_THREADS |
| 5237 | if (res < 0) |
| 5238 | return posix_error(); |
| 5239 | Py_INCREF(Py_None); |
| 5240 | return Py_None; |
| 5241 | } |
| 5242 | #endif |
| 5243 | |
| 5244 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5245 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5246 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5247 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5248 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5249 | named filename. mode specifies both the permissions to use and the\n\ |
| 5250 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5251 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\ |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5252 | device defines the newly created device special file (probably using\n\ |
| 5253 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5254 | |
| 5255 | |
| 5256 | static PyObject * |
| 5257 | posix_mknod(PyObject *self, PyObject *args) |
| 5258 | { |
| 5259 | char *filename; |
| 5260 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5261 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5262 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5263 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5264 | return NULL; |
| 5265 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5266 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5267 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5268 | if (res < 0) |
| 5269 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5270 | Py_INCREF(Py_None); |
| 5271 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5272 | } |
| 5273 | #endif |
| 5274 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5275 | #ifdef HAVE_DEVICE_MACROS |
| 5276 | PyDoc_STRVAR(posix_major__doc__, |
| 5277 | "major(device) -> major number\n\ |
| 5278 | Extracts a device major number from a raw device number."); |
| 5279 | |
| 5280 | static PyObject * |
| 5281 | posix_major(PyObject *self, PyObject *args) |
| 5282 | { |
| 5283 | int device; |
| 5284 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5285 | return NULL; |
| 5286 | return PyInt_FromLong((long)major(device)); |
| 5287 | } |
| 5288 | |
| 5289 | PyDoc_STRVAR(posix_minor__doc__, |
| 5290 | "minor(device) -> minor number\n\ |
| 5291 | Extracts a device minor number from a raw device number."); |
| 5292 | |
| 5293 | static PyObject * |
| 5294 | posix_minor(PyObject *self, PyObject *args) |
| 5295 | { |
| 5296 | int device; |
| 5297 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5298 | return NULL; |
| 5299 | return PyInt_FromLong((long)minor(device)); |
| 5300 | } |
| 5301 | |
| 5302 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5303 | "makedev(major, minor) -> device number\n\ |
| 5304 | Composes a raw device number from the major and minor device numbers."); |
| 5305 | |
| 5306 | static PyObject * |
| 5307 | posix_makedev(PyObject *self, PyObject *args) |
| 5308 | { |
| 5309 | int major, minor; |
| 5310 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5311 | return NULL; |
| 5312 | return PyInt_FromLong((long)makedev(major, minor)); |
| 5313 | } |
| 5314 | #endif /* device macros */ |
| 5315 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5316 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5317 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5318 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5319 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5320 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5321 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5322 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5323 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5324 | { |
| 5325 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5326 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5327 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5328 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5329 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5330 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5331 | return NULL; |
| 5332 | |
| 5333 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5334 | length = PyInt_AsLong(lenobj); |
| 5335 | #else |
| 5336 | length = PyLong_Check(lenobj) ? |
| 5337 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 5338 | #endif |
| 5339 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5340 | return NULL; |
| 5341 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5342 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5343 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5344 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5345 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5346 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5347 | return NULL; |
| 5348 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5349 | Py_INCREF(Py_None); |
| 5350 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5351 | } |
| 5352 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5353 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5354 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5355 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5356 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5357 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5358 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5359 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5360 | * get re-set with another call for the same key. */ |
| 5361 | static PyObject *posix_putenv_garbage; |
| 5362 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5363 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5364 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5365 | { |
| 5366 | char *s1, *s2; |
| 5367 | char *new; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5368 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5369 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5370 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5371 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5372 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5373 | |
| 5374 | #if defined(PYOS_OS2) |
| 5375 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5376 | APIRET rc; |
| 5377 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5378 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5379 | if (rc != NO_ERROR) |
| 5380 | return os2_error(rc); |
| 5381 | |
| 5382 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5383 | APIRET rc; |
| 5384 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5385 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5386 | if (rc != NO_ERROR) |
| 5387 | return os2_error(rc); |
| 5388 | } else { |
| 5389 | #endif |
| 5390 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5391 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5392 | len = strlen(s1) + strlen(s2) + 2; |
| 5393 | /* len includes space for a trailing \0; the size arg to |
| 5394 | PyString_FromStringAndSize does not count that */ |
| 5395 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5396 | if (newstr == NULL) |
| 5397 | return PyErr_NoMemory(); |
| 5398 | new = PyString_AS_STRING(newstr); |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5399 | PyOS_snprintf(new, len, "%s=%s", s1, s2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5400 | if (putenv(new)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5401 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5402 | posix_error(); |
| 5403 | return NULL; |
| 5404 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5405 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5406 | * this will cause previous value to be collected. This has to |
| 5407 | * happen after the real putenv() call because the old value |
| 5408 | * was still accessible until then. */ |
| 5409 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5410 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5411 | /* really not much we can do; just leak */ |
| 5412 | PyErr_Clear(); |
| 5413 | } |
| 5414 | else { |
| 5415 | Py_DECREF(newstr); |
| 5416 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5417 | |
| 5418 | #if defined(PYOS_OS2) |
| 5419 | } |
| 5420 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5421 | Py_INCREF(Py_None); |
| 5422 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5423 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5424 | #endif /* putenv */ |
| 5425 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5426 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5427 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5428 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5429 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5430 | |
| 5431 | static PyObject * |
| 5432 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5433 | { |
| 5434 | char *s1; |
| 5435 | |
| 5436 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5437 | return NULL; |
| 5438 | |
| 5439 | unsetenv(s1); |
| 5440 | |
| 5441 | /* Remove the key from posix_putenv_garbage; |
| 5442 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5443 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5444 | * old value was still accessible until then. |
| 5445 | */ |
| 5446 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5447 | PyTuple_GET_ITEM(args, 0))) { |
| 5448 | /* really not much we can do; just leak */ |
| 5449 | PyErr_Clear(); |
| 5450 | } |
| 5451 | |
| 5452 | Py_INCREF(Py_None); |
| 5453 | return Py_None; |
| 5454 | } |
| 5455 | #endif /* unsetenv */ |
| 5456 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5457 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5458 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5459 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5460 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5461 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5462 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5463 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5464 | { |
| 5465 | int code; |
| 5466 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5467 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5468 | return NULL; |
| 5469 | message = strerror(code); |
| 5470 | if (message == NULL) { |
| 5471 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5472 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5473 | return NULL; |
| 5474 | } |
| 5475 | return PyString_FromString(message); |
| 5476 | } |
| 5477 | #endif /* strerror */ |
| 5478 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5479 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5480 | #ifdef HAVE_SYS_WAIT_H |
| 5481 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5482 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5483 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5484 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5485 | 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] | 5486 | |
| 5487 | static PyObject * |
| 5488 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5489 | { |
| 5490 | #ifdef UNION_WAIT |
| 5491 | union wait status; |
| 5492 | #define status_i (status.w_status) |
| 5493 | #else |
| 5494 | int status; |
| 5495 | #define status_i status |
| 5496 | #endif |
| 5497 | status_i = 0; |
| 5498 | |
| 5499 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &status_i)) |
| 5500 | { |
| 5501 | return NULL; |
| 5502 | } |
| 5503 | |
| 5504 | return PyBool_FromLong(WCOREDUMP(status)); |
| 5505 | #undef status_i |
| 5506 | } |
| 5507 | #endif /* WCOREDUMP */ |
| 5508 | |
| 5509 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5510 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5511 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5512 | 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] | 5513 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5514 | |
| 5515 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5516 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5517 | { |
| 5518 | #ifdef UNION_WAIT |
| 5519 | union wait status; |
| 5520 | #define status_i (status.w_status) |
| 5521 | #else |
| 5522 | int status; |
| 5523 | #define status_i status |
| 5524 | #endif |
| 5525 | status_i = 0; |
| 5526 | |
| 5527 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &status_i)) |
| 5528 | { |
| 5529 | return NULL; |
| 5530 | } |
| 5531 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5532 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5533 | #undef status_i |
| 5534 | } |
| 5535 | #endif /* WIFCONTINUED */ |
| 5536 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5537 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5538 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5539 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5540 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5541 | |
| 5542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5543 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5544 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5545 | #ifdef UNION_WAIT |
| 5546 | union wait status; |
| 5547 | #define status_i (status.w_status) |
| 5548 | #else |
| 5549 | int status; |
| 5550 | #define status_i status |
| 5551 | #endif |
| 5552 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5553 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5554 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5555 | { |
| 5556 | return NULL; |
| 5557 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5558 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5559 | return PyBool_FromLong(WIFSTOPPED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5560 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5561 | } |
| 5562 | #endif /* WIFSTOPPED */ |
| 5563 | |
| 5564 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5565 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5566 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5567 | 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] | 5568 | |
| 5569 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5570 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5571 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5572 | #ifdef UNION_WAIT |
| 5573 | union wait status; |
| 5574 | #define status_i (status.w_status) |
| 5575 | #else |
| 5576 | int status; |
| 5577 | #define status_i status |
| 5578 | #endif |
| 5579 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5580 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5581 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5582 | { |
| 5583 | return NULL; |
| 5584 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5585 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5586 | return PyBool_FromLong(WIFSIGNALED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5587 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5588 | } |
| 5589 | #endif /* WIFSIGNALED */ |
| 5590 | |
| 5591 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5592 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5593 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5594 | 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] | 5595 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5596 | |
| 5597 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5598 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5599 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5600 | #ifdef UNION_WAIT |
| 5601 | union wait status; |
| 5602 | #define status_i (status.w_status) |
| 5603 | #else |
| 5604 | int status; |
| 5605 | #define status_i status |
| 5606 | #endif |
| 5607 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5608 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5609 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5610 | { |
| 5611 | return NULL; |
| 5612 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5613 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5614 | return PyBool_FromLong(WIFEXITED(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5615 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5616 | } |
| 5617 | #endif /* WIFEXITED */ |
| 5618 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5619 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5620 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5621 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5622 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5623 | |
| 5624 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5625 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5626 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5627 | #ifdef UNION_WAIT |
| 5628 | union wait status; |
| 5629 | #define status_i (status.w_status) |
| 5630 | #else |
| 5631 | int status; |
| 5632 | #define status_i status |
| 5633 | #endif |
| 5634 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5635 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5636 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5637 | { |
| 5638 | return NULL; |
| 5639 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5640 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5641 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5642 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5643 | } |
| 5644 | #endif /* WEXITSTATUS */ |
| 5645 | |
| 5646 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5647 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5648 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5649 | 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] | 5650 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5651 | |
| 5652 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5653 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5654 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5655 | #ifdef UNION_WAIT |
| 5656 | union wait status; |
| 5657 | #define status_i (status.w_status) |
| 5658 | #else |
| 5659 | int status; |
| 5660 | #define status_i status |
| 5661 | #endif |
| 5662 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5663 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5664 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5665 | { |
| 5666 | return NULL; |
| 5667 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5668 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5669 | return Py_BuildValue("i", WTERMSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5670 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5671 | } |
| 5672 | #endif /* WTERMSIG */ |
| 5673 | |
| 5674 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5675 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5676 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5677 | Return the signal that stopped the process that provided\n\ |
| 5678 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5679 | |
| 5680 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5681 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5682 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5683 | #ifdef UNION_WAIT |
| 5684 | union wait status; |
| 5685 | #define status_i (status.w_status) |
| 5686 | #else |
| 5687 | int status; |
| 5688 | #define status_i status |
| 5689 | #endif |
| 5690 | status_i = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5691 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5692 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &status_i)) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5693 | { |
| 5694 | return NULL; |
| 5695 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5696 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5697 | return Py_BuildValue("i", WSTOPSIG(status)); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5698 | #undef status_i |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5699 | } |
| 5700 | #endif /* WSTOPSIG */ |
| 5701 | |
| 5702 | #endif /* HAVE_SYS_WAIT_H */ |
| 5703 | |
| 5704 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5705 | #if defined(HAVE_FSTATVFS) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5706 | #ifdef _SCO_DS |
| 5707 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5708 | needed definitions in sys/statvfs.h */ |
| 5709 | #define _SVID3 |
| 5710 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5711 | #include <sys/statvfs.h> |
| 5712 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5713 | static PyObject* |
| 5714 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5715 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5716 | if (v == NULL) |
| 5717 | return NULL; |
| 5718 | |
| 5719 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5720 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5721 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 5722 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 5723 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 5724 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 5725 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 5726 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 5727 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 5728 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5729 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5730 | #else |
| 5731 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 5732 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5733 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5734 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5735 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5736 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5737 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5738 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5739 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5740 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5741 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5742 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5743 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5744 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5745 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 5746 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 5747 | #endif |
| 5748 | |
| 5749 | return v; |
| 5750 | } |
| 5751 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5752 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5753 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5754 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5755 | |
| 5756 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5757 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5758 | { |
| 5759 | int fd, res; |
| 5760 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5761 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5762 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5763 | return NULL; |
| 5764 | Py_BEGIN_ALLOW_THREADS |
| 5765 | res = fstatvfs(fd, &st); |
| 5766 | Py_END_ALLOW_THREADS |
| 5767 | if (res != 0) |
| 5768 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5769 | |
| 5770 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5771 | } |
| 5772 | #endif /* HAVE_FSTATVFS */ |
| 5773 | |
| 5774 | |
| 5775 | #if defined(HAVE_STATVFS) |
| 5776 | #include <sys/statvfs.h> |
| 5777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5778 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5779 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5780 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5781 | |
| 5782 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5783 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5784 | { |
| 5785 | char *path; |
| 5786 | int res; |
| 5787 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5788 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5789 | return NULL; |
| 5790 | Py_BEGIN_ALLOW_THREADS |
| 5791 | res = statvfs(path, &st); |
| 5792 | Py_END_ALLOW_THREADS |
| 5793 | if (res != 0) |
| 5794 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5795 | |
| 5796 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5797 | } |
| 5798 | #endif /* HAVE_STATVFS */ |
| 5799 | |
| 5800 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5801 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5802 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5803 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5804 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 5805 | 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] | 5806 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5807 | |
| 5808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5809 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5810 | { |
| 5811 | PyObject *result = NULL; |
| 5812 | char *dir = NULL; |
| 5813 | char *pfx = NULL; |
| 5814 | char *name; |
| 5815 | |
| 5816 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 5817 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5818 | |
| 5819 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5820 | "tempnam is a potential security risk to your program") < 0) |
| 5821 | return NULL; |
| 5822 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5823 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5824 | name = _tempnam(dir, pfx); |
| 5825 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5826 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 5827 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5828 | if (name == NULL) |
| 5829 | return PyErr_NoMemory(); |
| 5830 | result = PyString_FromString(name); |
| 5831 | free(name); |
| 5832 | return result; |
| 5833 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 5834 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5835 | |
| 5836 | |
| 5837 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5838 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5839 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5840 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5841 | |
| 5842 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5843 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5844 | { |
| 5845 | FILE *fp; |
| 5846 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5847 | fp = tmpfile(); |
| 5848 | if (fp == NULL) |
| 5849 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 5850 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5851 | } |
| 5852 | #endif |
| 5853 | |
| 5854 | |
| 5855 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5856 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5857 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5858 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5859 | |
| 5860 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5861 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5862 | { |
| 5863 | char buffer[L_tmpnam]; |
| 5864 | char *name; |
| 5865 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 5866 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 5867 | "tmpnam is a potential security risk to your program") < 0) |
| 5868 | return NULL; |
| 5869 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5870 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5871 | name = tmpnam_r(buffer); |
| 5872 | #else |
| 5873 | name = tmpnam(buffer); |
| 5874 | #endif |
| 5875 | if (name == NULL) { |
| 5876 | PyErr_SetObject(PyExc_OSError, |
| 5877 | Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 5878 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5879 | "unexpected NULL from tmpnam_r" |
| 5880 | #else |
| 5881 | "unexpected NULL from tmpnam" |
| 5882 | #endif |
| 5883 | )); |
| 5884 | return NULL; |
| 5885 | } |
| 5886 | return PyString_FromString(buffer); |
| 5887 | } |
| 5888 | #endif |
| 5889 | |
| 5890 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5891 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5892 | * It maps strings representing configuration variable names to |
| 5893 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5894 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5895 | * rarely-used constants. There are three separate tables that use |
| 5896 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5897 | * |
| 5898 | * This code is always included, even if none of the interfaces that |
| 5899 | * need it are included. The #if hackery needed to avoid it would be |
| 5900 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5901 | */ |
| 5902 | struct constdef { |
| 5903 | char *name; |
| 5904 | long value; |
| 5905 | }; |
| 5906 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5907 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5908 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 5909 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5910 | { |
| 5911 | if (PyInt_Check(arg)) { |
| 5912 | *valuep = PyInt_AS_LONG(arg); |
| 5913 | return 1; |
| 5914 | } |
| 5915 | if (PyString_Check(arg)) { |
| 5916 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5917 | size_t lo = 0; |
| 5918 | size_t mid; |
| 5919 | size_t hi = tablesize; |
| 5920 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5921 | char *confname = PyString_AS_STRING(arg); |
| 5922 | while (lo < hi) { |
| 5923 | mid = (lo + hi) / 2; |
| 5924 | cmp = strcmp(confname, table[mid].name); |
| 5925 | if (cmp < 0) |
| 5926 | hi = mid; |
| 5927 | else if (cmp > 0) |
| 5928 | lo = mid + 1; |
| 5929 | else { |
| 5930 | *valuep = table[mid].value; |
| 5931 | return 1; |
| 5932 | } |
| 5933 | } |
| 5934 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 5935 | } |
| 5936 | else |
| 5937 | PyErr_SetString(PyExc_TypeError, |
| 5938 | "configuration names must be strings or integers"); |
| 5939 | return 0; |
| 5940 | } |
| 5941 | |
| 5942 | |
| 5943 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5944 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5945 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5946 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5947 | #endif |
| 5948 | #ifdef _PC_ABI_ASYNC_IO |
| 5949 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5950 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5951 | #ifdef _PC_ASYNC_IO |
| 5952 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5953 | #endif |
| 5954 | #ifdef _PC_CHOWN_RESTRICTED |
| 5955 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5956 | #endif |
| 5957 | #ifdef _PC_FILESIZEBITS |
| 5958 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5959 | #endif |
| 5960 | #ifdef _PC_LAST |
| 5961 | {"PC_LAST", _PC_LAST}, |
| 5962 | #endif |
| 5963 | #ifdef _PC_LINK_MAX |
| 5964 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5965 | #endif |
| 5966 | #ifdef _PC_MAX_CANON |
| 5967 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5968 | #endif |
| 5969 | #ifdef _PC_MAX_INPUT |
| 5970 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5971 | #endif |
| 5972 | #ifdef _PC_NAME_MAX |
| 5973 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5974 | #endif |
| 5975 | #ifdef _PC_NO_TRUNC |
| 5976 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5977 | #endif |
| 5978 | #ifdef _PC_PATH_MAX |
| 5979 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5980 | #endif |
| 5981 | #ifdef _PC_PIPE_BUF |
| 5982 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5983 | #endif |
| 5984 | #ifdef _PC_PRIO_IO |
| 5985 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5986 | #endif |
| 5987 | #ifdef _PC_SOCK_MAXBUF |
| 5988 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5989 | #endif |
| 5990 | #ifdef _PC_SYNC_IO |
| 5991 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5992 | #endif |
| 5993 | #ifdef _PC_VDISABLE |
| 5994 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5995 | #endif |
| 5996 | }; |
| 5997 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5998 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5999 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6000 | { |
| 6001 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6002 | sizeof(posix_constants_pathconf) |
| 6003 | / sizeof(struct constdef)); |
| 6004 | } |
| 6005 | #endif |
| 6006 | |
| 6007 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6008 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6009 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6010 | 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] | 6011 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6012 | |
| 6013 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6014 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6015 | { |
| 6016 | PyObject *result = NULL; |
| 6017 | int name, fd; |
| 6018 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6019 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6020 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6021 | long limit; |
| 6022 | |
| 6023 | errno = 0; |
| 6024 | limit = fpathconf(fd, name); |
| 6025 | if (limit == -1 && errno != 0) |
| 6026 | posix_error(); |
| 6027 | else |
| 6028 | result = PyInt_FromLong(limit); |
| 6029 | } |
| 6030 | return result; |
| 6031 | } |
| 6032 | #endif |
| 6033 | |
| 6034 | |
| 6035 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6036 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6037 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6038 | 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] | 6039 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6040 | |
| 6041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6042 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6043 | { |
| 6044 | PyObject *result = NULL; |
| 6045 | int name; |
| 6046 | char *path; |
| 6047 | |
| 6048 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6049 | conv_path_confname, &name)) { |
| 6050 | long limit; |
| 6051 | |
| 6052 | errno = 0; |
| 6053 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6054 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6055 | if (errno == EINVAL) |
| 6056 | /* could be a path or name problem */ |
| 6057 | posix_error(); |
| 6058 | else |
| 6059 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6060 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6061 | else |
| 6062 | result = PyInt_FromLong(limit); |
| 6063 | } |
| 6064 | return result; |
| 6065 | } |
| 6066 | #endif |
| 6067 | |
| 6068 | #ifdef HAVE_CONFSTR |
| 6069 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6070 | #ifdef _CS_ARCHITECTURE |
| 6071 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6072 | #endif |
| 6073 | #ifdef _CS_HOSTNAME |
| 6074 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6075 | #endif |
| 6076 | #ifdef _CS_HW_PROVIDER |
| 6077 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6078 | #endif |
| 6079 | #ifdef _CS_HW_SERIAL |
| 6080 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6081 | #endif |
| 6082 | #ifdef _CS_INITTAB_NAME |
| 6083 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6084 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6085 | #ifdef _CS_LFS64_CFLAGS |
| 6086 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6087 | #endif |
| 6088 | #ifdef _CS_LFS64_LDFLAGS |
| 6089 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6090 | #endif |
| 6091 | #ifdef _CS_LFS64_LIBS |
| 6092 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6093 | #endif |
| 6094 | #ifdef _CS_LFS64_LINTFLAGS |
| 6095 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6096 | #endif |
| 6097 | #ifdef _CS_LFS_CFLAGS |
| 6098 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6099 | #endif |
| 6100 | #ifdef _CS_LFS_LDFLAGS |
| 6101 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6102 | #endif |
| 6103 | #ifdef _CS_LFS_LIBS |
| 6104 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6105 | #endif |
| 6106 | #ifdef _CS_LFS_LINTFLAGS |
| 6107 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6108 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6109 | #ifdef _CS_MACHINE |
| 6110 | {"CS_MACHINE", _CS_MACHINE}, |
| 6111 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6112 | #ifdef _CS_PATH |
| 6113 | {"CS_PATH", _CS_PATH}, |
| 6114 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6115 | #ifdef _CS_RELEASE |
| 6116 | {"CS_RELEASE", _CS_RELEASE}, |
| 6117 | #endif |
| 6118 | #ifdef _CS_SRPC_DOMAIN |
| 6119 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6120 | #endif |
| 6121 | #ifdef _CS_SYSNAME |
| 6122 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6123 | #endif |
| 6124 | #ifdef _CS_VERSION |
| 6125 | {"CS_VERSION", _CS_VERSION}, |
| 6126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6127 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6128 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6129 | #endif |
| 6130 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6131 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6132 | #endif |
| 6133 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6134 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6135 | #endif |
| 6136 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6137 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6138 | #endif |
| 6139 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6140 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6141 | #endif |
| 6142 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6143 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6144 | #endif |
| 6145 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6146 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6147 | #endif |
| 6148 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6149 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6150 | #endif |
| 6151 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6152 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6153 | #endif |
| 6154 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6155 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6156 | #endif |
| 6157 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6158 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6159 | #endif |
| 6160 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6161 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6162 | #endif |
| 6163 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6164 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6165 | #endif |
| 6166 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6167 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6168 | #endif |
| 6169 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6170 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6171 | #endif |
| 6172 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6173 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6174 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6175 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6176 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6177 | #endif |
| 6178 | #ifdef _MIPS_CS_BASE |
| 6179 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6180 | #endif |
| 6181 | #ifdef _MIPS_CS_HOSTID |
| 6182 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6183 | #endif |
| 6184 | #ifdef _MIPS_CS_HW_NAME |
| 6185 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6186 | #endif |
| 6187 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6188 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6189 | #endif |
| 6190 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6191 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6192 | #endif |
| 6193 | #ifdef _MIPS_CS_OSREL_MIN |
| 6194 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6195 | #endif |
| 6196 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6197 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6198 | #endif |
| 6199 | #ifdef _MIPS_CS_OS_NAME |
| 6200 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6201 | #endif |
| 6202 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6203 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6204 | #endif |
| 6205 | #ifdef _MIPS_CS_PROCESSORS |
| 6206 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6207 | #endif |
| 6208 | #ifdef _MIPS_CS_SERIAL |
| 6209 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6210 | #endif |
| 6211 | #ifdef _MIPS_CS_VENDOR |
| 6212 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6213 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6214 | }; |
| 6215 | |
| 6216 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6217 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6218 | { |
| 6219 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6220 | sizeof(posix_constants_confstr) |
| 6221 | / sizeof(struct constdef)); |
| 6222 | } |
| 6223 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6224 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6225 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6226 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6227 | |
| 6228 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6229 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6230 | { |
| 6231 | PyObject *result = NULL; |
| 6232 | int name; |
| 6233 | char buffer[64]; |
| 6234 | |
| 6235 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
| 6236 | int len = confstr(name, buffer, sizeof(buffer)); |
| 6237 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6238 | errno = 0; |
| 6239 | if (len == 0) { |
| 6240 | if (errno != 0) |
| 6241 | posix_error(); |
| 6242 | else |
| 6243 | result = PyString_FromString(""); |
| 6244 | } |
| 6245 | else { |
| 6246 | if (len >= sizeof(buffer)) { |
| 6247 | result = PyString_FromStringAndSize(NULL, len); |
| 6248 | if (result != NULL) |
| 6249 | confstr(name, PyString_AS_STRING(result), len+1); |
| 6250 | } |
| 6251 | else |
| 6252 | result = PyString_FromString(buffer); |
| 6253 | } |
| 6254 | } |
| 6255 | return result; |
| 6256 | } |
| 6257 | #endif |
| 6258 | |
| 6259 | |
| 6260 | #ifdef HAVE_SYSCONF |
| 6261 | static struct constdef posix_constants_sysconf[] = { |
| 6262 | #ifdef _SC_2_CHAR_TERM |
| 6263 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6264 | #endif |
| 6265 | #ifdef _SC_2_C_BIND |
| 6266 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6267 | #endif |
| 6268 | #ifdef _SC_2_C_DEV |
| 6269 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6270 | #endif |
| 6271 | #ifdef _SC_2_C_VERSION |
| 6272 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6273 | #endif |
| 6274 | #ifdef _SC_2_FORT_DEV |
| 6275 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6276 | #endif |
| 6277 | #ifdef _SC_2_FORT_RUN |
| 6278 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6279 | #endif |
| 6280 | #ifdef _SC_2_LOCALEDEF |
| 6281 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6282 | #endif |
| 6283 | #ifdef _SC_2_SW_DEV |
| 6284 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6285 | #endif |
| 6286 | #ifdef _SC_2_UPE |
| 6287 | {"SC_2_UPE", _SC_2_UPE}, |
| 6288 | #endif |
| 6289 | #ifdef _SC_2_VERSION |
| 6290 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6291 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6292 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6293 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6294 | #endif |
| 6295 | #ifdef _SC_ACL |
| 6296 | {"SC_ACL", _SC_ACL}, |
| 6297 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6298 | #ifdef _SC_AIO_LISTIO_MAX |
| 6299 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6300 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6301 | #ifdef _SC_AIO_MAX |
| 6302 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6303 | #endif |
| 6304 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6305 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6306 | #endif |
| 6307 | #ifdef _SC_ARG_MAX |
| 6308 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6309 | #endif |
| 6310 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6311 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6312 | #endif |
| 6313 | #ifdef _SC_ATEXIT_MAX |
| 6314 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6315 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6316 | #ifdef _SC_AUDIT |
| 6317 | {"SC_AUDIT", _SC_AUDIT}, |
| 6318 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6319 | #ifdef _SC_AVPHYS_PAGES |
| 6320 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6321 | #endif |
| 6322 | #ifdef _SC_BC_BASE_MAX |
| 6323 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6324 | #endif |
| 6325 | #ifdef _SC_BC_DIM_MAX |
| 6326 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6327 | #endif |
| 6328 | #ifdef _SC_BC_SCALE_MAX |
| 6329 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6330 | #endif |
| 6331 | #ifdef _SC_BC_STRING_MAX |
| 6332 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6333 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6334 | #ifdef _SC_CAP |
| 6335 | {"SC_CAP", _SC_CAP}, |
| 6336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6337 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6338 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6339 | #endif |
| 6340 | #ifdef _SC_CHAR_BIT |
| 6341 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6342 | #endif |
| 6343 | #ifdef _SC_CHAR_MAX |
| 6344 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6345 | #endif |
| 6346 | #ifdef _SC_CHAR_MIN |
| 6347 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6348 | #endif |
| 6349 | #ifdef _SC_CHILD_MAX |
| 6350 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6351 | #endif |
| 6352 | #ifdef _SC_CLK_TCK |
| 6353 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6354 | #endif |
| 6355 | #ifdef _SC_COHER_BLKSZ |
| 6356 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6357 | #endif |
| 6358 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6359 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6360 | #endif |
| 6361 | #ifdef _SC_DCACHE_ASSOC |
| 6362 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6363 | #endif |
| 6364 | #ifdef _SC_DCACHE_BLKSZ |
| 6365 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6366 | #endif |
| 6367 | #ifdef _SC_DCACHE_LINESZ |
| 6368 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6369 | #endif |
| 6370 | #ifdef _SC_DCACHE_SZ |
| 6371 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6372 | #endif |
| 6373 | #ifdef _SC_DCACHE_TBLKSZ |
| 6374 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6375 | #endif |
| 6376 | #ifdef _SC_DELAYTIMER_MAX |
| 6377 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6378 | #endif |
| 6379 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6380 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6381 | #endif |
| 6382 | #ifdef _SC_EXPR_NEST_MAX |
| 6383 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6384 | #endif |
| 6385 | #ifdef _SC_FSYNC |
| 6386 | {"SC_FSYNC", _SC_FSYNC}, |
| 6387 | #endif |
| 6388 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6389 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6390 | #endif |
| 6391 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6392 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6393 | #endif |
| 6394 | #ifdef _SC_ICACHE_ASSOC |
| 6395 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6396 | #endif |
| 6397 | #ifdef _SC_ICACHE_BLKSZ |
| 6398 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6399 | #endif |
| 6400 | #ifdef _SC_ICACHE_LINESZ |
| 6401 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6402 | #endif |
| 6403 | #ifdef _SC_ICACHE_SZ |
| 6404 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6405 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6406 | #ifdef _SC_INF |
| 6407 | {"SC_INF", _SC_INF}, |
| 6408 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6409 | #ifdef _SC_INT_MAX |
| 6410 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6411 | #endif |
| 6412 | #ifdef _SC_INT_MIN |
| 6413 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6414 | #endif |
| 6415 | #ifdef _SC_IOV_MAX |
| 6416 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6417 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6418 | #ifdef _SC_IP_SECOPTS |
| 6419 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6420 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6421 | #ifdef _SC_JOB_CONTROL |
| 6422 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6423 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6424 | #ifdef _SC_KERN_POINTERS |
| 6425 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6426 | #endif |
| 6427 | #ifdef _SC_KERN_SIM |
| 6428 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6429 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6430 | #ifdef _SC_LINE_MAX |
| 6431 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6432 | #endif |
| 6433 | #ifdef _SC_LOGIN_NAME_MAX |
| 6434 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6435 | #endif |
| 6436 | #ifdef _SC_LOGNAME_MAX |
| 6437 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6438 | #endif |
| 6439 | #ifdef _SC_LONG_BIT |
| 6440 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6441 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6442 | #ifdef _SC_MAC |
| 6443 | {"SC_MAC", _SC_MAC}, |
| 6444 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6445 | #ifdef _SC_MAPPED_FILES |
| 6446 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6447 | #endif |
| 6448 | #ifdef _SC_MAXPID |
| 6449 | {"SC_MAXPID", _SC_MAXPID}, |
| 6450 | #endif |
| 6451 | #ifdef _SC_MB_LEN_MAX |
| 6452 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6453 | #endif |
| 6454 | #ifdef _SC_MEMLOCK |
| 6455 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6456 | #endif |
| 6457 | #ifdef _SC_MEMLOCK_RANGE |
| 6458 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6459 | #endif |
| 6460 | #ifdef _SC_MEMORY_PROTECTION |
| 6461 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6462 | #endif |
| 6463 | #ifdef _SC_MESSAGE_PASSING |
| 6464 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6465 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6466 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6467 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6468 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6469 | #ifdef _SC_MQ_OPEN_MAX |
| 6470 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6471 | #endif |
| 6472 | #ifdef _SC_MQ_PRIO_MAX |
| 6473 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6474 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6475 | #ifdef _SC_NACLS_MAX |
| 6476 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6477 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6478 | #ifdef _SC_NGROUPS_MAX |
| 6479 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6480 | #endif |
| 6481 | #ifdef _SC_NL_ARGMAX |
| 6482 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6483 | #endif |
| 6484 | #ifdef _SC_NL_LANGMAX |
| 6485 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6486 | #endif |
| 6487 | #ifdef _SC_NL_MSGMAX |
| 6488 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6489 | #endif |
| 6490 | #ifdef _SC_NL_NMAX |
| 6491 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6492 | #endif |
| 6493 | #ifdef _SC_NL_SETMAX |
| 6494 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6495 | #endif |
| 6496 | #ifdef _SC_NL_TEXTMAX |
| 6497 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6498 | #endif |
| 6499 | #ifdef _SC_NPROCESSORS_CONF |
| 6500 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6501 | #endif |
| 6502 | #ifdef _SC_NPROCESSORS_ONLN |
| 6503 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6504 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6505 | #ifdef _SC_NPROC_CONF |
| 6506 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6507 | #endif |
| 6508 | #ifdef _SC_NPROC_ONLN |
| 6509 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6510 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6511 | #ifdef _SC_NZERO |
| 6512 | {"SC_NZERO", _SC_NZERO}, |
| 6513 | #endif |
| 6514 | #ifdef _SC_OPEN_MAX |
| 6515 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6516 | #endif |
| 6517 | #ifdef _SC_PAGESIZE |
| 6518 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6519 | #endif |
| 6520 | #ifdef _SC_PAGE_SIZE |
| 6521 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6522 | #endif |
| 6523 | #ifdef _SC_PASS_MAX |
| 6524 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6525 | #endif |
| 6526 | #ifdef _SC_PHYS_PAGES |
| 6527 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6528 | #endif |
| 6529 | #ifdef _SC_PII |
| 6530 | {"SC_PII", _SC_PII}, |
| 6531 | #endif |
| 6532 | #ifdef _SC_PII_INTERNET |
| 6533 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6534 | #endif |
| 6535 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6536 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6537 | #endif |
| 6538 | #ifdef _SC_PII_INTERNET_STREAM |
| 6539 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6540 | #endif |
| 6541 | #ifdef _SC_PII_OSI |
| 6542 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6543 | #endif |
| 6544 | #ifdef _SC_PII_OSI_CLTS |
| 6545 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6546 | #endif |
| 6547 | #ifdef _SC_PII_OSI_COTS |
| 6548 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6549 | #endif |
| 6550 | #ifdef _SC_PII_OSI_M |
| 6551 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6552 | #endif |
| 6553 | #ifdef _SC_PII_SOCKET |
| 6554 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6555 | #endif |
| 6556 | #ifdef _SC_PII_XTI |
| 6557 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6558 | #endif |
| 6559 | #ifdef _SC_POLL |
| 6560 | {"SC_POLL", _SC_POLL}, |
| 6561 | #endif |
| 6562 | #ifdef _SC_PRIORITIZED_IO |
| 6563 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6564 | #endif |
| 6565 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6566 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6567 | #endif |
| 6568 | #ifdef _SC_REALTIME_SIGNALS |
| 6569 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6570 | #endif |
| 6571 | #ifdef _SC_RE_DUP_MAX |
| 6572 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6573 | #endif |
| 6574 | #ifdef _SC_RTSIG_MAX |
| 6575 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6576 | #endif |
| 6577 | #ifdef _SC_SAVED_IDS |
| 6578 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6579 | #endif |
| 6580 | #ifdef _SC_SCHAR_MAX |
| 6581 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6582 | #endif |
| 6583 | #ifdef _SC_SCHAR_MIN |
| 6584 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6585 | #endif |
| 6586 | #ifdef _SC_SELECT |
| 6587 | {"SC_SELECT", _SC_SELECT}, |
| 6588 | #endif |
| 6589 | #ifdef _SC_SEMAPHORES |
| 6590 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6591 | #endif |
| 6592 | #ifdef _SC_SEM_NSEMS_MAX |
| 6593 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6594 | #endif |
| 6595 | #ifdef _SC_SEM_VALUE_MAX |
| 6596 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6597 | #endif |
| 6598 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6599 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6600 | #endif |
| 6601 | #ifdef _SC_SHRT_MAX |
| 6602 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6603 | #endif |
| 6604 | #ifdef _SC_SHRT_MIN |
| 6605 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6606 | #endif |
| 6607 | #ifdef _SC_SIGQUEUE_MAX |
| 6608 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6609 | #endif |
| 6610 | #ifdef _SC_SIGRT_MAX |
| 6611 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6612 | #endif |
| 6613 | #ifdef _SC_SIGRT_MIN |
| 6614 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6615 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6616 | #ifdef _SC_SOFTPOWER |
| 6617 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6618 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6619 | #ifdef _SC_SPLIT_CACHE |
| 6620 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6621 | #endif |
| 6622 | #ifdef _SC_SSIZE_MAX |
| 6623 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6624 | #endif |
| 6625 | #ifdef _SC_STACK_PROT |
| 6626 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6627 | #endif |
| 6628 | #ifdef _SC_STREAM_MAX |
| 6629 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6630 | #endif |
| 6631 | #ifdef _SC_SYNCHRONIZED_IO |
| 6632 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6633 | #endif |
| 6634 | #ifdef _SC_THREADS |
| 6635 | {"SC_THREADS", _SC_THREADS}, |
| 6636 | #endif |
| 6637 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6638 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6639 | #endif |
| 6640 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6641 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6642 | #endif |
| 6643 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6644 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6645 | #endif |
| 6646 | #ifdef _SC_THREAD_KEYS_MAX |
| 6647 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6648 | #endif |
| 6649 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6650 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6651 | #endif |
| 6652 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6653 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6654 | #endif |
| 6655 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6656 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6657 | #endif |
| 6658 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6659 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6660 | #endif |
| 6661 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6662 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6663 | #endif |
| 6664 | #ifdef _SC_THREAD_STACK_MIN |
| 6665 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6666 | #endif |
| 6667 | #ifdef _SC_THREAD_THREADS_MAX |
| 6668 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6669 | #endif |
| 6670 | #ifdef _SC_TIMERS |
| 6671 | {"SC_TIMERS", _SC_TIMERS}, |
| 6672 | #endif |
| 6673 | #ifdef _SC_TIMER_MAX |
| 6674 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6675 | #endif |
| 6676 | #ifdef _SC_TTY_NAME_MAX |
| 6677 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6678 | #endif |
| 6679 | #ifdef _SC_TZNAME_MAX |
| 6680 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6681 | #endif |
| 6682 | #ifdef _SC_T_IOV_MAX |
| 6683 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6684 | #endif |
| 6685 | #ifdef _SC_UCHAR_MAX |
| 6686 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6687 | #endif |
| 6688 | #ifdef _SC_UINT_MAX |
| 6689 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6690 | #endif |
| 6691 | #ifdef _SC_UIO_MAXIOV |
| 6692 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6693 | #endif |
| 6694 | #ifdef _SC_ULONG_MAX |
| 6695 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6696 | #endif |
| 6697 | #ifdef _SC_USHRT_MAX |
| 6698 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6699 | #endif |
| 6700 | #ifdef _SC_VERSION |
| 6701 | {"SC_VERSION", _SC_VERSION}, |
| 6702 | #endif |
| 6703 | #ifdef _SC_WORD_BIT |
| 6704 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6705 | #endif |
| 6706 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6707 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6708 | #endif |
| 6709 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6710 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6711 | #endif |
| 6712 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6713 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6714 | #endif |
| 6715 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6716 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6717 | #endif |
| 6718 | #ifdef _SC_XOPEN_CRYPT |
| 6719 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6720 | #endif |
| 6721 | #ifdef _SC_XOPEN_ENH_I18N |
| 6722 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6723 | #endif |
| 6724 | #ifdef _SC_XOPEN_LEGACY |
| 6725 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6726 | #endif |
| 6727 | #ifdef _SC_XOPEN_REALTIME |
| 6728 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6729 | #endif |
| 6730 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6731 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6732 | #endif |
| 6733 | #ifdef _SC_XOPEN_SHM |
| 6734 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6735 | #endif |
| 6736 | #ifdef _SC_XOPEN_UNIX |
| 6737 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6738 | #endif |
| 6739 | #ifdef _SC_XOPEN_VERSION |
| 6740 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6741 | #endif |
| 6742 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6743 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6744 | #endif |
| 6745 | #ifdef _SC_XOPEN_XPG2 |
| 6746 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6747 | #endif |
| 6748 | #ifdef _SC_XOPEN_XPG3 |
| 6749 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6750 | #endif |
| 6751 | #ifdef _SC_XOPEN_XPG4 |
| 6752 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6753 | #endif |
| 6754 | }; |
| 6755 | |
| 6756 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6757 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6758 | { |
| 6759 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6760 | sizeof(posix_constants_sysconf) |
| 6761 | / sizeof(struct constdef)); |
| 6762 | } |
| 6763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6764 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6765 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6766 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6767 | |
| 6768 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6769 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6770 | { |
| 6771 | PyObject *result = NULL; |
| 6772 | int name; |
| 6773 | |
| 6774 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6775 | int value; |
| 6776 | |
| 6777 | errno = 0; |
| 6778 | value = sysconf(name); |
| 6779 | if (value == -1 && errno != 0) |
| 6780 | posix_error(); |
| 6781 | else |
| 6782 | result = PyInt_FromLong(value); |
| 6783 | } |
| 6784 | return result; |
| 6785 | } |
| 6786 | #endif |
| 6787 | |
| 6788 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6789 | /* This code is used to ensure that the tables of configuration value names |
| 6790 | * are in sorted order as required by conv_confname(), and also to build the |
| 6791 | * the exported dictionaries that are used to publish information about the |
| 6792 | * names available on the host platform. |
| 6793 | * |
| 6794 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6795 | * when used, even for platforms we're not able to test on. It also makes |
| 6796 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6797 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6798 | |
| 6799 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6800 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6801 | { |
| 6802 | const struct constdef *c1 = |
| 6803 | (const struct constdef *) v1; |
| 6804 | const struct constdef *c2 = |
| 6805 | (const struct constdef *) v2; |
| 6806 | |
| 6807 | return strcmp(c1->name, c2->name); |
| 6808 | } |
| 6809 | |
| 6810 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6811 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6812 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6813 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6814 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6815 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6816 | |
| 6817 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6818 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6819 | if (d == NULL) |
| 6820 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6821 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6822 | for (i=0; i < tablesize; ++i) { |
| 6823 | PyObject *o = PyInt_FromLong(table[i].value); |
| 6824 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6825 | Py_XDECREF(o); |
| 6826 | Py_DECREF(d); |
| 6827 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6828 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6829 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6830 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6831 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6832 | } |
| 6833 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6834 | /* Return -1 on failure, 0 on success. */ |
| 6835 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6836 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6837 | { |
| 6838 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6839 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6840 | sizeof(posix_constants_pathconf) |
| 6841 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6842 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6843 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6844 | #endif |
| 6845 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6846 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6847 | sizeof(posix_constants_confstr) |
| 6848 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6849 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6850 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6851 | #endif |
| 6852 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6853 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6854 | sizeof(posix_constants_sysconf) |
| 6855 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6856 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6857 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6858 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6859 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6860 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6861 | |
| 6862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6863 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6864 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6865 | 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] | 6866 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6867 | |
| 6868 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6869 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6870 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6871 | abort(); |
| 6872 | /*NOTREACHED*/ |
| 6873 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6874 | return NULL; |
| 6875 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6876 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6877 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6878 | PyDoc_STRVAR(win32_startfile__doc__, |
| 6879 | "startfile(filepath) - Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6880 | \n\ |
| 6881 | This acts like double-clicking the file in Explorer, or giving the file\n\ |
| 6882 | name as an argument to the DOS \"start\" command: the file is opened\n\ |
| 6883 | with whatever application (if any) its extension is associated.\n\ |
| 6884 | \n\ |
| 6885 | startfile returns as soon as the associated application is launched.\n\ |
| 6886 | There is no option to wait for the application to close, and no way\n\ |
| 6887 | to retrieve the application's exit status.\n\ |
| 6888 | \n\ |
| 6889 | The filepath is relative to the current directory. If you want to use\n\ |
| 6890 | 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] | 6891 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6892 | |
| 6893 | static PyObject * |
| 6894 | win32_startfile(PyObject *self, PyObject *args) |
| 6895 | { |
| 6896 | char *filepath; |
| 6897 | HINSTANCE rc; |
| 6898 | if (!PyArg_ParseTuple(args, "s:startfile", &filepath)) |
| 6899 | return NULL; |
| 6900 | Py_BEGIN_ALLOW_THREADS |
| 6901 | rc = ShellExecute((HWND)0, NULL, filepath, NULL, NULL, SW_SHOWNORMAL); |
| 6902 | Py_END_ALLOW_THREADS |
| 6903 | if (rc <= (HINSTANCE)32) |
| 6904 | return win32_error("startfile", filepath); |
| 6905 | Py_INCREF(Py_None); |
| 6906 | return Py_None; |
| 6907 | } |
| 6908 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6909 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6910 | #ifdef HAVE_GETLOADAVG |
| 6911 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6912 | "getloadavg() -> (float, float, float)\n\n\ |
| 6913 | Return the number of processes in the system run queue averaged over\n\ |
| 6914 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6915 | was unobtainable"); |
| 6916 | |
| 6917 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6918 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6919 | { |
| 6920 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6921 | if (getloadavg(loadavg, 3)!=3) { |
| 6922 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6923 | return NULL; |
| 6924 | } else |
| 6925 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6926 | } |
| 6927 | #endif |
| 6928 | |
| 6929 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6930 | static PyMethodDef posix_methods[] = { |
| 6931 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 6932 | #ifdef HAVE_TTYNAME |
| 6933 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 6934 | #endif |
| 6935 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 6936 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6937 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6938 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6939 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 6940 | #ifdef HAVE_LCHOWN |
| 6941 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 6942 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 6943 | #ifdef HAVE_CHROOT |
| 6944 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 6945 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6946 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6947 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6948 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6949 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6950 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6951 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6952 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 6953 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 6954 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6955 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6956 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6957 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6958 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 6959 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 6960 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6961 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6962 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6963 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6964 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6965 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6966 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6967 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 6968 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 6969 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 6970 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6971 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6972 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6973 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6974 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6975 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6976 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6977 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6978 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6979 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6980 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6981 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 6982 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 6983 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6984 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6985 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6986 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6987 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6988 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6989 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 6990 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6991 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6992 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6993 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 6994 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6995 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6996 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6997 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6998 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6999 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7000 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7001 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7002 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7003 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7004 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7005 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7006 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7007 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7008 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7009 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7010 | #endif /* HAVE_GETEGID */ |
| 7011 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7012 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7013 | #endif /* HAVE_GETEUID */ |
| 7014 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7015 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7016 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7017 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7018 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7019 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7020 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7021 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7022 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7023 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7024 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7025 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7026 | #endif /* HAVE_GETPPID */ |
| 7027 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7028 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7029 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7030 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7031 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7032 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7033 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7034 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7035 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7036 | #ifdef HAVE_KILLPG |
| 7037 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7038 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7039 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7040 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7041 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7042 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7043 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7044 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7045 | {"popen2", win32_popen2, METH_VARARGS}, |
| 7046 | {"popen3", win32_popen3, METH_VARARGS}, |
| 7047 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7048 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7049 | #else |
| 7050 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7051 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 7052 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 7053 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 7054 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 7055 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7056 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7058 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7059 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7060 | #ifdef HAVE_SETEUID |
| 7061 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7062 | #endif /* HAVE_SETEUID */ |
| 7063 | #ifdef HAVE_SETEGID |
| 7064 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7065 | #endif /* HAVE_SETEGID */ |
| 7066 | #ifdef HAVE_SETREUID |
| 7067 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7068 | #endif /* HAVE_SETREUID */ |
| 7069 | #ifdef HAVE_SETREGID |
| 7070 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7071 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7072 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7073 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7074 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7075 | #ifdef HAVE_SETGROUPS |
| 7076 | {"setgroups", posix_setgroups, METH_VARARGS, posix_setgroups__doc__}, |
| 7077 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_GETPGID |
| 7079 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7080 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7081 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7082 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7083 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7084 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7085 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7086 | #endif /* HAVE_WAIT */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7087 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7088 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7089 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7090 | #ifdef HAVE_GETSID |
| 7091 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7092 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7093 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7094 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7095 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7096 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7097 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7098 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7099 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7100 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7101 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7102 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7103 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7104 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7105 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7106 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 7107 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7108 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7109 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7110 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7111 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7112 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 7113 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7114 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7115 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7116 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7117 | #endif |
| 7118 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7119 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7120 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7121 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7122 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7123 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7124 | #ifdef HAVE_DEVICE_MACROS |
| 7125 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7126 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7127 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7128 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7129 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7130 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7131 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7132 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7133 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7134 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7135 | #ifdef HAVE_UNSETENV |
| 7136 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7137 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7138 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7139 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7140 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7141 | #ifdef HAVE_FCHDIR |
| 7142 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7143 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7144 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7145 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7146 | #endif |
| 7147 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7148 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7149 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7150 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7151 | #ifdef WCOREDUMP |
| 7152 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7153 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7154 | #ifdef WIFCONTINUED |
| 7155 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7156 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7157 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7159 | #endif /* WIFSTOPPED */ |
| 7160 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7161 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7162 | #endif /* WIFSIGNALED */ |
| 7163 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7164 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7165 | #endif /* WIFEXITED */ |
| 7166 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7167 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7168 | #endif /* WEXITSTATUS */ |
| 7169 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7170 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7171 | #endif /* WTERMSIG */ |
| 7172 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7173 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7174 | #endif /* WSTOPSIG */ |
| 7175 | #endif /* HAVE_SYS_WAIT_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7176 | #ifdef HAVE_FSTATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7177 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7178 | #endif |
| 7179 | #ifdef HAVE_STATVFS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7180 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7181 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 7182 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7183 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7184 | #endif |
| 7185 | #ifdef HAVE_TEMPNAM |
| 7186 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 7187 | #endif |
| 7188 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7189 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7190 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7191 | #ifdef HAVE_CONFSTR |
| 7192 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7193 | #endif |
| 7194 | #ifdef HAVE_SYSCONF |
| 7195 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7196 | #endif |
| 7197 | #ifdef HAVE_FPATHCONF |
| 7198 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7199 | #endif |
| 7200 | #ifdef HAVE_PATHCONF |
| 7201 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7202 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7203 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7204 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7205 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7206 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7207 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7208 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7209 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7210 | {NULL, NULL} /* Sentinel */ |
| 7211 | }; |
| 7212 | |
| 7213 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7214 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7215 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7216 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7217 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7220 | #if defined(PYOS_OS2) |
| 7221 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7222 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7223 | { |
| 7224 | APIRET rc; |
| 7225 | ULONG values[QSV_MAX+1]; |
| 7226 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7227 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7228 | |
| 7229 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7230 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7231 | Py_END_ALLOW_THREADS |
| 7232 | |
| 7233 | if (rc != NO_ERROR) { |
| 7234 | os2_error(rc); |
| 7235 | return -1; |
| 7236 | } |
| 7237 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7238 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7239 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7240 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7241 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7242 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7243 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7244 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7245 | |
| 7246 | switch (values[QSV_VERSION_MINOR]) { |
| 7247 | case 0: ver = "2.00"; break; |
| 7248 | case 10: ver = "2.10"; break; |
| 7249 | case 11: ver = "2.11"; break; |
| 7250 | case 30: ver = "3.00"; break; |
| 7251 | case 40: ver = "4.00"; break; |
| 7252 | case 50: ver = "5.00"; break; |
| 7253 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7254 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7255 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7256 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7257 | ver = &tmp[0]; |
| 7258 | } |
| 7259 | |
| 7260 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7261 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7262 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7263 | |
| 7264 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7265 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7266 | tmp[1] = ':'; |
| 7267 | tmp[2] = '\0'; |
| 7268 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7269 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7270 | } |
| 7271 | #endif |
| 7272 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7273 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7274 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7275 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7276 | #ifdef F_OK |
| 7277 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7278 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7279 | #ifdef R_OK |
| 7280 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7281 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7282 | #ifdef W_OK |
| 7283 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7284 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7285 | #ifdef X_OK |
| 7286 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7287 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7288 | #ifdef NGROUPS_MAX |
| 7289 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7290 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7291 | #ifdef TMP_MAX |
| 7292 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7293 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7294 | #ifdef WCONTINUED |
| 7295 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7296 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7297 | #ifdef WNOHANG |
| 7298 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7299 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7300 | #ifdef WUNTRACED |
| 7301 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7302 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7303 | #ifdef O_RDONLY |
| 7304 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7305 | #endif |
| 7306 | #ifdef O_WRONLY |
| 7307 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7308 | #endif |
| 7309 | #ifdef O_RDWR |
| 7310 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7311 | #endif |
| 7312 | #ifdef O_NDELAY |
| 7313 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7314 | #endif |
| 7315 | #ifdef O_NONBLOCK |
| 7316 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7317 | #endif |
| 7318 | #ifdef O_APPEND |
| 7319 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7320 | #endif |
| 7321 | #ifdef O_DSYNC |
| 7322 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7323 | #endif |
| 7324 | #ifdef O_RSYNC |
| 7325 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7326 | #endif |
| 7327 | #ifdef O_SYNC |
| 7328 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7329 | #endif |
| 7330 | #ifdef O_NOCTTY |
| 7331 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7332 | #endif |
| 7333 | #ifdef O_CREAT |
| 7334 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7335 | #endif |
| 7336 | #ifdef O_EXCL |
| 7337 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7338 | #endif |
| 7339 | #ifdef O_TRUNC |
| 7340 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7341 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7342 | #ifdef O_BINARY |
| 7343 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7344 | #endif |
| 7345 | #ifdef O_TEXT |
| 7346 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7347 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7348 | #ifdef O_LARGEFILE |
| 7349 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7350 | #endif |
| 7351 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7352 | /* MS Windows */ |
| 7353 | #ifdef O_NOINHERIT |
| 7354 | /* Don't inherit in child processes. */ |
| 7355 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7356 | #endif |
| 7357 | #ifdef _O_SHORT_LIVED |
| 7358 | /* Optimize for short life (keep in memory). */ |
| 7359 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7360 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7361 | #endif |
| 7362 | #ifdef O_TEMPORARY |
| 7363 | /* Automatically delete when last handle is closed. */ |
| 7364 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7365 | #endif |
| 7366 | #ifdef O_RANDOM |
| 7367 | /* Optimize for random access. */ |
| 7368 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7369 | #endif |
| 7370 | #ifdef O_SEQUENTIAL |
| 7371 | /* Optimize for sequential access. */ |
| 7372 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7373 | #endif |
| 7374 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7375 | /* GNU extensions. */ |
| 7376 | #ifdef O_DIRECT |
| 7377 | /* Direct disk access. */ |
| 7378 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7379 | #endif |
| 7380 | #ifdef O_DIRECTORY |
| 7381 | /* Must be a directory. */ |
| 7382 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7383 | #endif |
| 7384 | #ifdef O_NOFOLLOW |
| 7385 | /* Do not follow links. */ |
| 7386 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7387 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7388 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7389 | /* These come from sysexits.h */ |
| 7390 | #ifdef EX_OK |
| 7391 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7392 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7393 | #ifdef EX_USAGE |
| 7394 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7395 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7396 | #ifdef EX_DATAERR |
| 7397 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7398 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7399 | #ifdef EX_NOINPUT |
| 7400 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7401 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7402 | #ifdef EX_NOUSER |
| 7403 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7404 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7405 | #ifdef EX_NOHOST |
| 7406 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7407 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7408 | #ifdef EX_UNAVAILABLE |
| 7409 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7410 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7411 | #ifdef EX_SOFTWARE |
| 7412 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7413 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7414 | #ifdef EX_OSERR |
| 7415 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7416 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7417 | #ifdef EX_OSFILE |
| 7418 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7419 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7420 | #ifdef EX_CANTCREAT |
| 7421 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7422 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7423 | #ifdef EX_IOERR |
| 7424 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7425 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7426 | #ifdef EX_TEMPFAIL |
| 7427 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7428 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7429 | #ifdef EX_PROTOCOL |
| 7430 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7431 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7432 | #ifdef EX_NOPERM |
| 7433 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7434 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7435 | #ifdef EX_CONFIG |
| 7436 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7437 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7438 | #ifdef EX_NOTFOUND |
| 7439 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7440 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7441 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7442 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7443 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7444 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7445 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7446 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7447 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7448 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7449 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7450 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7451 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7452 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7453 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7454 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7455 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7456 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7457 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7458 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7459 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7460 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7461 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7462 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7463 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7464 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7465 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7466 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7467 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7468 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7469 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7470 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7471 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7472 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7473 | #if defined(PYOS_OS2) |
| 7474 | if (insertvalues(d)) return -1; |
| 7475 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7476 | return 0; |
| 7477 | } |
| 7478 | |
| 7479 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7480 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7481 | #define INITFUNC initnt |
| 7482 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7483 | |
| 7484 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7485 | #define INITFUNC initos2 |
| 7486 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7487 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7488 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7489 | #define INITFUNC initposix |
| 7490 | #define MODNAME "posix" |
| 7491 | #endif |
| 7492 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7493 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7494 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7495 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7496 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7497 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7498 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7499 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7500 | posix__doc__); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7501 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7502 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7503 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7504 | Py_XINCREF(v); |
| 7505 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7506 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7507 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7508 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7509 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7510 | return; |
| 7511 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7512 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7513 | return; |
| 7514 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7515 | Py_INCREF(PyExc_OSError); |
| 7516 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7517 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7518 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7519 | if (posix_putenv_garbage == NULL) |
| 7520 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7521 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7522 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7523 | stat_result_desc.name = MODNAME ".stat_result"; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7524 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7525 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7526 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7527 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7528 | structseq_new = StatResultType.tp_new; |
| 7529 | StatResultType.tp_new = statresult_new; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7530 | Py_INCREF((PyObject*) &StatResultType); |
| 7531 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7532 | |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 7533 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7534 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7535 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7536 | PyModule_AddObject(m, "statvfs_result", |
| 7537 | (PyObject*) &StatVFSResultType); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7538 | } |