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 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
| 17 | /* |
| 18 | * Step 1 of support for weak-linking a number of symbols existing on |
| 19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 20 | * at the end of this file for more information. |
| 21 | */ |
| 22 | # pragma weak lchown |
| 23 | # pragma weak statvfs |
| 24 | # pragma weak fstatvfs |
| 25 | |
| 26 | #endif /* __APPLE__ */ |
| 27 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 28 | #define PY_SSIZE_T_CLEAN |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | #include "Python.h" |
| 31 | #include "structseq.h" |
| 32 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 34 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 35 | #endif /* defined(__VMS) */ |
| 36 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 42 | "This module provides access to operating system functionality that is\n\ |
| 43 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 44 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 47 | #ifndef Py_USING_UNICODE |
| 48 | /* This is used in signatures of functions. */ |
| 49 | #define Py_UNICODE void |
| 50 | #endif |
| 51 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 52 | #if defined(PYOS_OS2) |
| 53 | #define INCL_DOS |
| 54 | #define INCL_DOSERRORS |
| 55 | #define INCL_DOSPROCESS |
| 56 | #define INCL_NOPMAPI |
| 57 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #if defined(PYCC_GCC) |
| 59 | #include <ctype.h> |
| 60 | #include <io.h> |
| 61 | #include <stdio.h> |
| 62 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 63 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 64 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 67 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_TYPES_H */ |
| 70 | |
| 71 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | #include <sys/stat.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 73 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SYS_WAIT_H |
| 76 | #include <sys/wait.h> /* For WNOHANG */ |
| 77 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 79 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 80 | #include <signal.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 81 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | #ifdef HAVE_FCNTL_H |
| 84 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 85 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 87 | #ifdef HAVE_GRP_H |
| 88 | #include <grp.h> |
| 89 | #endif |
| 90 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYSEXITS_H |
| 92 | #include <sysexits.h> |
| 93 | #endif /* HAVE_SYSEXITS_H */ |
| 94 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 95 | #ifdef HAVE_SYS_LOADAVG_H |
| 96 | #include <sys/loadavg.h> |
| 97 | #endif |
| 98 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 99 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 100 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 102 | #include <process.h> |
| 103 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 105 | #define HAVE_GETCWD 1 |
| 106 | #define HAVE_OPENDIR 1 |
| 107 | #define HAVE_SYSTEM 1 |
| 108 | #if defined(__OS2__) |
| 109 | #define HAVE_EXECV 1 |
| 110 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 112 | #include <process.h> |
| 113 | #else |
| 114 | #ifdef __BORLANDC__ /* Borland compiler */ |
| 115 | #define HAVE_EXECV 1 |
| 116 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 117 | #define HAVE_OPENDIR 1 |
| 118 | #define HAVE_PIPE 1 |
| 119 | #define HAVE_POPEN 1 |
| 120 | #define HAVE_SYSTEM 1 |
| 121 | #define HAVE_WAIT 1 |
| 122 | #else |
| 123 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 124 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 125 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 126 | #define HAVE_EXECV 1 |
| 127 | #define HAVE_PIPE 1 |
| 128 | #define HAVE_POPEN 1 |
| 129 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 130 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 131 | #define HAVE_FSYNC 1 |
| 132 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 133 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 134 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 135 | /* 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] | 136 | #else /* all other compilers */ |
| 137 | /* Unix functions that the configure script doesn't check for */ |
| 138 | #define HAVE_EXECV 1 |
| 139 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 140 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 141 | #define HAVE_FORK1 1 |
| 142 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 143 | #define HAVE_GETCWD 1 |
| 144 | #define HAVE_GETEGID 1 |
| 145 | #define HAVE_GETEUID 1 |
| 146 | #define HAVE_GETGID 1 |
| 147 | #define HAVE_GETPPID 1 |
| 148 | #define HAVE_GETUID 1 |
| 149 | #define HAVE_KILL 1 |
| 150 | #define HAVE_OPENDIR 1 |
| 151 | #define HAVE_PIPE 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 152 | #ifndef __rtems__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 153 | #define HAVE_POPEN 1 |
Martin v. Löwis | 212ede6 | 2003-09-20 11:20:30 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 155 | #define HAVE_SYSTEM 1 |
| 156 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 157 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 158 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #endif /* _MSC_VER */ |
| 160 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 161 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 162 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 164 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 166 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 167 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 168 | (default) */ |
| 169 | extern char *ctermid_r(char *); |
| 170 | #endif |
| 171 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 172 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 173 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 175 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 176 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 177 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 179 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 180 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 181 | #endif |
| 182 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 183 | extern int chdir(char *); |
| 184 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 186 | extern int chdir(const char *); |
| 187 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 188 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 189 | #ifdef __BORLANDC__ |
| 190 | extern int chmod(const char *, int); |
| 191 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 192 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 193 | #endif |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 194 | extern int chown(const char *, uid_t, gid_t); |
| 195 | extern char *getcwd(char *, int); |
| 196 | extern char *strerror(int); |
| 197 | extern int link(const char *, const char *); |
| 198 | extern int rename(const char *, const char *); |
| 199 | extern int stat(const char *, struct stat *); |
| 200 | extern int unlink(const char *); |
| 201 | extern int pclose(FILE *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 204 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 207 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 210 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | #ifdef HAVE_UTIME_H |
| 213 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 214 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 216 | #ifdef HAVE_SYS_UTIME_H |
| 217 | #include <sys/utime.h> |
| 218 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 219 | #endif /* HAVE_SYS_UTIME_H */ |
| 220 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | #ifdef HAVE_SYS_TIMES_H |
| 222 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 223 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef HAVE_SYS_PARAM_H |
| 226 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 227 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef HAVE_SYS_UTSNAME_H |
| 230 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 233 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 235 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 236 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 237 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 238 | #include <direct.h> |
| 239 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 240 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 242 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 244 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 246 | #endif |
| 247 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 252 | #endif |
| 253 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 255 | #ifdef _MSC_VER |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 256 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #include <direct.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 258 | #endif |
| 259 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #include <io.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 261 | #endif |
| 262 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #include <process.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 266 | #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #include <windows.h> |
Tim Peters | ee66d0c | 2002-07-15 16:10:55 +0000 | [diff] [blame] | 268 | #include <shellapi.h> /* for ShellExecute() */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #define popen _popen |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 270 | #define pclose _pclose |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 271 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 273 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 275 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 277 | #ifndef MAXPATHLEN |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 278 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 279 | #define MAXPATHLEN PATH_MAX |
| 280 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #define MAXPATHLEN 1024 |
Thomas Wouters | 1ddba60 | 2006-04-25 15:29:46 +0000 | [diff] [blame] | 282 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 283 | #endif /* MAXPATHLEN */ |
| 284 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 285 | #ifdef UNION_WAIT |
| 286 | /* Emulate some macros on systems that have a union instead of macros */ |
| 287 | |
| 288 | #ifndef WIFEXITED |
| 289 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 290 | #endif |
| 291 | |
| 292 | #ifndef WEXITSTATUS |
| 293 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 294 | #endif |
| 295 | |
| 296 | #ifndef WTERMSIG |
| 297 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 298 | #endif |
| 299 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 300 | #define WAIT_TYPE union wait |
| 301 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 302 | |
| 303 | #else /* !UNION_WAIT */ |
| 304 | #define WAIT_TYPE int |
| 305 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 306 | #endif /* UNION_WAIT */ |
| 307 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 308 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 309 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 310 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 311 | #define USE_CTERMID_R |
| 312 | #endif |
| 313 | |
| 314 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD) |
| 315 | #define USE_TMPNAM_R |
| 316 | #endif |
| 317 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 318 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 319 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 320 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 321 | # define STAT win32_stat |
| 322 | # define FSTAT win32_fstat |
| 323 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 324 | #else |
| 325 | # define STAT stat |
| 326 | # define FSTAT fstat |
| 327 | # define STRUCT_STAT struct stat |
| 328 | #endif |
| 329 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 330 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 331 | #include <sys/mkdev.h> |
| 332 | #else |
| 333 | #if defined(MAJOR_IN_SYSMACROS) |
| 334 | #include <sys/sysmacros.h> |
| 335 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 336 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 337 | #include <sys/mkdev.h> |
| 338 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 339 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 342 | #ifdef WITH_NEXT_FRAMEWORK |
| 343 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 344 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 345 | */ |
| 346 | #include <crt_externs.h> |
| 347 | static char **environ; |
| 348 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 349 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 350 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 352 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 353 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 355 | PyObject *d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 356 | char **e; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 357 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | if (d == NULL) |
| 359 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 360 | #ifdef WITH_NEXT_FRAMEWORK |
| 361 | if (environ == NULL) |
| 362 | environ = *_NSGetEnviron(); |
| 363 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | if (environ == NULL) |
| 365 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 366 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 367 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 368 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 369 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | char *p = strchr(*e, '='); |
| 371 | if (p == NULL) |
| 372 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 373 | k = PyString_FromStringAndSize(*e, (int)(p-*e)); |
| 374 | if (k == NULL) { |
| 375 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 377 | } |
| 378 | v = PyString_FromString(p+1); |
| 379 | if (v == NULL) { |
| 380 | PyErr_Clear(); |
| 381 | Py_DECREF(k); |
| 382 | continue; |
| 383 | } |
| 384 | if (PyDict_GetItem(d, k) == NULL) { |
| 385 | if (PyDict_SetItem(d, k, v) != 0) |
| 386 | PyErr_Clear(); |
| 387 | } |
| 388 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 389 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | } |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 391 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 392 | { |
| 393 | APIRET rc; |
| 394 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 395 | |
| 396 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 397 | 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] | 398 | PyObject *v = PyString_FromString(buffer); |
| 399 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 400 | Py_DECREF(v); |
| 401 | } |
| 402 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 403 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 404 | PyObject *v = PyString_FromString(buffer); |
| 405 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 406 | Py_DECREF(v); |
| 407 | } |
| 408 | } |
| 409 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | return d; |
| 411 | } |
| 412 | |
| 413 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 415 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 416 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 417 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 418 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 419 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 420 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 421 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 422 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 423 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 424 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 427 | #ifdef Py_WIN_WIDE_FILENAMES |
| 428 | static PyObject * |
| 429 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 430 | { |
| 431 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 432 | } |
| 433 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 434 | |
| 435 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 436 | static PyObject * |
| 437 | posix_error_with_allocated_filename(char* name) |
| 438 | { |
| 439 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
| 440 | PyMem_Free(name); |
| 441 | return rc; |
| 442 | } |
| 443 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 444 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 445 | static PyObject * |
| 446 | win32_error(char* function, char* filename) |
| 447 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 448 | /* XXX We should pass the function name along in the future. |
| 449 | (_winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 450 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 451 | Windows error object, which is non-trivial. |
| 452 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 453 | errno = GetLastError(); |
| 454 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 455 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 456 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 457 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 458 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 459 | |
| 460 | #ifdef Py_WIN_WIDE_FILENAMES |
| 461 | static PyObject * |
| 462 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 463 | { |
| 464 | /* XXX - see win32_error for comments on 'function' */ |
| 465 | errno = GetLastError(); |
| 466 | if (filename) |
| 467 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 468 | else |
| 469 | return PyErr_SetFromWindowsErr(errno); |
| 470 | } |
| 471 | |
| 472 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) |
| 473 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Function suitable for O& conversion */ |
| 477 | static int |
| 478 | convert_to_unicode(PyObject *arg, void* _param) |
| 479 | { |
| 480 | PyObject **param = (PyObject**)_param; |
| 481 | if (PyUnicode_CheckExact(arg)) { |
| 482 | Py_INCREF(arg); |
| 483 | *param = arg; |
| 484 | } |
| 485 | else if (PyUnicode_Check(arg)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 486 | /* For a Unicode subtype that's not a Unicode object, |
| 487 | return a true Unicode object with the same data. */ |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 488 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg), |
| 489 | PyUnicode_GET_SIZE(arg)); |
| 490 | return *param != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 491 | } |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 492 | else |
| 493 | *param = PyUnicode_FromEncodedObject(arg, |
| 494 | Py_FileSystemDefaultEncoding, |
| 495 | "strict"); |
| 496 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 500 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 501 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 503 | #if defined(PYOS_OS2) |
| 504 | /********************************************************************** |
| 505 | * Helper Function to Trim and Format OS/2 Messages |
| 506 | **********************************************************************/ |
| 507 | static void |
| 508 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 509 | { |
| 510 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 511 | |
| 512 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 513 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 514 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 515 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 516 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 517 | } |
| 518 | |
| 519 | /* Add Optional Reason Text */ |
| 520 | if (reason) { |
| 521 | strcat(msgbuf, " : "); |
| 522 | strcat(msgbuf, reason); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /********************************************************************** |
| 527 | * Decode an OS/2 Operating System Error Code |
| 528 | * |
| 529 | * A convenience function to lookup an OS/2 error code and return a |
| 530 | * text message we can use to raise a Python exception. |
| 531 | * |
| 532 | * Notes: |
| 533 | * The messages for errors returned from the OS/2 kernel reside in |
| 534 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 535 | * |
| 536 | **********************************************************************/ |
| 537 | static char * |
| 538 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 539 | { |
| 540 | APIRET rc; |
| 541 | ULONG msglen; |
| 542 | |
| 543 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 544 | Py_BEGIN_ALLOW_THREADS |
| 545 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 546 | errorcode, "oso001.msg", &msglen); |
| 547 | Py_END_ALLOW_THREADS |
| 548 | |
| 549 | if (rc == NO_ERROR) |
| 550 | os2_formatmsg(msgbuf, msglen, reason); |
| 551 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 552 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 553 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 554 | |
| 555 | return msgbuf; |
| 556 | } |
| 557 | |
| 558 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 559 | errors are not in a global variable e.g. 'errno' nor are |
| 560 | they congruent with posix error numbers. */ |
| 561 | |
| 562 | static PyObject * os2_error(int code) |
| 563 | { |
| 564 | char text[1024]; |
| 565 | PyObject *v; |
| 566 | |
| 567 | os2_strerror(text, sizeof(text), code, ""); |
| 568 | |
| 569 | v = Py_BuildValue("(is)", code, text); |
| 570 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 571 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 572 | Py_DECREF(v); |
| 573 | } |
| 574 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 575 | } |
| 576 | |
| 577 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | |
| 579 | /* POSIX generic methods */ |
| 580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 581 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 582 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 583 | { |
| 584 | int fd; |
| 585 | int res; |
| 586 | fd = PyObject_AsFileDescriptor(fdobj); |
| 587 | if (fd < 0) |
| 588 | return NULL; |
| 589 | Py_BEGIN_ALLOW_THREADS |
| 590 | res = (*func)(fd); |
| 591 | Py_END_ALLOW_THREADS |
| 592 | if (res < 0) |
| 593 | return posix_error(); |
| 594 | Py_INCREF(Py_None); |
| 595 | return Py_None; |
| 596 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 597 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 598 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 599 | static int |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 600 | unicode_file_names(void) |
| 601 | { |
| 602 | static int canusewide = -1; |
| 603 | if (canusewide == -1) { |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 604 | /* As per doc for ::GetVersion(), this is the correct test for |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 605 | the Windows NT family. */ |
| 606 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0; |
| 607 | } |
| 608 | return canusewide; |
| 609 | } |
| 610 | #endif |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 612 | static PyObject * |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 613 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 615 | char *path1 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 616 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 617 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 618 | Py_FileSystemDefaultEncoding, &path1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 620 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 621 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 622 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 623 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 624 | return posix_error_with_allocated_filename(path1); |
| 625 | PyMem_Free(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 626 | Py_INCREF(Py_None); |
| 627 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 630 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 631 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 632 | char *format, |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 633 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 634 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 635 | char *path1 = NULL, *path2 = NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 636 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 637 | if (!PyArg_ParseTuple(args, format, |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 638 | Py_FileSystemDefaultEncoding, &path1, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 639 | Py_FileSystemDefaultEncoding, &path2)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 641 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 642 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 643 | Py_END_ALLOW_THREADS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 644 | PyMem_Free(path1); |
| 645 | PyMem_Free(path2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 646 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 647 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 649 | Py_INCREF(Py_None); |
| 650 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 653 | #ifdef Py_WIN_WIDE_FILENAMES |
| 654 | static PyObject* |
| 655 | win32_1str(PyObject* args, char* func, |
| 656 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 657 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 658 | { |
| 659 | PyObject *uni; |
| 660 | char *ansi; |
| 661 | BOOL result; |
| 662 | if (unicode_file_names()) { |
| 663 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 664 | PyErr_Clear(); |
| 665 | else { |
| 666 | Py_BEGIN_ALLOW_THREADS |
| 667 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 668 | Py_END_ALLOW_THREADS |
| 669 | if (!result) |
| 670 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 671 | Py_INCREF(Py_None); |
| 672 | return Py_None; |
| 673 | } |
| 674 | } |
| 675 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 676 | return NULL; |
| 677 | Py_BEGIN_ALLOW_THREADS |
| 678 | result = funcA(ansi); |
| 679 | Py_END_ALLOW_THREADS |
| 680 | if (!result) |
| 681 | return win32_error(func, ansi); |
| 682 | Py_INCREF(Py_None); |
| 683 | return Py_None; |
| 684 | |
| 685 | } |
| 686 | |
| 687 | /* This is a reimplementation of the C library's chdir function, |
| 688 | but one that produces Win32 errors instead of DOS error codes. |
| 689 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 690 | it also needs to set "magic" environment variables indicating |
| 691 | the per-drive current directory, which are of the form =<drive>: */ |
| 692 | BOOL __stdcall |
| 693 | win32_chdir(LPCSTR path) |
| 694 | { |
| 695 | char new_path[MAX_PATH+1]; |
| 696 | int result; |
| 697 | char env[4] = "=x:"; |
| 698 | |
| 699 | if(!SetCurrentDirectoryA(path)) |
| 700 | return FALSE; |
| 701 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 702 | if (!result) |
| 703 | return FALSE; |
| 704 | /* In the ANSI API, there should not be any paths longer |
| 705 | than MAX_PATH. */ |
| 706 | assert(result <= MAX_PATH+1); |
| 707 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 708 | strncmp(new_path, "//", 2) == 0) |
| 709 | /* UNC path, nothing to do. */ |
| 710 | return TRUE; |
| 711 | env[1] = new_path[0]; |
| 712 | return SetEnvironmentVariableA(env, new_path); |
| 713 | } |
| 714 | |
| 715 | /* The Unicode version differs from the ANSI version |
| 716 | since the current directory might exceed MAX_PATH characters */ |
| 717 | BOOL __stdcall |
| 718 | win32_wchdir(LPCWSTR path) |
| 719 | { |
| 720 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 721 | int result; |
| 722 | wchar_t env[4] = L"=x:"; |
| 723 | |
| 724 | if(!SetCurrentDirectoryW(path)) |
| 725 | return FALSE; |
| 726 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 727 | if (!result) |
| 728 | return FALSE; |
| 729 | if (result > MAX_PATH+1) { |
| 730 | new_path = malloc(result); |
| 731 | if (!new_path) { |
| 732 | SetLastError(ERROR_OUTOFMEMORY); |
| 733 | return FALSE; |
| 734 | } |
| 735 | } |
| 736 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 737 | wcsncmp(new_path, L"//", 2) == 0) |
| 738 | /* UNC path, nothing to do. */ |
| 739 | return TRUE; |
| 740 | env[1] = new_path[0]; |
| 741 | result = SetEnvironmentVariableW(env, new_path); |
| 742 | if (new_path != _new_path) |
| 743 | free(new_path); |
| 744 | return result; |
| 745 | } |
| 746 | #endif |
| 747 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 748 | #ifdef MS_WINDOWS |
| 749 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 750 | - time stamps are restricted to second resolution |
| 751 | - file modification times suffer from forth-and-back conversions between |
| 752 | UTC and local time |
| 753 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 754 | */ |
| 755 | #define HAVE_STAT_NSEC 1 |
| 756 | |
| 757 | struct win32_stat{ |
| 758 | int st_dev; |
| 759 | __int64 st_ino; |
| 760 | unsigned short st_mode; |
| 761 | int st_nlink; |
| 762 | int st_uid; |
| 763 | int st_gid; |
| 764 | int st_rdev; |
| 765 | __int64 st_size; |
| 766 | int st_atime; |
| 767 | int st_atime_nsec; |
| 768 | int st_mtime; |
| 769 | int st_mtime_nsec; |
| 770 | int st_ctime; |
| 771 | int st_ctime_nsec; |
| 772 | }; |
| 773 | |
| 774 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 775 | |
| 776 | static void |
| 777 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 778 | { |
Martin v. Löwis | 77c176d | 2006-05-12 17:22:04 +0000 | [diff] [blame] | 779 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 780 | /* Cannot simply cast and dereference in_ptr, |
| 781 | since it might not be aligned properly */ |
| 782 | __int64 in; |
| 783 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 784 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 785 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 786 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 787 | } |
| 788 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 789 | static void |
| 790 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 791 | { |
| 792 | /* XXX endianness */ |
| 793 | __int64 out; |
| 794 | out = time_in + secs_between_epochs; |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 795 | out = out * 10000000 + nsec_in / 100; |
Martin v. Löwis | 77c176d | 2006-05-12 17:22:04 +0000 | [diff] [blame] | 796 | memcpy(out_ptr, &out, sizeof(out)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 799 | /* Below, we *know* that ugo+r is 0444 */ |
| 800 | #if _S_IREAD != 0400 |
| 801 | #error Unsupported C library |
| 802 | #endif |
| 803 | static int |
| 804 | attributes_to_mode(DWORD attr) |
| 805 | { |
| 806 | int m = 0; |
| 807 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 808 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 809 | else |
| 810 | m |= _S_IFREG; |
| 811 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 812 | m |= 0444; |
| 813 | else |
| 814 | m |= 0666; |
| 815 | return m; |
| 816 | } |
| 817 | |
| 818 | static int |
| 819 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 820 | { |
| 821 | memset(result, 0, sizeof(*result)); |
| 822 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 823 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 824 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 825 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 826 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static int |
| 832 | win32_stat(const char* path, struct win32_stat *result) |
| 833 | { |
| 834 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 835 | int code; |
| 836 | char *dot; |
| 837 | /* XXX not supported on Win95 and NT 3.x */ |
| 838 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
| 839 | /* Protocol violation: we explicitly clear errno, instead of |
| 840 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 841 | errno = 0; |
| 842 | return -1; |
| 843 | } |
| 844 | code = attribute_data_to_stat(&info, result); |
| 845 | if (code != 0) |
| 846 | return code; |
| 847 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 848 | dot = strrchr(path, '.'); |
| 849 | if (dot) { |
| 850 | if (stricmp(dot, ".bat") == 0 || |
| 851 | stricmp(dot, ".cmd") == 0 || |
| 852 | stricmp(dot, ".exe") == 0 || |
| 853 | stricmp(dot, ".com") == 0) |
| 854 | result->st_mode |= 0111; |
| 855 | } |
| 856 | return code; |
| 857 | } |
| 858 | |
| 859 | static int |
| 860 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 861 | { |
| 862 | int code; |
| 863 | const wchar_t *dot; |
| 864 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 865 | /* XXX not supported on Win95 and NT 3.x */ |
| 866 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
| 867 | /* Protocol violation: we explicitly clear errno, instead of |
| 868 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 869 | errno = 0; |
| 870 | return -1; |
| 871 | } |
| 872 | code = attribute_data_to_stat(&info, result); |
| 873 | if (code < 0) |
| 874 | return code; |
| 875 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 876 | dot = wcsrchr(path, '.'); |
| 877 | if (dot) { |
| 878 | if (_wcsicmp(dot, L".bat") == 0 || |
| 879 | _wcsicmp(dot, L".cmd") == 0 || |
| 880 | _wcsicmp(dot, L".exe") == 0 || |
| 881 | _wcsicmp(dot, L".com") == 0) |
| 882 | result->st_mode |= 0111; |
| 883 | } |
| 884 | return code; |
| 885 | } |
| 886 | |
| 887 | static int |
| 888 | win32_fstat(int file_number, struct win32_stat *result) |
| 889 | { |
| 890 | BY_HANDLE_FILE_INFORMATION info; |
| 891 | HANDLE h; |
| 892 | int type; |
| 893 | |
| 894 | h = (HANDLE)_get_osfhandle(file_number); |
| 895 | |
| 896 | /* Protocol violation: we explicitly clear errno, instead of |
| 897 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 898 | errno = 0; |
| 899 | |
| 900 | if (h == INVALID_HANDLE_VALUE) { |
| 901 | /* This is really a C library error (invalid file handle). |
| 902 | We set the Win32 error to the closes one matching. */ |
| 903 | SetLastError(ERROR_INVALID_HANDLE); |
| 904 | return -1; |
| 905 | } |
| 906 | memset(result, 0, sizeof(*result)); |
| 907 | |
| 908 | type = GetFileType(h); |
| 909 | if (type == FILE_TYPE_UNKNOWN) { |
| 910 | DWORD error = GetLastError(); |
| 911 | if (error != 0) { |
| 912 | return -1; |
| 913 | } |
| 914 | /* else: valid but unknown file */ |
| 915 | } |
| 916 | |
| 917 | if (type != FILE_TYPE_DISK) { |
| 918 | if (type == FILE_TYPE_CHAR) |
| 919 | result->st_mode = _S_IFCHR; |
| 920 | else if (type == FILE_TYPE_PIPE) |
| 921 | result->st_mode = _S_IFIFO; |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | if (!GetFileInformationByHandle(h, &info)) { |
| 926 | return -1; |
| 927 | } |
| 928 | |
| 929 | /* similar to stat() */ |
| 930 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 931 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 932 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 933 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 934 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 935 | /* specific to fstat() */ |
| 936 | result->st_nlink = info.nNumberOfLinks; |
| 937 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | #endif /* MS_WINDOWS */ |
| 942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 943 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 944 | "stat_result: Result from stat or lstat.\n\n\ |
| 945 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 946 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 947 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 948 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 949 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 950 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 951 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 952 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 953 | |
| 954 | static PyStructSequence_Field stat_result_fields[] = { |
| 955 | {"st_mode", "protection bits"}, |
| 956 | {"st_ino", "inode"}, |
| 957 | {"st_dev", "device"}, |
| 958 | {"st_nlink", "number of hard links"}, |
| 959 | {"st_uid", "user ID of owner"}, |
| 960 | {"st_gid", "group ID of owner"}, |
| 961 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 962 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 963 | {NULL, "integer time of last access"}, |
| 964 | {NULL, "integer time of last modification"}, |
| 965 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 966 | {"st_atime", "time of last access"}, |
| 967 | {"st_mtime", "time of last modification"}, |
| 968 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 969 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 970 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 971 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 972 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 973 | {"st_blocks", "number of blocks allocated"}, |
| 974 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 975 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 976 | {"st_rdev", "device type (if inode device)"}, |
| 977 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 978 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 979 | {"st_flags", "user defined flags for file"}, |
| 980 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 981 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 982 | {"st_gen", "generation number"}, |
| 983 | #endif |
| 984 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 985 | {"st_birthtime", "time of creation"}, |
| 986 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 987 | {0} |
| 988 | }; |
| 989 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 990 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 991 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 992 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 993 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 994 | #endif |
| 995 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 996 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 997 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 998 | #else |
| 999 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1000 | #endif |
| 1001 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1002 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1003 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1004 | #else |
| 1005 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1006 | #endif |
| 1007 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1008 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1009 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1010 | #else |
| 1011 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1012 | #endif |
| 1013 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1014 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1015 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1016 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1017 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1018 | #endif |
| 1019 | |
| 1020 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1021 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1022 | #else |
| 1023 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1024 | #endif |
| 1025 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1026 | static PyStructSequence_Desc stat_result_desc = { |
| 1027 | "stat_result", /* name */ |
| 1028 | stat_result__doc__, /* doc */ |
| 1029 | stat_result_fields, |
| 1030 | 10 |
| 1031 | }; |
| 1032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1033 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1034 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1035 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1036 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1037 | 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] | 1038 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1039 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1040 | |
| 1041 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1042 | {"f_bsize", }, |
| 1043 | {"f_frsize", }, |
| 1044 | {"f_blocks", }, |
| 1045 | {"f_bfree", }, |
| 1046 | {"f_bavail", }, |
| 1047 | {"f_files", }, |
| 1048 | {"f_ffree", }, |
| 1049 | {"f_favail", }, |
| 1050 | {"f_flag", }, |
| 1051 | {"f_namemax",}, |
| 1052 | {0} |
| 1053 | }; |
| 1054 | |
| 1055 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1056 | "statvfs_result", /* name */ |
| 1057 | statvfs_result__doc__, /* doc */ |
| 1058 | statvfs_result_fields, |
| 1059 | 10 |
| 1060 | }; |
| 1061 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 1062 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1063 | static PyTypeObject StatResultType; |
| 1064 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1065 | static newfunc structseq_new; |
| 1066 | |
| 1067 | static PyObject * |
| 1068 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1069 | { |
| 1070 | PyStructSequence *result; |
| 1071 | int i; |
| 1072 | |
| 1073 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1074 | if (!result) |
| 1075 | return NULL; |
| 1076 | /* If we have been initialized from a tuple, |
| 1077 | st_?time might be set to None. Initialize it |
| 1078 | from the int slots. */ |
| 1079 | for (i = 7; i <= 9; i++) { |
| 1080 | if (result->ob_item[i+3] == Py_None) { |
| 1081 | Py_DECREF(Py_None); |
| 1082 | Py_INCREF(result->ob_item[i]); |
| 1083 | result->ob_item[i+3] = result->ob_item[i]; |
| 1084 | } |
| 1085 | } |
| 1086 | return (PyObject*)result; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1092 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1093 | |
| 1094 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1095 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1096 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1097 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1098 | future calls return ints. \n\ |
| 1099 | If newval is omitted, return the current setting.\n"); |
| 1100 | |
| 1101 | static PyObject* |
| 1102 | stat_float_times(PyObject* self, PyObject *args) |
| 1103 | { |
| 1104 | int newval = -1; |
| 1105 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1106 | return NULL; |
| 1107 | if (newval == -1) |
| 1108 | /* Return old value */ |
| 1109 | return PyBool_FromLong(_stat_float_times); |
| 1110 | _stat_float_times = newval; |
| 1111 | Py_INCREF(Py_None); |
| 1112 | return Py_None; |
| 1113 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1114 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1115 | static void |
| 1116 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1117 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1118 | PyObject *fval,*ival; |
| 1119 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1120 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1121 | #else |
| 1122 | ival = PyInt_FromLong((long)sec); |
| 1123 | #endif |
Neal Norwitz | e0a81af | 2006-08-12 01:50:38 +0000 | [diff] [blame] | 1124 | if (!ival) |
| 1125 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1126 | if (_stat_float_times) { |
| 1127 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1128 | } else { |
| 1129 | fval = ival; |
| 1130 | Py_INCREF(fval); |
| 1131 | } |
| 1132 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1133 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1136 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1137 | (used by posix_stat() and posix_fstat()) */ |
| 1138 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1139 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1140 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1141 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1142 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1143 | if (v == NULL) |
| 1144 | return NULL; |
| 1145 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1146 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1147 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1148 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1149 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1150 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1151 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1152 | #endif |
| 1153 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1154 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1155 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1156 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1157 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1158 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1159 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); |
| 1160 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); |
| 1161 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1162 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1163 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1164 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1165 | #else |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1166 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1167 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1168 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1169 | #if defined(HAVE_STAT_TV_NSEC) |
| 1170 | ansec = st->st_atim.tv_nsec; |
| 1171 | mnsec = st->st_mtim.tv_nsec; |
| 1172 | cnsec = st->st_ctim.tv_nsec; |
| 1173 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1174 | ansec = st->st_atimespec.tv_nsec; |
| 1175 | mnsec = st->st_mtimespec.tv_nsec; |
| 1176 | cnsec = st->st_ctimespec.tv_nsec; |
| 1177 | #elif defined(HAVE_STAT_NSEC) |
| 1178 | ansec = st->st_atime_nsec; |
| 1179 | mnsec = st->st_mtime_nsec; |
| 1180 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1181 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1182 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1183 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1184 | fill_time(v, 7, st->st_atime, ansec); |
| 1185 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1186 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1187 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1188 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1189 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1190 | PyInt_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1191 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1192 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1193 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1194 | PyInt_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1195 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1196 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1197 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1198 | PyInt_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1199 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1200 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1201 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1202 | PyInt_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1203 | #endif |
| 1204 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1205 | { |
| 1206 | PyObject *val; |
| 1207 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1208 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1209 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1210 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1211 | #else |
| 1212 | bnsec = 0; |
| 1213 | #endif |
| 1214 | if (_stat_float_times) { |
| 1215 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1216 | } else { |
| 1217 | val = PyInt_FromLong((long)bsec); |
| 1218 | } |
| 1219 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1220 | val); |
| 1221 | } |
| 1222 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1223 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1224 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1225 | PyInt_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1226 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (PyErr_Occurred()) { |
| 1229 | Py_DECREF(v); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | return v; |
| 1234 | } |
| 1235 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1236 | #ifdef MS_WINDOWS |
| 1237 | |
| 1238 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1239 | where / can be used in place of \ and the trailing slash is optional. |
| 1240 | Both SERVER and SHARE must have at least one character. |
| 1241 | */ |
| 1242 | |
| 1243 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1244 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1245 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1246 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Kristján Valur Jónsson | dbeaa69 | 2006-06-09 16:28:01 +0000 | [diff] [blame] | 1247 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1248 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1249 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1250 | IsUNCRootA(char *path, int pathlen) |
| 1251 | { |
| 1252 | #define ISSLASH ISSLASHA |
| 1253 | |
| 1254 | int i, share; |
| 1255 | |
| 1256 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1257 | /* minimum UNCRoot is \\x\y */ |
| 1258 | return FALSE; |
| 1259 | for (i = 2; i < pathlen ; i++) |
| 1260 | if (ISSLASH(path[i])) break; |
| 1261 | if (i == 2 || i == pathlen) |
| 1262 | /* do not allow \\\SHARE or \\SERVER */ |
| 1263 | return FALSE; |
| 1264 | share = i+1; |
| 1265 | for (i = share; i < pathlen; i++) |
| 1266 | if (ISSLASH(path[i])) break; |
| 1267 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1268 | |
| 1269 | #undef ISSLASH |
| 1270 | } |
| 1271 | |
| 1272 | #ifdef Py_WIN_WIDE_FILENAMES |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1273 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1274 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1275 | { |
| 1276 | #define ISSLASH ISSLASHW |
| 1277 | |
| 1278 | int i, share; |
| 1279 | |
| 1280 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1281 | /* minimum UNCRoot is \\x\y */ |
| 1282 | return FALSE; |
| 1283 | for (i = 2; i < pathlen ; i++) |
| 1284 | if (ISSLASH(path[i])) break; |
| 1285 | if (i == 2 || i == pathlen) |
| 1286 | /* do not allow \\\SHARE or \\SERVER */ |
| 1287 | return FALSE; |
| 1288 | share = i+1; |
| 1289 | for (i = share; i < pathlen; i++) |
| 1290 | if (ISSLASH(path[i])) break; |
| 1291 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1292 | |
| 1293 | #undef ISSLASH |
| 1294 | } |
| 1295 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 1296 | #endif /* MS_WINDOWS */ |
| 1297 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1298 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1299 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1300 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1301 | #ifdef __VMS |
| 1302 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1303 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1304 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1305 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1306 | char *wformat, |
| 1307 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1308 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1309 | STRUCT_STAT st; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1310 | char *path = NULL; /* pass this to stat; do not free() it */ |
| 1311 | char *pathfree = NULL; /* this memory must be free'd */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1312 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1313 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1314 | |
| 1315 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1316 | /* If on wide-character-capable OS see if argument |
| 1317 | is Unicode and if so use wide API. */ |
| 1318 | if (unicode_file_names()) { |
| 1319 | PyUnicodeObject *po; |
| 1320 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1321 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 1322 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1323 | Py_BEGIN_ALLOW_THREADS |
| 1324 | /* PyUnicode_AS_UNICODE result OK without |
| 1325 | thread lock as it is a simple dereference. */ |
| 1326 | res = wstatfunc(wpath, &st); |
| 1327 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1328 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1329 | if (res != 0) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1330 | return win32_error_unicode("stat", wpath); |
| 1331 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1332 | } |
| 1333 | /* Drop the argument parsing error as narrow strings |
| 1334 | are also valid. */ |
| 1335 | PyErr_Clear(); |
| 1336 | } |
| 1337 | #endif |
| 1338 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1339 | if (!PyArg_ParseTuple(args, format, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1340 | Py_FileSystemDefaultEncoding, &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | return NULL; |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1342 | pathfree = path; |
Guido van Rossum | ace88ae | 2000-04-21 18:54:45 +0000 | [diff] [blame] | 1343 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1344 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1345 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1346 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1347 | |
| 1348 | if (res != 0) { |
| 1349 | #ifdef MS_WINDOWS |
| 1350 | result = win32_error("stat", pathfree); |
| 1351 | #else |
| 1352 | result = posix_error_with_filename(pathfree); |
| 1353 | #endif |
| 1354 | } |
| 1355 | else |
| 1356 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1357 | |
Tim Peters | 500bd03 | 2001-12-19 19:05:01 +0000 | [diff] [blame] | 1358 | PyMem_Free(pathfree); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1359 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1362 | /* POSIX methods */ |
| 1363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1364 | PyDoc_STRVAR(posix_access__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1365 | "access(path, mode) -> 1 if granted, 0 otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1366 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1367 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1368 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1369 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1370 | 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] | 1371 | |
| 1372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1373 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1374 | { |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1375 | char *path; |
| 1376 | int mode; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1377 | |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1378 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1379 | DWORD attr; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1380 | if (unicode_file_names()) { |
| 1381 | PyUnicodeObject *po; |
| 1382 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1383 | Py_BEGIN_ALLOW_THREADS |
| 1384 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1385 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1386 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1387 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1388 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1389 | } |
| 1390 | /* Drop the argument parsing error as narrow strings |
| 1391 | are also valid. */ |
| 1392 | PyErr_Clear(); |
| 1393 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1394 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1395 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 1396 | return 0; |
| 1397 | Py_BEGIN_ALLOW_THREADS |
| 1398 | attr = GetFileAttributesA(path); |
| 1399 | Py_END_ALLOW_THREADS |
| 1400 | PyMem_Free(path); |
| 1401 | finish: |
| 1402 | if (attr == 0xFFFFFFFF) |
| 1403 | /* File does not exist, or cannot read attributes */ |
| 1404 | return PyBool_FromLong(0); |
| 1405 | /* Access is possible if either write access wasn't requested, or |
| 1406 | the file isn't read-only. */ |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 1407 | return PyBool_FromLong(!(mode & 2) || !(attr & FILE_ATTRIBUTE_READONLY)); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1408 | #else |
| 1409 | int res; |
Martin v. Löwis | b60ae99 | 2005-03-08 09:10:29 +0000 | [diff] [blame] | 1410 | if (!PyArg_ParseTuple(args, "eti:access", |
| 1411 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1412 | return NULL; |
| 1413 | Py_BEGIN_ALLOW_THREADS |
| 1414 | res = access(path, mode); |
| 1415 | Py_END_ALLOW_THREADS |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 1416 | PyMem_Free(path); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1417 | return PyBool_FromLong(res == 0); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1418 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1421 | #ifndef F_OK |
| 1422 | #define F_OK 0 |
| 1423 | #endif |
| 1424 | #ifndef R_OK |
| 1425 | #define R_OK 4 |
| 1426 | #endif |
| 1427 | #ifndef W_OK |
| 1428 | #define W_OK 2 |
| 1429 | #endif |
| 1430 | #ifndef X_OK |
| 1431 | #define X_OK 1 |
| 1432 | #endif |
| 1433 | |
| 1434 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1435 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1436 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1437 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1438 | |
| 1439 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1440 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1441 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1442 | int id; |
| 1443 | char *ret; |
| 1444 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1445 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1446 | return NULL; |
| 1447 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1448 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1449 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1450 | if (id == 0) { |
| 1451 | ret = ttyname(); |
| 1452 | } |
| 1453 | else { |
| 1454 | ret = NULL; |
| 1455 | } |
| 1456 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1457 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1458 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1459 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1460 | return posix_error(); |
| 1461 | return PyString_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1462 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1463 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1464 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1465 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1466 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1467 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1468 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1469 | |
| 1470 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1471 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1472 | { |
| 1473 | char *ret; |
| 1474 | char buffer[L_ctermid]; |
| 1475 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1476 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1477 | ret = ctermid_r(buffer); |
| 1478 | #else |
| 1479 | ret = ctermid(buffer); |
| 1480 | #endif |
| 1481 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1482 | return posix_error(); |
| 1483 | return PyString_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1484 | } |
| 1485 | #endif |
| 1486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1487 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1488 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1489 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1490 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1491 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1492 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1493 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1494 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1495 | return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1496 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1497 | return posix_1str(args, "et:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1498 | #elif defined(__VMS) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1499 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1500 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1501 | return posix_1str(args, "et:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1502 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1505 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1506 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1507 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1508 | 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] | 1509 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1510 | |
| 1511 | static PyObject * |
| 1512 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1513 | { |
| 1514 | return posix_fildes(fdobj, fchdir); |
| 1515 | } |
| 1516 | #endif /* HAVE_FCHDIR */ |
| 1517 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1519 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1520 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1521 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1522 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1523 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1524 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1525 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1526 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1527 | int i; |
| 1528 | int res; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1529 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1530 | DWORD attr; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1531 | if (unicode_file_names()) { |
| 1532 | PyUnicodeObject *po; |
| 1533 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1534 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1535 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1536 | if (attr != 0xFFFFFFFF) { |
| 1537 | if (i & _S_IWRITE) |
| 1538 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1539 | else |
| 1540 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1541 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 1542 | } |
| 1543 | else |
| 1544 | res = 0; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1545 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1546 | if (!res) |
| 1547 | return win32_error_unicode("chmod", |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1548 | PyUnicode_AS_UNICODE(po)); |
| 1549 | Py_INCREF(Py_None); |
| 1550 | return Py_None; |
| 1551 | } |
| 1552 | /* Drop the argument parsing error as narrow strings |
| 1553 | are also valid. */ |
| 1554 | PyErr_Clear(); |
| 1555 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1556 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
| 1557 | &path, &i)) |
| 1558 | return NULL; |
| 1559 | Py_BEGIN_ALLOW_THREADS |
| 1560 | attr = GetFileAttributesA(path); |
| 1561 | if (attr != 0xFFFFFFFF) { |
| 1562 | if (i & _S_IWRITE) |
| 1563 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1564 | else |
| 1565 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1566 | res = SetFileAttributesA(path, attr); |
| 1567 | } |
| 1568 | else |
| 1569 | res = 0; |
| 1570 | Py_END_ALLOW_THREADS |
| 1571 | if (!res) { |
| 1572 | win32_error("chmod", path); |
| 1573 | PyMem_Free(path); |
| 1574 | return NULL; |
| 1575 | } |
Martin v. Löwis | 9f485bc | 2006-05-08 05:25:56 +0000 | [diff] [blame] | 1576 | PyMem_Free(path); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1577 | Py_INCREF(Py_None); |
| 1578 | return Py_None; |
| 1579 | #else /* Py_WIN_WIDE_FILENAMES */ |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1580 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1581 | &path, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1582 | return NULL; |
| 1583 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1584 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1585 | Py_END_ALLOW_THREADS |
| 1586 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1587 | return posix_error_with_allocated_filename(path); |
| 1588 | PyMem_Free(path); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1589 | Py_INCREF(Py_None); |
| 1590 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1591 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1594 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1595 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1596 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1597 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1598 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1599 | |
| 1600 | static PyObject * |
| 1601 | posix_chroot(PyObject *self, PyObject *args) |
| 1602 | { |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 1603 | return posix_1str(args, "et:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1604 | } |
| 1605 | #endif |
| 1606 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1607 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1608 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1609 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1610 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1611 | |
| 1612 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1613 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1614 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1615 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1616 | } |
| 1617 | #endif /* HAVE_FSYNC */ |
| 1618 | |
| 1619 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1620 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1621 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1622 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1623 | #endif |
| 1624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1625 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1626 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1627 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1628 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1629 | |
| 1630 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1631 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1632 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1633 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1634 | } |
| 1635 | #endif /* HAVE_FDATASYNC */ |
| 1636 | |
| 1637 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1638 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1639 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1640 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1641 | 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] | 1642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1644 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1645 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1646 | char *path = NULL; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1647 | int uid, gid; |
| 1648 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1649 | if (!PyArg_ParseTuple(args, "etii:chown", |
| 1650 | Py_FileSystemDefaultEncoding, &path, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1651 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1652 | return NULL; |
| 1653 | Py_BEGIN_ALLOW_THREADS |
| 1654 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1655 | Py_END_ALLOW_THREADS |
| 1656 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1657 | return posix_error_with_allocated_filename(path); |
| 1658 | PyMem_Free(path); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1659 | Py_INCREF(Py_None); |
| 1660 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1661 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1662 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1663 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1664 | #ifdef HAVE_LCHOWN |
| 1665 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1666 | "lchown(path, uid, gid)\n\n\ |
| 1667 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1668 | This function will not follow symbolic links."); |
| 1669 | |
| 1670 | static PyObject * |
| 1671 | posix_lchown(PyObject *self, PyObject *args) |
| 1672 | { |
| 1673 | char *path = NULL; |
| 1674 | int uid, gid; |
| 1675 | int res; |
| 1676 | if (!PyArg_ParseTuple(args, "etii:lchown", |
| 1677 | Py_FileSystemDefaultEncoding, &path, |
| 1678 | &uid, &gid)) |
| 1679 | return NULL; |
| 1680 | Py_BEGIN_ALLOW_THREADS |
| 1681 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 1682 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1683 | if (res < 0) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1684 | return posix_error_with_allocated_filename(path); |
| 1685 | PyMem_Free(path); |
| 1686 | Py_INCREF(Py_None); |
| 1687 | return Py_None; |
| 1688 | } |
| 1689 | #endif /* HAVE_LCHOWN */ |
| 1690 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1691 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1692 | #ifdef HAVE_GETCWD |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1693 | PyDoc_STRVAR(posix_getcwd__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1694 | "getcwd() -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1695 | Return a string representing the current working directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1696 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1697 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1698 | posix_getcwd(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1699 | { |
| 1700 | char buf[1026]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1701 | char *res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1702 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1703 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1704 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1705 | res = _getcwd2(buf, sizeof buf); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1706 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1707 | res = getcwd(buf, sizeof buf); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1708 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1709 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1710 | if (res == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1711 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1712 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1713 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1714 | |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1715 | #ifdef Py_USING_UNICODE |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1716 | PyDoc_STRVAR(posix_getcwdu__doc__, |
| 1717 | "getcwdu() -> path\n\n\ |
| 1718 | Return a unicode string representing the current working directory."); |
| 1719 | |
| 1720 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1721 | posix_getcwdu(PyObject *self, PyObject *noargs) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1722 | { |
| 1723 | char buf[1026]; |
| 1724 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1725 | |
| 1726 | #ifdef Py_WIN_WIDE_FILENAMES |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1727 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1728 | if (unicode_file_names()) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1729 | wchar_t wbuf[1026]; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1730 | wchar_t *wbuf2 = wbuf; |
| 1731 | PyObject *resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1732 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1733 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 1734 | /* If the buffer is large enough, len does not include the |
| 1735 | terminating \0. If the buffer is too small, len includes |
| 1736 | the space needed for the terminator. */ |
| 1737 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 1738 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 1739 | if (wbuf2) |
| 1740 | len = GetCurrentDirectoryW(len, wbuf2); |
| 1741 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1742 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 1743 | if (!wbuf2) { |
| 1744 | PyErr_NoMemory(); |
| 1745 | return NULL; |
| 1746 | } |
| 1747 | if (!len) { |
| 1748 | if (wbuf2 != wbuf) free(wbuf2); |
| 1749 | return win32_error("getcwdu", NULL); |
| 1750 | } |
| 1751 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 1752 | if (wbuf2 != wbuf) free(wbuf2); |
| 1753 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1754 | } |
| 1755 | #endif |
| 1756 | |
| 1757 | Py_BEGIN_ALLOW_THREADS |
| 1758 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 1759 | res = _getcwd2(buf, sizeof buf); |
| 1760 | #else |
| 1761 | res = getcwd(buf, sizeof buf); |
| 1762 | #endif |
| 1763 | Py_END_ALLOW_THREADS |
| 1764 | if (res == NULL) |
| 1765 | return posix_error(); |
| 1766 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); |
| 1767 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 1768 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 1769 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1770 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1771 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1772 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1773 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1774 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1775 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1776 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1777 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1778 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1779 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 1780 | return posix_2str(args, "etet:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1781 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1782 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1783 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1785 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1786 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1787 | Return a list containing the names of the entries in the directory.\n\ |
| 1788 | \n\ |
| 1789 | path: path of directory to list\n\ |
| 1790 | \n\ |
| 1791 | 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] | 1792 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1794 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1795 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1796 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1797 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 1798 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1799 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1800 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1801 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1802 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1803 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1804 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1805 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1806 | char *bufptr = namebuf; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1807 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1808 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1809 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1810 | /* If on wide-character-capable OS see if argument |
| 1811 | is Unicode and if so use wide API. */ |
| 1812 | if (unicode_file_names()) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1813 | PyObject *po; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1814 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 1815 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1816 | Py_UNICODE *wnamebuf; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1817 | Py_UNICODE wch; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1818 | /* Overallocate for \\*.*\0 */ |
| 1819 | len = PyUnicode_GET_SIZE(po); |
| 1820 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 1821 | if (!wnamebuf) { |
| 1822 | PyErr_NoMemory(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1823 | return NULL; |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1824 | } |
| 1825 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 1826 | wch = len > 0 ? wnamebuf[len-1] : '\0'; |
| 1827 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 1828 | wnamebuf[len++] = L'\\'; |
| 1829 | wcscpy(wnamebuf + len, L"*.*"); |
| 1830 | if ((d = PyList_New(0)) == NULL) { |
| 1831 | free(wnamebuf); |
| 1832 | return NULL; |
| 1833 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1834 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 1835 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1836 | int error = GetLastError(); |
| 1837 | if (error == ERROR_FILE_NOT_FOUND) { |
| 1838 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1839 | return d; |
| 1840 | } |
| 1841 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1842 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 1843 | free(wnamebuf); |
| 1844 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1845 | } |
| 1846 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1847 | /* Skip over . and .. */ |
| 1848 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 1849 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 1850 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 1851 | if (v == NULL) { |
| 1852 | Py_DECREF(d); |
| 1853 | d = NULL; |
| 1854 | break; |
| 1855 | } |
| 1856 | if (PyList_Append(d, v) != 0) { |
| 1857 | Py_DECREF(v); |
| 1858 | Py_DECREF(d); |
| 1859 | d = NULL; |
| 1860 | break; |
| 1861 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1862 | Py_DECREF(v); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1863 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1864 | Py_BEGIN_ALLOW_THREADS |
| 1865 | result = FindNextFileW(hFindFile, &wFileData); |
| 1866 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 1867 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 1868 | it got to the end of the directory. */ |
| 1869 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 1870 | Py_DECREF(d); |
| 1871 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 1872 | FindClose(hFindFile); |
| 1873 | free(wnamebuf); |
| 1874 | return NULL; |
| 1875 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1876 | } while (result == TRUE); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1877 | |
| 1878 | if (FindClose(hFindFile) == FALSE) { |
| 1879 | Py_DECREF(d); |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1880 | win32_error_unicode("FindClose", wnamebuf); |
| 1881 | free(wnamebuf); |
| 1882 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1883 | } |
Martin v. Löwis | e3edaea | 2006-05-15 05:51:36 +0000 | [diff] [blame] | 1884 | free(wnamebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1885 | return d; |
| 1886 | } |
| 1887 | /* Drop the argument parsing error as narrow strings |
| 1888 | are also valid. */ |
| 1889 | PyErr_Clear(); |
| 1890 | } |
| 1891 | #endif |
| 1892 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1893 | if (!PyArg_ParseTuple(args, "et#:listdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1894 | Py_FileSystemDefaultEncoding, &bufptr, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1895 | return NULL; |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 1896 | if (len > 0) { |
| 1897 | char ch = namebuf[len-1]; |
| 1898 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 1899 | namebuf[len++] = '/'; |
| 1900 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1901 | strcpy(namebuf + len, "*.*"); |
| 1902 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1903 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1904 | return NULL; |
| 1905 | |
| 1906 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 1907 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Martin v. Löwis | 682b1bb | 2006-05-12 12:27:28 +0000 | [diff] [blame] | 1908 | int error = GetLastError(); |
| 1909 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1910 | return d; |
| 1911 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1912 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1913 | } |
| 1914 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 1915 | /* Skip over . and .. */ |
| 1916 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 1917 | strcmp(FileData.cFileName, "..") != 0) { |
| 1918 | v = PyString_FromString(FileData.cFileName); |
| 1919 | if (v == NULL) { |
| 1920 | Py_DECREF(d); |
| 1921 | d = NULL; |
| 1922 | break; |
| 1923 | } |
| 1924 | if (PyList_Append(d, v) != 0) { |
| 1925 | Py_DECREF(v); |
| 1926 | Py_DECREF(d); |
| 1927 | d = NULL; |
| 1928 | break; |
| 1929 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1930 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1931 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1932 | Py_BEGIN_ALLOW_THREADS |
| 1933 | result = FindNextFile(hFindFile, &FileData); |
| 1934 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 982e9fe | 2006-07-24 12:54:17 +0000 | [diff] [blame] | 1935 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 1936 | it got to the end of the directory. */ |
| 1937 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 1938 | Py_DECREF(d); |
| 1939 | win32_error("FindNextFile", namebuf); |
| 1940 | FindClose(hFindFile); |
| 1941 | return NULL; |
| 1942 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 1943 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1944 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1945 | if (FindClose(hFindFile) == FALSE) { |
| 1946 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1947 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1948 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1949 | |
| 1950 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1951 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 1952 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1953 | |
| 1954 | #ifndef MAX_PATH |
| 1955 | #define MAX_PATH CCHMAXPATH |
| 1956 | #endif |
| 1957 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 1958 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1959 | PyObject *d, *v; |
| 1960 | char namebuf[MAX_PATH+5]; |
| 1961 | HDIR hdir = 1; |
| 1962 | ULONG srchcnt = 1; |
| 1963 | FILEFINDBUF3 ep; |
| 1964 | APIRET rc; |
| 1965 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1966 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1967 | return NULL; |
| 1968 | if (len >= MAX_PATH) { |
| 1969 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | strcpy(namebuf, name); |
| 1973 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1974 | if (*pt == ALTSEP) |
| 1975 | *pt = SEP; |
| 1976 | if (namebuf[len-1] != SEP) |
| 1977 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1978 | strcpy(namebuf + len, "*.*"); |
| 1979 | |
| 1980 | if ((d = PyList_New(0)) == NULL) |
| 1981 | return NULL; |
| 1982 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1983 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 1984 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1985 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1986 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 1987 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 1988 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1989 | |
| 1990 | if (rc != NO_ERROR) { |
| 1991 | errno = ENOENT; |
Barry Warsaw | f63b8cc | 1999-05-27 23:13:21 +0000 | [diff] [blame] | 1992 | return posix_error_with_filename(name); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1995 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 1996 | do { |
| 1997 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1998 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 1999 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2000 | |
| 2001 | strcpy(namebuf, ep.achName); |
| 2002 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2003 | /* Leave Case of Name Alone -- In Native Form */ |
| 2004 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2005 | |
| 2006 | v = PyString_FromString(namebuf); |
| 2007 | if (v == NULL) { |
| 2008 | Py_DECREF(d); |
| 2009 | d = NULL; |
| 2010 | break; |
| 2011 | } |
| 2012 | if (PyList_Append(d, v) != 0) { |
| 2013 | Py_DECREF(v); |
| 2014 | Py_DECREF(d); |
| 2015 | d = NULL; |
| 2016 | break; |
| 2017 | } |
| 2018 | Py_DECREF(v); |
| 2019 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2020 | } |
| 2021 | |
| 2022 | return d; |
| 2023 | #else |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2024 | |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2025 | char *name = NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2026 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2027 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2028 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2029 | int arg_is_unicode = 1; |
| 2030 | |
Georg Brandl | 05e89b8 | 2006-04-11 07:04:06 +0000 | [diff] [blame] | 2031 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2032 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2033 | arg_is_unicode = 0; |
| 2034 | PyErr_Clear(); |
| 2035 | } |
| 2036 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2037 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2038 | if ((dirp = opendir(name)) == NULL) { |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2039 | return posix_error_with_allocated_filename(name); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2040 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2041 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2042 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2043 | PyMem_Free(name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2044 | return NULL; |
| 2045 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2046 | for (;;) { |
| 2047 | Py_BEGIN_ALLOW_THREADS |
| 2048 | ep = readdir(dirp); |
| 2049 | Py_END_ALLOW_THREADS |
| 2050 | if (ep == NULL) |
| 2051 | break; |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2052 | if (ep->d_name[0] == '.' && |
| 2053 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2054 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2055 | continue; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2056 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2057 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2058 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2059 | d = NULL; |
| 2060 | break; |
| 2061 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2062 | #ifdef Py_USING_UNICODE |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2063 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2064 | PyObject *w; |
| 2065 | |
| 2066 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2067 | Py_FileSystemDefaultEncoding, |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2068 | "strict"); |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2069 | if (w != NULL) { |
| 2070 | Py_DECREF(v); |
| 2071 | v = w; |
| 2072 | } |
| 2073 | else { |
| 2074 | /* fall back to the original byte string, as |
| 2075 | discussed in patch #683592 */ |
| 2076 | PyErr_Clear(); |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2077 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2078 | } |
| 2079 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2080 | if (PyList_Append(d, v) != 0) { |
| 2081 | Py_DECREF(v); |
| 2082 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2083 | d = NULL; |
| 2084 | break; |
| 2085 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2086 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2087 | } |
Georg Brandl | bbfe4fa | 2006-04-11 06:47:43 +0000 | [diff] [blame] | 2088 | if (errno != 0 && d != NULL) { |
| 2089 | /* readdir() returned NULL and set errno */ |
| 2090 | closedir(dirp); |
| 2091 | Py_DECREF(d); |
| 2092 | return posix_error_with_allocated_filename(name); |
| 2093 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2094 | closedir(dirp); |
Just van Rossum | 2fe07fd | 2003-03-03 19:07:13 +0000 | [diff] [blame] | 2095 | PyMem_Free(name); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2096 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2097 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2098 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2099 | #endif /* which OS */ |
| 2100 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2101 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2102 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2103 | /* A helper function for abspath on win32 */ |
| 2104 | static PyObject * |
| 2105 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2106 | { |
| 2107 | /* assume encoded strings wont more than double no of chars */ |
| 2108 | char inbuf[MAX_PATH*2]; |
| 2109 | char *inbufp = inbuf; |
Tim Peters | 67d70eb | 2006-03-01 04:35:45 +0000 | [diff] [blame] | 2110 | Py_ssize_t insize = sizeof(inbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2111 | char outbuf[MAX_PATH*2]; |
| 2112 | char *temp; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2113 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2114 | if (unicode_file_names()) { |
| 2115 | PyUnicodeObject *po; |
| 2116 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2117 | Py_UNICODE woutbuf[MAX_PATH*2]; |
| 2118 | Py_UNICODE *wtemp; |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2119 | if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2120 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2121 | woutbuf, &wtemp)) |
| 2122 | return win32_error("GetFullPathName", ""); |
| 2123 | return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); |
| 2124 | } |
| 2125 | /* Drop the argument parsing error as narrow strings |
| 2126 | are also valid. */ |
| 2127 | PyErr_Clear(); |
| 2128 | } |
| 2129 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2130 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname", |
| 2131 | Py_FileSystemDefaultEncoding, &inbufp, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2132 | &insize)) |
| 2133 | return NULL; |
| 2134 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2135 | outbuf, &temp)) |
| 2136 | return win32_error("GetFullPathName", inbuf); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2137 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2138 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2139 | Py_FileSystemDefaultEncoding, NULL); |
| 2140 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2141 | return PyString_FromString(outbuf); |
| 2142 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2143 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2145 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2146 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2148 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2149 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2150 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2151 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2152 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2153 | char *path = NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2154 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2155 | |
| 2156 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2157 | if (unicode_file_names()) { |
| 2158 | PyUnicodeObject *po; |
Mark Hammond | 05107b6 | 2003-02-19 04:08:27 +0000 | [diff] [blame] | 2159 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2160 | Py_BEGIN_ALLOW_THREADS |
| 2161 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2162 | it is a simple dereference. */ |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2163 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2164 | Py_END_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2165 | if (!res) |
| 2166 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2167 | Py_INCREF(Py_None); |
| 2168 | return Py_None; |
| 2169 | } |
| 2170 | /* Drop the argument parsing error as narrow strings |
| 2171 | are also valid. */ |
| 2172 | PyErr_Clear(); |
| 2173 | } |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2174 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
| 2175 | Py_FileSystemDefaultEncoding, &path, &mode)) |
| 2176 | return NULL; |
| 2177 | Py_BEGIN_ALLOW_THREADS |
| 2178 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2179 | it is a simple dereference. */ |
| 2180 | res = CreateDirectoryA(path, NULL); |
| 2181 | Py_END_ALLOW_THREADS |
| 2182 | if (!res) { |
| 2183 | win32_error("mkdir", path); |
| 2184 | PyMem_Free(path); |
| 2185 | return NULL; |
| 2186 | } |
| 2187 | PyMem_Free(path); |
| 2188 | Py_INCREF(Py_None); |
| 2189 | return Py_None; |
| 2190 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2191 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2192 | if (!PyArg_ParseTuple(args, "et|i:mkdir", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2193 | Py_FileSystemDefaultEncoding, &path, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2194 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2195 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2196 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2197 | res = mkdir(path); |
| 2198 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2199 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2200 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2201 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2202 | if (res < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2203 | return posix_error_with_allocated_filename(path); |
| 2204 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2205 | Py_INCREF(Py_None); |
| 2206 | return Py_None; |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2207 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2210 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2211 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2212 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2213 | #include <sys/resource.h> |
| 2214 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2215 | |
Neal Norwitz | 1818ed7 | 2006-03-26 00:29:48 +0000 | [diff] [blame] | 2216 | |
| 2217 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2218 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2219 | "nice(inc) -> new_priority\n\n\ |
| 2220 | 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] | 2221 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2222 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2223 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2224 | { |
| 2225 | int increment, value; |
| 2226 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2227 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2228 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2229 | |
| 2230 | /* There are two flavours of 'nice': one that returns the new |
| 2231 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2232 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2233 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2234 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2235 | If we are of the nice family that returns the new priority, we |
| 2236 | need to clear errno before the call, and check if errno is filled |
| 2237 | before calling posix_error() on a returnvalue of -1, because the |
| 2238 | -1 may be the actual new priority! */ |
| 2239 | |
| 2240 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2241 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2242 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2243 | if (value == 0) |
| 2244 | value = getpriority(PRIO_PROCESS, 0); |
| 2245 | #endif |
| 2246 | if (value == -1 && errno != 0) |
| 2247 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2248 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2249 | return PyInt_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2250 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2251 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2253 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2254 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2255 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2256 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2257 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2258 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2259 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2260 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2261 | PyObject *o1, *o2; |
| 2262 | char *p1, *p2; |
| 2263 | BOOL result; |
| 2264 | if (unicode_file_names()) { |
| 2265 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 2266 | convert_to_unicode, &o1, |
| 2267 | convert_to_unicode, &o2)) |
| 2268 | PyErr_Clear(); |
| 2269 | else { |
| 2270 | Py_BEGIN_ALLOW_THREADS |
| 2271 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2272 | PyUnicode_AsUnicode(o2)); |
| 2273 | Py_END_ALLOW_THREADS |
| 2274 | Py_DECREF(o1); |
| 2275 | Py_DECREF(o2); |
| 2276 | if (!result) |
| 2277 | return win32_error("rename", NULL); |
| 2278 | Py_INCREF(Py_None); |
| 2279 | return Py_None; |
| 2280 | } |
| 2281 | } |
| 2282 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2283 | return NULL; |
| 2284 | Py_BEGIN_ALLOW_THREADS |
| 2285 | result = MoveFileA(p1, p2); |
| 2286 | Py_END_ALLOW_THREADS |
| 2287 | if (!result) |
| 2288 | return win32_error("rename", NULL); |
| 2289 | Py_INCREF(Py_None); |
| 2290 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2291 | #else |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 2292 | return posix_2str(args, "etet:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2293 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2297 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2298 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2299 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2301 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2302 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2303 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2304 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2305 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2306 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2307 | return posix_1str(args, "et:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2308 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2312 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2313 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2314 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2316 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2317 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2318 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2319 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2320 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2321 | #else |
| 2322 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); |
| 2323 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2326 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2327 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2328 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2329 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2330 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2331 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2332 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2333 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2334 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2335 | char *command; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2336 | long sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2337 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2338 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2339 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2340 | sts = system(command); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2341 | Py_END_ALLOW_THREADS |
| 2342 | return PyInt_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2343 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2344 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2345 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2347 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2348 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2349 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2350 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2351 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2352 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2353 | { |
| 2354 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2355 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2356 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2357 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2358 | if (i < 0) |
| 2359 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2360 | return PyInt_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2364 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2365 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2366 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2368 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2369 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2370 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2371 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2373 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2374 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2375 | #ifdef MS_WINDOWS |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2376 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2377 | #else |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 2378 | return posix_1str(args, "et:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2379 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2382 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2383 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2384 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2385 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2386 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2387 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2388 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2389 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2390 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2391 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2392 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2393 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2394 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2395 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2396 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2397 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2398 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2399 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2400 | u.sysname, |
| 2401 | u.nodename, |
| 2402 | u.release, |
| 2403 | u.version, |
| 2404 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2406 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2407 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2408 | static int |
| 2409 | extract_time(PyObject *t, long* sec, long* usec) |
| 2410 | { |
| 2411 | long intval; |
| 2412 | if (PyFloat_Check(t)) { |
| 2413 | double tval = PyFloat_AsDouble(t); |
| 2414 | PyObject *intobj = t->ob_type->tp_as_number->nb_int(t); |
| 2415 | if (!intobj) |
| 2416 | return -1; |
| 2417 | intval = PyInt_AsLong(intobj); |
| 2418 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2419 | if (intval == -1 && PyErr_Occurred()) |
| 2420 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2421 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2422 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2423 | if (*usec < 0) |
| 2424 | /* If rounding gave us a negative number, |
| 2425 | truncate. */ |
| 2426 | *usec = 0; |
| 2427 | return 0; |
| 2428 | } |
| 2429 | intval = PyInt_AsLong(t); |
| 2430 | if (intval == -1 && PyErr_Occurred()) |
| 2431 | return -1; |
| 2432 | *sec = intval; |
| 2433 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2434 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2435 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2437 | PyDoc_STRVAR(posix_utime__doc__, |
Georg Brandl | 9e5b5e4 | 2006-05-17 14:18:20 +0000 | [diff] [blame] | 2438 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2439 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2440 | 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] | 2441 | 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] | 2442 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2443 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2444 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2445 | { |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2446 | #ifdef Py_WIN_WIDE_FILENAMES |
| 2447 | PyObject *arg; |
| 2448 | PyUnicodeObject *obwpath; |
| 2449 | wchar_t *wpath = NULL; |
| 2450 | char *apath = NULL; |
| 2451 | HANDLE hFile; |
| 2452 | long atimesec, mtimesec, ausec, musec; |
| 2453 | FILETIME atime, mtime; |
| 2454 | PyObject *result = NULL; |
| 2455 | |
| 2456 | if (unicode_file_names()) { |
| 2457 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2458 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2459 | Py_BEGIN_ALLOW_THREADS |
| 2460 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 2461 | NULL, OPEN_EXISTING, |
| 2462 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2463 | Py_END_ALLOW_THREADS |
| 2464 | if (hFile == INVALID_HANDLE_VALUE) |
| 2465 | return win32_error_unicode("utime", wpath); |
| 2466 | } else |
| 2467 | /* Drop the argument parsing error as narrow strings |
| 2468 | are also valid. */ |
| 2469 | PyErr_Clear(); |
| 2470 | } |
| 2471 | if (!wpath) { |
| 2472 | if (!PyArg_ParseTuple(args, "etO:utime", |
| 2473 | Py_FileSystemDefaultEncoding, &apath, &arg)) |
| 2474 | return NULL; |
| 2475 | Py_BEGIN_ALLOW_THREADS |
| 2476 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 2477 | NULL, OPEN_EXISTING, |
| 2478 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2479 | Py_END_ALLOW_THREADS |
| 2480 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2481 | win32_error("utime", apath); |
| 2482 | PyMem_Free(apath); |
| 2483 | return NULL; |
| 2484 | } |
| 2485 | PyMem_Free(apath); |
| 2486 | } |
| 2487 | |
| 2488 | if (arg == Py_None) { |
| 2489 | SYSTEMTIME now; |
| 2490 | GetSystemTime(&now); |
| 2491 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2492 | !SystemTimeToFileTime(&now, &atime)) { |
| 2493 | win32_error("utime", NULL); |
| 2494 | goto done; |
| 2495 | } |
| 2496 | } |
| 2497 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2498 | PyErr_SetString(PyExc_TypeError, |
| 2499 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2500 | goto done; |
| 2501 | } |
| 2502 | else { |
| 2503 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2504 | &atimesec, &ausec) == -1) |
| 2505 | goto done; |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 2506 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2507 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2508 | &mtimesec, &musec) == -1) |
| 2509 | goto done; |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 2510 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2511 | } |
| 2512 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2513 | /* Avoid putting the file name into the error here, |
| 2514 | as that may confuse the user into believing that |
| 2515 | something is wrong with the file, when it also |
| 2516 | could be the time stamp that gives a problem. */ |
| 2517 | win32_error("utime", NULL); |
| 2518 | } |
| 2519 | Py_INCREF(Py_None); |
| 2520 | result = Py_None; |
| 2521 | done: |
| 2522 | CloseHandle(hFile); |
| 2523 | return result; |
| 2524 | #else /* Py_WIN_WIDE_FILENAMES */ |
| 2525 | |
Neal Norwitz | 2adf210 | 2004-06-09 01:46:02 +0000 | [diff] [blame] | 2526 | char *path = NULL; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2527 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2528 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2529 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2530 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2531 | #if defined(HAVE_UTIMES) |
| 2532 | struct timeval buf[2]; |
| 2533 | #define ATIME buf[0].tv_sec |
| 2534 | #define MTIME buf[1].tv_sec |
| 2535 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2536 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2537 | struct utimbuf buf; |
| 2538 | #define ATIME buf.actime |
| 2539 | #define MTIME buf.modtime |
| 2540 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2541 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2542 | time_t buf[2]; |
| 2543 | #define ATIME buf[0] |
| 2544 | #define MTIME buf[1] |
| 2545 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2546 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2547 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2548 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2549 | if (!PyArg_ParseTuple(args, "etO:utime", |
Hye-Shik Chang | 2b2c973 | 2004-01-04 13:54:25 +0000 | [diff] [blame] | 2550 | Py_FileSystemDefaultEncoding, &path, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2551 | return NULL; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2552 | if (arg == Py_None) { |
| 2553 | /* optional time values not given */ |
| 2554 | Py_BEGIN_ALLOW_THREADS |
| 2555 | res = utime(path, NULL); |
| 2556 | Py_END_ALLOW_THREADS |
| 2557 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2558 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2559 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2560 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2561 | PyMem_Free(path); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2562 | return NULL; |
| 2563 | } |
| 2564 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2565 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2566 | &atime, &ausec) == -1) { |
| 2567 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2568 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2569 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2570 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2571 | &mtime, &musec) == -1) { |
| 2572 | PyMem_Free(path); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2573 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2574 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2575 | ATIME = atime; |
| 2576 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2577 | #ifdef HAVE_UTIMES |
| 2578 | buf[0].tv_usec = ausec; |
| 2579 | buf[1].tv_usec = musec; |
| 2580 | Py_BEGIN_ALLOW_THREADS |
| 2581 | res = utimes(path, buf); |
| 2582 | Py_END_ALLOW_THREADS |
| 2583 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2584 | Py_BEGIN_ALLOW_THREADS |
| 2585 | res = utime(path, UTIME_ARG); |
| 2586 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2587 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2588 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2589 | if (res < 0) { |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2590 | return posix_error_with_allocated_filename(path); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2591 | } |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2592 | PyMem_Free(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2593 | Py_INCREF(Py_None); |
| 2594 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2595 | #undef UTIME_ARG |
| 2596 | #undef ATIME |
| 2597 | #undef MTIME |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 2598 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2601 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2602 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2604 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2605 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2606 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2607 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2608 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2609 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2610 | { |
| 2611 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2612 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2613 | return NULL; |
| 2614 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2615 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2618 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2619 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2620 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2621 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2622 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2623 | for (i = 0; i < count; i++) |
| 2624 | PyMem_Free(array[i]); |
| 2625 | PyMem_DEL(array); |
| 2626 | } |
| 2627 | #endif |
| 2628 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2629 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2630 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2631 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2632 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2633 | Execute an executable path with arguments, replacing current process.\n\ |
| 2634 | \n\ |
| 2635 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2636 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2637 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2638 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2639 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2640 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2641 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2642 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2643 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2644 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2645 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2646 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 2647 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2648 | argv is a list or tuple of strings. */ |
| 2649 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2650 | if (!PyArg_ParseTuple(args, "etO:execv", |
| 2651 | Py_FileSystemDefaultEncoding, |
| 2652 | &path, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2653 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2654 | if (PyList_Check(argv)) { |
| 2655 | argc = PyList_Size(argv); |
| 2656 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2657 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2658 | else if (PyTuple_Check(argv)) { |
| 2659 | argc = PyTuple_Size(argv); |
| 2660 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2661 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2662 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2663 | 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] | 2664 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2665 | return NULL; |
| 2666 | } |
| 2667 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2668 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2669 | if (argvlist == NULL) { |
| 2670 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2671 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2672 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2673 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2674 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2675 | Py_FileSystemDefaultEncoding, |
| 2676 | &argvlist[i])) { |
| 2677 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2678 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2679 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2680 | PyMem_Free(path); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 2681 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2682 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2683 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2684 | } |
| 2685 | argvlist[argc] = NULL; |
| 2686 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2687 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2688 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2689 | /* If we get here it's definitely an error */ |
| 2690 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2691 | free_string_array(argvlist, argc); |
| 2692 | PyMem_Free(path); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2693 | return posix_error(); |
| 2694 | } |
| 2695 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2697 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2698 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2699 | Execute a path with arguments and environment, replacing current process.\n\ |
| 2700 | \n\ |
| 2701 | path: path of executable file\n\ |
| 2702 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2703 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2705 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2706 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2707 | { |
| 2708 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2709 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2710 | char **argvlist; |
| 2711 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2712 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2713 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2714 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2715 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2716 | |
| 2717 | /* execve has three arguments: (path, argv, env), where |
| 2718 | argv is a list or tuple of strings and env is a dictionary |
| 2719 | like posix.environ. */ |
| 2720 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2721 | if (!PyArg_ParseTuple(args, "etOO:execve", |
| 2722 | Py_FileSystemDefaultEncoding, |
| 2723 | &path, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2724 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2725 | if (PyList_Check(argv)) { |
| 2726 | argc = PyList_Size(argv); |
| 2727 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2728 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2729 | else if (PyTuple_Check(argv)) { |
| 2730 | argc = PyTuple_Size(argv); |
| 2731 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2732 | } |
| 2733 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2734 | PyErr_SetString(PyExc_TypeError, |
| 2735 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2736 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2737 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2738 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2739 | PyErr_SetString(PyExc_TypeError, |
| 2740 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2741 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2744 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2745 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2746 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2747 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2748 | } |
| 2749 | for (i = 0; i < argc; i++) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2750 | if (!PyArg_Parse((*getitem)(argv, i), |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2751 | "et;execve() arg 2 must contain only strings", |
Guido van Rossum | 1e700d2 | 2002-10-16 16:52:11 +0000 | [diff] [blame] | 2752 | Py_FileSystemDefaultEncoding, |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2753 | &argvlist[i])) |
| 2754 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2755 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2756 | goto fail_1; |
| 2757 | } |
| 2758 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2759 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2760 | argvlist[argc] = NULL; |
| 2761 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2762 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2763 | if (i < 0) |
| 2764 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2765 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2766 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2767 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2768 | goto fail_1; |
| 2769 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2770 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2771 | keys = PyMapping_Keys(env); |
| 2772 | vals = PyMapping_Values(env); |
| 2773 | if (!keys || !vals) |
| 2774 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2775 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 2776 | PyErr_SetString(PyExc_TypeError, |
| 2777 | "execve(): env.keys() or env.values() is not a list"); |
| 2778 | goto fail_2; |
| 2779 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2780 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2781 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2782 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2783 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2784 | |
| 2785 | key = PyList_GetItem(keys, pos); |
| 2786 | val = PyList_GetItem(vals, pos); |
| 2787 | if (!key || !val) |
| 2788 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2789 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2790 | if (!PyArg_Parse( |
| 2791 | key, |
| 2792 | "s;execve() arg 3 contains a non-string key", |
| 2793 | &k) || |
| 2794 | !PyArg_Parse( |
| 2795 | val, |
| 2796 | "s;execve() arg 3 contains a non-string value", |
| 2797 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2798 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2799 | goto fail_2; |
| 2800 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2801 | |
| 2802 | #if defined(PYOS_OS2) |
| 2803 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 2804 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 2805 | #endif |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2806 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 2807 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2808 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2809 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2810 | goto fail_2; |
| 2811 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 2812 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2813 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 2814 | #if defined(PYOS_OS2) |
| 2815 | } |
| 2816 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2817 | } |
| 2818 | envlist[envc] = 0; |
| 2819 | |
| 2820 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2821 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2822 | /* If we get here it's definitely an error */ |
| 2823 | |
| 2824 | (void) posix_error(); |
| 2825 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2826 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2827 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2828 | PyMem_DEL(envlist[envc]); |
| 2829 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2830 | fail_1: |
| 2831 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 2832 | Py_XDECREF(vals); |
| 2833 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2834 | fail_0: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2835 | PyMem_Free(path); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2836 | return NULL; |
| 2837 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2838 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 2839 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2840 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2841 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2842 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2843 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2844 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2845 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2846 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2847 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2848 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2849 | |
| 2850 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2851 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2852 | { |
| 2853 | char *path; |
| 2854 | PyObject *argv; |
| 2855 | char **argvlist; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2856 | int mode, i; |
| 2857 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2858 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2859 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2860 | |
| 2861 | /* spawnv has three arguments: (mode, path, argv), where |
| 2862 | argv is a list or tuple of strings. */ |
| 2863 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2864 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, |
| 2865 | Py_FileSystemDefaultEncoding, |
| 2866 | &path, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2867 | return NULL; |
| 2868 | if (PyList_Check(argv)) { |
| 2869 | argc = PyList_Size(argv); |
| 2870 | getitem = PyList_GetItem; |
| 2871 | } |
| 2872 | else if (PyTuple_Check(argv)) { |
| 2873 | argc = PyTuple_Size(argv); |
| 2874 | getitem = PyTuple_GetItem; |
| 2875 | } |
| 2876 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2877 | PyErr_SetString(PyExc_TypeError, |
| 2878 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2879 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2880 | return NULL; |
| 2881 | } |
| 2882 | |
| 2883 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2884 | if (argvlist == NULL) { |
| 2885 | PyMem_Free(path); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2886 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2887 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2888 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2889 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 2890 | Py_FileSystemDefaultEncoding, |
| 2891 | &argvlist[i])) { |
| 2892 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2893 | PyErr_SetString( |
| 2894 | PyExc_TypeError, |
| 2895 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2896 | PyMem_Free(path); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 2897 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | argvlist[argc] = NULL; |
| 2901 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2902 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2903 | Py_BEGIN_ALLOW_THREADS |
| 2904 | spawnval = spawnv(mode, path, argvlist); |
| 2905 | Py_END_ALLOW_THREADS |
| 2906 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 2907 | if (mode == _OLD_P_OVERLAY) |
| 2908 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2909 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2910 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2911 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2912 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2913 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2914 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2915 | free_string_array(argvlist, argc); |
| 2916 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2917 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2918 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2919 | return posix_error(); |
| 2920 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 2921 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 2922 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2923 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2924 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2925 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2929 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2930 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 2931 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2932 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 2933 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2934 | path: path of executable file\n\ |
| 2935 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2936 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2937 | |
| 2938 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2939 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2940 | { |
| 2941 | char *path; |
| 2942 | PyObject *argv, *env; |
| 2943 | char **argvlist; |
| 2944 | char **envlist; |
| 2945 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2946 | int mode, pos, envc; |
| 2947 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2948 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2949 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 2950 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2951 | |
| 2952 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 2953 | argv is a list or tuple of strings and env is a dictionary |
| 2954 | like posix.environ. */ |
| 2955 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2956 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, |
| 2957 | Py_FileSystemDefaultEncoding, |
| 2958 | &path, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2959 | return NULL; |
| 2960 | if (PyList_Check(argv)) { |
| 2961 | argc = PyList_Size(argv); |
| 2962 | getitem = PyList_GetItem; |
| 2963 | } |
| 2964 | else if (PyTuple_Check(argv)) { |
| 2965 | argc = PyTuple_Size(argv); |
| 2966 | getitem = PyTuple_GetItem; |
| 2967 | } |
| 2968 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2969 | PyErr_SetString(PyExc_TypeError, |
| 2970 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2971 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2972 | } |
| 2973 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2974 | PyErr_SetString(PyExc_TypeError, |
| 2975 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2976 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | argvlist = PyMem_NEW(char *, argc+1); |
| 2980 | if (argvlist == NULL) { |
| 2981 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2982 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2983 | } |
| 2984 | for (i = 0; i < argc; i++) { |
| 2985 | if (!PyArg_Parse((*getitem)(argv, i), |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2986 | "et;spawnve() arg 2 must contain only strings", |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2987 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2988 | &argvlist[i])) |
| 2989 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2990 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2991 | goto fail_1; |
| 2992 | } |
| 2993 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2994 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 2995 | argvlist[argc] = NULL; |
| 2996 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2997 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 2998 | if (i < 0) |
| 2999 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3000 | envlist = PyMem_NEW(char *, i + 1); |
| 3001 | if (envlist == NULL) { |
| 3002 | PyErr_NoMemory(); |
| 3003 | goto fail_1; |
| 3004 | } |
| 3005 | envc = 0; |
| 3006 | keys = PyMapping_Keys(env); |
| 3007 | vals = PyMapping_Values(env); |
| 3008 | if (!keys || !vals) |
| 3009 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3010 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3011 | PyErr_SetString(PyExc_TypeError, |
| 3012 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3013 | goto fail_2; |
| 3014 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3015 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3016 | for (pos = 0; pos < i; pos++) { |
| 3017 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3018 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3019 | |
| 3020 | key = PyList_GetItem(keys, pos); |
| 3021 | val = PyList_GetItem(vals, pos); |
| 3022 | if (!key || !val) |
| 3023 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3024 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3025 | if (!PyArg_Parse( |
| 3026 | key, |
| 3027 | "s;spawnve() arg 3 contains a non-string key", |
| 3028 | &k) || |
| 3029 | !PyArg_Parse( |
| 3030 | val, |
| 3031 | "s;spawnve() arg 3 contains a non-string value", |
| 3032 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3033 | { |
| 3034 | goto fail_2; |
| 3035 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3036 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3037 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3038 | if (p == NULL) { |
| 3039 | PyErr_NoMemory(); |
| 3040 | goto fail_2; |
| 3041 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3042 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3043 | envlist[envc++] = p; |
| 3044 | } |
| 3045 | envlist[envc] = 0; |
| 3046 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3047 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3048 | Py_BEGIN_ALLOW_THREADS |
| 3049 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3050 | Py_END_ALLOW_THREADS |
| 3051 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3052 | if (mode == _OLD_P_OVERLAY) |
| 3053 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3054 | |
| 3055 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3056 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3057 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3058 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3059 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3060 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3061 | (void) posix_error(); |
| 3062 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3063 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3064 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3065 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3066 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3067 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3068 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3069 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3070 | while (--envc >= 0) |
| 3071 | PyMem_DEL(envlist[envc]); |
| 3072 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3073 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3074 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3075 | Py_XDECREF(vals); |
| 3076 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3077 | fail_0: |
| 3078 | PyMem_Free(path); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3079 | return res; |
| 3080 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3083 | #if defined(PYOS_OS2) |
| 3084 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3085 | "spawnvp(mode, file, args)\n\n\ |
| 3086 | Execute the program 'file' in a new process, using the environment\n\ |
| 3087 | search path to find the file.\n\ |
| 3088 | \n\ |
| 3089 | mode: mode of process creation\n\ |
| 3090 | file: executable file name\n\ |
| 3091 | args: tuple or list of strings"); |
| 3092 | |
| 3093 | static PyObject * |
| 3094 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3095 | { |
| 3096 | char *path; |
| 3097 | PyObject *argv; |
| 3098 | char **argvlist; |
| 3099 | int mode, i, argc; |
| 3100 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3101 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3102 | |
| 3103 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3104 | argv is a list or tuple of strings. */ |
| 3105 | |
| 3106 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, |
| 3107 | Py_FileSystemDefaultEncoding, |
| 3108 | &path, &argv)) |
| 3109 | return NULL; |
| 3110 | if (PyList_Check(argv)) { |
| 3111 | argc = PyList_Size(argv); |
| 3112 | getitem = PyList_GetItem; |
| 3113 | } |
| 3114 | else if (PyTuple_Check(argv)) { |
| 3115 | argc = PyTuple_Size(argv); |
| 3116 | getitem = PyTuple_GetItem; |
| 3117 | } |
| 3118 | else { |
| 3119 | PyErr_SetString(PyExc_TypeError, |
| 3120 | "spawnvp() arg 2 must be a tuple or list"); |
| 3121 | PyMem_Free(path); |
| 3122 | return NULL; |
| 3123 | } |
| 3124 | |
| 3125 | argvlist = PyMem_NEW(char *, argc+1); |
| 3126 | if (argvlist == NULL) { |
| 3127 | PyMem_Free(path); |
| 3128 | return PyErr_NoMemory(); |
| 3129 | } |
| 3130 | for (i = 0; i < argc; i++) { |
| 3131 | if (!PyArg_Parse((*getitem)(argv, i), "et", |
| 3132 | Py_FileSystemDefaultEncoding, |
| 3133 | &argvlist[i])) { |
| 3134 | free_string_array(argvlist, i); |
| 3135 | PyErr_SetString( |
| 3136 | PyExc_TypeError, |
| 3137 | "spawnvp() arg 2 must contain only strings"); |
| 3138 | PyMem_Free(path); |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | } |
| 3142 | argvlist[argc] = NULL; |
| 3143 | |
| 3144 | Py_BEGIN_ALLOW_THREADS |
| 3145 | #if defined(PYCC_GCC) |
| 3146 | spawnval = spawnvp(mode, path, argvlist); |
| 3147 | #else |
| 3148 | spawnval = _spawnvp(mode, path, argvlist); |
| 3149 | #endif |
| 3150 | Py_END_ALLOW_THREADS |
| 3151 | |
| 3152 | free_string_array(argvlist, argc); |
| 3153 | PyMem_Free(path); |
| 3154 | |
| 3155 | if (spawnval == -1) |
| 3156 | return posix_error(); |
| 3157 | else |
| 3158 | return Py_BuildValue("l", (long) spawnval); |
| 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3163 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3164 | Execute the program 'file' in a new process, using the environment\n\ |
| 3165 | search path to find the file.\n\ |
| 3166 | \n\ |
| 3167 | mode: mode of process creation\n\ |
| 3168 | file: executable file name\n\ |
| 3169 | args: tuple or list of arguments\n\ |
| 3170 | env: dictionary of strings mapping to strings"); |
| 3171 | |
| 3172 | static PyObject * |
| 3173 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3174 | { |
| 3175 | char *path; |
| 3176 | PyObject *argv, *env; |
| 3177 | char **argvlist; |
| 3178 | char **envlist; |
| 3179 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3180 | int mode, i, pos, argc, envc; |
| 3181 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3182 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3183 | int lastarg = 0; |
| 3184 | |
| 3185 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3186 | argv is a list or tuple of strings and env is a dictionary |
| 3187 | like posix.environ. */ |
| 3188 | |
| 3189 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 3190 | Py_FileSystemDefaultEncoding, |
| 3191 | &path, &argv, &env)) |
| 3192 | return NULL; |
| 3193 | if (PyList_Check(argv)) { |
| 3194 | argc = PyList_Size(argv); |
| 3195 | getitem = PyList_GetItem; |
| 3196 | } |
| 3197 | else if (PyTuple_Check(argv)) { |
| 3198 | argc = PyTuple_Size(argv); |
| 3199 | getitem = PyTuple_GetItem; |
| 3200 | } |
| 3201 | else { |
| 3202 | PyErr_SetString(PyExc_TypeError, |
| 3203 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3204 | goto fail_0; |
| 3205 | } |
| 3206 | if (!PyMapping_Check(env)) { |
| 3207 | PyErr_SetString(PyExc_TypeError, |
| 3208 | "spawnvpe() arg 3 must be a mapping object"); |
| 3209 | goto fail_0; |
| 3210 | } |
| 3211 | |
| 3212 | argvlist = PyMem_NEW(char *, argc+1); |
| 3213 | if (argvlist == NULL) { |
| 3214 | PyErr_NoMemory(); |
| 3215 | goto fail_0; |
| 3216 | } |
| 3217 | for (i = 0; i < argc; i++) { |
| 3218 | if (!PyArg_Parse((*getitem)(argv, i), |
| 3219 | "et;spawnvpe() arg 2 must contain only strings", |
| 3220 | Py_FileSystemDefaultEncoding, |
| 3221 | &argvlist[i])) |
| 3222 | { |
| 3223 | lastarg = i; |
| 3224 | goto fail_1; |
| 3225 | } |
| 3226 | } |
| 3227 | lastarg = argc; |
| 3228 | argvlist[argc] = NULL; |
| 3229 | |
| 3230 | i = PyMapping_Size(env); |
| 3231 | if (i < 0) |
| 3232 | goto fail_1; |
| 3233 | envlist = PyMem_NEW(char *, i + 1); |
| 3234 | if (envlist == NULL) { |
| 3235 | PyErr_NoMemory(); |
| 3236 | goto fail_1; |
| 3237 | } |
| 3238 | envc = 0; |
| 3239 | keys = PyMapping_Keys(env); |
| 3240 | vals = PyMapping_Values(env); |
| 3241 | if (!keys || !vals) |
| 3242 | goto fail_2; |
| 3243 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3244 | PyErr_SetString(PyExc_TypeError, |
| 3245 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3246 | goto fail_2; |
| 3247 | } |
| 3248 | |
| 3249 | for (pos = 0; pos < i; pos++) { |
| 3250 | char *p, *k, *v; |
| 3251 | size_t len; |
| 3252 | |
| 3253 | key = PyList_GetItem(keys, pos); |
| 3254 | val = PyList_GetItem(vals, pos); |
| 3255 | if (!key || !val) |
| 3256 | goto fail_2; |
| 3257 | |
| 3258 | if (!PyArg_Parse( |
| 3259 | key, |
| 3260 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3261 | &k) || |
| 3262 | !PyArg_Parse( |
| 3263 | val, |
| 3264 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3265 | &v)) |
| 3266 | { |
| 3267 | goto fail_2; |
| 3268 | } |
| 3269 | len = PyString_Size(key) + PyString_Size(val) + 2; |
| 3270 | p = PyMem_NEW(char, len); |
| 3271 | if (p == NULL) { |
| 3272 | PyErr_NoMemory(); |
| 3273 | goto fail_2; |
| 3274 | } |
| 3275 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3276 | envlist[envc++] = p; |
| 3277 | } |
| 3278 | envlist[envc] = 0; |
| 3279 | |
| 3280 | Py_BEGIN_ALLOW_THREADS |
| 3281 | #if defined(PYCC_GCC) |
| 3282 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3283 | #else |
| 3284 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 3285 | #endif |
| 3286 | Py_END_ALLOW_THREADS |
| 3287 | |
| 3288 | if (spawnval == -1) |
| 3289 | (void) posix_error(); |
| 3290 | else |
| 3291 | res = Py_BuildValue("l", (long) spawnval); |
| 3292 | |
| 3293 | fail_2: |
| 3294 | while (--envc >= 0) |
| 3295 | PyMem_DEL(envlist[envc]); |
| 3296 | PyMem_DEL(envlist); |
| 3297 | fail_1: |
| 3298 | free_string_array(argvlist, lastarg); |
| 3299 | Py_XDECREF(vals); |
| 3300 | Py_XDECREF(keys); |
| 3301 | fail_0: |
| 3302 | PyMem_Free(path); |
| 3303 | return res; |
| 3304 | } |
| 3305 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3306 | #endif /* HAVE_SPAWNV */ |
| 3307 | |
| 3308 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3309 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3310 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3311 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3312 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3313 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3314 | 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] | 3315 | |
| 3316 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3317 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3318 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3319 | int pid = fork1(); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3320 | if (pid == -1) |
| 3321 | return posix_error(); |
| 3322 | PyOS_AfterFork(); |
| 3323 | return PyInt_FromLong((long)pid); |
| 3324 | } |
| 3325 | #endif |
| 3326 | |
| 3327 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3328 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3329 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3330 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3331 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3332 | 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] | 3333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3334 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3335 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3336 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3337 | int pid = fork(); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3338 | if (pid == -1) |
| 3339 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3340 | if (pid == 0) |
| 3341 | PyOS_AfterFork(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3342 | return PyInt_FromLong((long)pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3343 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3344 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3345 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3346 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3347 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3348 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3349 | #define DEV_PTY_FILE "/dev/ptc" |
| 3350 | #define HAVE_DEV_PTMX |
| 3351 | #else |
| 3352 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3353 | #endif |
| 3354 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3355 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3356 | #ifdef HAVE_PTY_H |
| 3357 | #include <pty.h> |
| 3358 | #else |
| 3359 | #ifdef HAVE_LIBUTIL_H |
| 3360 | #include <libutil.h> |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3361 | #endif /* HAVE_LIBUTIL_H */ |
| 3362 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3363 | #ifdef HAVE_STROPTS_H |
| 3364 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3365 | #endif |
| 3366 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3367 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3368 | #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] | 3369 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3370 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3371 | 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] | 3372 | |
| 3373 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3374 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3375 | { |
| 3376 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3377 | #ifndef HAVE_OPENPTY |
| 3378 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3379 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3380 | #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] | 3381 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3382 | #ifdef sun |
Neal Norwitz | 84a98e0 | 2006-04-10 07:44:23 +0000 | [diff] [blame] | 3383 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3384 | #endif |
| 3385 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3386 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3387 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3388 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3389 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3390 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3391 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3392 | if (slave_name == NULL) |
| 3393 | return posix_error(); |
| 3394 | |
| 3395 | slave_fd = open(slave_name, O_RDWR); |
| 3396 | if (slave_fd < 0) |
| 3397 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3398 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3399 | 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] | 3400 | if (master_fd < 0) |
| 3401 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3402 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3403 | /* change permission of slave */ |
| 3404 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3405 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3406 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3407 | } |
| 3408 | /* unlock slave */ |
| 3409 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3410 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3411 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3412 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3413 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3414 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3415 | if (slave_name == NULL) |
| 3416 | return posix_error(); |
| 3417 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3418 | if (slave_fd < 0) |
| 3419 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3420 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3421 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3422 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3423 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3424 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3425 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3426 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3427 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3428 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3429 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3430 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3431 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3432 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3433 | |
| 3434 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3435 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3436 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3437 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3438 | 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] | 3439 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3440 | |
| 3441 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3442 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3443 | { |
Neal Norwitz | 24b3c22 | 2005-09-19 06:43:44 +0000 | [diff] [blame] | 3444 | int master_fd = -1, pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3445 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3446 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 3447 | if (pid == -1) |
| 3448 | return posix_error(); |
Fred Drake | 49b0c3b | 2000-07-06 19:42:19 +0000 | [diff] [blame] | 3449 | if (pid == 0) |
| 3450 | PyOS_AfterFork(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3451 | return Py_BuildValue("(ii)", pid, master_fd); |
| 3452 | } |
| 3453 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3454 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3455 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3456 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3457 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3458 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3459 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3460 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3461 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3462 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3463 | return PyInt_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3464 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3465 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3466 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3467 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3468 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3469 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3470 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3471 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3472 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3473 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3474 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3475 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3476 | return PyInt_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3477 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3478 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3479 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3480 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3481 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3482 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3483 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3484 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3485 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3486 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3487 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3488 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3489 | return PyInt_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3490 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3491 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3492 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3494 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3495 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3496 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3497 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3498 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3499 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3500 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3501 | return PyInt_FromLong((long)getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3504 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3505 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3506 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3507 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3508 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3509 | |
| 3510 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3511 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3512 | { |
| 3513 | PyObject *result = NULL; |
| 3514 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3515 | #ifdef NGROUPS_MAX |
| 3516 | #define MAX_GROUPS NGROUPS_MAX |
| 3517 | #else |
| 3518 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3519 | #define MAX_GROUPS 64 |
| 3520 | #endif |
| 3521 | gid_t grouplist[MAX_GROUPS]; |
| 3522 | int n; |
| 3523 | |
| 3524 | n = getgroups(MAX_GROUPS, grouplist); |
| 3525 | if (n < 0) |
| 3526 | posix_error(); |
| 3527 | else { |
| 3528 | result = PyList_New(n); |
| 3529 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3530 | int i; |
| 3531 | for (i = 0; i < n; ++i) { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3532 | PyObject *o = PyInt_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3533 | if (o == NULL) { |
| 3534 | Py_DECREF(result); |
| 3535 | result = NULL; |
| 3536 | break; |
| 3537 | } |
| 3538 | PyList_SET_ITEM(result, i, o); |
| 3539 | } |
| 3540 | } |
| 3541 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3542 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3543 | return result; |
| 3544 | } |
| 3545 | #endif |
| 3546 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3547 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3548 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3549 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 3550 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 3551 | |
| 3552 | static PyObject * |
| 3553 | posix_getpgid(PyObject *self, PyObject *args) |
| 3554 | { |
| 3555 | int pid, pgid; |
| 3556 | if (!PyArg_ParseTuple(args, "i:getpgid", &pid)) |
| 3557 | return NULL; |
| 3558 | pgid = getpgid(pid); |
| 3559 | if (pgid < 0) |
| 3560 | return posix_error(); |
| 3561 | return PyInt_FromLong((long)pgid); |
| 3562 | } |
| 3563 | #endif /* HAVE_GETPGID */ |
| 3564 | |
| 3565 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3566 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3567 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3568 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3569 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3571 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3572 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3573 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3574 | #ifdef GETPGRP_HAVE_ARG |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3575 | return PyInt_FromLong((long)getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3576 | #else /* GETPGRP_HAVE_ARG */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3577 | return PyInt_FromLong((long)getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3578 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3579 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3580 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 3581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3582 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3583 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3584 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3585 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3586 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3588 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3589 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3590 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3591 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3592 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3593 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3594 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 3595 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 3596 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3597 | Py_INCREF(Py_None); |
| 3598 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3601 | #endif /* HAVE_SETPGRP */ |
| 3602 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3603 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3604 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3605 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3606 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3607 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3608 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3609 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3610 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3611 | return PyInt_FromLong((long)getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3612 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3613 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3615 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3616 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3617 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3618 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3619 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3620 | |
| 3621 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3622 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3623 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3624 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3625 | char *name; |
| 3626 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3627 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3628 | errno = 0; |
| 3629 | name = getlogin(); |
| 3630 | if (name == NULL) { |
| 3631 | if (errno) |
| 3632 | posix_error(); |
| 3633 | else |
| 3634 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 3635 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3636 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3637 | else |
| 3638 | result = PyString_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 3639 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3640 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 3641 | return result; |
| 3642 | } |
| 3643 | #endif |
| 3644 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3645 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3646 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3647 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3648 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3649 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3650 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3651 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3652 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3653 | return PyInt_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3654 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3655 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3656 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3657 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3658 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3659 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3660 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3661 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3662 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3664 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3665 | { |
| 3666 | int pid, sig; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3667 | if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3668 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3669 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3670 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 3671 | APIRET rc; |
| 3672 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3673 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3674 | |
| 3675 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 3676 | APIRET rc; |
| 3677 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3678 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3679 | |
| 3680 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 3681 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3682 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3683 | if (kill(pid, sig) == -1) |
| 3684 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3685 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3686 | Py_INCREF(Py_None); |
| 3687 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3688 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3689 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3690 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3691 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3692 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3693 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3694 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 3695 | |
| 3696 | static PyObject * |
| 3697 | posix_killpg(PyObject *self, PyObject *args) |
| 3698 | { |
| 3699 | int pgid, sig; |
| 3700 | if (!PyArg_ParseTuple(args, "ii:killpg", &pgid, &sig)) |
| 3701 | return NULL; |
| 3702 | if (killpg(pgid, sig) == -1) |
| 3703 | return posix_error(); |
| 3704 | Py_INCREF(Py_None); |
| 3705 | return Py_None; |
| 3706 | } |
| 3707 | #endif |
| 3708 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3709 | #ifdef HAVE_PLOCK |
| 3710 | |
| 3711 | #ifdef HAVE_SYS_LOCK_H |
| 3712 | #include <sys/lock.h> |
| 3713 | #endif |
| 3714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3715 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3716 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3717 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3718 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3720 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3721 | { |
| 3722 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3723 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3724 | return NULL; |
| 3725 | if (plock(op) == -1) |
| 3726 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3727 | Py_INCREF(Py_None); |
| 3728 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 3729 | } |
| 3730 | #endif |
| 3731 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3732 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3733 | #ifdef HAVE_POPEN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3734 | PyDoc_STRVAR(posix_popen__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3735 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3736 | Open a pipe to/from a command returning a file object."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3737 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3738 | #if defined(PYOS_OS2) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3739 | #if defined(PYCC_VACPP) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3740 | static int |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3741 | async_system(const char *command) |
| 3742 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3743 | char errormsg[256], args[1024]; |
| 3744 | RESULTCODES rcodes; |
| 3745 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3746 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3747 | char *shell = getenv("COMSPEC"); |
| 3748 | if (!shell) |
| 3749 | shell = "cmd"; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3750 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3751 | /* avoid overflowing the argument buffer */ |
| 3752 | if (strlen(shell) + 3 + strlen(command) >= 1024) |
| 3753 | return ERROR_NOT_ENOUGH_MEMORY |
| 3754 | |
| 3755 | args[0] = '\0'; |
| 3756 | strcat(args, shell); |
| 3757 | strcat(args, "/c "); |
| 3758 | strcat(args, command); |
| 3759 | |
| 3760 | /* execute asynchronously, inheriting the environment */ |
| 3761 | rc = DosExecPgm(errormsg, |
| 3762 | sizeof(errormsg), |
| 3763 | EXEC_ASYNC, |
| 3764 | args, |
| 3765 | NULL, |
| 3766 | &rcodes, |
| 3767 | shell); |
| 3768 | return rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3771 | static FILE * |
| 3772 | popen(const char *command, const char *mode, int pipesize, int *err) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3773 | { |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3774 | int oldfd, tgtfd; |
| 3775 | HFILE pipeh[2]; |
| 3776 | APIRET rc; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3777 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3778 | /* mode determines which of stdin or stdout is reconnected to |
| 3779 | * the pipe to the child |
| 3780 | */ |
| 3781 | if (strchr(mode, 'r') != NULL) { |
| 3782 | tgt_fd = 1; /* stdout */ |
| 3783 | } else if (strchr(mode, 'w')) { |
| 3784 | tgt_fd = 0; /* stdin */ |
| 3785 | } else { |
| 3786 | *err = ERROR_INVALID_ACCESS; |
| 3787 | return NULL; |
| 3788 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3789 | |
Andrew MacIntyre | a3be258 | 2004-12-18 09:51:05 +0000 | [diff] [blame] | 3790 | /* setup the pipe */ |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3791 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { |
| 3792 | *err = rc; |
| 3793 | return NULL; |
| 3794 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3795 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3796 | /* prevent other threads accessing stdio */ |
| 3797 | DosEnterCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3798 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3799 | /* reconnect stdio and execute child */ |
| 3800 | oldfd = dup(tgtfd); |
| 3801 | close(tgtfd); |
| 3802 | if (dup2(pipeh[tgtfd], tgtfd) == 0) { |
| 3803 | DosClose(pipeh[tgtfd]); |
| 3804 | rc = async_system(command); |
| 3805 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3806 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3807 | /* restore stdio */ |
| 3808 | dup2(oldfd, tgtfd); |
| 3809 | close(oldfd); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3810 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3811 | /* allow other threads access to stdio */ |
| 3812 | DosExitCritSec(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3813 | |
Andrew MacIntyre | a4a8afb | 2004-12-12 08:30:51 +0000 | [diff] [blame] | 3814 | /* if execution of child was successful return file stream */ |
| 3815 | if (rc == NO_ERROR) |
| 3816 | return fdopen(pipeh[1 - tgtfd], mode); |
| 3817 | else { |
| 3818 | DosClose(pipeh[1 - tgtfd]); |
| 3819 | *err = rc; |
| 3820 | return NULL; |
| 3821 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3825 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3826 | { |
| 3827 | char *name; |
| 3828 | char *mode = "r"; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3829 | int err, bufsize = -1; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3830 | FILE *fp; |
| 3831 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3832 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3833 | return NULL; |
| 3834 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3835 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3836 | Py_END_ALLOW_THREADS |
| 3837 | if (fp == NULL) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3838 | return os2_error(err); |
| 3839 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3840 | f = PyFile_FromFile(fp, name, mode, fclose); |
| 3841 | if (f != NULL) |
| 3842 | PyFile_SetBufSize(f, bufsize); |
| 3843 | return f; |
| 3844 | } |
| 3845 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3846 | #elif defined(PYCC_GCC) |
| 3847 | |
| 3848 | /* standard posix version of popen() support */ |
| 3849 | static PyObject * |
| 3850 | posix_popen(PyObject *self, PyObject *args) |
| 3851 | { |
| 3852 | char *name; |
| 3853 | char *mode = "r"; |
| 3854 | int bufsize = -1; |
| 3855 | FILE *fp; |
| 3856 | PyObject *f; |
| 3857 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
| 3858 | return NULL; |
| 3859 | Py_BEGIN_ALLOW_THREADS |
| 3860 | fp = popen(name, mode); |
| 3861 | Py_END_ALLOW_THREADS |
| 3862 | if (fp == NULL) |
| 3863 | return posix_error(); |
| 3864 | f = PyFile_FromFile(fp, name, mode, pclose); |
| 3865 | if (f != NULL) |
| 3866 | PyFile_SetBufSize(f, bufsize); |
| 3867 | return f; |
| 3868 | } |
| 3869 | |
| 3870 | /* fork() under OS/2 has lots'o'warts |
| 3871 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]() |
| 3872 | * most of this code is a ripoff of the win32 code, but using the |
| 3873 | * capabilities of EMX's C library routines |
| 3874 | */ |
| 3875 | |
| 3876 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */ |
| 3877 | #define POPEN_1 1 |
| 3878 | #define POPEN_2 2 |
| 3879 | #define POPEN_3 3 |
| 3880 | #define POPEN_4 4 |
| 3881 | |
| 3882 | static PyObject *_PyPopen(char *, int, int, int); |
| 3883 | static int _PyPclose(FILE *file); |
| 3884 | |
| 3885 | /* |
| 3886 | * Internal dictionary mapping popen* file pointers to process handles, |
| 3887 | * for use when retrieving the process exit code. See _PyPclose() below |
| 3888 | * for more information on this dictionary's use. |
| 3889 | */ |
| 3890 | static PyObject *_PyPopenProcs = NULL; |
| 3891 | |
| 3892 | /* os2emx version of popen2() |
| 3893 | * |
| 3894 | * The result of this function is a pipe (file) connected to the |
| 3895 | * process's stdin, and a pipe connected to the process's stdout. |
| 3896 | */ |
| 3897 | |
| 3898 | static PyObject * |
| 3899 | os2emx_popen2(PyObject *self, PyObject *args) |
| 3900 | { |
| 3901 | PyObject *f; |
| 3902 | int tm=0; |
| 3903 | |
| 3904 | char *cmdstring; |
| 3905 | char *mode = "t"; |
| 3906 | int bufsize = -1; |
| 3907 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
| 3908 | return NULL; |
| 3909 | |
| 3910 | if (*mode == 't') |
| 3911 | tm = O_TEXT; |
| 3912 | else if (*mode != 'b') { |
| 3913 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3914 | return NULL; |
| 3915 | } else |
| 3916 | tm = O_BINARY; |
| 3917 | |
| 3918 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); |
| 3919 | |
| 3920 | return f; |
| 3921 | } |
| 3922 | |
| 3923 | /* |
| 3924 | * Variation on os2emx.popen2 |
| 3925 | * |
| 3926 | * The result of this function is 3 pipes - the process's stdin, |
| 3927 | * stdout and stderr |
| 3928 | */ |
| 3929 | |
| 3930 | static PyObject * |
| 3931 | os2emx_popen3(PyObject *self, PyObject *args) |
| 3932 | { |
| 3933 | PyObject *f; |
| 3934 | int tm = 0; |
| 3935 | |
| 3936 | char *cmdstring; |
| 3937 | char *mode = "t"; |
| 3938 | int bufsize = -1; |
| 3939 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
| 3940 | return NULL; |
| 3941 | |
| 3942 | if (*mode == 't') |
| 3943 | tm = O_TEXT; |
| 3944 | else if (*mode != 'b') { |
| 3945 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3946 | return NULL; |
| 3947 | } else |
| 3948 | tm = O_BINARY; |
| 3949 | |
| 3950 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); |
| 3951 | |
| 3952 | return f; |
| 3953 | } |
| 3954 | |
| 3955 | /* |
| 3956 | * Variation on os2emx.popen2 |
| 3957 | * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 3958 | * The result of this function is 2 pipes - the processes stdin, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3959 | * and stdout+stderr combined as a single pipe. |
| 3960 | */ |
| 3961 | |
| 3962 | static PyObject * |
| 3963 | os2emx_popen4(PyObject *self, PyObject *args) |
| 3964 | { |
| 3965 | PyObject *f; |
| 3966 | int tm = 0; |
| 3967 | |
| 3968 | char *cmdstring; |
| 3969 | char *mode = "t"; |
| 3970 | int bufsize = -1; |
| 3971 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
| 3972 | return NULL; |
| 3973 | |
| 3974 | if (*mode == 't') |
| 3975 | tm = O_TEXT; |
| 3976 | else if (*mode != 'b') { |
| 3977 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); |
| 3978 | return NULL; |
| 3979 | } else |
| 3980 | tm = O_BINARY; |
| 3981 | |
| 3982 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); |
| 3983 | |
| 3984 | return f; |
| 3985 | } |
| 3986 | |
| 3987 | /* a couple of structures for convenient handling of multiple |
| 3988 | * file handles and pipes |
| 3989 | */ |
| 3990 | struct file_ref |
| 3991 | { |
| 3992 | int handle; |
| 3993 | int flags; |
| 3994 | }; |
| 3995 | |
| 3996 | struct pipe_ref |
| 3997 | { |
| 3998 | int rd; |
| 3999 | int wr; |
| 4000 | }; |
| 4001 | |
| 4002 | /* The following code is derived from the win32 code */ |
| 4003 | |
| 4004 | static PyObject * |
| 4005 | _PyPopen(char *cmdstring, int mode, int n, int bufsize) |
| 4006 | { |
| 4007 | struct file_ref stdio[3]; |
| 4008 | struct pipe_ref p_fd[3]; |
| 4009 | FILE *p_s[3]; |
| 4010 | int file_count, i, pipe_err, pipe_pid; |
| 4011 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode; |
| 4012 | PyObject *f, *p_f[3]; |
| 4013 | |
| 4014 | /* file modes for subsequent fdopen's on pipe handles */ |
| 4015 | if (mode == O_TEXT) |
| 4016 | { |
| 4017 | rd_mode = "rt"; |
| 4018 | wr_mode = "wt"; |
| 4019 | } |
| 4020 | else |
| 4021 | { |
| 4022 | rd_mode = "rb"; |
| 4023 | wr_mode = "wb"; |
| 4024 | } |
| 4025 | |
| 4026 | /* prepare shell references */ |
| 4027 | if ((shell = getenv("EMXSHELL")) == NULL) |
| 4028 | if ((shell = getenv("COMSPEC")) == NULL) |
| 4029 | { |
| 4030 | errno = ENOENT; |
| 4031 | return posix_error(); |
| 4032 | } |
| 4033 | |
| 4034 | sh_name = _getname(shell); |
| 4035 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) |
| 4036 | opt = "/c"; |
| 4037 | else |
| 4038 | opt = "-c"; |
| 4039 | |
| 4040 | /* save current stdio fds + their flags, and set not inheritable */ |
| 4041 | i = pipe_err = 0; |
| 4042 | while (pipe_err >= 0 && i < 3) |
| 4043 | { |
| 4044 | pipe_err = stdio[i].handle = dup(i); |
| 4045 | stdio[i].flags = fcntl(i, F_GETFD, 0); |
| 4046 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); |
| 4047 | i++; |
| 4048 | } |
| 4049 | if (pipe_err < 0) |
| 4050 | { |
| 4051 | /* didn't get them all saved - clean up and bail out */ |
| 4052 | int saved_err = errno; |
| 4053 | while (i-- > 0) |
| 4054 | { |
| 4055 | close(stdio[i].handle); |
| 4056 | } |
| 4057 | errno = saved_err; |
| 4058 | return posix_error(); |
| 4059 | } |
| 4060 | |
| 4061 | /* create pipe ends */ |
| 4062 | file_count = 2; |
| 4063 | if (n == POPEN_3) |
| 4064 | file_count = 3; |
| 4065 | i = pipe_err = 0; |
| 4066 | while ((pipe_err == 0) && (i < file_count)) |
| 4067 | pipe_err = pipe((int *)&p_fd[i++]); |
| 4068 | if (pipe_err < 0) |
| 4069 | { |
| 4070 | /* didn't get them all made - clean up and bail out */ |
| 4071 | while (i-- > 0) |
| 4072 | { |
| 4073 | close(p_fd[i].wr); |
| 4074 | close(p_fd[i].rd); |
| 4075 | } |
| 4076 | errno = EPIPE; |
| 4077 | return posix_error(); |
| 4078 | } |
| 4079 | |
| 4080 | /* change the actual standard IO streams over temporarily, |
| 4081 | * making the retained pipe ends non-inheritable |
| 4082 | */ |
| 4083 | pipe_err = 0; |
| 4084 | |
| 4085 | /* - stdin */ |
| 4086 | if (dup2(p_fd[0].rd, 0) == 0) |
| 4087 | { |
| 4088 | close(p_fd[0].rd); |
| 4089 | i = fcntl(p_fd[0].wr, F_GETFD, 0); |
| 4090 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); |
| 4091 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) |
| 4092 | { |
| 4093 | close(p_fd[0].wr); |
| 4094 | pipe_err = -1; |
| 4095 | } |
| 4096 | } |
| 4097 | else |
| 4098 | { |
| 4099 | pipe_err = -1; |
| 4100 | } |
| 4101 | |
| 4102 | /* - stdout */ |
| 4103 | if (pipe_err == 0) |
| 4104 | { |
| 4105 | if (dup2(p_fd[1].wr, 1) == 1) |
| 4106 | { |
| 4107 | close(p_fd[1].wr); |
| 4108 | i = fcntl(p_fd[1].rd, F_GETFD, 0); |
| 4109 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); |
| 4110 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) |
| 4111 | { |
| 4112 | close(p_fd[1].rd); |
| 4113 | pipe_err = -1; |
| 4114 | } |
| 4115 | } |
| 4116 | else |
| 4117 | { |
| 4118 | pipe_err = -1; |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | /* - stderr, as required */ |
| 4123 | if (pipe_err == 0) |
| 4124 | switch (n) |
| 4125 | { |
| 4126 | case POPEN_3: |
| 4127 | { |
| 4128 | if (dup2(p_fd[2].wr, 2) == 2) |
| 4129 | { |
| 4130 | close(p_fd[2].wr); |
| 4131 | i = fcntl(p_fd[2].rd, F_GETFD, 0); |
| 4132 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); |
| 4133 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) |
| 4134 | { |
| 4135 | close(p_fd[2].rd); |
| 4136 | pipe_err = -1; |
| 4137 | } |
| 4138 | } |
| 4139 | else |
| 4140 | { |
| 4141 | pipe_err = -1; |
| 4142 | } |
| 4143 | break; |
| 4144 | } |
| 4145 | |
| 4146 | case POPEN_4: |
| 4147 | { |
| 4148 | if (dup2(1, 2) != 2) |
| 4149 | { |
| 4150 | pipe_err = -1; |
| 4151 | } |
| 4152 | break; |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | /* spawn the child process */ |
| 4157 | if (pipe_err == 0) |
| 4158 | { |
| 4159 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); |
| 4160 | if (pipe_pid == -1) |
| 4161 | { |
| 4162 | pipe_err = -1; |
| 4163 | } |
| 4164 | else |
| 4165 | { |
| 4166 | /* save the PID into the FILE structure |
| 4167 | * NOTE: this implementation doesn't actually |
| 4168 | * take advantage of this, but do it for |
| 4169 | * completeness - AIM Apr01 |
| 4170 | */ |
| 4171 | for (i = 0; i < file_count; i++) |
| 4172 | p_s[i]->_pid = pipe_pid; |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | /* reset standard IO to normal */ |
| 4177 | for (i = 0; i < 3; i++) |
| 4178 | { |
| 4179 | dup2(stdio[i].handle, i); |
| 4180 | fcntl(i, F_SETFD, stdio[i].flags); |
| 4181 | close(stdio[i].handle); |
| 4182 | } |
| 4183 | |
| 4184 | /* if any remnant problems, clean up and bail out */ |
| 4185 | if (pipe_err < 0) |
| 4186 | { |
| 4187 | for (i = 0; i < 3; i++) |
| 4188 | { |
| 4189 | close(p_fd[i].rd); |
| 4190 | close(p_fd[i].wr); |
| 4191 | } |
| 4192 | errno = EPIPE; |
| 4193 | return posix_error_with_filename(cmdstring); |
| 4194 | } |
| 4195 | |
| 4196 | /* build tuple of file objects to return */ |
| 4197 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) |
| 4198 | PyFile_SetBufSize(p_f[0], bufsize); |
| 4199 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4200 | PyFile_SetBufSize(p_f[1], bufsize); |
| 4201 | if (n == POPEN_3) |
| 4202 | { |
| 4203 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) |
| 4204 | PyFile_SetBufSize(p_f[0], bufsize); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4205 | 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] | 4206 | } |
| 4207 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4208 | f = PyTuple_Pack(2, p_f[0], p_f[1]); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4209 | |
| 4210 | /* |
| 4211 | * Insert the files we've created into the process dictionary |
| 4212 | * all referencing the list with the process handle and the |
| 4213 | * initial number of files (see description below in _PyPclose). |
| 4214 | * Since if _PyPclose later tried to wait on a process when all |
| 4215 | * handles weren't closed, it could create a deadlock with the |
| 4216 | * child, we spend some energy here to try to ensure that we |
| 4217 | * either insert all file handles into the dictionary or none |
| 4218 | * at all. It's a little clumsy with the various popen modes |
| 4219 | * and variable number of files involved. |
| 4220 | */ |
| 4221 | if (!_PyPopenProcs) |
| 4222 | { |
| 4223 | _PyPopenProcs = PyDict_New(); |
| 4224 | } |
| 4225 | |
| 4226 | if (_PyPopenProcs) |
| 4227 | { |
| 4228 | PyObject *procObj, *pidObj, *intObj, *fileObj[3]; |
| 4229 | int ins_rc[3]; |
| 4230 | |
| 4231 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4232 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4233 | |
| 4234 | procObj = PyList_New(2); |
| 4235 | pidObj = PyInt_FromLong((long) pipe_pid); |
| 4236 | intObj = PyInt_FromLong((long) file_count); |
| 4237 | |
| 4238 | if (procObj && pidObj && intObj) |
| 4239 | { |
| 4240 | PyList_SetItem(procObj, 0, pidObj); |
| 4241 | PyList_SetItem(procObj, 1, intObj); |
| 4242 | |
| 4243 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]); |
| 4244 | if (fileObj[0]) |
| 4245 | { |
| 4246 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4247 | fileObj[0], |
| 4248 | procObj); |
| 4249 | } |
| 4250 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]); |
| 4251 | if (fileObj[1]) |
| 4252 | { |
| 4253 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4254 | fileObj[1], |
| 4255 | procObj); |
| 4256 | } |
| 4257 | if (file_count >= 3) |
| 4258 | { |
| 4259 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]); |
| 4260 | if (fileObj[2]) |
| 4261 | { |
| 4262 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 4263 | fileObj[2], |
| 4264 | procObj); |
| 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 4269 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 4270 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) |
| 4271 | { |
| 4272 | /* Something failed - remove any dictionary |
| 4273 | * entries that did make it. |
| 4274 | */ |
| 4275 | if (!ins_rc[0] && fileObj[0]) |
| 4276 | { |
| 4277 | PyDict_DelItem(_PyPopenProcs, |
| 4278 | fileObj[0]); |
| 4279 | } |
| 4280 | if (!ins_rc[1] && fileObj[1]) |
| 4281 | { |
| 4282 | PyDict_DelItem(_PyPopenProcs, |
| 4283 | fileObj[1]); |
| 4284 | } |
| 4285 | if (!ins_rc[2] && fileObj[2]) |
| 4286 | { |
| 4287 | PyDict_DelItem(_PyPopenProcs, |
| 4288 | fileObj[2]); |
| 4289 | } |
| 4290 | } |
| 4291 | } |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4292 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * Clean up our localized references for the dictionary keys |
| 4295 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 4296 | * that got placed in the dictionary. |
| 4297 | */ |
| 4298 | Py_XDECREF(procObj); |
| 4299 | Py_XDECREF(fileObj[0]); |
| 4300 | Py_XDECREF(fileObj[1]); |
| 4301 | Py_XDECREF(fileObj[2]); |
| 4302 | } |
| 4303 | |
| 4304 | /* Child is launched. */ |
| 4305 | return f; |
| 4306 | } |
| 4307 | |
| 4308 | /* |
| 4309 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 4310 | * exit code for the child process and return as a result of the close. |
| 4311 | * |
| 4312 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 4313 | * input file pointer to information about the process that was |
| 4314 | * originally created by the popen* call that created the file pointer. |
| 4315 | * The dictionary uses the file pointer as a key (with one entry |
| 4316 | * inserted for each file returned by the original popen* call) and a |
| 4317 | * single list object as the value for all files from a single call. |
| 4318 | * The list object contains the Win32 process handle at [0], and a file |
| 4319 | * count at [1], which is initialized to the total number of file |
| 4320 | * handles using that list. |
| 4321 | * |
| 4322 | * This function closes whichever handle it is passed, and decrements |
| 4323 | * the file count in the dictionary for the process handle pointed to |
| 4324 | * by this file. On the last close (when the file count reaches zero), |
| 4325 | * this function will wait for the child process and then return its |
| 4326 | * exit code as the result of the close() operation. This permits the |
| 4327 | * files to be closed in any order - it is always the close() of the |
| 4328 | * final handle that will return the exit code. |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4329 | * |
| 4330 | * NOTE: This function is currently called with the GIL released. |
| 4331 | * hence we use the GILState API to manage our state. |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4332 | */ |
| 4333 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4334 | static int _PyPclose(FILE *file) |
| 4335 | { |
| 4336 | int result; |
| 4337 | int exit_code; |
| 4338 | int pipe_pid; |
| 4339 | PyObject *procObj, *pidObj, *intObj, *fileObj; |
| 4340 | int file_count; |
| 4341 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4342 | PyGILState_STATE state; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4343 | #endif |
| 4344 | |
| 4345 | /* Close the file handle first, to ensure it can't block the |
| 4346 | * child from exiting if it's the last handle. |
| 4347 | */ |
| 4348 | result = fclose(file); |
| 4349 | |
| 4350 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4351 | state = PyGILState_Ensure(); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4352 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4353 | if (_PyPopenProcs) |
| 4354 | { |
| 4355 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 4356 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 4357 | fileObj)) != NULL && |
| 4358 | (pidObj = PyList_GetItem(procObj,0)) != NULL && |
| 4359 | (intObj = PyList_GetItem(procObj,1)) != NULL) |
| 4360 | { |
| 4361 | pipe_pid = (int) PyInt_AsLong(pidObj); |
| 4362 | file_count = (int) PyInt_AsLong(intObj); |
| 4363 | |
| 4364 | if (file_count > 1) |
| 4365 | { |
| 4366 | /* Still other files referencing process */ |
| 4367 | file_count--; |
| 4368 | PyList_SetItem(procObj,1, |
| 4369 | PyInt_FromLong((long) file_count)); |
| 4370 | } |
| 4371 | else |
| 4372 | { |
| 4373 | /* Last file for this process */ |
| 4374 | if (result != EOF && |
| 4375 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid) |
| 4376 | { |
| 4377 | /* extract exit status */ |
| 4378 | if (WIFEXITED(exit_code)) |
| 4379 | { |
| 4380 | result = WEXITSTATUS(exit_code); |
| 4381 | } |
| 4382 | else |
| 4383 | { |
| 4384 | errno = EPIPE; |
| 4385 | result = -1; |
| 4386 | } |
| 4387 | } |
| 4388 | else |
| 4389 | { |
| 4390 | /* Indicate failure - this will cause the file object |
| 4391 | * to raise an I/O error and translate the last |
| 4392 | * error code from errno. We do have a problem with |
| 4393 | * last errors that overlap the normal errno table, |
| 4394 | * but that's a consistent problem with the file object. |
| 4395 | */ |
| 4396 | result = -1; |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | /* Remove this file pointer from dictionary */ |
| 4401 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 4402 | |
| 4403 | if (PyDict_Size(_PyPopenProcs) == 0) |
| 4404 | { |
| 4405 | Py_DECREF(_PyPopenProcs); |
| 4406 | _PyPopenProcs = NULL; |
| 4407 | } |
| 4408 | |
| 4409 | } /* if object retrieval ok */ |
| 4410 | |
| 4411 | Py_XDECREF(fileObj); |
| 4412 | } /* if _PyPopenProcs */ |
| 4413 | |
| 4414 | #ifdef WITH_THREAD |
Andrew MacIntyre | baf25b0 | 2003-04-21 14:22:36 +0000 | [diff] [blame] | 4415 | PyGILState_Release(state); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4416 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4417 | return result; |
| 4418 | } |
| 4419 | |
| 4420 | #endif /* PYCC_??? */ |
| 4421 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4422 | #elif defined(MS_WINDOWS) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4423 | |
| 4424 | /* |
| 4425 | * Portable 'popen' replacement for Win32. |
| 4426 | * |
| 4427 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks |
| 4428 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com> |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4429 | * Return code handling by David Bolen <db3l@fitlinxx.com>. |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4430 | */ |
| 4431 | |
| 4432 | #include <malloc.h> |
| 4433 | #include <io.h> |
| 4434 | #include <fcntl.h> |
| 4435 | |
| 4436 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */ |
| 4437 | #define POPEN_1 1 |
| 4438 | #define POPEN_2 2 |
| 4439 | #define POPEN_3 3 |
| 4440 | #define POPEN_4 4 |
| 4441 | |
| 4442 | static PyObject *_PyPopen(char *, int, int); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4443 | static int _PyPclose(FILE *file); |
| 4444 | |
| 4445 | /* |
| 4446 | * Internal dictionary mapping popen* file pointers to process handles, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4447 | * for use when retrieving the process exit code. See _PyPclose() below |
| 4448 | * for more information on this dictionary's use. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4449 | */ |
| 4450 | static PyObject *_PyPopenProcs = NULL; |
| 4451 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4452 | |
| 4453 | /* popen that works from a GUI. |
| 4454 | * |
| 4455 | * The result of this function is a pipe (file) connected to the |
| 4456 | * processes stdin or stdout, depending on the requested mode. |
| 4457 | */ |
| 4458 | |
| 4459 | static PyObject * |
| 4460 | posix_popen(PyObject *self, PyObject *args) |
| 4461 | { |
Raymond Hettinger | b5cb665 | 2003-09-01 22:25:41 +0000 | [diff] [blame] | 4462 | PyObject *f; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4463 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4464 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4465 | char *cmdstring; |
| 4466 | char *mode = "r"; |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4467 | int bufsize = -1; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4468 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4469 | return NULL; |
| 4470 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4471 | if (*mode == 'r') |
| 4472 | tm = _O_RDONLY; |
| 4473 | else if (*mode != 'w') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4474 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4475 | return NULL; |
| 4476 | } else |
| 4477 | tm = _O_WRONLY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4478 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4479 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4480 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4481 | return NULL; |
| 4482 | } |
| 4483 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4484 | if (*(mode+1) == 't') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4485 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4486 | else if (*(mode+1) == 'b') |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4487 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4488 | else |
| 4489 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); |
| 4490 | |
| 4491 | return f; |
| 4492 | } |
| 4493 | |
| 4494 | /* Variation on win32pipe.popen |
| 4495 | * |
| 4496 | * The result of this function is a pipe (file) connected to the |
| 4497 | * process's stdin, and a pipe connected to the process's stdout. |
| 4498 | */ |
| 4499 | |
| 4500 | static PyObject * |
| 4501 | win32_popen2(PyObject *self, PyObject *args) |
| 4502 | { |
| 4503 | PyObject *f; |
| 4504 | int tm=0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4505 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4506 | char *cmdstring; |
| 4507 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4508 | int bufsize = -1; |
| 4509 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4510 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4511 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4512 | if (*mode == 't') |
| 4513 | tm = _O_TEXT; |
| 4514 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4515 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4516 | return NULL; |
| 4517 | } else |
| 4518 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4519 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4520 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4521 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4522 | return NULL; |
| 4523 | } |
| 4524 | |
| 4525 | f = _PyPopen(cmdstring, tm, POPEN_2); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4526 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4527 | return f; |
| 4528 | } |
| 4529 | |
| 4530 | /* |
| 4531 | * Variation on <om win32pipe.popen> |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4532 | * |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4533 | * The result of this function is 3 pipes - the process's stdin, |
| 4534 | * stdout and stderr |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4535 | */ |
| 4536 | |
| 4537 | static PyObject * |
| 4538 | win32_popen3(PyObject *self, PyObject *args) |
| 4539 | { |
| 4540 | PyObject *f; |
| 4541 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4542 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4543 | char *cmdstring; |
| 4544 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4545 | int bufsize = -1; |
| 4546 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4547 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4548 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4549 | if (*mode == 't') |
| 4550 | tm = _O_TEXT; |
| 4551 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4552 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4553 | return NULL; |
| 4554 | } else |
| 4555 | tm = _O_BINARY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4556 | |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4557 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4558 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4559 | return NULL; |
| 4560 | } |
| 4561 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4562 | f = _PyPopen(cmdstring, tm, POPEN_3); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4563 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4564 | return f; |
| 4565 | } |
| 4566 | |
| 4567 | /* |
| 4568 | * Variation on win32pipe.popen |
| 4569 | * |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4570 | * The result of this function is 2 pipes - the processes stdin, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4571 | * and stdout+stderr combined as a single pipe. |
| 4572 | */ |
| 4573 | |
| 4574 | static PyObject * |
| 4575 | win32_popen4(PyObject *self, PyObject *args) |
| 4576 | { |
| 4577 | PyObject *f; |
| 4578 | int tm = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4579 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4580 | char *cmdstring; |
| 4581 | char *mode = "t"; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4582 | int bufsize = -1; |
| 4583 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4584 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4585 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4586 | if (*mode == 't') |
| 4587 | tm = _O_TEXT; |
| 4588 | else if (*mode != 'b') { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4589 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4590 | return NULL; |
| 4591 | } else |
| 4592 | tm = _O_BINARY; |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4593 | |
| 4594 | if (bufsize != -1) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4595 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4596 | return NULL; |
| 4597 | } |
| 4598 | |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 4599 | f = _PyPopen(cmdstring, tm, POPEN_4); |
Fredrik Lundh | 766ccdc | 2000-07-09 17:41:01 +0000 | [diff] [blame] | 4600 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4601 | return f; |
| 4602 | } |
| 4603 | |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4604 | static BOOL |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4605 | _PyPopenCreateProcess(char *cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4606 | HANDLE hStdin, |
| 4607 | HANDLE hStdout, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4608 | HANDLE hStderr, |
| 4609 | HANDLE *hProcess) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4610 | { |
| 4611 | PROCESS_INFORMATION piProcInfo; |
| 4612 | STARTUPINFO siStartInfo; |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4613 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4614 | char *s1,*s2, *s3 = " /c "; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4615 | const char *szConsoleSpawn = "w9xpopen.exe"; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4616 | int i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4617 | Py_ssize_t x; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4618 | |
| 4619 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4620 | char *comshell; |
| 4621 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4622 | s1 = (char *)alloca(i); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4623 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4624 | /* x < i, so x fits into an integer */ |
| 4625 | return (int)x; |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4626 | |
| 4627 | /* Explicitly check if we are using COMMAND.COM. If we are |
| 4628 | * then use the w9xpopen hack. |
| 4629 | */ |
| 4630 | comshell = s1 + x; |
| 4631 | while (comshell >= s1 && *comshell != '\\') |
| 4632 | --comshell; |
| 4633 | ++comshell; |
| 4634 | |
| 4635 | if (GetVersion() < 0x80000000 && |
| 4636 | _stricmp(comshell, "command.com") != 0) { |
| 4637 | /* NT/2000 and not using command.com. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4638 | x = i + strlen(s3) + strlen(cmdstring) + 1; |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4639 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4640 | ZeroMemory(s2, x); |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4641 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4642 | } |
| 4643 | else { |
| 4644 | /* |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4645 | * Oh gag, we're on Win9x or using COMMAND.COM. Use |
| 4646 | * the workaround listed in KB: Q150956 |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4647 | */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4648 | char modulepath[_MAX_PATH]; |
| 4649 | struct stat statinfo; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4650 | GetModuleFileName(NULL, modulepath, sizeof(modulepath)); |
Martin v. Löwis | 26fd960 | 2006-04-22 11:15:41 +0000 | [diff] [blame] | 4651 | for (x = i = 0; modulepath[i]; i++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4652 | if (modulepath[i] == SEP) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4653 | x = i+1; |
| 4654 | modulepath[x] = '\0'; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4655 | /* Create the full-name to w9xpopen, so we can test it exists */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4656 | strncat(modulepath, |
| 4657 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4658 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4659 | -strlen(modulepath)); |
| 4660 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4661 | /* Eeek - file-not-found - possibly an embedding |
| 4662 | situation - see if we can locate it in sys.prefix |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4663 | */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4664 | strncpy(modulepath, |
| 4665 | Py_GetExecPrefix(), |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4666 | sizeof(modulepath)/sizeof(modulepath[0])); |
| 4667 | if (modulepath[strlen(modulepath)-1] != '\\') |
| 4668 | strcat(modulepath, "\\"); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4669 | strncat(modulepath, |
| 4670 | szConsoleSpawn, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4671 | (sizeof(modulepath)/sizeof(modulepath[0])) |
| 4672 | -strlen(modulepath)); |
| 4673 | /* No where else to look - raise an easily identifiable |
| 4674 | error, rather than leaving Windows to report |
| 4675 | "file not found" - as the user is probably blissfully |
| 4676 | unaware this shim EXE is used, and it will confuse them. |
| 4677 | (well, it confused me for a while ;-) |
| 4678 | */ |
| 4679 | if (stat(modulepath, &statinfo) != 0) { |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4680 | PyErr_Format(PyExc_RuntimeError, |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4681 | "Can not locate '%s' which is needed " |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4682 | "for popen to work with your shell " |
| 4683 | "or platform.", |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4684 | szConsoleSpawn); |
| 4685 | return FALSE; |
| 4686 | } |
| 4687 | } |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4688 | x = i + strlen(s3) + strlen(cmdstring) + 1 + |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4689 | strlen(modulepath) + |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4690 | strlen(szConsoleSpawn) + 1; |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4691 | |
Tim Peters | 92e4dd8 | 2002-10-05 01:47:34 +0000 | [diff] [blame] | 4692 | s2 = (char *)alloca(x); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4693 | ZeroMemory(s2, x); |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4694 | /* To maintain correct argument passing semantics, |
| 4695 | we pass the command-line as it stands, and allow |
| 4696 | quoting to be applied. w9xpopen.exe will then |
| 4697 | use its argv vector, and re-quote the necessary |
| 4698 | args for the ultimate child process. |
| 4699 | */ |
Tim Peters | 75cdad5 | 2001-11-28 22:07:30 +0000 | [diff] [blame] | 4700 | PyOS_snprintf( |
| 4701 | s2, x, |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 4702 | "\"%s\" %s%s%s", |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4703 | modulepath, |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4704 | s1, |
| 4705 | s3, |
| 4706 | cmdstring); |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4707 | /* Not passing CREATE_NEW_CONSOLE has been known to |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 4708 | cause random failures on win9x. Specifically a |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4709 | dialog: |
| 4710 | "Your program accessed mem currently in use at xxx" |
| 4711 | and a hopeful warning about the stability of your |
| 4712 | system. |
| 4713 | Cost is Ctrl+C wont kill children, but anyone |
| 4714 | who cares can have a go! |
| 4715 | */ |
| 4716 | dwProcessFlags |= CREATE_NEW_CONSOLE; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | /* Could be an else here to try cmd.exe / command.com in the path |
| 4721 | Now we'll just error out.. */ |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4722 | else { |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4723 | PyErr_SetString(PyExc_RuntimeError, |
| 4724 | "Cannot locate a COMSPEC environment variable to " |
| 4725 | "use as the shell"); |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4726 | return FALSE; |
| 4727 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4728 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4729 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 4730 | siStartInfo.cb = sizeof(STARTUPINFO); |
| 4731 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 4732 | siStartInfo.hStdInput = hStdin; |
| 4733 | siStartInfo.hStdOutput = hStdout; |
| 4734 | siStartInfo.hStdError = hStderr; |
| 4735 | siStartInfo.wShowWindow = SW_HIDE; |
| 4736 | |
| 4737 | if (CreateProcess(NULL, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4738 | s2, |
| 4739 | NULL, |
| 4740 | NULL, |
| 4741 | TRUE, |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4742 | dwProcessFlags, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4743 | NULL, |
| 4744 | NULL, |
| 4745 | &siStartInfo, |
| 4746 | &piProcInfo) ) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4747 | /* Close the handles now so anyone waiting is woken. */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4748 | CloseHandle(piProcInfo.hThread); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4749 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4750 | /* Return process handle */ |
| 4751 | *hProcess = piProcInfo.hProcess; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4752 | return TRUE; |
| 4753 | } |
Tim Peters | 402d598 | 2001-08-27 06:37:48 +0000 | [diff] [blame] | 4754 | win32_error("CreateProcess", s2); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4755 | return FALSE; |
| 4756 | } |
| 4757 | |
| 4758 | /* The following code is based off of KB: Q190351 */ |
| 4759 | |
| 4760 | static PyObject * |
| 4761 | _PyPopen(char *cmdstring, int mode, int n) |
| 4762 | { |
| 4763 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, |
| 4764 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4765 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4766 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4767 | SECURITY_ATTRIBUTES saAttr; |
| 4768 | BOOL fSuccess; |
| 4769 | int fd1, fd2, fd3; |
| 4770 | FILE *f1, *f2, *f3; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4771 | long file_count; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4772 | PyObject *f; |
| 4773 | |
| 4774 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 4775 | saAttr.bInheritHandle = TRUE; |
| 4776 | saAttr.lpSecurityDescriptor = NULL; |
| 4777 | |
| 4778 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) |
| 4779 | return win32_error("CreatePipe", NULL); |
| 4780 | |
| 4781 | /* Create new output read handle and the input write handle. Set |
| 4782 | * the inheritance properties to FALSE. Otherwise, the child inherits |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 4783 | * these handles; resulting in non-closeable handles to the pipes |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4784 | * being created. */ |
| 4785 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4786 | GetCurrentProcess(), &hChildStdinWrDup, 0, |
| 4787 | FALSE, |
| 4788 | DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4789 | if (!fSuccess) |
| 4790 | return win32_error("DuplicateHandle", NULL); |
| 4791 | |
| 4792 | /* Close the inheritable version of ChildStdin |
| 4793 | that we're using. */ |
| 4794 | CloseHandle(hChildStdinWr); |
| 4795 | |
| 4796 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) |
| 4797 | return win32_error("CreatePipe", NULL); |
| 4798 | |
| 4799 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4800 | GetCurrentProcess(), &hChildStdoutRdDup, 0, |
| 4801 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4802 | if (!fSuccess) |
| 4803 | return win32_error("DuplicateHandle", NULL); |
| 4804 | |
| 4805 | /* Close the inheritable version of ChildStdout |
| 4806 | that we're using. */ |
| 4807 | CloseHandle(hChildStdoutRd); |
| 4808 | |
| 4809 | if (n != POPEN_4) { |
| 4810 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) |
| 4811 | return win32_error("CreatePipe", NULL); |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4812 | fSuccess = DuplicateHandle(GetCurrentProcess(), |
| 4813 | hChildStderrRd, |
| 4814 | GetCurrentProcess(), |
| 4815 | &hChildStderrRdDup, 0, |
| 4816 | FALSE, DUPLICATE_SAME_ACCESS); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4817 | if (!fSuccess) |
| 4818 | return win32_error("DuplicateHandle", NULL); |
| 4819 | /* Close the inheritable version of ChildStdErr that we're using. */ |
| 4820 | CloseHandle(hChildStderrRd); |
| 4821 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4822 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4823 | switch (n) { |
| 4824 | case POPEN_1: |
| 4825 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { |
| 4826 | case _O_WRONLY | _O_TEXT: |
| 4827 | /* Case for writing to child Stdin in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4828 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4829 | f1 = _fdopen(fd1, "w"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4830 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4831 | PyFile_SetBufSize(f, 0); |
| 4832 | /* We don't care about these pipes anymore, so close them. */ |
| 4833 | CloseHandle(hChildStdoutRdDup); |
| 4834 | CloseHandle(hChildStderrRdDup); |
| 4835 | break; |
| 4836 | |
| 4837 | case _O_RDONLY | _O_TEXT: |
| 4838 | /* Case for reading from child Stdout in text mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4839 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4840 | f1 = _fdopen(fd1, "r"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4841 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4842 | PyFile_SetBufSize(f, 0); |
| 4843 | /* We don't care about these pipes anymore, so close them. */ |
| 4844 | CloseHandle(hChildStdinWrDup); |
| 4845 | CloseHandle(hChildStderrRdDup); |
| 4846 | break; |
| 4847 | |
| 4848 | case _O_RDONLY | _O_BINARY: |
| 4849 | /* Case for readinig from child Stdout in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4850 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4851 | f1 = _fdopen(fd1, "rb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4852 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4853 | PyFile_SetBufSize(f, 0); |
| 4854 | /* We don't care about these pipes anymore, so close them. */ |
| 4855 | CloseHandle(hChildStdinWrDup); |
| 4856 | CloseHandle(hChildStderrRdDup); |
| 4857 | break; |
| 4858 | |
| 4859 | case _O_WRONLY | _O_BINARY: |
| 4860 | /* Case for writing to child Stdin in binary mode. */ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4861 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4862 | f1 = _fdopen(fd1, "wb"); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4863 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4864 | PyFile_SetBufSize(f, 0); |
| 4865 | /* We don't care about these pipes anymore, so close them. */ |
| 4866 | CloseHandle(hChildStdoutRdDup); |
| 4867 | CloseHandle(hChildStderrRdDup); |
| 4868 | break; |
| 4869 | } |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4870 | file_count = 1; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4871 | break; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4872 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4873 | case POPEN_2: |
| 4874 | case POPEN_4: |
| 4875 | { |
| 4876 | char *m1, *m2; |
| 4877 | PyObject *p1, *p2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4878 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4879 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4880 | m1 = "r"; |
| 4881 | m2 = "w"; |
| 4882 | } else { |
| 4883 | m1 = "rb"; |
| 4884 | m2 = "wb"; |
| 4885 | } |
| 4886 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4887 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4888 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4889 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4890 | f2 = _fdopen(fd2, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4891 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4892 | PyFile_SetBufSize(p1, 0); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4893 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4894 | PyFile_SetBufSize(p2, 0); |
| 4895 | |
| 4896 | if (n != 4) |
| 4897 | CloseHandle(hChildStderrRdDup); |
| 4898 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4899 | f = PyTuple_Pack(2,p1,p2); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4900 | Py_XDECREF(p1); |
| 4901 | Py_XDECREF(p2); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4902 | file_count = 2; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4903 | break; |
| 4904 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4905 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4906 | case POPEN_3: |
| 4907 | { |
| 4908 | char *m1, *m2; |
| 4909 | PyObject *p1, *p2, *p3; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4910 | |
Tim Peters | 7dca21e | 2002-08-19 00:42:29 +0000 | [diff] [blame] | 4911 | if (mode & _O_TEXT) { |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4912 | m1 = "r"; |
| 4913 | m2 = "w"; |
| 4914 | } else { |
| 4915 | m1 = "rb"; |
| 4916 | m2 = "wb"; |
| 4917 | } |
| 4918 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4919 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4920 | f1 = _fdopen(fd1, m2); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4921 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4922 | f2 = _fdopen(fd2, m1); |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 4923 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4924 | f3 = _fdopen(fd3, m1); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 4925 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4926 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); |
| 4927 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4928 | PyFile_SetBufSize(p1, 0); |
| 4929 | PyFile_SetBufSize(p2, 0); |
| 4930 | PyFile_SetBufSize(p3, 0); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4931 | f = PyTuple_Pack(3,p1,p2,p3); |
Mark Hammond | 64aae66 | 2001-01-31 05:38:47 +0000 | [diff] [blame] | 4932 | Py_XDECREF(p1); |
| 4933 | Py_XDECREF(p2); |
| 4934 | Py_XDECREF(p3); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4935 | file_count = 3; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4936 | break; |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | if (n == POPEN_4) { |
| 4941 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4942 | hChildStdinRd, |
| 4943 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4944 | hChildStdoutWr, |
| 4945 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4946 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4947 | } |
| 4948 | else { |
| 4949 | if (!_PyPopenCreateProcess(cmdstring, |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 4950 | hChildStdinRd, |
| 4951 | hChildStdoutWr, |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4952 | hChildStderrWr, |
| 4953 | &hProcess)) |
Mark Hammond | 0850137 | 2001-01-31 07:30:29 +0000 | [diff] [blame] | 4954 | return NULL; |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 4957 | /* |
| 4958 | * Insert the files we've created into the process dictionary |
| 4959 | * all referencing the list with the process handle and the |
| 4960 | * initial number of files (see description below in _PyPclose). |
| 4961 | * Since if _PyPclose later tried to wait on a process when all |
| 4962 | * handles weren't closed, it could create a deadlock with the |
| 4963 | * child, we spend some energy here to try to ensure that we |
| 4964 | * either insert all file handles into the dictionary or none |
| 4965 | * at all. It's a little clumsy with the various popen modes |
| 4966 | * and variable number of files involved. |
| 4967 | */ |
| 4968 | if (!_PyPopenProcs) { |
| 4969 | _PyPopenProcs = PyDict_New(); |
| 4970 | } |
| 4971 | |
| 4972 | if (_PyPopenProcs) { |
| 4973 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; |
| 4974 | int ins_rc[3]; |
| 4975 | |
| 4976 | fileObj[0] = fileObj[1] = fileObj[2] = NULL; |
| 4977 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; |
| 4978 | |
| 4979 | procObj = PyList_New(2); |
| 4980 | hProcessObj = PyLong_FromVoidPtr(hProcess); |
| 4981 | intObj = PyInt_FromLong(file_count); |
| 4982 | |
| 4983 | if (procObj && hProcessObj && intObj) { |
| 4984 | PyList_SetItem(procObj,0,hProcessObj); |
| 4985 | PyList_SetItem(procObj,1,intObj); |
| 4986 | |
| 4987 | fileObj[0] = PyLong_FromVoidPtr(f1); |
| 4988 | if (fileObj[0]) { |
| 4989 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs, |
| 4990 | fileObj[0], |
| 4991 | procObj); |
| 4992 | } |
| 4993 | if (file_count >= 2) { |
| 4994 | fileObj[1] = PyLong_FromVoidPtr(f2); |
| 4995 | if (fileObj[1]) { |
| 4996 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs, |
| 4997 | fileObj[1], |
| 4998 | procObj); |
| 4999 | } |
| 5000 | } |
| 5001 | if (file_count >= 3) { |
| 5002 | fileObj[2] = PyLong_FromVoidPtr(f3); |
| 5003 | if (fileObj[2]) { |
| 5004 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs, |
| 5005 | fileObj[2], |
| 5006 | procObj); |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | if (ins_rc[0] < 0 || !fileObj[0] || |
| 5011 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || |
| 5012 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { |
| 5013 | /* Something failed - remove any dictionary |
| 5014 | * entries that did make it. |
| 5015 | */ |
| 5016 | if (!ins_rc[0] && fileObj[0]) { |
| 5017 | PyDict_DelItem(_PyPopenProcs, |
| 5018 | fileObj[0]); |
| 5019 | } |
| 5020 | if (!ins_rc[1] && fileObj[1]) { |
| 5021 | PyDict_DelItem(_PyPopenProcs, |
| 5022 | fileObj[1]); |
| 5023 | } |
| 5024 | if (!ins_rc[2] && fileObj[2]) { |
| 5025 | PyDict_DelItem(_PyPopenProcs, |
| 5026 | fileObj[2]); |
| 5027 | } |
| 5028 | } |
| 5029 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5030 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5031 | /* |
| 5032 | * Clean up our localized references for the dictionary keys |
| 5033 | * and value since PyDict_SetItem will Py_INCREF any copies |
| 5034 | * that got placed in the dictionary. |
| 5035 | */ |
| 5036 | Py_XDECREF(procObj); |
| 5037 | Py_XDECREF(fileObj[0]); |
| 5038 | Py_XDECREF(fileObj[1]); |
| 5039 | Py_XDECREF(fileObj[2]); |
| 5040 | } |
| 5041 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5042 | /* Child is launched. Close the parents copy of those pipe |
| 5043 | * handles that only the child should have open. You need to |
| 5044 | * make sure that no handles to the write end of the output pipe |
| 5045 | * are maintained in this process or else the pipe will not close |
| 5046 | * when the child process exits and the ReadFile will hang. */ |
| 5047 | |
| 5048 | if (!CloseHandle(hChildStdinRd)) |
| 5049 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5050 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5051 | if (!CloseHandle(hChildStdoutWr)) |
| 5052 | return win32_error("CloseHandle", NULL); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5053 | |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5054 | if ((n != 4) && (!CloseHandle(hChildStderrWr))) |
| 5055 | return win32_error("CloseHandle", NULL); |
| 5056 | |
| 5057 | return f; |
| 5058 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5059 | |
| 5060 | /* |
| 5061 | * Wrapper for fclose() to use for popen* files, so we can retrieve the |
| 5062 | * 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] | 5063 | * |
| 5064 | * This function uses the _PyPopenProcs dictionary in order to map the |
| 5065 | * input file pointer to information about the process that was |
| 5066 | * originally created by the popen* call that created the file pointer. |
| 5067 | * The dictionary uses the file pointer as a key (with one entry |
| 5068 | * inserted for each file returned by the original popen* call) and a |
| 5069 | * single list object as the value for all files from a single call. |
| 5070 | * The list object contains the Win32 process handle at [0], and a file |
| 5071 | * count at [1], which is initialized to the total number of file |
| 5072 | * handles using that list. |
| 5073 | * |
| 5074 | * This function closes whichever handle it is passed, and decrements |
| 5075 | * the file count in the dictionary for the process handle pointed to |
| 5076 | * by this file. On the last close (when the file count reaches zero), |
| 5077 | * this function will wait for the child process and then return its |
| 5078 | * exit code as the result of the close() operation. This permits the |
| 5079 | * files to be closed in any order - it is always the close() of the |
| 5080 | * final handle that will return the exit code. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5081 | * |
| 5082 | * NOTE: This function is currently called with the GIL released. |
| 5083 | * hence we use the GILState API to manage our state. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5084 | */ |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5085 | |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5086 | static int _PyPclose(FILE *file) |
| 5087 | { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5088 | int result; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5089 | DWORD exit_code; |
| 5090 | HANDLE hProcess; |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5091 | PyObject *procObj, *hProcessObj, *intObj, *fileObj; |
| 5092 | long file_count; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5093 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5094 | PyGILState_STATE state; |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5095 | #endif |
| 5096 | |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5097 | /* Close the file handle first, to ensure it can't block the |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5098 | * child from exiting if it's the last handle. |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5099 | */ |
| 5100 | result = fclose(file); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5101 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5102 | state = PyGILState_Ensure(); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5103 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5104 | if (_PyPopenProcs) { |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5105 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && |
| 5106 | (procObj = PyDict_GetItem(_PyPopenProcs, |
| 5107 | fileObj)) != NULL && |
| 5108 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL && |
| 5109 | (intObj = PyList_GetItem(procObj,1)) != NULL) { |
| 5110 | |
| 5111 | hProcess = PyLong_AsVoidPtr(hProcessObj); |
| 5112 | file_count = PyInt_AsLong(intObj); |
| 5113 | |
| 5114 | if (file_count > 1) { |
| 5115 | /* Still other files referencing process */ |
| 5116 | file_count--; |
| 5117 | PyList_SetItem(procObj,1, |
| 5118 | PyInt_FromLong(file_count)); |
| 5119 | } else { |
| 5120 | /* Last file for this process */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5121 | if (result != EOF && |
| 5122 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && |
| 5123 | GetExitCodeProcess(hProcess, &exit_code)) { |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5124 | /* Possible truncation here in 16-bit environments, but |
| 5125 | * real exit codes are just the lower byte in any event. |
| 5126 | */ |
| 5127 | result = exit_code; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5128 | } else { |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5129 | /* Indicate failure - this will cause the file object |
| 5130 | * to raise an I/O error and translate the last Win32 |
| 5131 | * error code from errno. We do have a problem with |
| 5132 | * last errors that overlap the normal errno table, |
| 5133 | * but that's a consistent problem with the file object. |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5134 | */ |
Fredrik Lundh | 2031893 | 2000-07-26 17:29:12 +0000 | [diff] [blame] | 5135 | if (result != EOF) { |
| 5136 | /* If the error wasn't from the fclose(), then |
| 5137 | * set errno for the file object error handling. |
| 5138 | */ |
| 5139 | errno = GetLastError(); |
| 5140 | } |
| 5141 | result = -1; |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | /* Free up the native handle at this point */ |
| 5145 | CloseHandle(hProcess); |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5146 | } |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5147 | |
Mark Hammond | b37a373 | 2000-08-14 04:47:33 +0000 | [diff] [blame] | 5148 | /* Remove this file pointer from dictionary */ |
| 5149 | PyDict_DelItem(_PyPopenProcs, fileObj); |
| 5150 | |
| 5151 | if (PyDict_Size(_PyPopenProcs) == 0) { |
| 5152 | Py_DECREF(_PyPopenProcs); |
| 5153 | _PyPopenProcs = NULL; |
| 5154 | } |
| 5155 | |
| 5156 | } /* if object retrieval ok */ |
| 5157 | |
| 5158 | Py_XDECREF(fileObj); |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5159 | } /* if _PyPopenProcs */ |
| 5160 | |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5161 | #ifdef WITH_THREAD |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 5162 | PyGILState_Release(state); |
Tim Peters | 736aa32 | 2000-09-01 06:51:24 +0000 | [diff] [blame] | 5163 | #endif |
Fredrik Lundh | 56055a4 | 2000-07-23 19:47:12 +0000 | [diff] [blame] | 5164 | return result; |
| 5165 | } |
Tim Peters | 9acdd3a | 2000-09-01 19:26:36 +0000 | [diff] [blame] | 5166 | |
| 5167 | #else /* which OS? */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5168 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5169 | posix_popen(PyObject *self, PyObject *args) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5170 | { |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5171 | char *name; |
| 5172 | char *mode = "r"; |
| 5173 | int bufsize = -1; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5174 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5175 | PyObject *f; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5176 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5177 | return NULL; |
Martin v. Löwis | 9ad853b | 2003-10-31 10:01:53 +0000 | [diff] [blame] | 5178 | /* Strip mode of binary or text modifiers */ |
| 5179 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) |
| 5180 | mode = "r"; |
| 5181 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) |
| 5182 | mode = "w"; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5183 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5184 | fp = popen(name, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5185 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5186 | if (fp == NULL) |
| 5187 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5188 | f = PyFile_FromFile(fp, name, mode, pclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5189 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5190 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 5191 | return f; |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5192 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5193 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5194 | #endif /* PYOS_??? */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5195 | #endif /* HAVE_POPEN */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5196 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5197 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5198 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5199 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5200 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5201 | Set the current process's user id."); |
| 5202 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5203 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5204 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5205 | { |
| 5206 | int uid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5207 | if (!PyArg_ParseTuple(args, "i:setuid", &uid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5208 | return NULL; |
| 5209 | if (setuid(uid) < 0) |
| 5210 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5211 | Py_INCREF(Py_None); |
| 5212 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5213 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5214 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5215 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5216 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5217 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5218 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5219 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5220 | Set the current process's effective user id."); |
| 5221 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5222 | static PyObject * |
| 5223 | posix_seteuid (PyObject *self, PyObject *args) |
| 5224 | { |
| 5225 | int euid; |
| 5226 | if (!PyArg_ParseTuple(args, "i", &euid)) { |
| 5227 | return NULL; |
| 5228 | } else if (seteuid(euid) < 0) { |
| 5229 | return posix_error(); |
| 5230 | } else { |
| 5231 | Py_INCREF(Py_None); |
| 5232 | return Py_None; |
| 5233 | } |
| 5234 | } |
| 5235 | #endif /* HAVE_SETEUID */ |
| 5236 | |
| 5237 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5238 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5239 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5240 | Set the current process's effective group id."); |
| 5241 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5242 | static PyObject * |
| 5243 | posix_setegid (PyObject *self, PyObject *args) |
| 5244 | { |
| 5245 | int egid; |
| 5246 | if (!PyArg_ParseTuple(args, "i", &egid)) { |
| 5247 | return NULL; |
| 5248 | } else if (setegid(egid) < 0) { |
| 5249 | return posix_error(); |
| 5250 | } else { |
| 5251 | Py_INCREF(Py_None); |
| 5252 | return Py_None; |
| 5253 | } |
| 5254 | } |
| 5255 | #endif /* HAVE_SETEGID */ |
| 5256 | |
| 5257 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5258 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5259 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5260 | Set the current process's real and effective user ids."); |
| 5261 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5262 | static PyObject * |
| 5263 | posix_setreuid (PyObject *self, PyObject *args) |
| 5264 | { |
| 5265 | int ruid, euid; |
| 5266 | if (!PyArg_ParseTuple(args, "ii", &ruid, &euid)) { |
| 5267 | return NULL; |
| 5268 | } else if (setreuid(ruid, euid) < 0) { |
| 5269 | return posix_error(); |
| 5270 | } else { |
| 5271 | Py_INCREF(Py_None); |
| 5272 | return Py_None; |
| 5273 | } |
| 5274 | } |
| 5275 | #endif /* HAVE_SETREUID */ |
| 5276 | |
| 5277 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5278 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5279 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5280 | Set the current process's real and effective group ids."); |
| 5281 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5282 | static PyObject * |
| 5283 | posix_setregid (PyObject *self, PyObject *args) |
| 5284 | { |
| 5285 | int rgid, egid; |
| 5286 | if (!PyArg_ParseTuple(args, "ii", &rgid, &egid)) { |
| 5287 | return NULL; |
| 5288 | } else if (setregid(rgid, egid) < 0) { |
| 5289 | return posix_error(); |
| 5290 | } else { |
| 5291 | Py_INCREF(Py_None); |
| 5292 | return Py_None; |
| 5293 | } |
| 5294 | } |
| 5295 | #endif /* HAVE_SETREGID */ |
| 5296 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5297 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5298 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5299 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5300 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5301 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5302 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5303 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5304 | { |
| 5305 | int gid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5306 | if (!PyArg_ParseTuple(args, "i:setgid", &gid)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5307 | return NULL; |
| 5308 | if (setgid(gid) < 0) |
| 5309 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5310 | Py_INCREF(Py_None); |
| 5311 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5312 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5313 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5314 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5315 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5316 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5317 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5318 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5319 | |
| 5320 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 5321 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5322 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5323 | int i, len; |
| 5324 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5325 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5326 | if (!PySequence_Check(groups)) { |
| 5327 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 5328 | return NULL; |
| 5329 | } |
| 5330 | len = PySequence_Size(groups); |
| 5331 | if (len > MAX_GROUPS) { |
| 5332 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 5333 | return NULL; |
| 5334 | } |
| 5335 | for(i = 0; i < len; i++) { |
| 5336 | PyObject *elem; |
| 5337 | elem = PySequence_GetItem(groups, i); |
| 5338 | if (!elem) |
| 5339 | return NULL; |
| 5340 | if (!PyInt_Check(elem)) { |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 5341 | if (!PyLong_Check(elem)) { |
| 5342 | PyErr_SetString(PyExc_TypeError, |
| 5343 | "groups must be integers"); |
| 5344 | Py_DECREF(elem); |
| 5345 | return NULL; |
| 5346 | } else { |
| 5347 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 5348 | if (PyErr_Occurred()) { |
| 5349 | PyErr_SetString(PyExc_TypeError, |
| 5350 | "group id too big"); |
| 5351 | Py_DECREF(elem); |
| 5352 | return NULL; |
| 5353 | } |
| 5354 | grouplist[i] = x; |
| 5355 | /* read back the value to see if it fitted in gid_t */ |
| 5356 | if (grouplist[i] != x) { |
| 5357 | PyErr_SetString(PyExc_TypeError, |
| 5358 | "group id too big"); |
| 5359 | Py_DECREF(elem); |
| 5360 | return NULL; |
| 5361 | } |
| 5362 | } |
| 5363 | } else { |
| 5364 | long x = PyInt_AsLong(elem); |
| 5365 | grouplist[i] = x; |
| 5366 | if (grouplist[i] != x) { |
| 5367 | PyErr_SetString(PyExc_TypeError, |
| 5368 | "group id too big"); |
| 5369 | Py_DECREF(elem); |
| 5370 | return NULL; |
| 5371 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5372 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5373 | Py_DECREF(elem); |
| 5374 | } |
| 5375 | |
| 5376 | if (setgroups(len, grouplist) < 0) |
| 5377 | return posix_error(); |
| 5378 | Py_INCREF(Py_None); |
| 5379 | return Py_None; |
| 5380 | } |
| 5381 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5382 | |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5383 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5384 | static PyObject * |
| 5385 | wait_helper(int pid, int status, struct rusage *ru) |
| 5386 | { |
| 5387 | PyObject *result; |
| 5388 | static PyObject *struct_rusage; |
| 5389 | |
| 5390 | if (pid == -1) |
| 5391 | return posix_error(); |
| 5392 | |
| 5393 | if (struct_rusage == NULL) { |
| 5394 | PyObject *m = PyImport_ImportModule("resource"); |
| 5395 | if (m == NULL) |
| 5396 | return NULL; |
| 5397 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 5398 | Py_DECREF(m); |
| 5399 | if (struct_rusage == NULL) |
| 5400 | return NULL; |
| 5401 | } |
| 5402 | |
| 5403 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 5404 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 5405 | if (!result) |
| 5406 | return NULL; |
| 5407 | |
| 5408 | #ifndef doubletime |
| 5409 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 5410 | #endif |
| 5411 | |
| 5412 | PyStructSequence_SET_ITEM(result, 0, |
| 5413 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 5414 | PyStructSequence_SET_ITEM(result, 1, |
| 5415 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 5416 | #define SET_INT(result, index, value)\ |
| 5417 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) |
| 5418 | SET_INT(result, 2, ru->ru_maxrss); |
| 5419 | SET_INT(result, 3, ru->ru_ixrss); |
| 5420 | SET_INT(result, 4, ru->ru_idrss); |
| 5421 | SET_INT(result, 5, ru->ru_isrss); |
| 5422 | SET_INT(result, 6, ru->ru_minflt); |
| 5423 | SET_INT(result, 7, ru->ru_majflt); |
| 5424 | SET_INT(result, 8, ru->ru_nswap); |
| 5425 | SET_INT(result, 9, ru->ru_inblock); |
| 5426 | SET_INT(result, 10, ru->ru_oublock); |
| 5427 | SET_INT(result, 11, ru->ru_msgsnd); |
| 5428 | SET_INT(result, 12, ru->ru_msgrcv); |
| 5429 | SET_INT(result, 13, ru->ru_nsignals); |
| 5430 | SET_INT(result, 14, ru->ru_nvcsw); |
| 5431 | SET_INT(result, 15, ru->ru_nivcsw); |
| 5432 | #undef SET_INT |
| 5433 | |
| 5434 | if (PyErr_Occurred()) { |
| 5435 | Py_DECREF(result); |
| 5436 | return NULL; |
| 5437 | } |
| 5438 | |
Neal Norwitz | 9b00a56 | 2006-03-20 08:47:12 +0000 | [diff] [blame] | 5439 | return Py_BuildValue("iiN", pid, status, result); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5440 | } |
Neal Norwitz | 6c2f913 | 2006-03-20 07:25:26 +0000 | [diff] [blame] | 5441 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5442 | |
| 5443 | #ifdef HAVE_WAIT3 |
| 5444 | PyDoc_STRVAR(posix_wait3__doc__, |
| 5445 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 5446 | Wait for completion of a child process."); |
| 5447 | |
| 5448 | static PyObject * |
| 5449 | posix_wait3(PyObject *self, PyObject *args) |
| 5450 | { |
| 5451 | int pid, options; |
| 5452 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5453 | WAIT_TYPE status; |
| 5454 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5455 | |
| 5456 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 5457 | return NULL; |
| 5458 | |
| 5459 | Py_BEGIN_ALLOW_THREADS |
| 5460 | pid = wait3(&status, options, &ru); |
| 5461 | Py_END_ALLOW_THREADS |
| 5462 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5463 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5464 | } |
| 5465 | #endif /* HAVE_WAIT3 */ |
| 5466 | |
| 5467 | #ifdef HAVE_WAIT4 |
| 5468 | PyDoc_STRVAR(posix_wait4__doc__, |
| 5469 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 5470 | Wait for completion of a given child process."); |
| 5471 | |
| 5472 | static PyObject * |
| 5473 | posix_wait4(PyObject *self, PyObject *args) |
| 5474 | { |
| 5475 | int pid, options; |
| 5476 | struct rusage ru; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5477 | WAIT_TYPE status; |
| 5478 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5479 | |
| 5480 | if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) |
| 5481 | return NULL; |
| 5482 | |
| 5483 | Py_BEGIN_ALLOW_THREADS |
| 5484 | pid = wait4(pid, &status, options, &ru); |
| 5485 | Py_END_ALLOW_THREADS |
| 5486 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5487 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 5488 | } |
| 5489 | #endif /* HAVE_WAIT4 */ |
| 5490 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5491 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5492 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5493 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5494 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5495 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5496 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5497 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5498 | { |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5499 | int pid, options; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5500 | WAIT_TYPE status; |
| 5501 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5502 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5503 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5504 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5505 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 5506 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5507 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5508 | if (pid == -1) |
| 5509 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5510 | |
| 5511 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5514 | #elif defined(HAVE_CWAIT) |
| 5515 | |
| 5516 | /* 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] | 5517 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5518 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5519 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5520 | |
| 5521 | static PyObject * |
| 5522 | posix_waitpid(PyObject *self, PyObject *args) |
| 5523 | { |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 5524 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5525 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5526 | |
| 5527 | if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) |
| 5528 | return NULL; |
| 5529 | Py_BEGIN_ALLOW_THREADS |
| 5530 | pid = _cwait(&status, pid, options); |
| 5531 | Py_END_ALLOW_THREADS |
| 5532 | if (pid == -1) |
| 5533 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5534 | |
| 5535 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 5536 | return Py_BuildValue("ii", pid, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 5537 | } |
| 5538 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5539 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5540 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5541 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5542 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5543 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5544 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5545 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5546 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5547 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5548 | int pid; |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5549 | WAIT_TYPE status; |
| 5550 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5551 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5552 | Py_BEGIN_ALLOW_THREADS |
| 5553 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5554 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 5555 | if (pid == -1) |
| 5556 | return posix_error(); |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 5557 | |
| 5558 | return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5559 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5560 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5561 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5563 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5564 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5565 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5566 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5567 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5568 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5569 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5570 | #ifdef HAVE_LSTAT |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5571 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5572 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5573 | #ifdef MS_WINDOWS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5574 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5575 | #else |
| 5576 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); |
| 5577 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5578 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5581 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5582 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5583 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5584 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5585 | 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] | 5586 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5587 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5588 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5589 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5590 | char buf[MAXPATHLEN]; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 5591 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5592 | int n; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5593 | if (!PyArg_ParseTuple(args, "s:readlink", &path)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5594 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5595 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 5596 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5597 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5598 | if (n < 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 5599 | return posix_error_with_filename(path); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5600 | return PyString_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5601 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5602 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5603 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5604 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5605 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5606 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5607 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 5608 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5609 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5610 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5611 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5612 | { |
Martin v. Löwis | 4fc2bda | 2006-05-04 12:04:27 +0000 | [diff] [blame] | 5613 | return posix_2str(args, "etet:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5614 | } |
| 5615 | #endif /* HAVE_SYMLINK */ |
| 5616 | |
| 5617 | |
| 5618 | #ifdef HAVE_TIMES |
| 5619 | #ifndef HZ |
| 5620 | #define HZ 60 /* Universal constant :-) */ |
| 5621 | #endif /* HZ */ |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5622 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5623 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 5624 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 5625 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5626 | { |
| 5627 | ULONG value = 0; |
| 5628 | |
| 5629 | Py_BEGIN_ALLOW_THREADS |
| 5630 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 5631 | Py_END_ALLOW_THREADS |
| 5632 | |
| 5633 | return value; |
| 5634 | } |
| 5635 | |
| 5636 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5637 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5638 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5639 | /* Currently Only Uptime is Provided -- Others Later */ |
| 5640 | return Py_BuildValue("ddddd", |
| 5641 | (double)0 /* t.tms_utime / HZ */, |
| 5642 | (double)0 /* t.tms_stime / HZ */, |
| 5643 | (double)0 /* t.tms_cutime / HZ */, |
| 5644 | (double)0 /* t.tms_cstime / HZ */, |
| 5645 | (double)system_uptime() / 1000); |
| 5646 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5647 | #else /* not OS2 */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5648 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5649 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5650 | { |
| 5651 | struct tms t; |
| 5652 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5653 | errno = 0; |
| 5654 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5655 | if (c == (clock_t) -1) |
| 5656 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5657 | return Py_BuildValue("ddddd", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5658 | (double)t.tms_utime / HZ, |
| 5659 | (double)t.tms_stime / HZ, |
| 5660 | (double)t.tms_cutime / HZ, |
| 5661 | (double)t.tms_cstime / HZ, |
| 5662 | (double)c / HZ); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5663 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5664 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5665 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5666 | |
| 5667 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5668 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5669 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5670 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5671 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5672 | { |
| 5673 | FILETIME create, exit, kernel, user; |
| 5674 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5675 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5676 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 5677 | /* The fields of a FILETIME structure are the hi and lo part |
| 5678 | of a 64-bit value expressed in 100 nanosecond units. |
| 5679 | 1e7 is one second in such units; 1e-7 the inverse. |
| 5680 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 5681 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5682 | return Py_BuildValue( |
| 5683 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 5684 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 5685 | kernel.dwLowDateTime*1e-7), |
| 5686 | (double)(user.dwHighDateTime*429.4967296 + |
| 5687 | user.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5688 | (double)0, |
| 5689 | (double)0, |
| 5690 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 5691 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5692 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5693 | |
| 5694 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5695 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5696 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5697 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 5698 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5699 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5700 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 5701 | #ifdef HAVE_GETSID |
| 5702 | PyDoc_STRVAR(posix_getsid__doc__, |
| 5703 | "getsid(pid) -> sid\n\n\ |
| 5704 | Call the system call getsid()."); |
| 5705 | |
| 5706 | static PyObject * |
| 5707 | posix_getsid(PyObject *self, PyObject *args) |
| 5708 | { |
| 5709 | int pid, sid; |
| 5710 | if (!PyArg_ParseTuple(args, "i:getsid", &pid)) |
| 5711 | return NULL; |
| 5712 | sid = getsid(pid); |
| 5713 | if (sid < 0) |
| 5714 | return posix_error(); |
| 5715 | return PyInt_FromLong((long)sid); |
| 5716 | } |
| 5717 | #endif /* HAVE_GETSID */ |
| 5718 | |
| 5719 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5720 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5721 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5722 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5723 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5724 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5725 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5726 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5727 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5728 | if (setsid() < 0) |
| 5729 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5730 | Py_INCREF(Py_None); |
| 5731 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5732 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5733 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5734 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5735 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5736 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5737 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5738 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5739 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5740 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5741 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5742 | { |
| 5743 | int pid, pgrp; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5744 | if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5745 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5746 | if (setpgid(pid, pgrp) < 0) |
| 5747 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5748 | Py_INCREF(Py_None); |
| 5749 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5750 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5751 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5752 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5753 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5754 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5755 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5756 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5757 | 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] | 5758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5760 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5761 | { |
| 5762 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5763 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5764 | return NULL; |
| 5765 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5766 | if (pgid < 0) |
| 5767 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5768 | return PyInt_FromLong((long)pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5769 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5770 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5771 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5772 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5773 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5774 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5775 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5776 | 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] | 5777 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5778 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5779 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5780 | { |
| 5781 | int fd, pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5782 | if (!PyArg_ParseTuple(args, "ii:tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5783 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5784 | if (tcsetpgrp(fd, pgid) < 0) |
| 5785 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5786 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5787 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 5788 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5789 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5790 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5791 | /* Functions acting on file descriptors */ |
| 5792 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5793 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5794 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5795 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5796 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5797 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5798 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5799 | { |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5800 | char *file = NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5801 | int flag; |
| 5802 | int mode = 0777; |
| 5803 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5804 | |
| 5805 | #ifdef MS_WINDOWS |
| 5806 | if (unicode_file_names()) { |
| 5807 | PyUnicodeObject *po; |
| 5808 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 5809 | Py_BEGIN_ALLOW_THREADS |
| 5810 | /* PyUnicode_AS_UNICODE OK without thread |
| 5811 | lock as it is a simple dereference. */ |
| 5812 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 5813 | Py_END_ALLOW_THREADS |
| 5814 | if (fd < 0) |
| 5815 | return posix_error(); |
| 5816 | return PyInt_FromLong((long)fd); |
| 5817 | } |
| 5818 | /* Drop the argument parsing error as narrow strings |
| 5819 | are also valid. */ |
| 5820 | PyErr_Clear(); |
| 5821 | } |
| 5822 | #endif |
| 5823 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5824 | if (!PyArg_ParseTuple(args, "eti|i", |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5825 | Py_FileSystemDefaultEncoding, &file, |
| 5826 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 5827 | return NULL; |
| 5828 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5829 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5830 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5831 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5832 | if (fd < 0) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 5833 | return posix_error_with_allocated_filename(file); |
| 5834 | PyMem_Free(file); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5835 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5839 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5840 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5841 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5842 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5843 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5844 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5845 | { |
| 5846 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5847 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5848 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5849 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5850 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5851 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5852 | if (res < 0) |
| 5853 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5854 | Py_INCREF(Py_None); |
| 5855 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5859 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5860 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5861 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5864 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5865 | { |
| 5866 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5867 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5868 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5869 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5870 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5871 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5872 | if (fd < 0) |
| 5873 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5874 | return PyInt_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5877 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5878 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5879 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5880 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5882 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5883 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5884 | { |
| 5885 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5886 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5887 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5888 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5889 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5890 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5891 | if (res < 0) |
| 5892 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5893 | Py_INCREF(Py_None); |
| 5894 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5898 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5899 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5900 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5901 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5902 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5903 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5904 | { |
| 5905 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5906 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5907 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5908 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5909 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5910 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5911 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5912 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5913 | return NULL; |
| 5914 | #ifdef SEEK_SET |
| 5915 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5916 | switch (how) { |
| 5917 | case 0: how = SEEK_SET; break; |
| 5918 | case 1: how = SEEK_CUR; break; |
| 5919 | case 2: how = SEEK_END; break; |
| 5920 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5921 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5922 | |
| 5923 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 5924 | pos = PyInt_AsLong(posobj); |
| 5925 | #else |
| 5926 | pos = PyLong_Check(posobj) ? |
| 5927 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); |
| 5928 | #endif |
| 5929 | if (PyErr_Occurred()) |
| 5930 | return NULL; |
| 5931 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5932 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5933 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5934 | res = _lseeki64(fd, pos, how); |
| 5935 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5936 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5937 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5938 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5939 | if (res < 0) |
| 5940 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5941 | |
| 5942 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5943 | return PyInt_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5944 | #else |
| 5945 | return PyLong_FromLongLong(res); |
| 5946 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5950 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5951 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5952 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5953 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5954 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5955 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5956 | { |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5957 | int fd, size, n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5958 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5959 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5960 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5961 | if (size < 0) { |
| 5962 | errno = EINVAL; |
| 5963 | return posix_error(); |
| 5964 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5965 | buffer = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5966 | if (buffer == NULL) |
| 5967 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5968 | Py_BEGIN_ALLOW_THREADS |
| 5969 | n = read(fd, PyString_AsString(buffer), size); |
| 5970 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5971 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5972 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5973 | return posix_error(); |
| 5974 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5975 | if (n != size) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5976 | _PyString_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5977 | return buffer; |
| 5978 | } |
| 5979 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5980 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5981 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5982 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5983 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5984 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5985 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5986 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5987 | { |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5988 | int fd; |
| 5989 | Py_ssize_t size; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5990 | char *buffer; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5991 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5992 | if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5993 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5994 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5995 | size = write(fd, buffer, (size_t)size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5996 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5997 | if (size < 0) |
| 5998 | return posix_error(); |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5999 | return PyInt_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6002 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6003 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6004 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6005 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6006 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6007 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6008 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6009 | { |
| 6010 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6011 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6012 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6013 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6014 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 6015 | #ifdef __VMS |
| 6016 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 6017 | fsync(fd); |
| 6018 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6019 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6020 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6021 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6022 | if (res != 0) { |
| 6023 | #ifdef MS_WINDOWS |
| 6024 | return win32_error("fstat", NULL); |
| 6025 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6026 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6027 | #endif |
| 6028 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6029 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 6030 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6031 | } |
| 6032 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6034 | PyDoc_STRVAR(posix_fdopen__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6035 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6036 | Return an open file object connected to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6037 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6039 | posix_fdopen(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6040 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6041 | int fd; |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6042 | char *mode = "r"; |
| 6043 | int bufsize = -1; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6044 | FILE *fp; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6045 | PyObject *f; |
| 6046 | if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6047 | return NULL; |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 6048 | |
Thomas Heller | 1f043e2 | 2002-11-07 16:00:59 +0000 | [diff] [blame] | 6049 | if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 6050 | PyErr_Format(PyExc_ValueError, |
| 6051 | "invalid file mode '%s'", mode); |
| 6052 | return NULL; |
| 6053 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6054 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6055 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6056 | if (mode[0] == 'a') { |
| 6057 | /* try to make sure the O_APPEND flag is set */ |
| 6058 | int flags; |
| 6059 | flags = fcntl(fd, F_GETFL); |
| 6060 | if (flags != -1) |
| 6061 | fcntl(fd, F_SETFL, flags | O_APPEND); |
| 6062 | fp = fdopen(fd, mode); |
Thomas Wouters | 2a9a6b0 | 2006-03-31 22:38:19 +0000 | [diff] [blame] | 6063 | if (fp == NULL && flags != -1) |
Georg Brandl | 54a188a | 2006-03-31 20:00:11 +0000 | [diff] [blame] | 6064 | /* restore old mode if fdopen failed */ |
| 6065 | fcntl(fd, F_SETFL, flags); |
| 6066 | } else { |
| 6067 | fp = fdopen(fd, mode); |
| 6068 | } |
Georg Brandl | 644b1e7 | 2006-03-31 20:27:22 +0000 | [diff] [blame] | 6069 | #else |
| 6070 | fp = fdopen(fd, mode); |
| 6071 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6072 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6073 | if (fp == NULL) |
| 6074 | return posix_error(); |
Guido van Rossum | bd6be7a | 2002-09-15 18:45:46 +0000 | [diff] [blame] | 6075 | f = PyFile_FromFile(fp, "<fdopen>", mode, fclose); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6076 | if (f != NULL) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6077 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | a6a1e53 | 1995-01-10 15:36:38 +0000 | [diff] [blame] | 6078 | return f; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6081 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6082 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6083 | 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] | 6084 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6085 | |
| 6086 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 6087 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6088 | { |
| 6089 | int fd; |
| 6090 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 6091 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6092 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 6093 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6094 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6095 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6096 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6097 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6098 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6099 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6100 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6101 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6102 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6103 | #if defined(PYOS_OS2) |
| 6104 | HFILE read, write; |
| 6105 | APIRET rc; |
| 6106 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6107 | Py_BEGIN_ALLOW_THREADS |
| 6108 | rc = DosCreatePipe( &read, &write, 4096); |
| 6109 | Py_END_ALLOW_THREADS |
| 6110 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6111 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6112 | |
| 6113 | return Py_BuildValue("(ii)", read, write); |
| 6114 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6115 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6116 | int fds[2]; |
| 6117 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6118 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6119 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6120 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6121 | if (res != 0) |
| 6122 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6123 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6124 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6125 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6126 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6127 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6128 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6129 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6130 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 6131 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 6132 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 6133 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 6134 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 6135 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6136 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 6137 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6138 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6139 | #endif /* HAVE_PIPE */ |
| 6140 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6141 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6142 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6143 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6144 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6145 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6146 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6147 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6148 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6149 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6150 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6151 | int mode = 0666; |
| 6152 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6153 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6154 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6155 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6156 | res = mkfifo(filename, mode); |
| 6157 | Py_END_ALLOW_THREADS |
| 6158 | if (res < 0) |
| 6159 | return posix_error(); |
| 6160 | Py_INCREF(Py_None); |
| 6161 | return Py_None; |
| 6162 | } |
| 6163 | #endif |
| 6164 | |
| 6165 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 6166 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6167 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 6168 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6169 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 6170 | named filename. mode specifies both the permissions to use and the\n\ |
| 6171 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 6172 | 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] | 6173 | device defines the newly created device special file (probably using\n\ |
| 6174 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6175 | |
| 6176 | |
| 6177 | static PyObject * |
| 6178 | posix_mknod(PyObject *self, PyObject *args) |
| 6179 | { |
| 6180 | char *filename; |
| 6181 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6182 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6183 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 6184 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 6185 | return NULL; |
| 6186 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6187 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6188 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6189 | if (res < 0) |
| 6190 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6191 | Py_INCREF(Py_None); |
| 6192 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6193 | } |
| 6194 | #endif |
| 6195 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 6196 | #ifdef HAVE_DEVICE_MACROS |
| 6197 | PyDoc_STRVAR(posix_major__doc__, |
| 6198 | "major(device) -> major number\n\ |
| 6199 | Extracts a device major number from a raw device number."); |
| 6200 | |
| 6201 | static PyObject * |
| 6202 | posix_major(PyObject *self, PyObject *args) |
| 6203 | { |
| 6204 | int device; |
| 6205 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 6206 | return NULL; |
| 6207 | return PyInt_FromLong((long)major(device)); |
| 6208 | } |
| 6209 | |
| 6210 | PyDoc_STRVAR(posix_minor__doc__, |
| 6211 | "minor(device) -> minor number\n\ |
| 6212 | Extracts a device minor number from a raw device number."); |
| 6213 | |
| 6214 | static PyObject * |
| 6215 | posix_minor(PyObject *self, PyObject *args) |
| 6216 | { |
| 6217 | int device; |
| 6218 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 6219 | return NULL; |
| 6220 | return PyInt_FromLong((long)minor(device)); |
| 6221 | } |
| 6222 | |
| 6223 | PyDoc_STRVAR(posix_makedev__doc__, |
| 6224 | "makedev(major, minor) -> device number\n\ |
| 6225 | Composes a raw device number from the major and minor device numbers."); |
| 6226 | |
| 6227 | static PyObject * |
| 6228 | posix_makedev(PyObject *self, PyObject *args) |
| 6229 | { |
| 6230 | int major, minor; |
| 6231 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 6232 | return NULL; |
| 6233 | return PyInt_FromLong((long)makedev(major, minor)); |
| 6234 | } |
| 6235 | #endif /* device macros */ |
| 6236 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6237 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6238 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6239 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6240 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6241 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6243 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6244 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6245 | { |
| 6246 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6247 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6248 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6249 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6250 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6251 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6252 | return NULL; |
| 6253 | |
| 6254 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6255 | length = PyInt_AsLong(lenobj); |
| 6256 | #else |
| 6257 | length = PyLong_Check(lenobj) ? |
| 6258 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); |
| 6259 | #endif |
| 6260 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6261 | return NULL; |
| 6262 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6263 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6264 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6265 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6266 | if (res < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6267 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6268 | return NULL; |
| 6269 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6270 | Py_INCREF(Py_None); |
| 6271 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 6272 | } |
| 6273 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6274 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6275 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6276 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6277 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6278 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6279 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6280 | /* Save putenv() parameters as values here, so we can collect them when they |
| 6281 | * get re-set with another call for the same key. */ |
| 6282 | static PyObject *posix_putenv_garbage; |
| 6283 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6285 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6286 | { |
| 6287 | char *s1, *s2; |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6288 | char *newenv; |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6289 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6290 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6291 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6292 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6293 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6294 | |
| 6295 | #if defined(PYOS_OS2) |
| 6296 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 6297 | APIRET rc; |
| 6298 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6299 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 6300 | if (rc != NO_ERROR) |
| 6301 | return os2_error(rc); |
| 6302 | |
| 6303 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 6304 | APIRET rc; |
| 6305 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6306 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 6307 | if (rc != NO_ERROR) |
| 6308 | return os2_error(rc); |
| 6309 | } else { |
| 6310 | #endif |
| 6311 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6312 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 6313 | len = strlen(s1) + strlen(s2) + 2; |
| 6314 | /* len includes space for a trailing \0; the size arg to |
| 6315 | PyString_FromStringAndSize does not count that */ |
| 6316 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6317 | if (newstr == NULL) |
| 6318 | return PyErr_NoMemory(); |
Anthony Baxter | 64182fe | 2006-04-11 12:14:09 +0000 | [diff] [blame] | 6319 | newenv = PyString_AS_STRING(newstr); |
| 6320 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 6321 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 6322 | Py_DECREF(newstr); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6323 | posix_error(); |
| 6324 | return NULL; |
| 6325 | } |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 6326 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 6327 | * this will cause previous value to be collected. This has to |
| 6328 | * happen after the real putenv() call because the old value |
| 6329 | * was still accessible until then. */ |
| 6330 | if (PyDict_SetItem(posix_putenv_garbage, |
| 6331 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 6332 | /* really not much we can do; just leak */ |
| 6333 | PyErr_Clear(); |
| 6334 | } |
| 6335 | else { |
| 6336 | Py_DECREF(newstr); |
| 6337 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6338 | |
| 6339 | #if defined(PYOS_OS2) |
| 6340 | } |
| 6341 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6342 | Py_INCREF(Py_None); |
| 6343 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6344 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6345 | #endif /* putenv */ |
| 6346 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6347 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6348 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6349 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6350 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6351 | |
| 6352 | static PyObject * |
| 6353 | posix_unsetenv(PyObject *self, PyObject *args) |
| 6354 | { |
| 6355 | char *s1; |
| 6356 | |
| 6357 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 6358 | return NULL; |
| 6359 | |
| 6360 | unsetenv(s1); |
| 6361 | |
| 6362 | /* Remove the key from posix_putenv_garbage; |
| 6363 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6364 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 6365 | * old value was still accessible until then. |
| 6366 | */ |
| 6367 | if (PyDict_DelItem(posix_putenv_garbage, |
| 6368 | PyTuple_GET_ITEM(args, 0))) { |
| 6369 | /* really not much we can do; just leak */ |
| 6370 | PyErr_Clear(); |
| 6371 | } |
| 6372 | |
| 6373 | Py_INCREF(Py_None); |
| 6374 | return Py_None; |
| 6375 | } |
| 6376 | #endif /* unsetenv */ |
| 6377 | |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6378 | #ifdef HAVE_STRERROR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6379 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6380 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6381 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6382 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6383 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6384 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6385 | { |
| 6386 | int code; |
| 6387 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6388 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6389 | return NULL; |
| 6390 | message = strerror(code); |
| 6391 | if (message == NULL) { |
| 6392 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 6393 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 6394 | return NULL; |
| 6395 | } |
| 6396 | return PyString_FromString(message); |
| 6397 | } |
| 6398 | #endif /* strerror */ |
| 6399 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 6400 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6401 | #ifdef HAVE_SYS_WAIT_H |
| 6402 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6403 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6405 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6406 | 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] | 6407 | |
| 6408 | static PyObject * |
| 6409 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 6410 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6411 | WAIT_TYPE status; |
| 6412 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6413 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6414 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6415 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6416 | |
| 6417 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6418 | } |
| 6419 | #endif /* WCOREDUMP */ |
| 6420 | |
| 6421 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6422 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6423 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6424 | 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] | 6425 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6426 | |
| 6427 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6428 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6429 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6430 | WAIT_TYPE status; |
| 6431 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6432 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6433 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6434 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6435 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 6436 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6437 | } |
| 6438 | #endif /* WIFCONTINUED */ |
| 6439 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6440 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6441 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6442 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6443 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6444 | |
| 6445 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6446 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6447 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6448 | WAIT_TYPE status; |
| 6449 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6450 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6451 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6452 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6453 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6454 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6455 | } |
| 6456 | #endif /* WIFSTOPPED */ |
| 6457 | |
| 6458 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6459 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6460 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6461 | 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] | 6462 | |
| 6463 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6464 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6465 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6466 | WAIT_TYPE status; |
| 6467 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6468 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6469 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6470 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6471 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6472 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6473 | } |
| 6474 | #endif /* WIFSIGNALED */ |
| 6475 | |
| 6476 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6477 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6478 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6479 | 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] | 6480 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6481 | |
| 6482 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6483 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6484 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6485 | WAIT_TYPE status; |
| 6486 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6487 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6488 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6489 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6490 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 6491 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6492 | } |
| 6493 | #endif /* WIFEXITED */ |
| 6494 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6495 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6496 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6497 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6498 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6499 | |
| 6500 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6501 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6502 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6503 | WAIT_TYPE status; |
| 6504 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6505 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6506 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6507 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6508 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6509 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 6510 | } |
| 6511 | #endif /* WEXITSTATUS */ |
| 6512 | |
| 6513 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6514 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6515 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 6516 | 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] | 6517 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6518 | |
| 6519 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6520 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6521 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6522 | WAIT_TYPE status; |
| 6523 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6524 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6525 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6526 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6527 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6528 | return Py_BuildValue("i", WTERMSIG(status)); |
| 6529 | } |
| 6530 | #endif /* WTERMSIG */ |
| 6531 | |
| 6532 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6533 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6534 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6535 | Return the signal that stopped the process that provided\n\ |
| 6536 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6537 | |
| 6538 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6539 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6540 | { |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6541 | WAIT_TYPE status; |
| 6542 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6543 | |
Neal Norwitz | d5a3754 | 2006-03-20 06:48:34 +0000 | [diff] [blame] | 6544 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6545 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6546 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 6547 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 6548 | } |
| 6549 | #endif /* WSTOPSIG */ |
| 6550 | |
| 6551 | #endif /* HAVE_SYS_WAIT_H */ |
| 6552 | |
| 6553 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6554 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 6555 | #ifdef _SCO_DS |
| 6556 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 6557 | needed definitions in sys/statvfs.h */ |
| 6558 | #define _SVID3 |
| 6559 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6560 | #include <sys/statvfs.h> |
| 6561 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6562 | static PyObject* |
| 6563 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 6564 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 6565 | if (v == NULL) |
| 6566 | return NULL; |
| 6567 | |
| 6568 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6569 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6570 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
| 6571 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); |
| 6572 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); |
| 6573 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); |
| 6574 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); |
| 6575 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); |
| 6576 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); |
| 6577 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6578 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6579 | #else |
| 6580 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); |
| 6581 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6582 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6583 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6584 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6585 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6586 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6587 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6588 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6589 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6590 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6591 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6592 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 6593 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6594 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); |
| 6595 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); |
| 6596 | #endif |
| 6597 | |
| 6598 | return v; |
| 6599 | } |
| 6600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6601 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6602 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6603 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6604 | |
| 6605 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6606 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6607 | { |
| 6608 | int fd, res; |
| 6609 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6610 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6611 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6612 | return NULL; |
| 6613 | Py_BEGIN_ALLOW_THREADS |
| 6614 | res = fstatvfs(fd, &st); |
| 6615 | Py_END_ALLOW_THREADS |
| 6616 | if (res != 0) |
| 6617 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6618 | |
| 6619 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6620 | } |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6621 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6622 | |
| 6623 | |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 6624 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6625 | #include <sys/statvfs.h> |
| 6626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6627 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6628 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6629 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6630 | |
| 6631 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6632 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6633 | { |
| 6634 | char *path; |
| 6635 | int res; |
| 6636 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6637 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6638 | return NULL; |
| 6639 | Py_BEGIN_ALLOW_THREADS |
| 6640 | res = statvfs(path, &st); |
| 6641 | Py_END_ALLOW_THREADS |
| 6642 | if (res != 0) |
| 6643 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 6644 | |
| 6645 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6646 | } |
| 6647 | #endif /* HAVE_STATVFS */ |
| 6648 | |
| 6649 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6650 | #ifdef HAVE_TEMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6651 | PyDoc_STRVAR(posix_tempnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6652 | "tempnam([dir[, prefix]]) -> string\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6653 | Return a unique name for a temporary file.\n\ |
Neal Norwitz | 50d5d4f | 2002-07-30 01:17:43 +0000 | [diff] [blame] | 6654 | 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] | 6655 | or None if not needed."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6656 | |
| 6657 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6658 | posix_tempnam(PyObject *self, PyObject *args) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6659 | { |
| 6660 | PyObject *result = NULL; |
| 6661 | char *dir = NULL; |
| 6662 | char *pfx = NULL; |
| 6663 | char *name; |
| 6664 | |
| 6665 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) |
| 6666 | return NULL; |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6667 | |
| 6668 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6669 | "tempnam is a potential security risk to your program") < 0) |
| 6670 | return NULL; |
| 6671 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6672 | #ifdef MS_WINDOWS |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6673 | name = _tempnam(dir, pfx); |
| 6674 | #else |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6675 | name = tempnam(dir, pfx); |
Fred Drake | 78b71c2 | 2001-07-17 20:37:36 +0000 | [diff] [blame] | 6676 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6677 | if (name == NULL) |
| 6678 | return PyErr_NoMemory(); |
| 6679 | result = PyString_FromString(name); |
| 6680 | free(name); |
| 6681 | return result; |
| 6682 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 6683 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6684 | |
| 6685 | |
| 6686 | #ifdef HAVE_TMPFILE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6687 | PyDoc_STRVAR(posix_tmpfile__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6688 | "tmpfile() -> file object\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6689 | Create a temporary file with no directory entries."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6690 | |
| 6691 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6692 | posix_tmpfile(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6693 | { |
| 6694 | FILE *fp; |
| 6695 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6696 | fp = tmpfile(); |
| 6697 | if (fp == NULL) |
| 6698 | return posix_error(); |
Guido van Rossum | db9198a | 2002-06-10 19:23:22 +0000 | [diff] [blame] | 6699 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6700 | } |
| 6701 | #endif |
| 6702 | |
| 6703 | |
| 6704 | #ifdef HAVE_TMPNAM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6705 | PyDoc_STRVAR(posix_tmpnam__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6706 | "tmpnam() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6707 | Return a unique name for a temporary file."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6708 | |
| 6709 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6710 | posix_tmpnam(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6711 | { |
| 6712 | char buffer[L_tmpnam]; |
| 6713 | char *name; |
| 6714 | |
Skip Montanaro | 95618b5 | 2001-08-18 18:52:10 +0000 | [diff] [blame] | 6715 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 6716 | "tmpnam is a potential security risk to your program") < 0) |
| 6717 | return NULL; |
| 6718 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6719 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6720 | name = tmpnam_r(buffer); |
| 6721 | #else |
| 6722 | name = tmpnam(buffer); |
| 6723 | #endif |
| 6724 | if (name == NULL) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6725 | PyObject *err = Py_BuildValue("is", 0, |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 6726 | #ifdef USE_TMPNAM_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6727 | "unexpected NULL from tmpnam_r" |
| 6728 | #else |
| 6729 | "unexpected NULL from tmpnam" |
| 6730 | #endif |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 6731 | ); |
| 6732 | PyErr_SetObject(PyExc_OSError, err); |
| 6733 | Py_XDECREF(err); |
| 6734 | return NULL; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6735 | } |
| 6736 | return PyString_FromString(buffer); |
| 6737 | } |
| 6738 | #endif |
| 6739 | |
| 6740 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6741 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 6742 | * It maps strings representing configuration variable names to |
| 6743 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6744 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6745 | * rarely-used constants. There are three separate tables that use |
| 6746 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6747 | * |
| 6748 | * This code is always included, even if none of the interfaces that |
| 6749 | * need it are included. The #if hackery needed to avoid it would be |
| 6750 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6751 | */ |
| 6752 | struct constdef { |
| 6753 | char *name; |
| 6754 | long value; |
| 6755 | }; |
| 6756 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6757 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6758 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
| 6759 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6760 | { |
| 6761 | if (PyInt_Check(arg)) { |
| 6762 | *valuep = PyInt_AS_LONG(arg); |
| 6763 | return 1; |
| 6764 | } |
| 6765 | if (PyString_Check(arg)) { |
| 6766 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6767 | size_t lo = 0; |
| 6768 | size_t mid; |
| 6769 | size_t hi = tablesize; |
| 6770 | int cmp; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6771 | char *confname = PyString_AS_STRING(arg); |
| 6772 | while (lo < hi) { |
| 6773 | mid = (lo + hi) / 2; |
| 6774 | cmp = strcmp(confname, table[mid].name); |
| 6775 | if (cmp < 0) |
| 6776 | hi = mid; |
| 6777 | else if (cmp > 0) |
| 6778 | lo = mid + 1; |
| 6779 | else { |
| 6780 | *valuep = table[mid].value; |
| 6781 | return 1; |
| 6782 | } |
| 6783 | } |
| 6784 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 6785 | } |
| 6786 | else |
| 6787 | PyErr_SetString(PyExc_TypeError, |
| 6788 | "configuration names must be strings or integers"); |
| 6789 | return 0; |
| 6790 | } |
| 6791 | |
| 6792 | |
| 6793 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 6794 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6795 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 6796 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 6797 | #endif |
| 6798 | #ifdef _PC_ABI_ASYNC_IO |
| 6799 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 6800 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6801 | #ifdef _PC_ASYNC_IO |
| 6802 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 6803 | #endif |
| 6804 | #ifdef _PC_CHOWN_RESTRICTED |
| 6805 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 6806 | #endif |
| 6807 | #ifdef _PC_FILESIZEBITS |
| 6808 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 6809 | #endif |
| 6810 | #ifdef _PC_LAST |
| 6811 | {"PC_LAST", _PC_LAST}, |
| 6812 | #endif |
| 6813 | #ifdef _PC_LINK_MAX |
| 6814 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 6815 | #endif |
| 6816 | #ifdef _PC_MAX_CANON |
| 6817 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 6818 | #endif |
| 6819 | #ifdef _PC_MAX_INPUT |
| 6820 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 6821 | #endif |
| 6822 | #ifdef _PC_NAME_MAX |
| 6823 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 6824 | #endif |
| 6825 | #ifdef _PC_NO_TRUNC |
| 6826 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 6827 | #endif |
| 6828 | #ifdef _PC_PATH_MAX |
| 6829 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 6830 | #endif |
| 6831 | #ifdef _PC_PIPE_BUF |
| 6832 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 6833 | #endif |
| 6834 | #ifdef _PC_PRIO_IO |
| 6835 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 6836 | #endif |
| 6837 | #ifdef _PC_SOCK_MAXBUF |
| 6838 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 6839 | #endif |
| 6840 | #ifdef _PC_SYNC_IO |
| 6841 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 6842 | #endif |
| 6843 | #ifdef _PC_VDISABLE |
| 6844 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 6845 | #endif |
| 6846 | }; |
| 6847 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6848 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6849 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6850 | { |
| 6851 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 6852 | sizeof(posix_constants_pathconf) |
| 6853 | / sizeof(struct constdef)); |
| 6854 | } |
| 6855 | #endif |
| 6856 | |
| 6857 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6858 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6859 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6860 | 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] | 6861 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6862 | |
| 6863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6864 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6865 | { |
| 6866 | PyObject *result = NULL; |
| 6867 | int name, fd; |
| 6868 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6869 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 6870 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6871 | long limit; |
| 6872 | |
| 6873 | errno = 0; |
| 6874 | limit = fpathconf(fd, name); |
| 6875 | if (limit == -1 && errno != 0) |
| 6876 | posix_error(); |
| 6877 | else |
| 6878 | result = PyInt_FromLong(limit); |
| 6879 | } |
| 6880 | return result; |
| 6881 | } |
| 6882 | #endif |
| 6883 | |
| 6884 | |
| 6885 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6886 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6887 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6888 | 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] | 6889 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6890 | |
| 6891 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6892 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6893 | { |
| 6894 | PyObject *result = NULL; |
| 6895 | int name; |
| 6896 | char *path; |
| 6897 | |
| 6898 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 6899 | conv_path_confname, &name)) { |
| 6900 | long limit; |
| 6901 | |
| 6902 | errno = 0; |
| 6903 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6904 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6905 | if (errno == EINVAL) |
| 6906 | /* could be a path or name problem */ |
| 6907 | posix_error(); |
| 6908 | else |
| 6909 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 6910 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6911 | else |
| 6912 | result = PyInt_FromLong(limit); |
| 6913 | } |
| 6914 | return result; |
| 6915 | } |
| 6916 | #endif |
| 6917 | |
| 6918 | #ifdef HAVE_CONFSTR |
| 6919 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6920 | #ifdef _CS_ARCHITECTURE |
| 6921 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 6922 | #endif |
| 6923 | #ifdef _CS_HOSTNAME |
| 6924 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 6925 | #endif |
| 6926 | #ifdef _CS_HW_PROVIDER |
| 6927 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 6928 | #endif |
| 6929 | #ifdef _CS_HW_SERIAL |
| 6930 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 6931 | #endif |
| 6932 | #ifdef _CS_INITTAB_NAME |
| 6933 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 6934 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6935 | #ifdef _CS_LFS64_CFLAGS |
| 6936 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 6937 | #endif |
| 6938 | #ifdef _CS_LFS64_LDFLAGS |
| 6939 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6940 | #endif |
| 6941 | #ifdef _CS_LFS64_LIBS |
| 6942 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6943 | #endif |
| 6944 | #ifdef _CS_LFS64_LINTFLAGS |
| 6945 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6946 | #endif |
| 6947 | #ifdef _CS_LFS_CFLAGS |
| 6948 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6949 | #endif |
| 6950 | #ifdef _CS_LFS_LDFLAGS |
| 6951 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6952 | #endif |
| 6953 | #ifdef _CS_LFS_LIBS |
| 6954 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6955 | #endif |
| 6956 | #ifdef _CS_LFS_LINTFLAGS |
| 6957 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6958 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6959 | #ifdef _CS_MACHINE |
| 6960 | {"CS_MACHINE", _CS_MACHINE}, |
| 6961 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6962 | #ifdef _CS_PATH |
| 6963 | {"CS_PATH", _CS_PATH}, |
| 6964 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6965 | #ifdef _CS_RELEASE |
| 6966 | {"CS_RELEASE", _CS_RELEASE}, |
| 6967 | #endif |
| 6968 | #ifdef _CS_SRPC_DOMAIN |
| 6969 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6970 | #endif |
| 6971 | #ifdef _CS_SYSNAME |
| 6972 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6973 | #endif |
| 6974 | #ifdef _CS_VERSION |
| 6975 | {"CS_VERSION", _CS_VERSION}, |
| 6976 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6977 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6978 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6979 | #endif |
| 6980 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6981 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6982 | #endif |
| 6983 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6984 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6985 | #endif |
| 6986 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6987 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6988 | #endif |
| 6989 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6990 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6991 | #endif |
| 6992 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6993 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6994 | #endif |
| 6995 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6996 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6997 | #endif |
| 6998 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6999 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 7000 | #endif |
| 7001 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 7002 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 7003 | #endif |
| 7004 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 7005 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 7006 | #endif |
| 7007 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 7008 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 7009 | #endif |
| 7010 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 7011 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 7012 | #endif |
| 7013 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 7014 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 7015 | #endif |
| 7016 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 7017 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 7018 | #endif |
| 7019 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 7020 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 7021 | #endif |
| 7022 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 7023 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 7024 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7025 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 7026 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 7027 | #endif |
| 7028 | #ifdef _MIPS_CS_BASE |
| 7029 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 7030 | #endif |
| 7031 | #ifdef _MIPS_CS_HOSTID |
| 7032 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 7033 | #endif |
| 7034 | #ifdef _MIPS_CS_HW_NAME |
| 7035 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 7036 | #endif |
| 7037 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 7038 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 7039 | #endif |
| 7040 | #ifdef _MIPS_CS_OSREL_MAJ |
| 7041 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 7042 | #endif |
| 7043 | #ifdef _MIPS_CS_OSREL_MIN |
| 7044 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 7045 | #endif |
| 7046 | #ifdef _MIPS_CS_OSREL_PATCH |
| 7047 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 7048 | #endif |
| 7049 | #ifdef _MIPS_CS_OS_NAME |
| 7050 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 7051 | #endif |
| 7052 | #ifdef _MIPS_CS_OS_PROVIDER |
| 7053 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 7054 | #endif |
| 7055 | #ifdef _MIPS_CS_PROCESSORS |
| 7056 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 7057 | #endif |
| 7058 | #ifdef _MIPS_CS_SERIAL |
| 7059 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 7060 | #endif |
| 7061 | #ifdef _MIPS_CS_VENDOR |
| 7062 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 7063 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7064 | }; |
| 7065 | |
| 7066 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7067 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7068 | { |
| 7069 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 7070 | sizeof(posix_constants_confstr) |
| 7071 | / sizeof(struct constdef)); |
| 7072 | } |
| 7073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7074 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7075 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7076 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7077 | |
| 7078 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7079 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7080 | { |
| 7081 | PyObject *result = NULL; |
| 7082 | int name; |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7083 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7084 | |
| 7085 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7086 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7087 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7088 | errno = 0; |
Skip Montanaro | dd527fc | 2006-04-18 00:49:49 +0000 | [diff] [blame] | 7089 | len = confstr(name, buffer, sizeof(buffer)); |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 7090 | if (len == 0) { |
| 7091 | if (errno) { |
| 7092 | posix_error(); |
| 7093 | } |
| 7094 | else { |
| 7095 | result = Py_None; |
| 7096 | Py_INCREF(Py_None); |
| 7097 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7098 | } |
| 7099 | else { |
Neal Norwitz | 0d21b1e | 2006-04-20 06:44:42 +0000 | [diff] [blame] | 7100 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7101 | result = PyString_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7102 | if (result != NULL) |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7103 | confstr(name, PyString_AS_STRING(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7104 | } |
| 7105 | else |
Neal Norwitz | 449b24e | 2006-04-20 06:56:05 +0000 | [diff] [blame] | 7106 | result = PyString_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7107 | } |
| 7108 | } |
| 7109 | return result; |
| 7110 | } |
| 7111 | #endif |
| 7112 | |
| 7113 | |
| 7114 | #ifdef HAVE_SYSCONF |
| 7115 | static struct constdef posix_constants_sysconf[] = { |
| 7116 | #ifdef _SC_2_CHAR_TERM |
| 7117 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 7118 | #endif |
| 7119 | #ifdef _SC_2_C_BIND |
| 7120 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 7121 | #endif |
| 7122 | #ifdef _SC_2_C_DEV |
| 7123 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 7124 | #endif |
| 7125 | #ifdef _SC_2_C_VERSION |
| 7126 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 7127 | #endif |
| 7128 | #ifdef _SC_2_FORT_DEV |
| 7129 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 7130 | #endif |
| 7131 | #ifdef _SC_2_FORT_RUN |
| 7132 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 7133 | #endif |
| 7134 | #ifdef _SC_2_LOCALEDEF |
| 7135 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 7136 | #endif |
| 7137 | #ifdef _SC_2_SW_DEV |
| 7138 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 7139 | #endif |
| 7140 | #ifdef _SC_2_UPE |
| 7141 | {"SC_2_UPE", _SC_2_UPE}, |
| 7142 | #endif |
| 7143 | #ifdef _SC_2_VERSION |
| 7144 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 7145 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7146 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 7147 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 7148 | #endif |
| 7149 | #ifdef _SC_ACL |
| 7150 | {"SC_ACL", _SC_ACL}, |
| 7151 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7152 | #ifdef _SC_AIO_LISTIO_MAX |
| 7153 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 7154 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7155 | #ifdef _SC_AIO_MAX |
| 7156 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 7157 | #endif |
| 7158 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 7159 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 7160 | #endif |
| 7161 | #ifdef _SC_ARG_MAX |
| 7162 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 7163 | #endif |
| 7164 | #ifdef _SC_ASYNCHRONOUS_IO |
| 7165 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 7166 | #endif |
| 7167 | #ifdef _SC_ATEXIT_MAX |
| 7168 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 7169 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7170 | #ifdef _SC_AUDIT |
| 7171 | {"SC_AUDIT", _SC_AUDIT}, |
| 7172 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7173 | #ifdef _SC_AVPHYS_PAGES |
| 7174 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 7175 | #endif |
| 7176 | #ifdef _SC_BC_BASE_MAX |
| 7177 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 7178 | #endif |
| 7179 | #ifdef _SC_BC_DIM_MAX |
| 7180 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 7181 | #endif |
| 7182 | #ifdef _SC_BC_SCALE_MAX |
| 7183 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 7184 | #endif |
| 7185 | #ifdef _SC_BC_STRING_MAX |
| 7186 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 7187 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7188 | #ifdef _SC_CAP |
| 7189 | {"SC_CAP", _SC_CAP}, |
| 7190 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7191 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 7192 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 7193 | #endif |
| 7194 | #ifdef _SC_CHAR_BIT |
| 7195 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 7196 | #endif |
| 7197 | #ifdef _SC_CHAR_MAX |
| 7198 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 7199 | #endif |
| 7200 | #ifdef _SC_CHAR_MIN |
| 7201 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 7202 | #endif |
| 7203 | #ifdef _SC_CHILD_MAX |
| 7204 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 7205 | #endif |
| 7206 | #ifdef _SC_CLK_TCK |
| 7207 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 7208 | #endif |
| 7209 | #ifdef _SC_COHER_BLKSZ |
| 7210 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 7211 | #endif |
| 7212 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 7213 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 7214 | #endif |
| 7215 | #ifdef _SC_DCACHE_ASSOC |
| 7216 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 7217 | #endif |
| 7218 | #ifdef _SC_DCACHE_BLKSZ |
| 7219 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 7220 | #endif |
| 7221 | #ifdef _SC_DCACHE_LINESZ |
| 7222 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 7223 | #endif |
| 7224 | #ifdef _SC_DCACHE_SZ |
| 7225 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 7226 | #endif |
| 7227 | #ifdef _SC_DCACHE_TBLKSZ |
| 7228 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 7229 | #endif |
| 7230 | #ifdef _SC_DELAYTIMER_MAX |
| 7231 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 7232 | #endif |
| 7233 | #ifdef _SC_EQUIV_CLASS_MAX |
| 7234 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 7235 | #endif |
| 7236 | #ifdef _SC_EXPR_NEST_MAX |
| 7237 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 7238 | #endif |
| 7239 | #ifdef _SC_FSYNC |
| 7240 | {"SC_FSYNC", _SC_FSYNC}, |
| 7241 | #endif |
| 7242 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 7243 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 7244 | #endif |
| 7245 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 7246 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 7247 | #endif |
| 7248 | #ifdef _SC_ICACHE_ASSOC |
| 7249 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 7250 | #endif |
| 7251 | #ifdef _SC_ICACHE_BLKSZ |
| 7252 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 7253 | #endif |
| 7254 | #ifdef _SC_ICACHE_LINESZ |
| 7255 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 7256 | #endif |
| 7257 | #ifdef _SC_ICACHE_SZ |
| 7258 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 7259 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7260 | #ifdef _SC_INF |
| 7261 | {"SC_INF", _SC_INF}, |
| 7262 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7263 | #ifdef _SC_INT_MAX |
| 7264 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 7265 | #endif |
| 7266 | #ifdef _SC_INT_MIN |
| 7267 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 7268 | #endif |
| 7269 | #ifdef _SC_IOV_MAX |
| 7270 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 7271 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7272 | #ifdef _SC_IP_SECOPTS |
| 7273 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 7274 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7275 | #ifdef _SC_JOB_CONTROL |
| 7276 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 7277 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7278 | #ifdef _SC_KERN_POINTERS |
| 7279 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 7280 | #endif |
| 7281 | #ifdef _SC_KERN_SIM |
| 7282 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 7283 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7284 | #ifdef _SC_LINE_MAX |
| 7285 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 7286 | #endif |
| 7287 | #ifdef _SC_LOGIN_NAME_MAX |
| 7288 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 7289 | #endif |
| 7290 | #ifdef _SC_LOGNAME_MAX |
| 7291 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 7292 | #endif |
| 7293 | #ifdef _SC_LONG_BIT |
| 7294 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 7295 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7296 | #ifdef _SC_MAC |
| 7297 | {"SC_MAC", _SC_MAC}, |
| 7298 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7299 | #ifdef _SC_MAPPED_FILES |
| 7300 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 7301 | #endif |
| 7302 | #ifdef _SC_MAXPID |
| 7303 | {"SC_MAXPID", _SC_MAXPID}, |
| 7304 | #endif |
| 7305 | #ifdef _SC_MB_LEN_MAX |
| 7306 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 7307 | #endif |
| 7308 | #ifdef _SC_MEMLOCK |
| 7309 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 7310 | #endif |
| 7311 | #ifdef _SC_MEMLOCK_RANGE |
| 7312 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 7313 | #endif |
| 7314 | #ifdef _SC_MEMORY_PROTECTION |
| 7315 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 7316 | #endif |
| 7317 | #ifdef _SC_MESSAGE_PASSING |
| 7318 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 7319 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7320 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 7321 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 7322 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7323 | #ifdef _SC_MQ_OPEN_MAX |
| 7324 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 7325 | #endif |
| 7326 | #ifdef _SC_MQ_PRIO_MAX |
| 7327 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 7328 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7329 | #ifdef _SC_NACLS_MAX |
| 7330 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 7331 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7332 | #ifdef _SC_NGROUPS_MAX |
| 7333 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 7334 | #endif |
| 7335 | #ifdef _SC_NL_ARGMAX |
| 7336 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 7337 | #endif |
| 7338 | #ifdef _SC_NL_LANGMAX |
| 7339 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 7340 | #endif |
| 7341 | #ifdef _SC_NL_MSGMAX |
| 7342 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 7343 | #endif |
| 7344 | #ifdef _SC_NL_NMAX |
| 7345 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 7346 | #endif |
| 7347 | #ifdef _SC_NL_SETMAX |
| 7348 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 7349 | #endif |
| 7350 | #ifdef _SC_NL_TEXTMAX |
| 7351 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 7352 | #endif |
| 7353 | #ifdef _SC_NPROCESSORS_CONF |
| 7354 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 7355 | #endif |
| 7356 | #ifdef _SC_NPROCESSORS_ONLN |
| 7357 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 7358 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7359 | #ifdef _SC_NPROC_CONF |
| 7360 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 7361 | #endif |
| 7362 | #ifdef _SC_NPROC_ONLN |
| 7363 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 7364 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7365 | #ifdef _SC_NZERO |
| 7366 | {"SC_NZERO", _SC_NZERO}, |
| 7367 | #endif |
| 7368 | #ifdef _SC_OPEN_MAX |
| 7369 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 7370 | #endif |
| 7371 | #ifdef _SC_PAGESIZE |
| 7372 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 7373 | #endif |
| 7374 | #ifdef _SC_PAGE_SIZE |
| 7375 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 7376 | #endif |
| 7377 | #ifdef _SC_PASS_MAX |
| 7378 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 7379 | #endif |
| 7380 | #ifdef _SC_PHYS_PAGES |
| 7381 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 7382 | #endif |
| 7383 | #ifdef _SC_PII |
| 7384 | {"SC_PII", _SC_PII}, |
| 7385 | #endif |
| 7386 | #ifdef _SC_PII_INTERNET |
| 7387 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 7388 | #endif |
| 7389 | #ifdef _SC_PII_INTERNET_DGRAM |
| 7390 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 7391 | #endif |
| 7392 | #ifdef _SC_PII_INTERNET_STREAM |
| 7393 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 7394 | #endif |
| 7395 | #ifdef _SC_PII_OSI |
| 7396 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 7397 | #endif |
| 7398 | #ifdef _SC_PII_OSI_CLTS |
| 7399 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 7400 | #endif |
| 7401 | #ifdef _SC_PII_OSI_COTS |
| 7402 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 7403 | #endif |
| 7404 | #ifdef _SC_PII_OSI_M |
| 7405 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 7406 | #endif |
| 7407 | #ifdef _SC_PII_SOCKET |
| 7408 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 7409 | #endif |
| 7410 | #ifdef _SC_PII_XTI |
| 7411 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 7412 | #endif |
| 7413 | #ifdef _SC_POLL |
| 7414 | {"SC_POLL", _SC_POLL}, |
| 7415 | #endif |
| 7416 | #ifdef _SC_PRIORITIZED_IO |
| 7417 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 7418 | #endif |
| 7419 | #ifdef _SC_PRIORITY_SCHEDULING |
| 7420 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 7421 | #endif |
| 7422 | #ifdef _SC_REALTIME_SIGNALS |
| 7423 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 7424 | #endif |
| 7425 | #ifdef _SC_RE_DUP_MAX |
| 7426 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 7427 | #endif |
| 7428 | #ifdef _SC_RTSIG_MAX |
| 7429 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 7430 | #endif |
| 7431 | #ifdef _SC_SAVED_IDS |
| 7432 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 7433 | #endif |
| 7434 | #ifdef _SC_SCHAR_MAX |
| 7435 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 7436 | #endif |
| 7437 | #ifdef _SC_SCHAR_MIN |
| 7438 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 7439 | #endif |
| 7440 | #ifdef _SC_SELECT |
| 7441 | {"SC_SELECT", _SC_SELECT}, |
| 7442 | #endif |
| 7443 | #ifdef _SC_SEMAPHORES |
| 7444 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 7445 | #endif |
| 7446 | #ifdef _SC_SEM_NSEMS_MAX |
| 7447 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 7448 | #endif |
| 7449 | #ifdef _SC_SEM_VALUE_MAX |
| 7450 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 7451 | #endif |
| 7452 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 7453 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 7454 | #endif |
| 7455 | #ifdef _SC_SHRT_MAX |
| 7456 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 7457 | #endif |
| 7458 | #ifdef _SC_SHRT_MIN |
| 7459 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 7460 | #endif |
| 7461 | #ifdef _SC_SIGQUEUE_MAX |
| 7462 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 7463 | #endif |
| 7464 | #ifdef _SC_SIGRT_MAX |
| 7465 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 7466 | #endif |
| 7467 | #ifdef _SC_SIGRT_MIN |
| 7468 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 7469 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7470 | #ifdef _SC_SOFTPOWER |
| 7471 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 7472 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7473 | #ifdef _SC_SPLIT_CACHE |
| 7474 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 7475 | #endif |
| 7476 | #ifdef _SC_SSIZE_MAX |
| 7477 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 7478 | #endif |
| 7479 | #ifdef _SC_STACK_PROT |
| 7480 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 7481 | #endif |
| 7482 | #ifdef _SC_STREAM_MAX |
| 7483 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 7484 | #endif |
| 7485 | #ifdef _SC_SYNCHRONIZED_IO |
| 7486 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 7487 | #endif |
| 7488 | #ifdef _SC_THREADS |
| 7489 | {"SC_THREADS", _SC_THREADS}, |
| 7490 | #endif |
| 7491 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 7492 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 7493 | #endif |
| 7494 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 7495 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 7496 | #endif |
| 7497 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 7498 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 7499 | #endif |
| 7500 | #ifdef _SC_THREAD_KEYS_MAX |
| 7501 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 7502 | #endif |
| 7503 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 7504 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 7505 | #endif |
| 7506 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 7507 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 7508 | #endif |
| 7509 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 7510 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 7511 | #endif |
| 7512 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 7513 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 7514 | #endif |
| 7515 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 7516 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 7517 | #endif |
| 7518 | #ifdef _SC_THREAD_STACK_MIN |
| 7519 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 7520 | #endif |
| 7521 | #ifdef _SC_THREAD_THREADS_MAX |
| 7522 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 7523 | #endif |
| 7524 | #ifdef _SC_TIMERS |
| 7525 | {"SC_TIMERS", _SC_TIMERS}, |
| 7526 | #endif |
| 7527 | #ifdef _SC_TIMER_MAX |
| 7528 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 7529 | #endif |
| 7530 | #ifdef _SC_TTY_NAME_MAX |
| 7531 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 7532 | #endif |
| 7533 | #ifdef _SC_TZNAME_MAX |
| 7534 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 7535 | #endif |
| 7536 | #ifdef _SC_T_IOV_MAX |
| 7537 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 7538 | #endif |
| 7539 | #ifdef _SC_UCHAR_MAX |
| 7540 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 7541 | #endif |
| 7542 | #ifdef _SC_UINT_MAX |
| 7543 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 7544 | #endif |
| 7545 | #ifdef _SC_UIO_MAXIOV |
| 7546 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 7547 | #endif |
| 7548 | #ifdef _SC_ULONG_MAX |
| 7549 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 7550 | #endif |
| 7551 | #ifdef _SC_USHRT_MAX |
| 7552 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 7553 | #endif |
| 7554 | #ifdef _SC_VERSION |
| 7555 | {"SC_VERSION", _SC_VERSION}, |
| 7556 | #endif |
| 7557 | #ifdef _SC_WORD_BIT |
| 7558 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 7559 | #endif |
| 7560 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 7561 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 7562 | #endif |
| 7563 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 7564 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 7565 | #endif |
| 7566 | #ifdef _SC_XBS5_LP64_OFF64 |
| 7567 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 7568 | #endif |
| 7569 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 7570 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 7571 | #endif |
| 7572 | #ifdef _SC_XOPEN_CRYPT |
| 7573 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 7574 | #endif |
| 7575 | #ifdef _SC_XOPEN_ENH_I18N |
| 7576 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 7577 | #endif |
| 7578 | #ifdef _SC_XOPEN_LEGACY |
| 7579 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 7580 | #endif |
| 7581 | #ifdef _SC_XOPEN_REALTIME |
| 7582 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 7583 | #endif |
| 7584 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 7585 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 7586 | #endif |
| 7587 | #ifdef _SC_XOPEN_SHM |
| 7588 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 7589 | #endif |
| 7590 | #ifdef _SC_XOPEN_UNIX |
| 7591 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 7592 | #endif |
| 7593 | #ifdef _SC_XOPEN_VERSION |
| 7594 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 7595 | #endif |
| 7596 | #ifdef _SC_XOPEN_XCU_VERSION |
| 7597 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 7598 | #endif |
| 7599 | #ifdef _SC_XOPEN_XPG2 |
| 7600 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 7601 | #endif |
| 7602 | #ifdef _SC_XOPEN_XPG3 |
| 7603 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 7604 | #endif |
| 7605 | #ifdef _SC_XOPEN_XPG4 |
| 7606 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 7607 | #endif |
| 7608 | }; |
| 7609 | |
| 7610 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7611 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7612 | { |
| 7613 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 7614 | sizeof(posix_constants_sysconf) |
| 7615 | / sizeof(struct constdef)); |
| 7616 | } |
| 7617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7618 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7619 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7620 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7621 | |
| 7622 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7623 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7624 | { |
| 7625 | PyObject *result = NULL; |
| 7626 | int name; |
| 7627 | |
| 7628 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 7629 | int value; |
| 7630 | |
| 7631 | errno = 0; |
| 7632 | value = sysconf(name); |
| 7633 | if (value == -1 && errno != 0) |
| 7634 | posix_error(); |
| 7635 | else |
| 7636 | result = PyInt_FromLong(value); |
| 7637 | } |
| 7638 | return result; |
| 7639 | } |
| 7640 | #endif |
| 7641 | |
| 7642 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7643 | /* This code is used to ensure that the tables of configuration value names |
| 7644 | * are in sorted order as required by conv_confname(), and also to build the |
| 7645 | * the exported dictionaries that are used to publish information about the |
| 7646 | * names available on the host platform. |
| 7647 | * |
| 7648 | * Sorting the table at runtime ensures that the table is properly ordered |
| 7649 | * when used, even for platforms we're not able to test on. It also makes |
| 7650 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7651 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7652 | |
| 7653 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7654 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7655 | { |
| 7656 | const struct constdef *c1 = |
| 7657 | (const struct constdef *) v1; |
| 7658 | const struct constdef *c2 = |
| 7659 | (const struct constdef *) v2; |
| 7660 | |
| 7661 | return strcmp(c1->name, c2->name); |
| 7662 | } |
| 7663 | |
| 7664 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7665 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7666 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7667 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7668 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7669 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7670 | |
| 7671 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 7672 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7673 | if (d == NULL) |
| 7674 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7675 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7676 | for (i=0; i < tablesize; ++i) { |
| 7677 | PyObject *o = PyInt_FromLong(table[i].value); |
| 7678 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 7679 | Py_XDECREF(o); |
| 7680 | Py_DECREF(d); |
| 7681 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7682 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 7683 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7684 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7685 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7686 | } |
| 7687 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7688 | /* Return -1 on failure, 0 on success. */ |
| 7689 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7690 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7691 | { |
| 7692 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7693 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7694 | sizeof(posix_constants_pathconf) |
| 7695 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7696 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7697 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7698 | #endif |
| 7699 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7700 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7701 | sizeof(posix_constants_confstr) |
| 7702 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7703 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7704 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7705 | #endif |
| 7706 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7707 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7708 | sizeof(posix_constants_sysconf) |
| 7709 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7710 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7711 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7712 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7713 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7714 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 7715 | |
| 7716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7717 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7718 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7719 | 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] | 7720 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7721 | |
| 7722 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7723 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7724 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7725 | abort(); |
| 7726 | /*NOTREACHED*/ |
| 7727 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 7728 | return NULL; |
| 7729 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7730 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7731 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7732 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7733 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 7734 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7735 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7736 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 7737 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 7738 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 7739 | application (if any) its extension is associated.\n\ |
| 7740 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 7741 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7742 | \n\ |
| 7743 | startfile returns as soon as the associated application is launched.\n\ |
| 7744 | There is no option to wait for the application to close, and no way\n\ |
| 7745 | to retrieve the application's exit status.\n\ |
| 7746 | \n\ |
| 7747 | The filepath is relative to the current directory. If you want to use\n\ |
| 7748 | 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] | 7749 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7750 | |
| 7751 | static PyObject * |
| 7752 | win32_startfile(PyObject *self, PyObject *args) |
| 7753 | { |
| 7754 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7755 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7756 | HINSTANCE rc; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7757 | #ifdef Py_WIN_WIDE_FILENAMES |
| 7758 | if (unicode_file_names()) { |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7759 | PyObject *unipath, *woperation = NULL; |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7760 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 7761 | &unipath, &operation)) { |
| 7762 | PyErr_Clear(); |
| 7763 | goto normal; |
| 7764 | } |
| 7765 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7766 | |
| 7767 | if (operation) { |
| 7768 | woperation = PyUnicode_DecodeASCII(operation, |
| 7769 | strlen(operation), NULL); |
| 7770 | if (!woperation) { |
| 7771 | PyErr_Clear(); |
| 7772 | operation = NULL; |
| 7773 | goto normal; |
| 7774 | } |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7778 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7779 | PyUnicode_AS_UNICODE(unipath), |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7780 | NULL, NULL, SW_SHOWNORMAL); |
| 7781 | Py_END_ALLOW_THREADS |
| 7782 | |
Martin v. Löwis | 5fe715f | 2006-04-03 23:01:24 +0000 | [diff] [blame] | 7783 | Py_XDECREF(woperation); |
Georg Brandl | ad89dc8 | 2006-04-03 12:26:26 +0000 | [diff] [blame] | 7784 | if (rc <= (HINSTANCE)32) { |
| 7785 | PyObject *errval = win32_error_unicode("startfile", |
| 7786 | PyUnicode_AS_UNICODE(unipath)); |
| 7787 | return errval; |
| 7788 | } |
| 7789 | Py_INCREF(Py_None); |
| 7790 | return Py_None; |
| 7791 | } |
| 7792 | #endif |
| 7793 | |
| 7794 | normal: |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7795 | if (!PyArg_ParseTuple(args, "et|s:startfile", |
| 7796 | Py_FileSystemDefaultEncoding, &filepath, |
| 7797 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7798 | return NULL; |
| 7799 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 7800 | rc = ShellExecute((HWND)0, operation, filepath, |
| 7801 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7802 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 7803 | if (rc <= (HINSTANCE)32) { |
| 7804 | PyObject *errval = win32_error("startfile", filepath); |
| 7805 | PyMem_Free(filepath); |
| 7806 | return errval; |
| 7807 | } |
| 7808 | PyMem_Free(filepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 7809 | Py_INCREF(Py_None); |
| 7810 | return Py_None; |
| 7811 | } |
| 7812 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7813 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7814 | #ifdef HAVE_GETLOADAVG |
| 7815 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 7816 | "getloadavg() -> (float, float, float)\n\n\ |
| 7817 | Return the number of processes in the system run queue averaged over\n\ |
| 7818 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 7819 | was unobtainable"); |
| 7820 | |
| 7821 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7822 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7823 | { |
| 7824 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7825 | if (getloadavg(loadavg, 3)!=3) { |
| 7826 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 7827 | return NULL; |
| 7828 | } else |
| 7829 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 7830 | } |
| 7831 | #endif |
| 7832 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7833 | #ifdef MS_WINDOWS |
| 7834 | |
| 7835 | PyDoc_STRVAR(win32_urandom__doc__, |
| 7836 | "urandom(n) -> str\n\n\ |
| 7837 | Return a string of n random bytes suitable for cryptographic use."); |
| 7838 | |
| 7839 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 7840 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 7841 | DWORD dwFlags ); |
| 7842 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 7843 | BYTE *pbBuffer ); |
| 7844 | |
| 7845 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 7846 | static HCRYPTPROV hCryptProv = 0; |
| 7847 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7848 | static PyObject* |
| 7849 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7850 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7851 | int howMany; |
| 7852 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7853 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7854 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 7855 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7856 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 7857 | if (howMany < 0) |
| 7858 | return PyErr_Format(PyExc_ValueError, |
| 7859 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7860 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7861 | if (hCryptProv == 0) { |
| 7862 | HINSTANCE hAdvAPI32 = NULL; |
| 7863 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7864 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7865 | /* Obtain handle to the DLL containing CryptoAPI |
| 7866 | This should not fail */ |
| 7867 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 7868 | if(hAdvAPI32 == NULL) |
| 7869 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7870 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7871 | /* Obtain pointers to the CryptoAPI functions |
| 7872 | This will fail on some early versions of Win95 */ |
| 7873 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 7874 | hAdvAPI32, |
| 7875 | "CryptAcquireContextA"); |
| 7876 | if (pCryptAcquireContext == NULL) |
| 7877 | return PyErr_Format(PyExc_NotImplementedError, |
| 7878 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7879 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7880 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 7881 | hAdvAPI32, "CryptGenRandom"); |
Georg Brandl | 74bb783 | 2006-09-06 06:03:59 +0000 | [diff] [blame] | 7882 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7883 | return PyErr_Format(PyExc_NotImplementedError, |
| 7884 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7885 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7886 | /* Acquire context */ |
| 7887 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 7888 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 7889 | return win32_error("CryptAcquireContext", NULL); |
| 7890 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7891 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7892 | /* Allocate bytes */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7893 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7894 | if (result != NULL) { |
| 7895 | /* Get random data */ |
| 7896 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 7897 | PyString_AS_STRING(result))) { |
| 7898 | Py_DECREF(result); |
| 7899 | return win32_error("CryptGenRandom", NULL); |
| 7900 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 7901 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 7902 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7903 | } |
| 7904 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7905 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 7906 | #ifdef __VMS |
| 7907 | /* Use openssl random routine */ |
| 7908 | #include <openssl/rand.h> |
| 7909 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7910 | "urandom(n) -> str\n\n\ |
| 7911 | Return a string of n random bytes suitable for cryptographic use."); |
| 7912 | |
| 7913 | static PyObject* |
| 7914 | vms_urandom(PyObject *self, PyObject *args) |
| 7915 | { |
| 7916 | int howMany; |
| 7917 | PyObject* result; |
| 7918 | |
| 7919 | /* Read arguments */ |
| 7920 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7921 | return NULL; |
| 7922 | if (howMany < 0) |
| 7923 | return PyErr_Format(PyExc_ValueError, |
| 7924 | "negative argument not allowed"); |
| 7925 | |
| 7926 | /* Allocate bytes */ |
| 7927 | result = PyString_FromStringAndSize(NULL, howMany); |
| 7928 | if (result != NULL) { |
| 7929 | /* Get random data */ |
| 7930 | if (RAND_pseudo_bytes((unsigned char*) |
| 7931 | PyString_AS_STRING(result), |
| 7932 | howMany) < 0) { |
| 7933 | Py_DECREF(result); |
| 7934 | return PyErr_Format(PyExc_ValueError, |
| 7935 | "RAND_pseudo_bytes"); |
| 7936 | } |
| 7937 | } |
| 7938 | return result; |
| 7939 | } |
| 7940 | #endif |
| 7941 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7942 | static PyMethodDef posix_methods[] = { |
| 7943 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7944 | #ifdef HAVE_TTYNAME |
| 7945 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7946 | #endif |
| 7947 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
| 7948 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7949 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7950 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7951 | #endif /* HAVE_CHOWN */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7952 | #ifdef HAVE_LCHOWN |
| 7953 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7954 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7955 | #ifdef HAVE_CHROOT |
| 7956 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7957 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7958 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7959 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7960 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7961 | #ifdef HAVE_GETCWD |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7962 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7963 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7964 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7965 | #endif |
Walter Dörwald | 3b918c3 | 2002-11-21 20:18:46 +0000 | [diff] [blame] | 7966 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7967 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7968 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7969 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7970 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7971 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7972 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7973 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7974 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7975 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7976 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7977 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7978 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7979 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7980 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7981 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7982 | {"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] | 7983 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7984 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7985 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7986 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7987 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7988 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7989 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7990 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7991 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7992 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7993 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7994 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7995 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7996 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7997 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7998 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7999 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8000 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8001 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 8002 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8003 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8004 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8005 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 8006 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 8007 | #if defined(PYOS_OS2) |
| 8008 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 8009 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 8010 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 8011 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8012 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8013 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 8014 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8015 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8016 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8017 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8018 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8019 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 8020 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8021 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8022 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8023 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8024 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8025 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8026 | #endif /* HAVE_GETEGID */ |
| 8027 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8028 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8029 | #endif /* HAVE_GETEUID */ |
| 8030 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8031 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8032 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8033 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8034 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8035 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8036 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8037 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8038 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8039 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8040 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8041 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8042 | #endif /* HAVE_GETPPID */ |
| 8043 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8044 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8045 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8046 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8047 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8048 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8049 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8050 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8051 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 8052 | #ifdef HAVE_KILLPG |
| 8053 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 8054 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8055 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8056 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8057 | #endif /* HAVE_PLOCK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8058 | #ifdef HAVE_POPEN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8059 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8060 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8061 | {"popen2", win32_popen2, METH_VARARGS}, |
| 8062 | {"popen3", win32_popen3, METH_VARARGS}, |
| 8063 | {"popen4", win32_popen4, METH_VARARGS}, |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 8064 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8065 | #else |
| 8066 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8067 | {"popen2", os2emx_popen2, METH_VARARGS}, |
| 8068 | {"popen3", os2emx_popen3, METH_VARARGS}, |
| 8069 | {"popen4", os2emx_popen4, METH_VARARGS}, |
| 8070 | #endif |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 8071 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8072 | #endif /* HAVE_POPEN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8073 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8074 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8075 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8076 | #ifdef HAVE_SETEUID |
| 8077 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 8078 | #endif /* HAVE_SETEUID */ |
| 8079 | #ifdef HAVE_SETEGID |
| 8080 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 8081 | #endif /* HAVE_SETEGID */ |
| 8082 | #ifdef HAVE_SETREUID |
| 8083 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 8084 | #endif /* HAVE_SETREUID */ |
| 8085 | #ifdef HAVE_SETREGID |
| 8086 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 8087 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8088 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8089 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8090 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8091 | #ifdef HAVE_SETGROUPS |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 8092 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8093 | #endif /* HAVE_SETGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8094 | #ifdef HAVE_GETPGID |
| 8095 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 8096 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8097 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8098 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8099 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8100 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8101 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8102 | #endif /* HAVE_WAIT */ |
Neal Norwitz | 05a4559 | 2006-03-20 06:30:08 +0000 | [diff] [blame] | 8103 | #ifdef HAVE_WAIT3 |
| 8104 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 8105 | #endif /* HAVE_WAIT3 */ |
| 8106 | #ifdef HAVE_WAIT4 |
| 8107 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 8108 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8109 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8110 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8111 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8112 | #ifdef HAVE_GETSID |
| 8113 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 8114 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8115 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8116 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8117 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8118 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8119 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8120 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8121 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8122 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8123 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8124 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8125 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8126 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8127 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 8128 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 8129 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 8130 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 8131 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 8132 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 8133 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 8134 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 8135 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 8136 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8137 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8138 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8139 | #endif |
| 8140 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8141 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8142 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 8143 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 8144 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 8145 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 8146 | #ifdef HAVE_DEVICE_MACROS |
| 8147 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 8148 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 8149 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 8150 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8151 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8152 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 8153 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8154 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8155 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 8156 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 8157 | #ifdef HAVE_UNSETENV |
| 8158 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 8159 | #endif |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8160 | #ifdef HAVE_STRERROR |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8161 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 8162 | #endif |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8163 | #ifdef HAVE_FCHDIR |
| 8164 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 8165 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8166 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8167 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8168 | #endif |
| 8169 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8170 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 8171 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8172 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8173 | #ifdef WCOREDUMP |
| 8174 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 8175 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 8176 | #ifdef WIFCONTINUED |
| 8177 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 8178 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8179 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8180 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8181 | #endif /* WIFSTOPPED */ |
| 8182 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8183 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8184 | #endif /* WIFSIGNALED */ |
| 8185 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8186 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8187 | #endif /* WIFEXITED */ |
| 8188 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8189 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8190 | #endif /* WEXITSTATUS */ |
| 8191 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8192 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8193 | #endif /* WTERMSIG */ |
| 8194 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8195 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8196 | #endif /* WSTOPSIG */ |
| 8197 | #endif /* HAVE_SYS_WAIT_H */ |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8198 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8199 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8200 | #endif |
Martin v. Löwis | 5f5d99c | 2006-05-16 07:05:37 +0000 | [diff] [blame] | 8201 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8202 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8203 | #endif |
Guido van Rossum | e2ad633 | 2001-01-08 17:51:55 +0000 | [diff] [blame] | 8204 | #ifdef HAVE_TMPFILE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8205 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8206 | #endif |
| 8207 | #ifdef HAVE_TEMPNAM |
| 8208 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, |
| 8209 | #endif |
| 8210 | #ifdef HAVE_TMPNAM |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8211 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8212 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8213 | #ifdef HAVE_CONFSTR |
| 8214 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 8215 | #endif |
| 8216 | #ifdef HAVE_SYSCONF |
| 8217 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 8218 | #endif |
| 8219 | #ifdef HAVE_FPATHCONF |
| 8220 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 8221 | #endif |
| 8222 | #ifdef HAVE_PATHCONF |
| 8223 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 8224 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8225 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8226 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 8227 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 8228 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8229 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8230 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 8231 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 8232 | #ifdef MS_WINDOWS |
| 8233 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 8234 | #endif |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 8235 | #ifdef __VMS |
| 8236 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 8237 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8238 | {NULL, NULL} /* Sentinel */ |
| 8239 | }; |
| 8240 | |
| 8241 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8242 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8243 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8244 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8245 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8246 | } |
| 8247 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8248 | #if defined(PYOS_OS2) |
| 8249 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8250 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8251 | { |
| 8252 | APIRET rc; |
| 8253 | ULONG values[QSV_MAX+1]; |
| 8254 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 8255 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8256 | |
| 8257 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 8258 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8259 | Py_END_ALLOW_THREADS |
| 8260 | |
| 8261 | if (rc != NO_ERROR) { |
| 8262 | os2_error(rc); |
| 8263 | return -1; |
| 8264 | } |
| 8265 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8266 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 8267 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 8268 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 8269 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 8270 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 8271 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 8272 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8273 | |
| 8274 | switch (values[QSV_VERSION_MINOR]) { |
| 8275 | case 0: ver = "2.00"; break; |
| 8276 | case 10: ver = "2.10"; break; |
| 8277 | case 11: ver = "2.11"; break; |
| 8278 | case 30: ver = "3.00"; break; |
| 8279 | case 40: ver = "4.00"; break; |
| 8280 | case 50: ver = "5.00"; break; |
| 8281 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 8282 | PyOS_snprintf(tmp, sizeof(tmp), |
| 8283 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 8284 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8285 | ver = &tmp[0]; |
| 8286 | } |
| 8287 | |
| 8288 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8289 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8290 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8291 | |
| 8292 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 8293 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 8294 | tmp[1] = ':'; |
| 8295 | tmp[2] = '\0'; |
| 8296 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8297 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8298 | } |
| 8299 | #endif |
| 8300 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8301 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8302 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8303 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8304 | #ifdef F_OK |
| 8305 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8306 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8307 | #ifdef R_OK |
| 8308 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8309 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8310 | #ifdef W_OK |
| 8311 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8312 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8313 | #ifdef X_OK |
| 8314 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8315 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8316 | #ifdef NGROUPS_MAX |
| 8317 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 8318 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 8319 | #ifdef TMP_MAX |
| 8320 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 8321 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8322 | #ifdef WCONTINUED |
| 8323 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 8324 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8325 | #ifdef WNOHANG |
| 8326 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8327 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 8328 | #ifdef WUNTRACED |
| 8329 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 8330 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8331 | #ifdef O_RDONLY |
| 8332 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 8333 | #endif |
| 8334 | #ifdef O_WRONLY |
| 8335 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 8336 | #endif |
| 8337 | #ifdef O_RDWR |
| 8338 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 8339 | #endif |
| 8340 | #ifdef O_NDELAY |
| 8341 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 8342 | #endif |
| 8343 | #ifdef O_NONBLOCK |
| 8344 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 8345 | #endif |
| 8346 | #ifdef O_APPEND |
| 8347 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 8348 | #endif |
| 8349 | #ifdef O_DSYNC |
| 8350 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 8351 | #endif |
| 8352 | #ifdef O_RSYNC |
| 8353 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 8354 | #endif |
| 8355 | #ifdef O_SYNC |
| 8356 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 8357 | #endif |
| 8358 | #ifdef O_NOCTTY |
| 8359 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 8360 | #endif |
| 8361 | #ifdef O_CREAT |
| 8362 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 8363 | #endif |
| 8364 | #ifdef O_EXCL |
| 8365 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 8366 | #endif |
| 8367 | #ifdef O_TRUNC |
| 8368 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 8369 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 8370 | #ifdef O_BINARY |
| 8371 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 8372 | #endif |
| 8373 | #ifdef O_TEXT |
| 8374 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 8375 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8376 | #ifdef O_LARGEFILE |
| 8377 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 8378 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 8379 | #ifdef O_SHLOCK |
| 8380 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 8381 | #endif |
| 8382 | #ifdef O_EXLOCK |
| 8383 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 8384 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8385 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8386 | /* MS Windows */ |
| 8387 | #ifdef O_NOINHERIT |
| 8388 | /* Don't inherit in child processes. */ |
| 8389 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 8390 | #endif |
| 8391 | #ifdef _O_SHORT_LIVED |
| 8392 | /* Optimize for short life (keep in memory). */ |
| 8393 | /* MS forgot to define this one with a non-underscore form too. */ |
| 8394 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 8395 | #endif |
| 8396 | #ifdef O_TEMPORARY |
| 8397 | /* Automatically delete when last handle is closed. */ |
| 8398 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 8399 | #endif |
| 8400 | #ifdef O_RANDOM |
| 8401 | /* Optimize for random access. */ |
| 8402 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 8403 | #endif |
| 8404 | #ifdef O_SEQUENTIAL |
| 8405 | /* Optimize for sequential access. */ |
| 8406 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 8407 | #endif |
| 8408 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 8409 | /* GNU extensions. */ |
| 8410 | #ifdef O_DIRECT |
| 8411 | /* Direct disk access. */ |
| 8412 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 8413 | #endif |
| 8414 | #ifdef O_DIRECTORY |
| 8415 | /* Must be a directory. */ |
| 8416 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 8417 | #endif |
| 8418 | #ifdef O_NOFOLLOW |
| 8419 | /* Do not follow links. */ |
| 8420 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 8421 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8422 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8423 | /* These come from sysexits.h */ |
| 8424 | #ifdef EX_OK |
| 8425 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8426 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8427 | #ifdef EX_USAGE |
| 8428 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8429 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8430 | #ifdef EX_DATAERR |
| 8431 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8432 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8433 | #ifdef EX_NOINPUT |
| 8434 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8435 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8436 | #ifdef EX_NOUSER |
| 8437 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8438 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8439 | #ifdef EX_NOHOST |
| 8440 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8441 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8442 | #ifdef EX_UNAVAILABLE |
| 8443 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8444 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8445 | #ifdef EX_SOFTWARE |
| 8446 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8447 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8448 | #ifdef EX_OSERR |
| 8449 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8450 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8451 | #ifdef EX_OSFILE |
| 8452 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8453 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8454 | #ifdef EX_CANTCREAT |
| 8455 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8456 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8457 | #ifdef EX_IOERR |
| 8458 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8459 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8460 | #ifdef EX_TEMPFAIL |
| 8461 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8462 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8463 | #ifdef EX_PROTOCOL |
| 8464 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8465 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8466 | #ifdef EX_NOPERM |
| 8467 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8468 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8469 | #ifdef EX_CONFIG |
| 8470 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8471 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8472 | #ifdef EX_NOTFOUND |
| 8473 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 8474 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 8475 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8476 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8477 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 8478 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 8479 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 8480 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 8481 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 8482 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 8483 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 8484 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 8485 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 8486 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 8487 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 8488 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 8489 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 8490 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 8491 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 8492 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 8493 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 8494 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 8495 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 8496 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 8497 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 8498 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 8499 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 8500 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 8501 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 8502 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 8503 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8504 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 8505 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 8506 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 8507 | #if defined(PYOS_OS2) |
| 8508 | if (insertvalues(d)) return -1; |
| 8509 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8510 | return 0; |
| 8511 | } |
| 8512 | |
| 8513 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8514 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8515 | #define INITFUNC initnt |
| 8516 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8517 | |
| 8518 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8519 | #define INITFUNC initos2 |
| 8520 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 8521 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 8522 | #else |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8523 | #define INITFUNC initposix |
| 8524 | #define MODNAME "posix" |
| 8525 | #endif |
| 8526 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 8527 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 8528 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8529 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8530 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8531 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8532 | m = Py_InitModule3(MODNAME, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8533 | posix_methods, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8534 | posix__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 8535 | if (m == NULL) |
| 8536 | return; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8537 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8538 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8539 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8540 | Py_XINCREF(v); |
| 8541 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 8542 | return; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8543 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8544 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8545 | if (all_ins(m)) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 8546 | return; |
| 8547 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8548 | if (setup_confname_tables(m)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8549 | return; |
| 8550 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8551 | Py_INCREF(PyExc_OSError); |
| 8552 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 8553 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8554 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 8555 | if (posix_putenv_garbage == NULL) |
| 8556 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 8557 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8558 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8559 | if (!initialized) { |
| 8560 | stat_result_desc.name = MODNAME ".stat_result"; |
| 8561 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 8562 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 8563 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 8564 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 8565 | structseq_new = StatResultType.tp_new; |
| 8566 | StatResultType.tp_new = statresult_new; |
| 8567 | |
| 8568 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 8569 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
| 8570 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8571 | Py_INCREF((PyObject*) &StatResultType); |
| 8572 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 8573 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 8574 | PyModule_AddObject(m, "statvfs_result", |
| 8575 | (PyObject*) &StatVFSResultType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 8576 | initialized = 1; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 8577 | |
| 8578 | #ifdef __APPLE__ |
| 8579 | /* |
| 8580 | * Step 2 of weak-linking support on Mac OS X. |
| 8581 | * |
| 8582 | * The code below removes functions that are not available on the |
| 8583 | * currently active platform. |
| 8584 | * |
| 8585 | * This block allow one to use a python binary that was build on |
| 8586 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 8587 | * OSX 10.4. |
| 8588 | */ |
| 8589 | #ifdef HAVE_FSTATVFS |
| 8590 | if (fstatvfs == NULL) { |
| 8591 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 8592 | return; |
| 8593 | } |
| 8594 | } |
| 8595 | #endif /* HAVE_FSTATVFS */ |
| 8596 | |
| 8597 | #ifdef HAVE_STATVFS |
| 8598 | if (statvfs == NULL) { |
| 8599 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 8600 | return; |
| 8601 | } |
| 8602 | } |
| 8603 | #endif /* HAVE_STATVFS */ |
| 8604 | |
| 8605 | # ifdef HAVE_LCHOWN |
| 8606 | if (lchown == NULL) { |
| 8607 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 8608 | return; |
| 8609 | } |
| 8610 | } |
| 8611 | #endif /* HAVE_LCHOWN */ |
| 8612 | |
| 8613 | |
| 8614 | #endif /* __APPLE__ */ |
| 8615 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8616 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8617 | |
| 8618 | #ifdef __cplusplus |
| 8619 | } |
| 8620 | #endif |
| 8621 | |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 8622 | |