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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #if defined(PYOS_OS2) |
| 49 | #define INCL_DOS |
| 50 | #define INCL_DOSERRORS |
| 51 | #define INCL_DOSPROCESS |
| 52 | #define INCL_NOPMAPI |
| 53 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 54 | #if defined(PYCC_GCC) |
| 55 | #include <ctype.h> |
| 56 | #include <io.h> |
| 57 | #include <stdio.h> |
| 58 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 59 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 60 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SYS_TYPES_H */ |
| 66 | |
| 67 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 68 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 71 | #ifdef HAVE_SYS_WAIT_H |
| 72 | #include <sys/wait.h> /* For WNOHANG */ |
| 73 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 76 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #ifdef HAVE_FCNTL_H |
| 80 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 81 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | #ifdef HAVE_GRP_H |
| 84 | #include <grp.h> |
| 85 | #endif |
| 86 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 87 | #ifdef HAVE_SYSEXITS_H |
| 88 | #include <sysexits.h> |
| 89 | #endif /* HAVE_SYSEXITS_H */ |
| 90 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_LOADAVG_H |
| 92 | #include <sys/loadavg.h> |
| 93 | #endif |
| 94 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 95 | #ifdef HAVE_LANGINFO_H |
| 96 | #include <langinfo.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 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 119 | #define HAVE_SYSTEM 1 |
| 120 | #define HAVE_WAIT 1 |
| 121 | #else |
| 122 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 123 | #define HAVE_GETCWD 1 |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 124 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 125 | #define HAVE_EXECV 1 |
| 126 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 127 | #define HAVE_SYSTEM 1 |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 128 | #define HAVE_CWAIT 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 129 | #define HAVE_FSYNC 1 |
| 130 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 131 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 132 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 133 | /* 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] | 134 | #else /* all other compilers */ |
| 135 | /* Unix functions that the configure script doesn't check for */ |
| 136 | #define HAVE_EXECV 1 |
| 137 | #define HAVE_FORK 1 |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 138 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 139 | #define HAVE_FORK1 1 |
| 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #define HAVE_GETCWD 1 |
| 142 | #define HAVE_GETEGID 1 |
| 143 | #define HAVE_GETEUID 1 |
| 144 | #define HAVE_GETGID 1 |
| 145 | #define HAVE_GETPPID 1 |
| 146 | #define HAVE_GETUID 1 |
| 147 | #define HAVE_KILL 1 |
| 148 | #define HAVE_OPENDIR 1 |
| 149 | #define HAVE_PIPE 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 150 | #define HAVE_SYSTEM 1 |
| 151 | #define HAVE_WAIT 1 |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 152 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 153 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 154 | #endif /* _MSC_VER */ |
| 155 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 156 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 157 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 159 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 161 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 162 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 163 | (default) */ |
| 164 | extern char *ctermid_r(char *); |
| 165 | #endif |
| 166 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 167 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 168 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 169 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 170 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 171 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 172 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 173 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 174 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 175 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 176 | #endif |
| 177 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 178 | extern int chdir(char *); |
| 179 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 180 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 181 | extern int chdir(const char *); |
| 182 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 183 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 184 | #ifdef __BORLANDC__ |
| 185 | extern int chmod(const char *, int); |
| 186 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 187 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 188 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 189 | /*#ifdef HAVE_FCHMOD |
| 190 | extern int fchmod(int, mode_t); |
| 191 | #endif*/ |
| 192 | /*#ifdef HAVE_LCHMOD |
| 193 | extern int lchmod(const char *, mode_t); |
| 194 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 195 | extern int chown(const char *, uid_t, gid_t); |
| 196 | extern char *getcwd(char *, int); |
| 197 | extern char *strerror(int); |
| 198 | extern int link(const char *, const char *); |
| 199 | extern int rename(const char *, const char *); |
| 200 | extern int stat(const char *, struct stat *); |
| 201 | extern int unlink(const char *); |
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 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 265 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 266 | #include <malloc.h> |
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 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 271 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 275 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 277 | #define MAXPATHLEN PATH_MAX |
| 278 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 279 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 280 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 281 | #endif /* MAXPATHLEN */ |
| 282 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 283 | #ifdef UNION_WAIT |
| 284 | /* Emulate some macros on systems that have a union instead of macros */ |
| 285 | |
| 286 | #ifndef WIFEXITED |
| 287 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 288 | #endif |
| 289 | |
| 290 | #ifndef WEXITSTATUS |
| 291 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 292 | #endif |
| 293 | |
| 294 | #ifndef WTERMSIG |
| 295 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 296 | #endif |
| 297 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 298 | #define WAIT_TYPE union wait |
| 299 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 300 | |
| 301 | #else /* !UNION_WAIT */ |
| 302 | #define WAIT_TYPE int |
| 303 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 304 | #endif /* UNION_WAIT */ |
| 305 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 306 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 307 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 308 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 309 | #define USE_CTERMID_R |
| 310 | #endif |
| 311 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 312 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 313 | #undef STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 314 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 315 | # define STAT win32_stat |
| 316 | # define FSTAT win32_fstat |
| 317 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 318 | #else |
| 319 | # define STAT stat |
| 320 | # define FSTAT fstat |
| 321 | # define STRUCT_STAT struct stat |
| 322 | #endif |
| 323 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 324 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 325 | #include <sys/mkdev.h> |
| 326 | #else |
| 327 | #if defined(MAJOR_IN_SYSMACROS) |
| 328 | #include <sys/sysmacros.h> |
| 329 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 330 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 331 | #include <sys/mkdev.h> |
| 332 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 333 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 334 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 335 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 336 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 337 | * valid and throw an assertion if it isn't. |
| 338 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 339 | * an assertion can be useful, but it does contradict the POSIX standard |
| 340 | * which for write(2) states: |
| 341 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 342 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 343 | * writing." |
| 344 | * Furthermore, python allows the user to enter any old integer |
| 345 | * as a fd and should merely raise a python exception on error. |
| 346 | * The Microsoft CRT doesn't provide an official way to check for the |
| 347 | * validity of a file descriptor, but we can emulate its internal behaviour |
| 348 | * by using the exported __pinfo data member and knowledge of the |
| 349 | * internal structures involved. |
| 350 | * The structures below must be updated for each version of visual studio |
| 351 | * according to the file internal.h in the CRT source, until MS comes |
| 352 | * up with a less hacky way to do this. |
| 353 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 354 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 355 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 356 | /* The actual size of the structure is determined at runtime. |
| 357 | * Only the first items must be present. |
| 358 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 359 | typedef struct { |
| 360 | intptr_t osfhnd; |
| 361 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 362 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 363 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 364 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 365 | #define IOINFO_L2E 5 |
| 366 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 367 | #define IOINFO_ARRAYS 64 |
| 368 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 369 | #define FOPEN 0x01 |
| 370 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 371 | |
| 372 | /* This function emulates what the windows CRT does to validate file handles */ |
| 373 | int |
| 374 | _PyVerify_fd(int fd) |
| 375 | { |
| 376 | const int i1 = fd >> IOINFO_L2E; |
| 377 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 378 | |
| 379 | static int sizeof_ioinfo = 0; |
| 380 | |
| 381 | /* Determine the actual size of the ioinfo structure, |
| 382 | * as used by the CRT loaded in memory |
| 383 | */ |
| 384 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 385 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 386 | } |
| 387 | if (sizeof_ioinfo == 0) { |
| 388 | /* This should not happen... */ |
| 389 | goto fail; |
| 390 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 391 | |
| 392 | /* See that it isn't a special CLEAR fileno */ |
| 393 | if (fd != _NO_CONSOLE_FILENO) { |
| 394 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 395 | * we check pointer validity and other info |
| 396 | */ |
| 397 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 398 | /* finally, check that the file is open */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 399 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 400 | if (info->osfile & FOPEN) { |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 401 | return 1; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 402 | } |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 405 | fail: |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 406 | errno = EBADF; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 411 | static int |
| 412 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 413 | { |
| 414 | if (!_PyVerify_fd(fd1)) |
| 415 | return 0; |
| 416 | if (fd2 == _NO_CONSOLE_FILENO) |
| 417 | return 0; |
| 418 | if ((unsigned)fd2 < _NHANDLE_) |
| 419 | return 1; |
| 420 | else |
| 421 | return 0; |
| 422 | } |
| 423 | #else |
| 424 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 425 | #define _PyVerify_fd_dup2(A, B) (1) |
| 426 | #endif |
| 427 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 429 | #ifdef WITH_NEXT_FRAMEWORK |
| 430 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 431 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 432 | */ |
| 433 | #include <crt_externs.h> |
| 434 | static char **environ; |
| 435 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 437 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 438 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 439 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 440 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 442 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 443 | #ifdef MS_WINDOWS |
| 444 | wchar_t **e; |
| 445 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 446 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 447 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 448 | d = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 449 | if (d == NULL) |
| 450 | return NULL; |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 451 | #ifdef WITH_NEXT_FRAMEWORK |
| 452 | if (environ == NULL) |
| 453 | environ = *_NSGetEnviron(); |
| 454 | #endif |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 455 | #ifdef MS_WINDOWS |
| 456 | /* _wenviron must be initialized in this way if the program is started |
| 457 | through main() instead of wmain(). */ |
| 458 | _wgetenv(L""); |
| 459 | if (_wenviron == NULL) |
| 460 | return d; |
| 461 | /* This part ignores errors */ |
| 462 | for (e = _wenviron; *e != NULL; e++) { |
| 463 | PyObject *k; |
| 464 | PyObject *v; |
| 465 | wchar_t *p = wcschr(*e, L'='); |
| 466 | if (p == NULL) |
| 467 | continue; |
| 468 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 469 | if (k == NULL) { |
| 470 | PyErr_Clear(); |
| 471 | continue; |
| 472 | } |
| 473 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 474 | if (v == NULL) { |
| 475 | PyErr_Clear(); |
| 476 | Py_DECREF(k); |
| 477 | continue; |
| 478 | } |
| 479 | if (PyDict_GetItem(d, k) == NULL) { |
| 480 | if (PyDict_SetItem(d, k, v) != 0) |
| 481 | PyErr_Clear(); |
| 482 | } |
| 483 | Py_DECREF(k); |
| 484 | Py_DECREF(v); |
| 485 | } |
| 486 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | if (environ == NULL) |
| 488 | return d; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 489 | /* This part ignores errors */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | for (e = environ; *e != NULL; e++) { |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 491 | PyObject *k; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 492 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | char *p = strchr(*e, '='); |
| 494 | if (p == NULL) |
| 495 | continue; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 496 | k = PyUnicode_Decode(*e, (int)(p-*e), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 497 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 498 | if (k == NULL) { |
| 499 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | continue; |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 501 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 502 | v = PyUnicode_Decode(p+1, strlen(p+1), |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 503 | Py_FileSystemDefaultEncoding, "surrogateescape"); |
Guido van Rossum | 6a619f4 | 1999-08-03 19:41:10 +0000 | [diff] [blame] | 504 | if (v == NULL) { |
| 505 | PyErr_Clear(); |
| 506 | Py_DECREF(k); |
| 507 | continue; |
| 508 | } |
| 509 | if (PyDict_GetItem(d, k) == NULL) { |
| 510 | if (PyDict_SetItem(d, k, v) != 0) |
| 511 | PyErr_Clear(); |
| 512 | } |
| 513 | Py_DECREF(k); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 514 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 516 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 517 | #if defined(PYOS_OS2) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 518 | { |
| 519 | APIRET rc; |
| 520 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 521 | |
| 522 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 523 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 524 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 525 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 526 | Py_DECREF(v); |
| 527 | } |
| 528 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 529 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 530 | PyObject *v = PyBytes_FromString(buffer); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 531 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 532 | Py_DECREF(v); |
| 533 | } |
| 534 | } |
| 535 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 536 | return d; |
| 537 | } |
| 538 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 539 | /* Convert a bytes object to a char*. Optionally lock the buffer if it is a |
| 540 | bytes array. */ |
| 541 | |
| 542 | static char* |
| 543 | bytes2str(PyObject* o, int lock) |
| 544 | { |
| 545 | if(PyBytes_Check(o)) |
| 546 | return PyBytes_AsString(o); |
| 547 | else if(PyByteArray_Check(o)) { |
| 548 | if (lock && PyObject_GetBuffer(o, NULL, 0) < 0) |
| 549 | /* On a bytearray, this should not fail. */ |
| 550 | PyErr_BadInternalCall(); |
| 551 | return PyByteArray_AsString(o); |
| 552 | } else { |
| 553 | /* The FS converter should have verified that this |
| 554 | is either bytes or bytearray. */ |
| 555 | Py_FatalError("bad object passed to bytes2str"); |
| 556 | /* not reached. */ |
| 557 | return ""; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* Release the lock, decref the object. */ |
| 562 | static void |
| 563 | release_bytes(PyObject* o) |
| 564 | { |
| 565 | if (PyByteArray_Check(o)) |
Antoine Pitrou | 1119a64 | 2010-01-17 12:16:23 +0000 | [diff] [blame] | 566 | o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 567 | Py_DECREF(o); |
| 568 | } |
| 569 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 572 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 573 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 574 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 575 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 576 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 577 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 578 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 579 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 580 | { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 581 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 584 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 585 | static PyObject * |
| 586 | posix_error_with_unicode_filename(Py_UNICODE* name) |
| 587 | { |
| 588 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); |
| 589 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 590 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 591 | |
| 592 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 593 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 594 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 595 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 596 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 597 | bytes2str(name, 0)); |
| 598 | release_bytes(name); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 599 | return rc; |
| 600 | } |
| 601 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 602 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 603 | static PyObject * |
| 604 | win32_error(char* function, char* filename) |
| 605 | { |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 606 | /* XXX We should pass the function name along in the future. |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 607 | (winreg.c also wants to pass the function name.) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 608 | This would however require an additional param to the |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 609 | Windows error object, which is non-trivial. |
| 610 | */ |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 611 | errno = GetLastError(); |
| 612 | if (filename) |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 613 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 614 | else |
Mark Hammond | 33a6da9 | 2000-08-15 00:46:38 +0000 | [diff] [blame] | 615 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 616 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 617 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 618 | static PyObject * |
| 619 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 620 | { |
| 621 | /* XXX - see win32_error for comments on 'function' */ |
| 622 | errno = GetLastError(); |
| 623 | if (filename) |
| 624 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 625 | else |
| 626 | return PyErr_SetFromWindowsErr(errno); |
| 627 | } |
| 628 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 629 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 630 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 631 | { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 632 | if (PyUnicode_CheckExact(*param)) |
| 633 | Py_INCREF(*param); |
| 634 | else if (PyUnicode_Check(*param)) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 635 | /* For a Unicode subtype that's not a Unicode object, |
| 636 | return a true Unicode object with the same data. */ |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 637 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 638 | PyUnicode_GET_SIZE(*param)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 639 | else |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 640 | *param = PyUnicode_FromEncodedObject(*param, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 641 | Py_FileSystemDefaultEncoding, |
| 642 | "strict"); |
| 643 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 646 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 648 | #if defined(PYOS_OS2) |
| 649 | /********************************************************************** |
| 650 | * Helper Function to Trim and Format OS/2 Messages |
| 651 | **********************************************************************/ |
| 652 | static void |
| 653 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 654 | { |
| 655 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 656 | |
| 657 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 658 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 659 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 660 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 661 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 662 | } |
| 663 | |
| 664 | /* Add Optional Reason Text */ |
| 665 | if (reason) { |
| 666 | strcat(msgbuf, " : "); |
| 667 | strcat(msgbuf, reason); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /********************************************************************** |
| 672 | * Decode an OS/2 Operating System Error Code |
| 673 | * |
| 674 | * A convenience function to lookup an OS/2 error code and return a |
| 675 | * text message we can use to raise a Python exception. |
| 676 | * |
| 677 | * Notes: |
| 678 | * The messages for errors returned from the OS/2 kernel reside in |
| 679 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 680 | * |
| 681 | **********************************************************************/ |
| 682 | static char * |
| 683 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 684 | { |
| 685 | APIRET rc; |
| 686 | ULONG msglen; |
| 687 | |
| 688 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 689 | Py_BEGIN_ALLOW_THREADS |
| 690 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 691 | errorcode, "oso001.msg", &msglen); |
| 692 | Py_END_ALLOW_THREADS |
| 693 | |
| 694 | if (rc == NO_ERROR) |
| 695 | os2_formatmsg(msgbuf, msglen, reason); |
| 696 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 697 | PyOS_snprintf(msgbuf, msgbuflen, |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 698 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 699 | |
| 700 | return msgbuf; |
| 701 | } |
| 702 | |
| 703 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 704 | errors are not in a global variable e.g. 'errno' nor are |
| 705 | they congruent with posix error numbers. */ |
| 706 | |
| 707 | static PyObject * os2_error(int code) |
| 708 | { |
| 709 | char text[1024]; |
| 710 | PyObject *v; |
| 711 | |
| 712 | os2_strerror(text, sizeof(text), code, ""); |
| 713 | |
| 714 | v = Py_BuildValue("(is)", code, text); |
| 715 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 716 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 717 | Py_DECREF(v); |
| 718 | } |
| 719 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 720 | } |
| 721 | |
| 722 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 723 | |
| 724 | /* POSIX generic methods */ |
| 725 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 726 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 727 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 728 | { |
| 729 | int fd; |
| 730 | int res; |
| 731 | fd = PyObject_AsFileDescriptor(fdobj); |
| 732 | if (fd < 0) |
| 733 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 734 | if (!_PyVerify_fd(fd)) |
| 735 | return posix_error(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 736 | Py_BEGIN_ALLOW_THREADS |
| 737 | res = (*func)(fd); |
| 738 | Py_END_ALLOW_THREADS |
| 739 | if (res < 0) |
| 740 | return posix_error(); |
| 741 | Py_INCREF(Py_None); |
| 742 | return Py_None; |
| 743 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 744 | |
| 745 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 746 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 747 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 748 | PyObject *opath1 = NULL; |
| 749 | char *path1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 750 | int res; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 751 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 752 | PyUnicode_FSConverter, &opath1)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 753 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 754 | path1 = bytes2str(opath1, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 755 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 756 | res = (*func)(path1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 757 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 758 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 759 | return posix_error_with_allocated_filename(opath1); |
| 760 | release_bytes(opath1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 761 | Py_INCREF(Py_None); |
| 762 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 765 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 766 | posix_2str(PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 767 | char *format, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 768 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 769 | { |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 770 | PyObject *opath1 = NULL, *opath2 = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 771 | char *path1, *path2; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 772 | int res; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 773 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 774 | PyUnicode_FSConverter, &opath1, |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 775 | PyUnicode_FSConverter, &opath2)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 776 | return NULL; |
Antoine Pitrou | a6bb984 | 2009-05-10 22:27:00 +0000 | [diff] [blame] | 777 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 778 | path1 = bytes2str(opath1, 1); |
| 779 | path2 = bytes2str(opath2, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 780 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 781 | res = (*func)(path1, path2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 782 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 783 | release_bytes(opath1); |
| 784 | release_bytes(opath2); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 785 | if (res != 0) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 786 | /* XXX how to report both path1 and path2??? */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 787 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 788 | Py_INCREF(Py_None); |
| 789 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 792 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 793 | static PyObject* |
| 794 | win32_1str(PyObject* args, char* func, |
| 795 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 796 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
| 797 | { |
| 798 | PyObject *uni; |
| 799 | char *ansi; |
| 800 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 801 | |
| 802 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 803 | PyErr_Clear(); |
| 804 | else { |
| 805 | Py_BEGIN_ALLOW_THREADS |
| 806 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 807 | Py_END_ALLOW_THREADS |
| 808 | if (!result) |
| 809 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 810 | Py_INCREF(Py_None); |
| 811 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 812 | } |
| 813 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 814 | return NULL; |
| 815 | Py_BEGIN_ALLOW_THREADS |
| 816 | result = funcA(ansi); |
| 817 | Py_END_ALLOW_THREADS |
| 818 | if (!result) |
| 819 | return win32_error(func, ansi); |
| 820 | Py_INCREF(Py_None); |
| 821 | return Py_None; |
| 822 | |
| 823 | } |
| 824 | |
| 825 | /* This is a reimplementation of the C library's chdir function, |
| 826 | but one that produces Win32 errors instead of DOS error codes. |
| 827 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 828 | it also needs to set "magic" environment variables indicating |
| 829 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 830 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 831 | win32_chdir(LPCSTR path) |
| 832 | { |
| 833 | char new_path[MAX_PATH+1]; |
| 834 | int result; |
| 835 | char env[4] = "=x:"; |
| 836 | |
| 837 | if(!SetCurrentDirectoryA(path)) |
| 838 | return FALSE; |
| 839 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 840 | if (!result) |
| 841 | return FALSE; |
| 842 | /* In the ANSI API, there should not be any paths longer |
| 843 | than MAX_PATH. */ |
| 844 | assert(result <= MAX_PATH+1); |
| 845 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 846 | strncmp(new_path, "//", 2) == 0) |
| 847 | /* UNC path, nothing to do. */ |
| 848 | return TRUE; |
| 849 | env[1] = new_path[0]; |
| 850 | return SetEnvironmentVariableA(env, new_path); |
| 851 | } |
| 852 | |
| 853 | /* The Unicode version differs from the ANSI version |
| 854 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 855 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 856 | win32_wchdir(LPCWSTR path) |
| 857 | { |
| 858 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 859 | int result; |
| 860 | wchar_t env[4] = L"=x:"; |
| 861 | |
| 862 | if(!SetCurrentDirectoryW(path)) |
| 863 | return FALSE; |
| 864 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 865 | if (!result) |
| 866 | return FALSE; |
| 867 | if (result > MAX_PATH+1) { |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 868 | new_path = malloc(result * sizeof(wchar_t)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | if (!new_path) { |
| 870 | SetLastError(ERROR_OUTOFMEMORY); |
| 871 | return FALSE; |
| 872 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 873 | result = GetCurrentDirectoryW(result, new_path); |
| 874 | if (!result) { |
| 875 | free(new_path); |
| 876 | return FALSE; |
| 877 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 878 | } |
| 879 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 880 | wcsncmp(new_path, L"//", 2) == 0) |
| 881 | /* UNC path, nothing to do. */ |
| 882 | return TRUE; |
| 883 | env[1] = new_path[0]; |
| 884 | result = SetEnvironmentVariableW(env, new_path); |
| 885 | if (new_path != _new_path) |
| 886 | free(new_path); |
| 887 | return result; |
| 888 | } |
| 889 | #endif |
| 890 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 891 | #ifdef MS_WINDOWS |
| 892 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 893 | - time stamps are restricted to second resolution |
| 894 | - file modification times suffer from forth-and-back conversions between |
| 895 | UTC and local time |
| 896 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 897 | */ |
| 898 | #define HAVE_STAT_NSEC 1 |
| 899 | |
| 900 | struct win32_stat{ |
| 901 | int st_dev; |
| 902 | __int64 st_ino; |
| 903 | unsigned short st_mode; |
| 904 | int st_nlink; |
| 905 | int st_uid; |
| 906 | int st_gid; |
| 907 | int st_rdev; |
| 908 | __int64 st_size; |
| 909 | int st_atime; |
| 910 | int st_atime_nsec; |
| 911 | int st_mtime; |
| 912 | int st_mtime_nsec; |
| 913 | int st_ctime; |
| 914 | int st_ctime_nsec; |
| 915 | }; |
| 916 | |
| 917 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 918 | |
| 919 | static void |
| 920 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) |
| 921 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 922 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 923 | /* Cannot simply cast and dereference in_ptr, |
| 924 | since it might not be aligned properly */ |
| 925 | __int64 in; |
| 926 | memcpy(&in, in_ptr, sizeof(in)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 927 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 928 | /* XXX Win32 supports time stamps past 2038; we currently don't */ |
| 929 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); |
| 930 | } |
| 931 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 932 | static void |
| 933 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr) |
| 934 | { |
| 935 | /* XXX endianness */ |
| 936 | __int64 out; |
| 937 | out = time_in + secs_between_epochs; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 938 | out = out * 10000000 + nsec_in / 100; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | memcpy(out_ptr, &out, sizeof(out)); |
| 940 | } |
| 941 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 942 | /* Below, we *know* that ugo+r is 0444 */ |
| 943 | #if _S_IREAD != 0400 |
| 944 | #error Unsupported C library |
| 945 | #endif |
| 946 | static int |
| 947 | attributes_to_mode(DWORD attr) |
| 948 | { |
| 949 | int m = 0; |
| 950 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 951 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 952 | else |
| 953 | m |= _S_IFREG; |
| 954 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 955 | m |= 0444; |
| 956 | else |
| 957 | m |= 0666; |
| 958 | return m; |
| 959 | } |
| 960 | |
| 961 | static int |
| 962 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) |
| 963 | { |
| 964 | memset(result, 0, sizeof(*result)); |
| 965 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 966 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 967 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 968 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 969 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 974 | static BOOL |
| 975 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 976 | { |
| 977 | HANDLE hFindFile; |
| 978 | WIN32_FIND_DATAA FileData; |
| 979 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 980 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 981 | return FALSE; |
| 982 | FindClose(hFindFile); |
| 983 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 984 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 985 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 986 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 987 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 988 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 989 | return TRUE; |
| 990 | } |
| 991 | |
| 992 | static BOOL |
| 993 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) |
| 994 | { |
| 995 | HANDLE hFindFile; |
| 996 | WIN32_FIND_DATAW FileData; |
| 997 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 998 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 999 | return FALSE; |
| 1000 | FindClose(hFindFile); |
| 1001 | pfad->dwFileAttributes = FileData.dwFileAttributes; |
| 1002 | pfad->ftCreationTime = FileData.ftCreationTime; |
| 1003 | pfad->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1004 | pfad->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1005 | pfad->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1006 | pfad->nFileSizeLow = FileData.nFileSizeLow; |
| 1007 | return TRUE; |
| 1008 | } |
| 1009 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1010 | static int |
| 1011 | win32_stat(const char* path, struct win32_stat *result) |
| 1012 | { |
| 1013 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 1014 | int code; |
| 1015 | char *dot; |
Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1016 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1017 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1018 | /* Protocol violation: we explicitly clear errno, instead of |
| 1019 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1020 | errno = 0; |
| 1021 | return -1; |
| 1022 | } else { |
| 1023 | /* Could not get attributes on open file. Fall back to |
| 1024 | reading the directory. */ |
| 1025 | if (!attributes_from_dir(path, &info)) { |
| 1026 | /* Very strange. This should not fail now */ |
| 1027 | errno = 0; |
| 1028 | return -1; |
| 1029 | } |
| 1030 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1031 | } |
| 1032 | code = attribute_data_to_stat(&info, result); |
| 1033 | if (code != 0) |
| 1034 | return code; |
| 1035 | /* Set S_IFEXEC if it is an .exe, .bat, ... */ |
| 1036 | dot = strrchr(path, '.'); |
| 1037 | if (dot) { |
| 1038 | if (stricmp(dot, ".bat") == 0 || |
| 1039 | stricmp(dot, ".cmd") == 0 || |
| 1040 | stricmp(dot, ".exe") == 0 || |
| 1041 | stricmp(dot, ".com") == 0) |
| 1042 | result->st_mode |= 0111; |
| 1043 | } |
| 1044 | return code; |
| 1045 | } |
| 1046 | |
| 1047 | static int |
| 1048 | win32_wstat(const wchar_t* path, struct win32_stat *result) |
| 1049 | { |
| 1050 | int code; |
| 1051 | const wchar_t *dot; |
| 1052 | WIN32_FILE_ATTRIBUTE_DATA info; |
Hirokazu Yamamoto | 6fbdfda | 2009-06-29 11:37:19 +0000 | [diff] [blame] | 1053 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1054 | if (GetLastError() != ERROR_SHARING_VIOLATION) { |
| 1055 | /* Protocol violation: we explicitly clear errno, instead of |
| 1056 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1057 | errno = 0; |
| 1058 | return -1; |
| 1059 | } else { |
| 1060 | /* Could not get attributes on open file. Fall back to |
| 1061 | reading the directory. */ |
| 1062 | if (!attributes_from_dir_w(path, &info)) { |
| 1063 | /* Very strange. This should not fail now */ |
| 1064 | errno = 0; |
| 1065 | return -1; |
| 1066 | } |
| 1067 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1068 | } |
| 1069 | code = attribute_data_to_stat(&info, result); |
| 1070 | if (code < 0) |
| 1071 | return code; |
| 1072 | /* Set IFEXEC if it is an .exe, .bat, ... */ |
| 1073 | dot = wcsrchr(path, '.'); |
| 1074 | if (dot) { |
| 1075 | if (_wcsicmp(dot, L".bat") == 0 || |
| 1076 | _wcsicmp(dot, L".cmd") == 0 || |
| 1077 | _wcsicmp(dot, L".exe") == 0 || |
| 1078 | _wcsicmp(dot, L".com") == 0) |
| 1079 | result->st_mode |= 0111; |
| 1080 | } |
| 1081 | return code; |
| 1082 | } |
| 1083 | |
| 1084 | static int |
| 1085 | win32_fstat(int file_number, struct win32_stat *result) |
| 1086 | { |
| 1087 | BY_HANDLE_FILE_INFORMATION info; |
| 1088 | HANDLE h; |
| 1089 | int type; |
| 1090 | |
| 1091 | h = (HANDLE)_get_osfhandle(file_number); |
| 1092 | |
| 1093 | /* Protocol violation: we explicitly clear errno, instead of |
| 1094 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1095 | errno = 0; |
| 1096 | |
| 1097 | if (h == INVALID_HANDLE_VALUE) { |
| 1098 | /* This is really a C library error (invalid file handle). |
| 1099 | We set the Win32 error to the closes one matching. */ |
| 1100 | SetLastError(ERROR_INVALID_HANDLE); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | memset(result, 0, sizeof(*result)); |
| 1104 | |
| 1105 | type = GetFileType(h); |
| 1106 | if (type == FILE_TYPE_UNKNOWN) { |
| 1107 | DWORD error = GetLastError(); |
| 1108 | if (error != 0) { |
| 1109 | return -1; |
| 1110 | } |
| 1111 | /* else: valid but unknown file */ |
| 1112 | } |
| 1113 | |
| 1114 | if (type != FILE_TYPE_DISK) { |
| 1115 | if (type == FILE_TYPE_CHAR) |
| 1116 | result->st_mode = _S_IFCHR; |
| 1117 | else if (type == FILE_TYPE_PIPE) |
| 1118 | result->st_mode = _S_IFIFO; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | if (!GetFileInformationByHandle(h, &info)) { |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | /* similar to stat() */ |
| 1127 | result->st_mode = attributes_to_mode(info.dwFileAttributes); |
| 1128 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; |
| 1129 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1130 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1131 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 1132 | /* specific to fstat() */ |
| 1133 | result->st_nlink = info.nNumberOfLinks; |
| 1134 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | #endif /* MS_WINDOWS */ |
| 1139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1140 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1141 | "stat_result: Result from stat or lstat.\n\n\ |
| 1142 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1143 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1144 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1145 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1146 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1147 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1148 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1149 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1150 | |
| 1151 | static PyStructSequence_Field stat_result_fields[] = { |
| 1152 | {"st_mode", "protection bits"}, |
| 1153 | {"st_ino", "inode"}, |
| 1154 | {"st_dev", "device"}, |
| 1155 | {"st_nlink", "number of hard links"}, |
| 1156 | {"st_uid", "user ID of owner"}, |
| 1157 | {"st_gid", "group ID of owner"}, |
| 1158 | {"st_size", "total size, in bytes"}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1159 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1160 | {NULL, "integer time of last access"}, |
| 1161 | {NULL, "integer time of last modification"}, |
| 1162 | {NULL, "integer time of last change"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1163 | {"st_atime", "time of last access"}, |
| 1164 | {"st_mtime", "time of last modification"}, |
| 1165 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1166 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1167 | {"st_blksize", "blocksize for filesystem I/O"}, |
| 1168 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1169 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1170 | {"st_blocks", "number of blocks allocated"}, |
| 1171 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1172 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1173 | {"st_rdev", "device type (if inode device)"}, |
| 1174 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1175 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1176 | {"st_flags", "user defined flags for file"}, |
| 1177 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1178 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1179 | {"st_gen", "generation number"}, |
| 1180 | #endif |
| 1181 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1182 | {"st_birthtime", "time of creation"}, |
| 1183 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1184 | {0} |
| 1185 | }; |
| 1186 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1187 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1188 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1189 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1190 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1191 | #endif |
| 1192 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1193 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1194 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1195 | #else |
| 1196 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1197 | #endif |
| 1198 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1199 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1200 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1201 | #else |
| 1202 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1203 | #endif |
| 1204 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1205 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1206 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1207 | #else |
| 1208 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1209 | #endif |
| 1210 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1211 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1212 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1213 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1214 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1215 | #endif |
| 1216 | |
| 1217 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1218 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1219 | #else |
| 1220 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1221 | #endif |
| 1222 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1223 | static PyStructSequence_Desc stat_result_desc = { |
| 1224 | "stat_result", /* name */ |
| 1225 | stat_result__doc__, /* doc */ |
| 1226 | stat_result_fields, |
| 1227 | 10 |
| 1228 | }; |
| 1229 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1230 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1231 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1232 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1233 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1234 | 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] | 1235 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1236 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1237 | |
| 1238 | static PyStructSequence_Field statvfs_result_fields[] = { |
| 1239 | {"f_bsize", }, |
| 1240 | {"f_frsize", }, |
| 1241 | {"f_blocks", }, |
| 1242 | {"f_bfree", }, |
| 1243 | {"f_bavail", }, |
| 1244 | {"f_files", }, |
| 1245 | {"f_ffree", }, |
| 1246 | {"f_favail", }, |
| 1247 | {"f_flag", }, |
| 1248 | {"f_namemax",}, |
| 1249 | {0} |
| 1250 | }; |
| 1251 | |
| 1252 | static PyStructSequence_Desc statvfs_result_desc = { |
| 1253 | "statvfs_result", /* name */ |
| 1254 | statvfs_result__doc__, /* doc */ |
| 1255 | statvfs_result_fields, |
| 1256 | 10 |
| 1257 | }; |
| 1258 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1259 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1260 | static PyTypeObject StatResultType; |
| 1261 | static PyTypeObject StatVFSResultType; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1262 | static newfunc structseq_new; |
| 1263 | |
| 1264 | static PyObject * |
| 1265 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1266 | { |
| 1267 | PyStructSequence *result; |
| 1268 | int i; |
| 1269 | |
| 1270 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1271 | if (!result) |
| 1272 | return NULL; |
| 1273 | /* If we have been initialized from a tuple, |
| 1274 | st_?time might be set to None. Initialize it |
| 1275 | from the int slots. */ |
| 1276 | for (i = 7; i <= 9; i++) { |
| 1277 | if (result->ob_item[i+3] == Py_None) { |
| 1278 | Py_DECREF(Py_None); |
| 1279 | Py_INCREF(result->ob_item[i]); |
| 1280 | result->ob_item[i+3] = result->ob_item[i]; |
| 1281 | } |
| 1282 | } |
| 1283 | return (PyObject*)result; |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1289 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1290 | |
| 1291 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1292 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1293 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1294 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1295 | future calls return ints. \n\ |
| 1296 | If newval is omitted, return the current setting.\n"); |
| 1297 | |
| 1298 | static PyObject* |
| 1299 | stat_float_times(PyObject* self, PyObject *args) |
| 1300 | { |
| 1301 | int newval = -1; |
| 1302 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1303 | return NULL; |
| 1304 | if (newval == -1) |
| 1305 | /* Return old value */ |
| 1306 | return PyBool_FromLong(_stat_float_times); |
| 1307 | _stat_float_times = newval; |
| 1308 | Py_INCREF(Py_None); |
| 1309 | return Py_None; |
| 1310 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1311 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1312 | static void |
| 1313 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1314 | { |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1315 | PyObject *fval,*ival; |
| 1316 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1317 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1318 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1319 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1320 | #endif |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1321 | if (!ival) |
| 1322 | return; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1323 | if (_stat_float_times) { |
| 1324 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1325 | } else { |
| 1326 | fval = ival; |
| 1327 | Py_INCREF(fval); |
| 1328 | } |
| 1329 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1330 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1333 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1334 | (used by posix_stat() and posix_fstat()) */ |
| 1335 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1336 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1337 | { |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1338 | unsigned long ansec, mnsec, cnsec; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1339 | PyObject *v = PyStructSequence_New(&StatResultType); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1340 | if (v == NULL) |
| 1341 | return NULL; |
| 1342 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1343 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1344 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1345 | PyStructSequence_SET_ITEM(v, 1, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1346 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1347 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1348 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1349 | #endif |
| 1350 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1351 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1352 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1353 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1354 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1355 | #endif |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1356 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1357 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1358 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1359 | #ifdef HAVE_LARGEFILE_SUPPORT |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1360 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1361 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1362 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1363 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1364 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1365 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1366 | #if defined(HAVE_STAT_TV_NSEC) |
| 1367 | ansec = st->st_atim.tv_nsec; |
| 1368 | mnsec = st->st_mtim.tv_nsec; |
| 1369 | cnsec = st->st_ctim.tv_nsec; |
| 1370 | #elif defined(HAVE_STAT_TV_NSEC2) |
| 1371 | ansec = st->st_atimespec.tv_nsec; |
| 1372 | mnsec = st->st_mtimespec.tv_nsec; |
| 1373 | cnsec = st->st_ctimespec.tv_nsec; |
| 1374 | #elif defined(HAVE_STAT_NSEC) |
| 1375 | ansec = st->st_atime_nsec; |
| 1376 | mnsec = st->st_mtime_nsec; |
| 1377 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1378 | #else |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1379 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1380 | #endif |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1381 | fill_time(v, 7, st->st_atime, ansec); |
| 1382 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1383 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1384 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1385 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1386 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1387 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1388 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1389 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1390 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1391 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1392 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1393 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1394 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1395 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1396 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1397 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
| 1398 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1399 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1400 | #endif |
| 1401 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1402 | { |
| 1403 | PyObject *val; |
| 1404 | unsigned long bsec,bnsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1405 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1406 | #ifdef HAVE_STAT_TV_NSEC2 |
Hye-Shik Chang | d69e034 | 2006-02-19 16:22:22 +0000 | [diff] [blame] | 1407 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1408 | #else |
| 1409 | bnsec = 0; |
| 1410 | #endif |
| 1411 | if (_stat_float_times) { |
| 1412 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1413 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1414 | val = PyLong_FromLong((long)bsec); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1415 | } |
| 1416 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1417 | val); |
| 1418 | } |
| 1419 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1420 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1421 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1422 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1423 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (PyErr_Occurred()) { |
| 1426 | Py_DECREF(v); |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
| 1430 | return v; |
| 1431 | } |
| 1432 | |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1433 | #ifdef MS_WINDOWS |
| 1434 | |
| 1435 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, |
| 1436 | where / can be used in place of \ and the trailing slash is optional. |
| 1437 | Both SERVER and SHARE must have at least one character. |
| 1438 | */ |
| 1439 | |
| 1440 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/') |
| 1441 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1442 | #ifndef ARRAYSIZE |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1443 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1444 | #endif |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1445 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1446 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1447 | IsUNCRootA(char *path, int pathlen) |
| 1448 | { |
| 1449 | #define ISSLASH ISSLASHA |
| 1450 | |
| 1451 | int i, share; |
| 1452 | |
| 1453 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1454 | /* minimum UNCRoot is \\x\y */ |
| 1455 | return FALSE; |
| 1456 | for (i = 2; i < pathlen ; i++) |
| 1457 | if (ISSLASH(path[i])) break; |
| 1458 | if (i == 2 || i == pathlen) |
| 1459 | /* do not allow \\\SHARE or \\SERVER */ |
| 1460 | return FALSE; |
| 1461 | share = i+1; |
| 1462 | for (i = share; i < pathlen; i++) |
| 1463 | if (ISSLASH(path[i])) break; |
| 1464 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1465 | |
| 1466 | #undef ISSLASH |
| 1467 | } |
| 1468 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 1469 | static BOOL |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1470 | IsUNCRootW(Py_UNICODE *path, int pathlen) |
| 1471 | { |
| 1472 | #define ISSLASH ISSLASHW |
| 1473 | |
| 1474 | int i, share; |
| 1475 | |
| 1476 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) |
| 1477 | /* minimum UNCRoot is \\x\y */ |
| 1478 | return FALSE; |
| 1479 | for (i = 2; i < pathlen ; i++) |
| 1480 | if (ISSLASH(path[i])) break; |
| 1481 | if (i == 2 || i == pathlen) |
| 1482 | /* do not allow \\\SHARE or \\SERVER */ |
| 1483 | return FALSE; |
| 1484 | share = i+1; |
| 1485 | for (i = share; i < pathlen; i++) |
| 1486 | if (ISSLASH(path[i])) break; |
| 1487 | return (i != share && (i == pathlen || i == pathlen-1)); |
| 1488 | |
| 1489 | #undef ISSLASH |
| 1490 | } |
Martin v. Löwis | d894872 | 2004-06-02 09:57:56 +0000 | [diff] [blame] | 1491 | #endif /* MS_WINDOWS */ |
| 1492 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1493 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1494 | posix_do_stat(PyObject *self, PyObject *args, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1495 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1496 | #ifdef __VMS |
| 1497 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
| 1498 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1499 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1500 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1501 | char *wformat, |
| 1502 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1503 | { |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1504 | STRUCT_STAT st; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1505 | PyObject *opath; |
| 1506 | char *path; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1507 | int res; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1508 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1509 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1510 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1511 | PyUnicodeObject *po; |
| 1512 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1513 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1514 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1515 | Py_BEGIN_ALLOW_THREADS |
| 1516 | /* PyUnicode_AS_UNICODE result OK without |
| 1517 | thread lock as it is a simple dereference. */ |
| 1518 | res = wstatfunc(wpath, &st); |
| 1519 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1520 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1521 | if (res != 0) |
| 1522 | return win32_error_unicode("stat", wpath); |
| 1523 | return _pystat_fromstructstat(&st); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1524 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1525 | /* Drop the argument parsing error as narrow strings |
| 1526 | are also valid. */ |
| 1527 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1528 | #endif |
| 1529 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1530 | if (!PyArg_ParseTuple(args, format, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1531 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1532 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1533 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1534 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1535 | res = (*statfunc)(path, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1536 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1537 | |
| 1538 | if (res != 0) { |
| 1539 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1540 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1541 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1542 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1543 | #endif |
| 1544 | } |
| 1545 | else |
| 1546 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1547 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1548 | release_bytes(opath); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1549 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1552 | /* POSIX methods */ |
| 1553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1554 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1555 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1556 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1557 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1558 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1559 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1560 | 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] | 1561 | |
| 1562 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1563 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1564 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1565 | PyObject *opath; |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1566 | char *path; |
| 1567 | int mode; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1568 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1569 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1570 | DWORD attr; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1571 | PyUnicodeObject *po; |
| 1572 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1573 | Py_BEGIN_ALLOW_THREADS |
| 1574 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1575 | it is a simple dereference. */ |
| 1576 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1577 | Py_END_ALLOW_THREADS |
| 1578 | goto finish; |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 1579 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1580 | /* Drop the argument parsing error as narrow strings |
| 1581 | are also valid. */ |
| 1582 | PyErr_Clear(); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1583 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1584 | PyUnicode_FSConverter, &opath, &mode)) |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1585 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1586 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1587 | Py_BEGIN_ALLOW_THREADS |
| 1588 | attr = GetFileAttributesA(path); |
| 1589 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1590 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1591 | finish: |
| 1592 | if (attr == 0xFFFFFFFF) |
| 1593 | /* File does not exist, or cannot read attributes */ |
| 1594 | return PyBool_FromLong(0); |
| 1595 | /* Access is possible if either write access wasn't requested, or |
Guido van Rossum | b00324f | 2007-12-04 01:13:14 +0000 | [diff] [blame] | 1596 | the file isn't read-only, or if it's a directory, as there are |
| 1597 | no read-only directories on Windows. */ |
| 1598 | return PyBool_FromLong(!(mode & 2) |
| 1599 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1600 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1601 | #else |
| 1602 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1603 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1604 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1605 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1606 | path = bytes2str(opath, 1); |
Guido van Rossum | 015f22a | 1999-01-06 22:52:38 +0000 | [diff] [blame] | 1607 | Py_BEGIN_ALLOW_THREADS |
| 1608 | res = access(path, mode); |
| 1609 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1610 | release_bytes(opath); |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1611 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1612 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1615 | #ifndef F_OK |
| 1616 | #define F_OK 0 |
| 1617 | #endif |
| 1618 | #ifndef R_OK |
| 1619 | #define R_OK 4 |
| 1620 | #endif |
| 1621 | #ifndef W_OK |
| 1622 | #define W_OK 2 |
| 1623 | #endif |
| 1624 | #ifndef X_OK |
| 1625 | #define X_OK 1 |
| 1626 | #endif |
| 1627 | |
| 1628 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1629 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1630 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1631 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1632 | |
| 1633 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1634 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1635 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1636 | int id; |
| 1637 | char *ret; |
| 1638 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1639 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1640 | return NULL; |
| 1641 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1642 | #if defined(__VMS) |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1643 | /* file descriptor 0 only, the default input device (stdin) */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1644 | if (id == 0) { |
| 1645 | ret = ttyname(); |
| 1646 | } |
| 1647 | else { |
| 1648 | ret = NULL; |
| 1649 | } |
| 1650 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1651 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1652 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1653 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1654 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1655 | return PyUnicode_FromString(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1656 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1657 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1658 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1659 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1660 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1661 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1662 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1663 | |
| 1664 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1665 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1666 | { |
| 1667 | char *ret; |
| 1668 | char buffer[L_ctermid]; |
| 1669 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1670 | #ifdef USE_CTERMID_R |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1671 | ret = ctermid_r(buffer); |
| 1672 | #else |
| 1673 | ret = ctermid(buffer); |
| 1674 | #endif |
| 1675 | if (ret == NULL) |
Neal Norwitz | 3efd0a1 | 2005-09-19 06:45:53 +0000 | [diff] [blame] | 1676 | return posix_error(); |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 1677 | return PyUnicode_FromString(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1678 | } |
| 1679 | #endif |
| 1680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1681 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1682 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1683 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1685 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1686 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1687 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1688 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 1689 | return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1690 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1691 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1692 | #elif defined(__VMS) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1693 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1694 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1695 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1696 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1699 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1700 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1701 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1702 | 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] | 1703 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1704 | |
| 1705 | static PyObject * |
| 1706 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 1707 | { |
| 1708 | return posix_fildes(fdobj, fchdir); |
| 1709 | } |
| 1710 | #endif /* HAVE_FCHDIR */ |
| 1711 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1713 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1714 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1715 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1716 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1717 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1718 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1719 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1720 | PyObject *opath = NULL; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1721 | char *path = NULL; |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1722 | int i; |
| 1723 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1724 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1725 | DWORD attr; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1726 | PyUnicodeObject *po; |
| 1727 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 1728 | Py_BEGIN_ALLOW_THREADS |
| 1729 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1730 | if (attr != 0xFFFFFFFF) { |
| 1731 | if (i & _S_IWRITE) |
| 1732 | attr &= ~FILE_ATTRIBUTE_READONLY; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1733 | else |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1734 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1735 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1736 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1737 | else |
| 1738 | res = 0; |
| 1739 | Py_END_ALLOW_THREADS |
| 1740 | if (!res) |
| 1741 | return win32_error_unicode("chmod", |
| 1742 | PyUnicode_AS_UNICODE(po)); |
| 1743 | Py_INCREF(Py_None); |
| 1744 | return Py_None; |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 1745 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 1746 | /* Drop the argument parsing error as narrow strings |
| 1747 | are also valid. */ |
| 1748 | PyErr_Clear(); |
| 1749 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1750 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1751 | &opath, &i)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1752 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1753 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1754 | Py_BEGIN_ALLOW_THREADS |
| 1755 | attr = GetFileAttributesA(path); |
| 1756 | if (attr != 0xFFFFFFFF) { |
| 1757 | if (i & _S_IWRITE) |
| 1758 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1759 | else |
| 1760 | attr |= FILE_ATTRIBUTE_READONLY; |
| 1761 | res = SetFileAttributesA(path, attr); |
| 1762 | } |
| 1763 | else |
| 1764 | res = 0; |
| 1765 | Py_END_ALLOW_THREADS |
| 1766 | if (!res) { |
| 1767 | win32_error("chmod", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1768 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1769 | return NULL; |
| 1770 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1771 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1772 | Py_INCREF(Py_None); |
| 1773 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1774 | #else /* MS_WINDOWS */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1775 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 1776 | &opath, &i)) |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1777 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1778 | path = bytes2str(opath, 1); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1779 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ef40e77 | 2000-03-31 01:26:23 +0000 | [diff] [blame] | 1780 | res = chmod(path, i); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1781 | Py_END_ALLOW_THREADS |
| 1782 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1783 | return posix_error_with_allocated_filename(opath); |
| 1784 | release_bytes(opath); |
Guido van Rossum | ffd15f5 | 2000-03-31 00:47:28 +0000 | [diff] [blame] | 1785 | Py_INCREF(Py_None); |
| 1786 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1787 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_FCHMOD |
| 1791 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 1792 | "fchmod(fd, mode)\n\n\ |
| 1793 | Change the access permissions of the file given by file\n\ |
| 1794 | descriptor fd."); |
| 1795 | |
| 1796 | static PyObject * |
| 1797 | posix_fchmod(PyObject *self, PyObject *args) |
| 1798 | { |
| 1799 | int fd, mode, res; |
| 1800 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 1801 | return NULL; |
| 1802 | Py_BEGIN_ALLOW_THREADS |
| 1803 | res = fchmod(fd, mode); |
| 1804 | Py_END_ALLOW_THREADS |
| 1805 | if (res < 0) |
| 1806 | return posix_error(); |
| 1807 | Py_RETURN_NONE; |
| 1808 | } |
| 1809 | #endif /* HAVE_FCHMOD */ |
| 1810 | |
| 1811 | #ifdef HAVE_LCHMOD |
| 1812 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 1813 | "lchmod(path, mode)\n\n\ |
| 1814 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 1815 | affects the link itself rather than the target."); |
| 1816 | |
| 1817 | static PyObject * |
| 1818 | posix_lchmod(PyObject *self, PyObject *args) |
| 1819 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1820 | PyObject *opath; |
| 1821 | char *path; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1822 | int i; |
| 1823 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1824 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 1825 | &opath, &i)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1826 | return NULL; |
Eric Smith | 86a05ec | 2009-05-05 13:07:30 +0000 | [diff] [blame] | 1827 | path = bytes2str(opath, 1); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1828 | Py_BEGIN_ALLOW_THREADS |
| 1829 | res = lchmod(path, i); |
| 1830 | Py_END_ALLOW_THREADS |
| 1831 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1832 | return posix_error_with_allocated_filename(opath); |
| 1833 | release_bytes(opath); |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1834 | Py_RETURN_NONE; |
| 1835 | } |
| 1836 | #endif /* HAVE_LCHMOD */ |
| 1837 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1838 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1839 | #ifdef HAVE_CHFLAGS |
| 1840 | PyDoc_STRVAR(posix_chflags__doc__, |
| 1841 | "chflags(path, flags)\n\n\ |
| 1842 | Set file flags."); |
| 1843 | |
| 1844 | static PyObject * |
| 1845 | posix_chflags(PyObject *self, PyObject *args) |
| 1846 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1847 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1848 | char *path; |
| 1849 | unsigned long flags; |
| 1850 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1851 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 1852 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1853 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1854 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1855 | Py_BEGIN_ALLOW_THREADS |
| 1856 | res = chflags(path, flags); |
| 1857 | Py_END_ALLOW_THREADS |
| 1858 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1859 | return posix_error_with_allocated_filename(opath); |
| 1860 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1861 | Py_INCREF(Py_None); |
| 1862 | return Py_None; |
| 1863 | } |
| 1864 | #endif /* HAVE_CHFLAGS */ |
| 1865 | |
| 1866 | #ifdef HAVE_LCHFLAGS |
| 1867 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 1868 | "lchflags(path, flags)\n\n\ |
| 1869 | Set file flags.\n\ |
| 1870 | This function will not follow symbolic links."); |
| 1871 | |
| 1872 | static PyObject * |
| 1873 | posix_lchflags(PyObject *self, PyObject *args) |
| 1874 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1875 | PyObject *opath; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1876 | char *path; |
| 1877 | unsigned long flags; |
| 1878 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1879 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
Martin v. Löwis | 4adbc34 | 2009-05-05 17:17:55 +0000 | [diff] [blame] | 1880 | PyUnicode_FSConverter, &opath, &flags)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1881 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1882 | path = bytes2str(opath, 1); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1883 | Py_BEGIN_ALLOW_THREADS |
| 1884 | res = lchflags(path, flags); |
| 1885 | Py_END_ALLOW_THREADS |
| 1886 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1887 | return posix_error_with_allocated_filename(opath); |
| 1888 | release_bytes(opath); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1889 | Py_INCREF(Py_None); |
| 1890 | return Py_None; |
| 1891 | } |
| 1892 | #endif /* HAVE_LCHFLAGS */ |
| 1893 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1894 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1895 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1896 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1898 | |
| 1899 | static PyObject * |
| 1900 | posix_chroot(PyObject *self, PyObject *args) |
| 1901 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1902 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 1903 | } |
| 1904 | #endif |
| 1905 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1906 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1907 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1908 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1909 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1910 | |
| 1911 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1912 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1913 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1914 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1915 | } |
| 1916 | #endif /* HAVE_FSYNC */ |
| 1917 | |
| 1918 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1919 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 1920 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 1921 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 1922 | #endif |
| 1923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1925 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1926 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1927 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1928 | |
| 1929 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1930 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1931 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1932 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1933 | } |
| 1934 | #endif /* HAVE_FDATASYNC */ |
| 1935 | |
| 1936 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 1937 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1938 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1939 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1940 | 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] | 1941 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1942 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1943 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1944 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1945 | PyObject *opath; |
| 1946 | char *path; |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 1947 | long uid, gid; |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1948 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1949 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 1950 | PyUnicode_FSConverter, &opath, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1951 | &uid, &gid)) |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1952 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1953 | path = bytes2str(opath, 1); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1954 | Py_BEGIN_ALLOW_THREADS |
| 1955 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 1956 | Py_END_ALLOW_THREADS |
| 1957 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1958 | return posix_error_with_allocated_filename(opath); |
| 1959 | release_bytes(opath); |
Fredrik Lundh | 44328e6 | 2000-07-10 15:59:30 +0000 | [diff] [blame] | 1960 | Py_INCREF(Py_None); |
| 1961 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1962 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1963 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1964 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1965 | #ifdef HAVE_FCHOWN |
| 1966 | PyDoc_STRVAR(posix_fchown__doc__, |
| 1967 | "fchown(fd, uid, gid)\n\n\ |
| 1968 | Change the owner and group id of the file given by file descriptor\n\ |
| 1969 | fd to the numeric uid and gid."); |
| 1970 | |
| 1971 | static PyObject * |
| 1972 | posix_fchown(PyObject *self, PyObject *args) |
| 1973 | { |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1974 | int fd; |
| 1975 | long uid, gid; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1976 | int res; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1977 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 1978 | return NULL; |
| 1979 | Py_BEGIN_ALLOW_THREADS |
| 1980 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 1981 | Py_END_ALLOW_THREADS |
| 1982 | if (res < 0) |
| 1983 | return posix_error(); |
| 1984 | Py_RETURN_NONE; |
| 1985 | } |
| 1986 | #endif /* HAVE_FCHOWN */ |
| 1987 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 1988 | #ifdef HAVE_LCHOWN |
| 1989 | PyDoc_STRVAR(posix_lchown__doc__, |
| 1990 | "lchown(path, uid, gid)\n\n\ |
| 1991 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 1992 | This function will not follow symbolic links."); |
| 1993 | |
| 1994 | static PyObject * |
| 1995 | posix_lchown(PyObject *self, PyObject *args) |
| 1996 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1997 | PyObject *opath; |
| 1998 | char *path; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1999 | long uid, gid; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2000 | int res; |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 2001 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2002 | PyUnicode_FSConverter, &opath, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2003 | &uid, &gid)) |
| 2004 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2005 | path = bytes2str(opath, 1); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2006 | Py_BEGIN_ALLOW_THREADS |
| 2007 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2008 | Py_END_ALLOW_THREADS |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2009 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2010 | return posix_error_with_allocated_filename(opath); |
| 2011 | release_bytes(opath); |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2012 | Py_INCREF(Py_None); |
| 2013 | return Py_None; |
| 2014 | } |
| 2015 | #endif /* HAVE_LCHOWN */ |
| 2016 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2017 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2018 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2019 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2020 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2021 | { |
| 2022 | char buf[1026]; |
| 2023 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2024 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2025 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2026 | if (!use_bytes) { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2027 | wchar_t wbuf[1026]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2028 | wchar_t *wbuf2 = wbuf; |
| 2029 | PyObject *resobj; |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2030 | DWORD len; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2031 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2032 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2033 | /* If the buffer is large enough, len does not include the |
| 2034 | terminating \0. If the buffer is too small, len includes |
| 2035 | the space needed for the terminator. */ |
| 2036 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2037 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2038 | if (wbuf2) |
| 2039 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2040 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2041 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2042 | if (!wbuf2) { |
| 2043 | PyErr_NoMemory(); |
| 2044 | return NULL; |
| 2045 | } |
| 2046 | if (!len) { |
| 2047 | if (wbuf2 != wbuf) free(wbuf2); |
| 2048 | return win32_error("getcwdu", NULL); |
| 2049 | } |
| 2050 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2051 | if (wbuf2 != wbuf) free(wbuf2); |
| 2052 | return resobj; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2053 | } |
| 2054 | #endif |
| 2055 | |
| 2056 | Py_BEGIN_ALLOW_THREADS |
| 2057 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 2058 | res = _getcwd2(buf, sizeof buf); |
| 2059 | #else |
| 2060 | res = getcwd(buf, sizeof buf); |
| 2061 | #endif |
| 2062 | Py_END_ALLOW_THREADS |
| 2063 | if (res == NULL) |
| 2064 | return posix_error(); |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2065 | if (use_bytes) |
| 2066 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2067 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape"); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2068 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2069 | |
| 2070 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2071 | "getcwd() -> path\n\n\ |
| 2072 | Return a unicode string representing the current working directory."); |
| 2073 | |
| 2074 | static PyObject * |
| 2075 | posix_getcwd_unicode(PyObject *self) |
| 2076 | { |
| 2077 | return posix_getcwd(0); |
| 2078 | } |
| 2079 | |
| 2080 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2081 | "getcwdb() -> path\n\n\ |
| 2082 | Return a bytes string representing the current working directory."); |
| 2083 | |
| 2084 | static PyObject * |
| 2085 | posix_getcwd_bytes(PyObject *self) |
| 2086 | { |
| 2087 | return posix_getcwd(1); |
| 2088 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2089 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2090 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2091 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2092 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2093 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2094 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2095 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2096 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2097 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2098 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2099 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2100 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2101 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2102 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2105 | PyDoc_STRVAR(posix_listdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2106 | "listdir(path) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2107 | Return a list containing the names of the entries in the directory.\n\ |
| 2108 | \n\ |
| 2109 | path: path of directory to list\n\ |
| 2110 | \n\ |
| 2111 | 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] | 2112 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2113 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2114 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2115 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2116 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2117 | /* XXX Should redo this putting the (now four) versions of opendir |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2118 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2119 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2120 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2121 | PyObject *d, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2122 | HANDLE hFindFile; |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2123 | BOOL result; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2124 | WIN32_FIND_DATA FileData; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2125 | PyObject *opath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2126 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2127 | char *bufptr = namebuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2128 | 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] | 2129 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2130 | PyObject *po; |
| 2131 | if (PyArg_ParseTuple(args, "U:listdir", &po)) { |
| 2132 | WIN32_FIND_DATAW wFileData; |
| 2133 | Py_UNICODE *wnamebuf; |
| 2134 | /* Overallocate for \\*.*\0 */ |
| 2135 | len = PyUnicode_GET_SIZE(po); |
| 2136 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2137 | if (!wnamebuf) { |
| 2138 | PyErr_NoMemory(); |
| 2139 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2140 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2141 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); |
| 2142 | if (len > 0) { |
| 2143 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2144 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2145 | wnamebuf[len++] = L'\\'; |
| 2146 | wcscpy(wnamebuf + len, L"*.*"); |
| 2147 | } |
| 2148 | if ((d = PyList_New(0)) == NULL) { |
| 2149 | free(wnamebuf); |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
| 2153 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2154 | int error = GetLastError(); |
| 2155 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2156 | free(wnamebuf); |
| 2157 | return d; |
| 2158 | } |
| 2159 | Py_DECREF(d); |
| 2160 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2161 | free(wnamebuf); |
| 2162 | return NULL; |
| 2163 | } |
| 2164 | do { |
| 2165 | /* Skip over . and .. */ |
| 2166 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2167 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2168 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2169 | if (v == NULL) { |
| 2170 | Py_DECREF(d); |
| 2171 | d = NULL; |
| 2172 | break; |
| 2173 | } |
| 2174 | if (PyList_Append(d, v) != 0) { |
| 2175 | Py_DECREF(v); |
| 2176 | Py_DECREF(d); |
| 2177 | d = NULL; |
| 2178 | break; |
| 2179 | } |
| 2180 | Py_DECREF(v); |
| 2181 | } |
| 2182 | Py_BEGIN_ALLOW_THREADS |
| 2183 | result = FindNextFileW(hFindFile, &wFileData); |
| 2184 | Py_END_ALLOW_THREADS |
| 2185 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2186 | it got to the end of the directory. */ |
| 2187 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2188 | Py_DECREF(d); |
| 2189 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2190 | FindClose(hFindFile); |
| 2191 | free(wnamebuf); |
| 2192 | return NULL; |
| 2193 | } |
| 2194 | } while (result == TRUE); |
| 2195 | |
| 2196 | if (FindClose(hFindFile) == FALSE) { |
| 2197 | Py_DECREF(d); |
| 2198 | win32_error_unicode("FindClose", wnamebuf); |
| 2199 | free(wnamebuf); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | free(wnamebuf); |
| 2203 | return d; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2204 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2205 | /* Drop the argument parsing error as narrow strings |
| 2206 | are also valid. */ |
| 2207 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2208 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2209 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2210 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2211 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2212 | if (PyObject_Size(opath)+1 > MAX_PATH) { |
| 2213 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2214 | Py_DECREF(opath); |
| 2215 | return NULL; |
| 2216 | } |
| 2217 | strcpy(namebuf, bytes2str(opath, 0)); |
| 2218 | len = PyObject_Size(opath); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2219 | if (len > 0) { |
| 2220 | char ch = namebuf[len-1]; |
| 2221 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2222 | namebuf[len++] = '/'; |
Hirokazu Yamamoto | bbb9be7 | 2009-05-04 05:56:46 +0000 | [diff] [blame] | 2223 | strcpy(namebuf + len, "*.*"); |
Neil Schemenauer | 94b866a | 2002-03-22 20:51:58 +0000 | [diff] [blame] | 2224 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2225 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2226 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2227 | return NULL; |
| 2228 | |
| 2229 | hFindFile = FindFirstFile(namebuf, &FileData); |
| 2230 | if (hFindFile == INVALID_HANDLE_VALUE) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2231 | int error = GetLastError(); |
| 2232 | if (error == ERROR_FILE_NOT_FOUND) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2233 | return d; |
| 2234 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2235 | return win32_error("FindFirstFile", namebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2236 | } |
| 2237 | do { |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2238 | /* Skip over . and .. */ |
| 2239 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2240 | strcmp(FileData.cFileName, "..") != 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2241 | v = PyBytes_FromString(FileData.cFileName); |
Martin v. Löwis | e920f0d | 2006-03-07 23:59:33 +0000 | [diff] [blame] | 2242 | if (v == NULL) { |
| 2243 | Py_DECREF(d); |
| 2244 | d = NULL; |
| 2245 | break; |
| 2246 | } |
| 2247 | if (PyList_Append(d, v) != 0) { |
| 2248 | Py_DECREF(v); |
| 2249 | Py_DECREF(d); |
| 2250 | d = NULL; |
| 2251 | break; |
| 2252 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2253 | Py_DECREF(v); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2254 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2255 | Py_BEGIN_ALLOW_THREADS |
| 2256 | result = FindNextFile(hFindFile, &FileData); |
| 2257 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2258 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2259 | it got to the end of the directory. */ |
| 2260 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2261 | Py_DECREF(d); |
| 2262 | win32_error("FindNextFile", namebuf); |
| 2263 | FindClose(hFindFile); |
| 2264 | return NULL; |
| 2265 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2266 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2267 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2268 | if (FindClose(hFindFile) == FALSE) { |
| 2269 | Py_DECREF(d); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2270 | return win32_error("FindClose", namebuf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2271 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2272 | |
| 2273 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2274 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2275 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2276 | |
| 2277 | #ifndef MAX_PATH |
| 2278 | #define MAX_PATH CCHMAXPATH |
| 2279 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2280 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2281 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2282 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2283 | PyObject *d, *v; |
| 2284 | char namebuf[MAX_PATH+5]; |
| 2285 | HDIR hdir = 1; |
| 2286 | ULONG srchcnt = 1; |
| 2287 | FILEFINDBUF3 ep; |
| 2288 | APIRET rc; |
| 2289 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2290 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2291 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2292 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2293 | name = bytes2str(oname); |
| 2294 | len = PyObject_Size(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2295 | if (len >= MAX_PATH) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2296 | release_bytes(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2297 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2298 | return NULL; |
| 2299 | } |
| 2300 | strcpy(namebuf, name); |
| 2301 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2302 | if (*pt == ALTSEP) |
| 2303 | *pt = SEP; |
| 2304 | if (namebuf[len-1] != SEP) |
| 2305 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2306 | strcpy(namebuf + len, "*.*"); |
| 2307 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2308 | if ((d = PyList_New(0)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2309 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2310 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2311 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2313 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2314 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2315 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2316 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2317 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2318 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2319 | |
| 2320 | if (rc != NO_ERROR) { |
| 2321 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2322 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2325 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2326 | do { |
| 2327 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2328 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2329 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2330 | |
| 2331 | strcpy(namebuf, ep.achName); |
| 2332 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2333 | /* Leave Case of Name Alone -- In Native Form */ |
| 2334 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2335 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2336 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2337 | if (v == NULL) { |
| 2338 | Py_DECREF(d); |
| 2339 | d = NULL; |
| 2340 | break; |
| 2341 | } |
| 2342 | if (PyList_Append(d, v) != 0) { |
| 2343 | Py_DECREF(v); |
| 2344 | Py_DECREF(d); |
| 2345 | d = NULL; |
| 2346 | break; |
| 2347 | } |
| 2348 | Py_DECREF(v); |
| 2349 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2350 | } |
| 2351 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2352 | release_bytes(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2353 | return d; |
| 2354 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2355 | PyObject *oname; |
| 2356 | char *name; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2357 | PyObject *d, *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2358 | DIR *dirp; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2359 | struct dirent *ep; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2360 | int arg_is_unicode = 1; |
| 2361 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2362 | errno = 0; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2363 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) { |
| 2364 | arg_is_unicode = 0; |
| 2365 | PyErr_Clear(); |
| 2366 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2367 | if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2368 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2369 | name = bytes2str(oname, 1); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2370 | if ((dirp = opendir(name)) == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2371 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2372 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2373 | if ((d = PyList_New(0)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2374 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2375 | release_bytes(oname); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2376 | return NULL; |
| 2377 | } |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2378 | for (;;) { |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2379 | errno = 0; |
Georg Brandl | 622927b | 2006-03-07 12:48:03 +0000 | [diff] [blame] | 2380 | Py_BEGIN_ALLOW_THREADS |
| 2381 | ep = readdir(dirp); |
| 2382 | Py_END_ALLOW_THREADS |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2383 | if (ep == NULL) { |
| 2384 | if (errno == 0) { |
| 2385 | break; |
| 2386 | } else { |
| 2387 | closedir(dirp); |
| 2388 | Py_DECREF(d); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2389 | return posix_error_with_allocated_filename(oname); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2392 | if (ep->d_name[0] == '.' && |
| 2393 | (NAMLEN(ep) == 1 || |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2394 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
Guido van Rossum | 24f42ac | 1995-07-18 18:16:52 +0000 | [diff] [blame] | 2395 | continue; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2396 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2397 | if (v == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2398 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2399 | d = NULL; |
| 2400 | break; |
| 2401 | } |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2402 | if (arg_is_unicode) { |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2403 | PyObject *w; |
| 2404 | |
| 2405 | w = PyUnicode_FromEncodedObject(v, |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 2406 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2407 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2408 | Py_DECREF(v); |
| 2409 | if (w != NULL) |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2410 | v = w; |
Just van Rossum | 6a42183 | 2003-03-04 19:30:44 +0000 | [diff] [blame] | 2411 | else { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2412 | /* Encoding failed to decode ASCII bytes. |
| 2413 | Raise exception. */ |
| 2414 | Py_DECREF(d); |
| 2415 | d = NULL; |
| 2416 | break; |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2417 | } |
Just van Rossum | 46c9784 | 2003-02-25 21:42:15 +0000 | [diff] [blame] | 2418 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2419 | if (PyList_Append(d, v) != 0) { |
| 2420 | Py_DECREF(v); |
| 2421 | Py_DECREF(d); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2422 | d = NULL; |
| 2423 | break; |
| 2424 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2425 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2426 | } |
| 2427 | closedir(dirp); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2428 | release_bytes(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2429 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2430 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2431 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2432 | #endif /* which OS */ |
| 2433 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2434 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2435 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2436 | /* A helper function for abspath on win32 */ |
| 2437 | static PyObject * |
| 2438 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2439 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2440 | PyObject *opath; |
| 2441 | char *path; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2442 | char outbuf[MAX_PATH*2]; |
| 2443 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2444 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2445 | PyUnicodeObject *po; |
| 2446 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2447 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2448 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2449 | Py_UNICODE *wtemp; |
| 2450 | DWORD result; |
| 2451 | PyObject *v; |
| 2452 | result = GetFullPathNameW(wpath, |
| 2453 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2454 | woutbuf, &wtemp); |
| 2455 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2456 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2457 | if (!woutbufp) |
| 2458 | return PyErr_NoMemory(); |
| 2459 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2460 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2461 | if (result) |
| 2462 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2463 | else |
| 2464 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2465 | if (woutbufp != woutbuf) |
| 2466 | free(woutbufp); |
| 2467 | return v; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2468 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2469 | /* Drop the argument parsing error as narrow strings |
| 2470 | are also valid. */ |
| 2471 | PyErr_Clear(); |
| 2472 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2473 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2474 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2475 | PyUnicode_FSConverter, &opath)) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2476 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2477 | path = bytes2str(opath, 1); |
| 2478 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2479 | outbuf, &temp)) { |
| 2480 | win32_error("GetFullPathName", path); |
| 2481 | release_bytes(opath); |
| 2482 | return NULL; |
| 2483 | } |
| 2484 | release_bytes(opath); |
Martin v. Löwis | 969297f | 2004-06-15 18:49:58 +0000 | [diff] [blame] | 2485 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2486 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2487 | Py_FileSystemDefaultEncoding, NULL); |
| 2488 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2489 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2490 | } /* end of posix__getfullpathname */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2491 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2493 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2494 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2495 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2496 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2497 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2498 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2499 | { |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2500 | int res; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2501 | PyObject *opath; |
| 2502 | char *path; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2503 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2504 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2505 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2506 | PyUnicodeObject *po; |
| 2507 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 2508 | Py_BEGIN_ALLOW_THREADS |
| 2509 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2510 | it is a simple dereference. */ |
| 2511 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 2512 | Py_END_ALLOW_THREADS |
| 2513 | if (!res) |
| 2514 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 2515 | Py_INCREF(Py_None); |
| 2516 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2517 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2518 | /* Drop the argument parsing error as narrow strings |
| 2519 | are also valid. */ |
| 2520 | PyErr_Clear(); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2521 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2522 | PyUnicode_FSConverter, &opath, &mode)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2523 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2524 | path = bytes2str(opath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2525 | Py_BEGIN_ALLOW_THREADS |
| 2526 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 2527 | it is a simple dereference. */ |
| 2528 | res = CreateDirectoryA(path, NULL); |
| 2529 | Py_END_ALLOW_THREADS |
| 2530 | if (!res) { |
| 2531 | win32_error("mkdir", path); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2532 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2533 | return NULL; |
| 2534 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2535 | release_bytes(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2536 | Py_INCREF(Py_None); |
| 2537 | return Py_None; |
| 2538 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2539 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2540 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 2541 | PyUnicode_FSConverter, &opath, &mode)) |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2542 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2543 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2544 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2545 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2546 | res = mkdir(path); |
| 2547 | #else |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2548 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2549 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2550 | Py_END_ALLOW_THREADS |
Guido van Rossum | b0824db | 1996-02-25 04:50:32 +0000 | [diff] [blame] | 2551 | if (res < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2552 | return posix_error_with_allocated_filename(opath); |
| 2553 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2554 | Py_INCREF(Py_None); |
| 2555 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2556 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2559 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2560 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 2561 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2562 | #include <sys/resource.h> |
| 2563 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2564 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2565 | |
| 2566 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2567 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2568 | "nice(inc) -> new_priority\n\n\ |
| 2569 | 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] | 2570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2571 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2572 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2573 | { |
| 2574 | int increment, value; |
| 2575 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2576 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2577 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2578 | |
| 2579 | /* There are two flavours of 'nice': one that returns the new |
| 2580 | priority (as required by almost all standards out there) and the |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2581 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 2582 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2583 | |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2584 | If we are of the nice family that returns the new priority, we |
| 2585 | need to clear errno before the call, and check if errno is filled |
| 2586 | before calling posix_error() on a returnvalue of -1, because the |
| 2587 | -1 may be the actual new priority! */ |
| 2588 | |
| 2589 | errno = 0; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2590 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 2591 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 2592 | if (value == 0) |
| 2593 | value = getpriority(PRIO_PROCESS, 0); |
| 2594 | #endif |
| 2595 | if (value == -1 && errno != 0) |
| 2596 | /* either nice() or getpriority() returned an error */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2597 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2598 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2599 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2600 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2602 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2603 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2604 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2605 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2606 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2607 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2608 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2609 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2610 | PyObject *o1, *o2; |
| 2611 | char *p1, *p2; |
| 2612 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2613 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2614 | goto error; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2615 | if (!convert_to_unicode(&o1)) |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2616 | goto error; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2617 | if (!convert_to_unicode(&o2)) { |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 2618 | Py_DECREF(o1); |
| 2619 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2620 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2621 | Py_BEGIN_ALLOW_THREADS |
| 2622 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 2623 | PyUnicode_AsUnicode(o2)); |
| 2624 | Py_END_ALLOW_THREADS |
| 2625 | Py_DECREF(o1); |
| 2626 | Py_DECREF(o2); |
| 2627 | if (!result) |
| 2628 | return win32_error("rename", NULL); |
| 2629 | Py_INCREF(Py_None); |
| 2630 | return Py_None; |
| 2631 | error: |
| 2632 | PyErr_Clear(); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2633 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 2634 | return NULL; |
| 2635 | Py_BEGIN_ALLOW_THREADS |
| 2636 | result = MoveFileA(p1, p2); |
| 2637 | Py_END_ALLOW_THREADS |
| 2638 | if (!result) |
| 2639 | return win32_error("rename", NULL); |
| 2640 | Py_INCREF(Py_None); |
| 2641 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2642 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2643 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2644 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2647 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2648 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2649 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2650 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2651 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2652 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2653 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2654 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2655 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2656 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2657 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2658 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2659 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2664 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2665 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2668 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2669 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2670 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2671 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2672 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2673 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2674 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2677 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2680 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2681 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2684 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2685 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2686 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2687 | #ifdef MS_WINDOWS |
| 2688 | wchar_t *command; |
| 2689 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 2690 | return NULL; |
| 2691 | #else |
| 2692 | char *command; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2693 | if (!PyArg_ParseTuple(args, "s:system", &command)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2694 | return NULL; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2695 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2696 | Py_BEGIN_ALLOW_THREADS |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2697 | #ifdef MS_WINDOWS |
| 2698 | sts = _wsystem(command); |
| 2699 | #else |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 2700 | sts = system(command); |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 2701 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2702 | Py_END_ALLOW_THREADS |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2703 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2704 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 2705 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2706 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2708 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2709 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2710 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2711 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2712 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2713 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2714 | { |
| 2715 | int i; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2716 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2717 | return NULL; |
Fred Drake | 0368bc4 | 2001-07-19 20:48:32 +0000 | [diff] [blame] | 2718 | i = (int)umask(i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2719 | if (i < 0) |
| 2720 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2721 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2725 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2726 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2729 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2730 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2731 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2732 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2733 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2734 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2735 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2736 | #ifdef MS_WINDOWS |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 2737 | return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2738 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2739 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2740 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2743 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2744 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2745 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2746 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2747 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2748 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2749 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2750 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2751 | { |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2752 | struct utsname u; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2753 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2754 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2755 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2756 | res = uname(&u); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2757 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2758 | if (res < 0) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2759 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2760 | return Py_BuildValue("(sssss)", |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 2761 | u.sysname, |
| 2762 | u.nodename, |
| 2763 | u.release, |
| 2764 | u.version, |
| 2765 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 2766 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2767 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2768 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2769 | static int |
| 2770 | extract_time(PyObject *t, long* sec, long* usec) |
| 2771 | { |
| 2772 | long intval; |
| 2773 | if (PyFloat_Check(t)) { |
| 2774 | double tval = PyFloat_AsDouble(t); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2775 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2776 | if (!intobj) |
| 2777 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2778 | intval = PyLong_AsLong(intobj); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2779 | Py_DECREF(intobj); |
Michael W. Hudson | b896381 | 2005-07-05 15:21:58 +0000 | [diff] [blame] | 2780 | if (intval == -1 && PyErr_Occurred()) |
| 2781 | return -1; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2782 | *sec = intval; |
Tim Peters | 96940cf | 2002-09-10 15:37:28 +0000 | [diff] [blame] | 2783 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2784 | if (*usec < 0) |
| 2785 | /* If rounding gave us a negative number, |
| 2786 | truncate. */ |
| 2787 | *usec = 0; |
| 2788 | return 0; |
| 2789 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2790 | intval = PyLong_AsLong(t); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2791 | if (intval == -1 && PyErr_Occurred()) |
| 2792 | return -1; |
| 2793 | *sec = intval; |
| 2794 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 2795 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2796 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2797 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2798 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2799 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2800 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2801 | 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] | 2802 | 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] | 2803 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2804 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2805 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2806 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2807 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2808 | PyObject *arg; |
| 2809 | PyUnicodeObject *obwpath; |
| 2810 | wchar_t *wpath = NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2811 | PyObject *oapath; |
| 2812 | char *apath; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2813 | HANDLE hFile; |
| 2814 | long atimesec, mtimesec, ausec, musec; |
| 2815 | FILETIME atime, mtime; |
| 2816 | PyObject *result = NULL; |
| 2817 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2818 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 2819 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 2820 | Py_BEGIN_ALLOW_THREADS |
| 2821 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 2822 | NULL, OPEN_EXISTING, |
| 2823 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2824 | Py_END_ALLOW_THREADS |
| 2825 | if (hFile == INVALID_HANDLE_VALUE) |
| 2826 | return win32_error_unicode("utime", wpath); |
| 2827 | } else |
| 2828 | /* Drop the argument parsing error as narrow strings |
| 2829 | are also valid. */ |
| 2830 | PyErr_Clear(); |
| 2831 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2832 | if (!wpath) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2833 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2834 | PyUnicode_FSConverter, &oapath, &arg)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2835 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2836 | apath = bytes2str(oapath, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2837 | Py_BEGIN_ALLOW_THREADS |
| 2838 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2839 | NULL, OPEN_EXISTING, |
| 2840 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2841 | Py_END_ALLOW_THREADS |
| 2842 | if (hFile == INVALID_HANDLE_VALUE) { |
| 2843 | win32_error("utime", apath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2844 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2845 | return NULL; |
| 2846 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2847 | release_bytes(oapath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | if (arg == Py_None) { |
| 2851 | SYSTEMTIME now; |
| 2852 | GetSystemTime(&now); |
| 2853 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 2854 | !SystemTimeToFileTime(&now, &atime)) { |
| 2855 | win32_error("utime", NULL); |
| 2856 | goto done; |
| 2857 | } |
| 2858 | } |
| 2859 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 2860 | PyErr_SetString(PyExc_TypeError, |
| 2861 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 2862 | goto done; |
| 2863 | } |
| 2864 | else { |
| 2865 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 2866 | &atimesec, &ausec) == -1) |
| 2867 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2868 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2869 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 2870 | &mtimesec, &musec) == -1) |
| 2871 | goto done; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2872 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2873 | } |
| 2874 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 2875 | /* Avoid putting the file name into the error here, |
| 2876 | as that may confuse the user into believing that |
| 2877 | something is wrong with the file, when it also |
| 2878 | could be the time stamp that gives a problem. */ |
| 2879 | win32_error("utime", NULL); |
| 2880 | } |
| 2881 | Py_INCREF(Py_None); |
| 2882 | result = Py_None; |
| 2883 | done: |
| 2884 | CloseHandle(hFile); |
| 2885 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2886 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2887 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2888 | PyObject *opath; |
| 2889 | char *path; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2890 | long atime, mtime, ausec, musec; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2891 | int res; |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2892 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2893 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2894 | #if defined(HAVE_UTIMES) |
| 2895 | struct timeval buf[2]; |
| 2896 | #define ATIME buf[0].tv_sec |
| 2897 | #define MTIME buf[1].tv_sec |
| 2898 | #elif defined(HAVE_UTIME_H) |
Guido van Rossum | 6d8841c | 1997-08-14 19:57:39 +0000 | [diff] [blame] | 2899 | /* XXX should define struct utimbuf instead, above */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2900 | struct utimbuf buf; |
| 2901 | #define ATIME buf.actime |
| 2902 | #define MTIME buf.modtime |
| 2903 | #define UTIME_ARG &buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2904 | #else /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2905 | time_t buf[2]; |
| 2906 | #define ATIME buf[0] |
| 2907 | #define MTIME buf[1] |
| 2908 | #define UTIME_ARG buf |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2909 | #endif /* HAVE_UTIMES */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2910 | |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2911 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2912 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 2913 | PyUnicode_FSConverter, &opath, &arg)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2914 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2915 | path = bytes2str(opath, 1); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2916 | if (arg == Py_None) { |
| 2917 | /* optional time values not given */ |
| 2918 | Py_BEGIN_ALLOW_THREADS |
| 2919 | res = utime(path, NULL); |
| 2920 | Py_END_ALLOW_THREADS |
| 2921 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2922 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2923 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2924 | "utime() arg 2 must be a tuple (atime, mtime)"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2925 | release_bytes(opath); |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2926 | return NULL; |
| 2927 | } |
| 2928 | else { |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2929 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2930 | &atime, &ausec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2931 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2932 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2933 | } |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2934 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2935 | &mtime, &musec) == -1) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2936 | release_bytes(opath); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2937 | return NULL; |
Neal Norwitz | 9665271 | 2004-06-06 20:40:27 +0000 | [diff] [blame] | 2938 | } |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2939 | ATIME = atime; |
| 2940 | MTIME = mtime; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 2941 | #ifdef HAVE_UTIMES |
| 2942 | buf[0].tv_usec = ausec; |
| 2943 | buf[1].tv_usec = musec; |
| 2944 | Py_BEGIN_ALLOW_THREADS |
| 2945 | res = utimes(path, buf); |
| 2946 | Py_END_ALLOW_THREADS |
| 2947 | #else |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2948 | Py_BEGIN_ALLOW_THREADS |
| 2949 | res = utime(path, UTIME_ARG); |
| 2950 | Py_END_ALLOW_THREADS |
Mark Hammond | 817c929 | 2003-12-03 01:22:38 +0000 | [diff] [blame] | 2951 | #endif /* HAVE_UTIMES */ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 2952 | } |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2953 | if (res < 0) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2954 | return posix_error_with_allocated_filename(opath); |
Mark Hammond | 2d5914b | 2004-05-04 08:10:37 +0000 | [diff] [blame] | 2955 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2956 | release_bytes(opath); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2957 | Py_INCREF(Py_None); |
| 2958 | return Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 2959 | #undef UTIME_ARG |
| 2960 | #undef ATIME |
| 2961 | #undef MTIME |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2962 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2965 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 2966 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2968 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2969 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2970 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2971 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2972 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2973 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2974 | { |
| 2975 | int sts; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2976 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2977 | return NULL; |
| 2978 | _exit(sts); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2979 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2982 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 2983 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2984 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2985 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2986 | Py_ssize_t i; |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 2987 | for (i = 0; i < count; i++) |
| 2988 | PyMem_Free(array[i]); |
| 2989 | PyMem_DEL(array); |
| 2990 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2991 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 2992 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2993 | int fsconvert_strdup(PyObject *o, char**out) |
| 2994 | { |
| 2995 | PyObject *bytes; |
| 2996 | Py_ssize_t size; |
| 2997 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 2998 | return 0; |
| 2999 | size = PyObject_Size(bytes); |
| 3000 | *out = PyMem_Malloc(size+1); |
| 3001 | if (!*out) |
| 3002 | return 0; |
| 3003 | /* Don't lock bytes, as we hold the GIL */ |
| 3004 | memcpy(*out, bytes2str(bytes, 0), size+1); |
| 3005 | Py_DECREF(bytes); |
| 3006 | return 1; |
| 3007 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3008 | #endif |
| 3009 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3010 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3011 | #ifdef HAVE_EXECV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3012 | PyDoc_STRVAR(posix_execv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3013 | "execv(path, args)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3014 | Execute an executable path with arguments, replacing current process.\n\ |
| 3015 | \n\ |
| 3016 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3017 | args: tuple or list of strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3018 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3019 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3020 | posix_execv(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3021 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3022 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3023 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3024 | PyObject *argv; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3025 | char **argvlist; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3026 | Py_ssize_t i, argc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3027 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3028 | |
Guido van Rossum | 89b3325 | 1993-10-22 14:26:06 +0000 | [diff] [blame] | 3029 | /* execv has two arguments: (path, argv), where |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3030 | argv is a list or tuple of strings. */ |
| 3031 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3032 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 3033 | PyUnicode_FSConverter, |
| 3034 | &opath, &argv)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3035 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3036 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3037 | if (PyList_Check(argv)) { |
| 3038 | argc = PyList_Size(argv); |
| 3039 | getitem = PyList_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3040 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3041 | else if (PyTuple_Check(argv)) { |
| 3042 | argc = PyTuple_Size(argv); |
| 3043 | getitem = PyTuple_GetItem; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3044 | } |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3045 | else { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3046 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3047 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3048 | return NULL; |
| 3049 | } |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3050 | if (argc < 1) { |
| 3051 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3052 | release_bytes(opath); |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 3053 | return NULL; |
| 3054 | } |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3055 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3056 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3057 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3058 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3059 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3060 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3061 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3062 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3063 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3064 | free_string_array(argvlist, i); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3065 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3066 | "execv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3067 | release_bytes(opath); |
Guido van Rossum | 50422b4 | 2000-04-26 20:34:28 +0000 | [diff] [blame] | 3068 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3069 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3070 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3071 | } |
| 3072 | argvlist[argc] = NULL; |
| 3073 | |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 3074 | execv(path, argvlist); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3075 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3076 | /* If we get here it's definitely an error */ |
| 3077 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3078 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3079 | release_bytes(opath); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3080 | return posix_error(); |
| 3081 | } |
| 3082 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3084 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3085 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3086 | Execute a path with arguments and environment, replacing current process.\n\ |
| 3087 | \n\ |
| 3088 | path: path of executable file\n\ |
| 3089 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3090 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3091 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3092 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3093 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3094 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3095 | PyObject *opath; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3096 | char *path; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3097 | PyObject *argv, *env; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3098 | char **argvlist; |
| 3099 | char **envlist; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3100 | PyObject *key, *val, *keys=NULL, *vals=NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3101 | Py_ssize_t i, pos, argc, envc; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3102 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3103 | Py_ssize_t lastarg = 0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3104 | |
| 3105 | /* execve has three arguments: (path, argv, env), where |
| 3106 | argv is a list or tuple of strings and env is a dictionary |
| 3107 | like posix.environ. */ |
| 3108 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3109 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 3110 | PyUnicode_FSConverter, |
| 3111 | &opath, &argv, &env)) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3112 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3113 | path = bytes2str(opath, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3114 | if (PyList_Check(argv)) { |
| 3115 | argc = PyList_Size(argv); |
| 3116 | getitem = PyList_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3117 | } |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3118 | else if (PyTuple_Check(argv)) { |
| 3119 | argc = PyTuple_Size(argv); |
| 3120 | getitem = PyTuple_GetItem; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3121 | } |
| 3122 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3123 | PyErr_SetString(PyExc_TypeError, |
| 3124 | "execve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3125 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3126 | } |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3127 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3128 | PyErr_SetString(PyExc_TypeError, |
| 3129 | "execve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3130 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3133 | argvlist = PyMem_NEW(char *, argc+1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3134 | if (argvlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3135 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3136 | goto fail_0; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3137 | } |
| 3138 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3139 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3140 | &argvlist[i])) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3141 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3142 | lastarg = i; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3143 | goto fail_1; |
| 3144 | } |
| 3145 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3146 | lastarg = argc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3147 | argvlist[argc] = NULL; |
| 3148 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3149 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3150 | if (i < 0) |
| 3151 | goto fail_1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3152 | envlist = PyMem_NEW(char *, i + 1); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3153 | if (envlist == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3154 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3155 | goto fail_1; |
| 3156 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3157 | envc = 0; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3158 | keys = PyMapping_Keys(env); |
| 3159 | vals = PyMapping_Values(env); |
| 3160 | if (!keys || !vals) |
| 3161 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3162 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3163 | PyErr_SetString(PyExc_TypeError, |
| 3164 | "execve(): env.keys() or env.values() is not a list"); |
| 3165 | goto fail_2; |
| 3166 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3167 | |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3168 | for (pos = 0; pos < i; pos++) { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3169 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3170 | size_t len; |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3171 | |
| 3172 | key = PyList_GetItem(keys, pos); |
| 3173 | val = PyList_GetItem(vals, pos); |
| 3174 | if (!key || !val) |
| 3175 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3176 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3177 | if (!PyArg_Parse( |
| 3178 | key, |
| 3179 | "s;execve() arg 3 contains a non-string key", |
| 3180 | &k) || |
| 3181 | !PyArg_Parse( |
| 3182 | val, |
| 3183 | "s;execve() arg 3 contains a non-string value", |
| 3184 | &v)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 3185 | { |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3186 | goto fail_2; |
| 3187 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3188 | |
| 3189 | #if defined(PYOS_OS2) |
| 3190 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3191 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
| 3192 | #endif |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3193 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3194 | p = PyMem_NEW(char, len); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3195 | if (p == NULL) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3196 | PyErr_NoMemory(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3197 | goto fail_2; |
| 3198 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3199 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3200 | envlist[envc++] = p; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 3201 | #if defined(PYOS_OS2) |
| 3202 | } |
| 3203 | #endif |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3204 | } |
| 3205 | envlist[envc] = 0; |
| 3206 | |
| 3207 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3208 | |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3209 | /* If we get here it's definitely an error */ |
| 3210 | |
| 3211 | (void) posix_error(); |
| 3212 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3213 | fail_2: |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3214 | while (--envc >= 0) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3215 | PyMem_DEL(envlist[envc]); |
| 3216 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3217 | fail_1: |
| 3218 | free_string_array(argvlist, lastarg); |
Barry Warsaw | 5ed19dc | 1997-01-29 15:08:24 +0000 | [diff] [blame] | 3219 | Py_XDECREF(vals); |
| 3220 | Py_XDECREF(keys); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3221 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3222 | release_bytes(opath); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3223 | return NULL; |
| 3224 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3225 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 3226 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3227 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3228 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3229 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3230 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3231 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3232 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3233 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3234 | path: path of executable file\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3235 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3236 | |
| 3237 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3238 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3239 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3240 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3241 | char *path; |
| 3242 | PyObject *argv; |
| 3243 | char **argvlist; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3244 | int mode, i; |
| 3245 | Py_ssize_t argc; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3246 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3247 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3248 | |
| 3249 | /* spawnv has three arguments: (mode, path, argv), where |
| 3250 | argv is a list or tuple of strings. */ |
| 3251 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3252 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 3253 | PyUnicode_FSConverter, |
| 3254 | &opath, &argv)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3255 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3256 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3257 | if (PyList_Check(argv)) { |
| 3258 | argc = PyList_Size(argv); |
| 3259 | getitem = PyList_GetItem; |
| 3260 | } |
| 3261 | else if (PyTuple_Check(argv)) { |
| 3262 | argc = PyTuple_Size(argv); |
| 3263 | getitem = PyTuple_GetItem; |
| 3264 | } |
| 3265 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3266 | PyErr_SetString(PyExc_TypeError, |
| 3267 | "spawnv() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3268 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3269 | return NULL; |
| 3270 | } |
| 3271 | |
| 3272 | argvlist = PyMem_NEW(char *, argc+1); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3273 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3274 | release_bytes(opath); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 3275 | return PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3276 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3277 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3278 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3279 | &argvlist[i])) { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3280 | free_string_array(argvlist, i); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3281 | PyErr_SetString( |
| 3282 | PyExc_TypeError, |
| 3283 | "spawnv() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3284 | release_bytes(opath); |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 3285 | return NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | argvlist[argc] = NULL; |
| 3289 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3290 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3291 | Py_BEGIN_ALLOW_THREADS |
| 3292 | spawnval = spawnv(mode, path, argvlist); |
| 3293 | Py_END_ALLOW_THREADS |
| 3294 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3295 | if (mode == _OLD_P_OVERLAY) |
| 3296 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3297 | |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3298 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3299 | spawnval = _spawnv(mode, path, argvlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3300 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3301 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3302 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3303 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3304 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3305 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3306 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3307 | return posix_error(); |
| 3308 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3309 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3310 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3311 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3312 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3313 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3317 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3318 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3319 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3320 | \n\ |
Fred Drake | a6dff3e | 1999-02-01 22:24:40 +0000 | [diff] [blame] | 3321 | mode: mode of process creation\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3322 | path: path of executable file\n\ |
| 3323 | args: tuple or list of arguments\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3324 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3325 | |
| 3326 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3327 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3328 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3329 | PyObject *opath; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3330 | char *path; |
| 3331 | PyObject *argv, *env; |
| 3332 | char **argvlist; |
| 3333 | char **envlist; |
| 3334 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3335 | int mode, pos, envc; |
| 3336 | Py_ssize_t argc, i; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 3337 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3338 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3339 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3340 | |
| 3341 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 3342 | argv is a list or tuple of strings and env is a dictionary |
| 3343 | like posix.environ. */ |
| 3344 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3345 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 3346 | PyUnicode_FSConverter, |
| 3347 | &opath, &argv, &env)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3348 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3349 | path = bytes2str(opath, 1); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3350 | if (PyList_Check(argv)) { |
| 3351 | argc = PyList_Size(argv); |
| 3352 | getitem = PyList_GetItem; |
| 3353 | } |
| 3354 | else if (PyTuple_Check(argv)) { |
| 3355 | argc = PyTuple_Size(argv); |
| 3356 | getitem = PyTuple_GetItem; |
| 3357 | } |
| 3358 | else { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3359 | PyErr_SetString(PyExc_TypeError, |
| 3360 | "spawnve() arg 2 must be a tuple or list"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3361 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3362 | } |
| 3363 | if (!PyMapping_Check(env)) { |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3364 | PyErr_SetString(PyExc_TypeError, |
| 3365 | "spawnve() arg 3 must be a mapping object"); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3366 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | argvlist = PyMem_NEW(char *, argc+1); |
| 3370 | if (argvlist == NULL) { |
| 3371 | PyErr_NoMemory(); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3372 | goto fail_0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3373 | } |
| 3374 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3375 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3376 | &argvlist[i])) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3377 | { |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3378 | lastarg = i; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3379 | goto fail_1; |
| 3380 | } |
| 3381 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3382 | lastarg = argc; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3383 | argvlist[argc] = NULL; |
| 3384 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 3385 | i = PyMapping_Size(env); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3386 | if (i < 0) |
| 3387 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3388 | envlist = PyMem_NEW(char *, i + 1); |
| 3389 | if (envlist == NULL) { |
| 3390 | PyErr_NoMemory(); |
| 3391 | goto fail_1; |
| 3392 | } |
| 3393 | envc = 0; |
| 3394 | keys = PyMapping_Keys(env); |
| 3395 | vals = PyMapping_Values(env); |
| 3396 | if (!keys || !vals) |
| 3397 | goto fail_2; |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3398 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3399 | PyErr_SetString(PyExc_TypeError, |
| 3400 | "spawnve(): env.keys() or env.values() is not a list"); |
| 3401 | goto fail_2; |
| 3402 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3403 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3404 | for (pos = 0; pos < i; pos++) { |
| 3405 | char *p, *k, *v; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3406 | size_t len; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3407 | |
| 3408 | key = PyList_GetItem(keys, pos); |
| 3409 | val = PyList_GetItem(vals, pos); |
| 3410 | if (!key || !val) |
| 3411 | goto fail_2; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3412 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3413 | if (!PyArg_Parse( |
| 3414 | key, |
| 3415 | "s;spawnve() arg 3 contains a non-string key", |
| 3416 | &k) || |
| 3417 | !PyArg_Parse( |
| 3418 | val, |
| 3419 | "s;spawnve() arg 3 contains a non-string value", |
| 3420 | &v)) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3421 | { |
| 3422 | goto fail_2; |
| 3423 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3424 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3425 | p = PyMem_NEW(char, len); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3426 | if (p == NULL) { |
| 3427 | PyErr_NoMemory(); |
| 3428 | goto fail_2; |
| 3429 | } |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 3430 | PyOS_snprintf(p, len, "%s=%s", k, v); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3431 | envlist[envc++] = p; |
| 3432 | } |
| 3433 | envlist[envc] = 0; |
| 3434 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3435 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 3436 | Py_BEGIN_ALLOW_THREADS |
| 3437 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 3438 | Py_END_ALLOW_THREADS |
| 3439 | #else |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 3440 | if (mode == _OLD_P_OVERLAY) |
| 3441 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3442 | |
| 3443 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3444 | spawnval = _spawnve(mode, path, argvlist, envlist); |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3445 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 3446 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 3447 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3448 | if (spawnval == -1) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3449 | (void) posix_error(); |
| 3450 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 3451 | #if SIZEOF_LONG == SIZEOF_VOID_P |
| 3452 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3453 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 3454 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 3455 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3456 | |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3457 | fail_2: |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3458 | while (--envc >= 0) |
| 3459 | PyMem_DEL(envlist[envc]); |
| 3460 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 3461 | fail_1: |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3462 | free_string_array(argvlist, lastarg); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3463 | Py_XDECREF(vals); |
| 3464 | Py_XDECREF(keys); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3465 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3466 | release_bytes(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3467 | return res; |
| 3468 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3469 | |
| 3470 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 3471 | #if defined(PYOS_OS2) |
| 3472 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 3473 | "spawnvp(mode, file, args)\n\n\ |
| 3474 | Execute the program 'file' in a new process, using the environment\n\ |
| 3475 | search path to find the file.\n\ |
| 3476 | \n\ |
| 3477 | mode: mode of process creation\n\ |
| 3478 | file: executable file name\n\ |
| 3479 | args: tuple or list of strings"); |
| 3480 | |
| 3481 | static PyObject * |
| 3482 | posix_spawnvp(PyObject *self, PyObject *args) |
| 3483 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3484 | PyObject *opath; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3485 | char *path; |
| 3486 | PyObject *argv; |
| 3487 | char **argvlist; |
| 3488 | int mode, i, argc; |
| 3489 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3490 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3491 | |
| 3492 | /* spawnvp has three arguments: (mode, path, argv), where |
| 3493 | argv is a list or tuple of strings. */ |
| 3494 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3495 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 3496 | PyUnicode_FSConverter, |
| 3497 | &opath, &argv)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3498 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3499 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3500 | if (PyList_Check(argv)) { |
| 3501 | argc = PyList_Size(argv); |
| 3502 | getitem = PyList_GetItem; |
| 3503 | } |
| 3504 | else if (PyTuple_Check(argv)) { |
| 3505 | argc = PyTuple_Size(argv); |
| 3506 | getitem = PyTuple_GetItem; |
| 3507 | } |
| 3508 | else { |
| 3509 | PyErr_SetString(PyExc_TypeError, |
| 3510 | "spawnvp() arg 2 must be a tuple or list"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3511 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3512 | return NULL; |
| 3513 | } |
| 3514 | |
| 3515 | argvlist = PyMem_NEW(char *, argc+1); |
| 3516 | if (argvlist == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3517 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3518 | return PyErr_NoMemory(); |
| 3519 | } |
| 3520 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3521 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3522 | &argvlist[i])) { |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3523 | free_string_array(argvlist, i); |
| 3524 | PyErr_SetString( |
| 3525 | PyExc_TypeError, |
| 3526 | "spawnvp() arg 2 must contain only strings"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3527 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3528 | return NULL; |
| 3529 | } |
| 3530 | } |
| 3531 | argvlist[argc] = NULL; |
| 3532 | |
| 3533 | Py_BEGIN_ALLOW_THREADS |
| 3534 | #if defined(PYCC_GCC) |
| 3535 | spawnval = spawnvp(mode, path, argvlist); |
| 3536 | #else |
| 3537 | spawnval = _spawnvp(mode, path, argvlist); |
| 3538 | #endif |
| 3539 | Py_END_ALLOW_THREADS |
| 3540 | |
| 3541 | free_string_array(argvlist, argc); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3542 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3543 | |
| 3544 | if (spawnval == -1) |
| 3545 | return posix_error(); |
| 3546 | else |
| 3547 | return Py_BuildValue("l", (long) spawnval); |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 3552 | "spawnvpe(mode, file, args, env)\n\n\ |
| 3553 | Execute the program 'file' in a new process, using the environment\n\ |
| 3554 | search path to find the file.\n\ |
| 3555 | \n\ |
| 3556 | mode: mode of process creation\n\ |
| 3557 | file: executable file name\n\ |
| 3558 | args: tuple or list of arguments\n\ |
| 3559 | env: dictionary of strings mapping to strings"); |
| 3560 | |
| 3561 | static PyObject * |
| 3562 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 3563 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3564 | PyObject *opath |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3565 | char *path; |
| 3566 | PyObject *argv, *env; |
| 3567 | char **argvlist; |
| 3568 | char **envlist; |
| 3569 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; |
| 3570 | int mode, i, pos, argc, envc; |
| 3571 | Py_intptr_t spawnval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3572 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3573 | int lastarg = 0; |
| 3574 | |
| 3575 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 3576 | argv is a list or tuple of strings and env is a dictionary |
| 3577 | like posix.environ. */ |
| 3578 | |
| 3579 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3580 | PyUnicode_FSConverter, |
| 3581 | &opath, &argv, &env)) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3582 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3583 | path = bytes2str(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3584 | if (PyList_Check(argv)) { |
| 3585 | argc = PyList_Size(argv); |
| 3586 | getitem = PyList_GetItem; |
| 3587 | } |
| 3588 | else if (PyTuple_Check(argv)) { |
| 3589 | argc = PyTuple_Size(argv); |
| 3590 | getitem = PyTuple_GetItem; |
| 3591 | } |
| 3592 | else { |
| 3593 | PyErr_SetString(PyExc_TypeError, |
| 3594 | "spawnvpe() arg 2 must be a tuple or list"); |
| 3595 | goto fail_0; |
| 3596 | } |
| 3597 | if (!PyMapping_Check(env)) { |
| 3598 | PyErr_SetString(PyExc_TypeError, |
| 3599 | "spawnvpe() arg 3 must be a mapping object"); |
| 3600 | goto fail_0; |
| 3601 | } |
| 3602 | |
| 3603 | argvlist = PyMem_NEW(char *, argc+1); |
| 3604 | if (argvlist == NULL) { |
| 3605 | PyErr_NoMemory(); |
| 3606 | goto fail_0; |
| 3607 | } |
| 3608 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 3610 | &argvlist[i])) |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3611 | { |
| 3612 | lastarg = i; |
| 3613 | goto fail_1; |
| 3614 | } |
| 3615 | } |
| 3616 | lastarg = argc; |
| 3617 | argvlist[argc] = NULL; |
| 3618 | |
| 3619 | i = PyMapping_Size(env); |
| 3620 | if (i < 0) |
| 3621 | goto fail_1; |
| 3622 | envlist = PyMem_NEW(char *, i + 1); |
| 3623 | if (envlist == NULL) { |
| 3624 | PyErr_NoMemory(); |
| 3625 | goto fail_1; |
| 3626 | } |
| 3627 | envc = 0; |
| 3628 | keys = PyMapping_Keys(env); |
| 3629 | vals = PyMapping_Values(env); |
| 3630 | if (!keys || !vals) |
| 3631 | goto fail_2; |
| 3632 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3633 | PyErr_SetString(PyExc_TypeError, |
| 3634 | "spawnvpe(): env.keys() or env.values() is not a list"); |
| 3635 | goto fail_2; |
| 3636 | } |
| 3637 | |
| 3638 | for (pos = 0; pos < i; pos++) { |
| 3639 | char *p, *k, *v; |
| 3640 | size_t len; |
| 3641 | |
| 3642 | key = PyList_GetItem(keys, pos); |
| 3643 | val = PyList_GetItem(vals, pos); |
| 3644 | if (!key || !val) |
| 3645 | goto fail_2; |
| 3646 | |
| 3647 | if (!PyArg_Parse( |
| 3648 | key, |
| 3649 | "s;spawnvpe() arg 3 contains a non-string key", |
| 3650 | &k) || |
| 3651 | !PyArg_Parse( |
| 3652 | val, |
| 3653 | "s;spawnvpe() arg 3 contains a non-string value", |
| 3654 | &v)) |
| 3655 | { |
| 3656 | goto fail_2; |
| 3657 | } |
Christian Heimes | 830a4bc | 2007-11-22 07:43:40 +0000 | [diff] [blame] | 3658 | len = PyUnicode_GetSize(key) + PyUnicode_GetSize(val) + 2; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3659 | p = PyMem_NEW(char, len); |
| 3660 | if (p == NULL) { |
| 3661 | PyErr_NoMemory(); |
| 3662 | goto fail_2; |
| 3663 | } |
| 3664 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3665 | envlist[envc++] = p; |
| 3666 | } |
| 3667 | envlist[envc] = 0; |
| 3668 | |
| 3669 | Py_BEGIN_ALLOW_THREADS |
| 3670 | #if defined(PYCC_GCC) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3671 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3672 | #else |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3673 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3674 | #endif |
| 3675 | Py_END_ALLOW_THREADS |
| 3676 | |
| 3677 | if (spawnval == -1) |
| 3678 | (void) posix_error(); |
| 3679 | else |
| 3680 | res = Py_BuildValue("l", (long) spawnval); |
| 3681 | |
| 3682 | fail_2: |
| 3683 | while (--envc >= 0) |
| 3684 | PyMem_DEL(envlist[envc]); |
| 3685 | PyMem_DEL(envlist); |
| 3686 | fail_1: |
| 3687 | free_string_array(argvlist, lastarg); |
| 3688 | Py_XDECREF(vals); |
| 3689 | Py_XDECREF(keys); |
| 3690 | fail_0: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3691 | release_bytes(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 3692 | return res; |
| 3693 | } |
| 3694 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 3695 | #endif /* HAVE_SPAWNV */ |
| 3696 | |
| 3697 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3698 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3699 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3700 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3701 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 3702 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3703 | 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] | 3704 | |
| 3705 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3706 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3707 | { |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3708 | pid_t pid; |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 3709 | int result = 0; |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3710 | _PyImport_AcquireLock(); |
| 3711 | pid = fork1(); |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 3712 | if (pid == 0) { |
| 3713 | /* child: this clobbers and resets the import lock. */ |
| 3714 | PyOS_AfterFork(); |
| 3715 | } else { |
| 3716 | /* parent: release the import lock. */ |
| 3717 | result = _PyImport_ReleaseLock(); |
| 3718 | } |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3719 | if (pid == -1) |
| 3720 | return posix_error(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3721 | if (result < 0) { |
| 3722 | /* Don't clobber the OSError if the fork failed. */ |
| 3723 | PyErr_SetString(PyExc_RuntimeError, |
| 3724 | "not holding the import lock"); |
| 3725 | return NULL; |
| 3726 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3727 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 3728 | } |
| 3729 | #endif |
| 3730 | |
| 3731 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3732 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3733 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3734 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3735 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3736 | 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] | 3737 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3738 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3739 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3740 | { |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3741 | pid_t pid; |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 3742 | int result = 0; |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3743 | _PyImport_AcquireLock(); |
| 3744 | pid = fork(); |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 3745 | if (pid == 0) { |
| 3746 | /* child: this clobbers and resets the import lock. */ |
| 3747 | PyOS_AfterFork(); |
| 3748 | } else { |
| 3749 | /* parent: release the import lock. */ |
| 3750 | result = _PyImport_ReleaseLock(); |
| 3751 | } |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3752 | if (pid == -1) |
| 3753 | return posix_error(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3754 | if (result < 0) { |
| 3755 | /* Don't clobber the OSError if the fork failed. */ |
| 3756 | PyErr_SetString(PyExc_RuntimeError, |
| 3757 | "not holding the import lock"); |
| 3758 | return NULL; |
| 3759 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3760 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3761 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3762 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3763 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3764 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 3765 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 3766 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3767 | #define DEV_PTY_FILE "/dev/ptc" |
| 3768 | #define HAVE_DEV_PTMX |
| 3769 | #else |
| 3770 | #define DEV_PTY_FILE "/dev/ptmx" |
| 3771 | #endif |
| 3772 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3773 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3774 | #ifdef HAVE_PTY_H |
| 3775 | #include <pty.h> |
| 3776 | #else |
| 3777 | #ifdef HAVE_LIBUTIL_H |
| 3778 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 3779 | #else |
| 3780 | #ifdef HAVE_UTIL_H |
| 3781 | #include <util.h> |
| 3782 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3783 | #endif /* HAVE_LIBUTIL_H */ |
| 3784 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 3785 | #ifdef HAVE_STROPTS_H |
| 3786 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3787 | #endif |
| 3788 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3789 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3790 | #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] | 3791 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3792 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3793 | 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] | 3794 | |
| 3795 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3796 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3797 | { |
| 3798 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3799 | #ifndef HAVE_OPENPTY |
| 3800 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3801 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3802 | #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] | 3803 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3804 | #ifdef sun |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3805 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3806 | #endif |
| 3807 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3808 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3809 | #ifdef HAVE_OPENPTY |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3810 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 3811 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3812 | #elif defined(HAVE__GETPTY) |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3813 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 3814 | if (slave_name == NULL) |
| 3815 | return posix_error(); |
| 3816 | |
| 3817 | slave_fd = open(slave_name, O_RDWR); |
| 3818 | if (slave_fd < 0) |
| 3819 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3820 | #else |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3821 | 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] | 3822 | if (master_fd < 0) |
| 3823 | return posix_error(); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3824 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3825 | /* change permission of slave */ |
| 3826 | if (grantpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3827 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3828 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3829 | } |
| 3830 | /* unlock slave */ |
| 3831 | if (unlockpt(master_fd) < 0) { |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3832 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3833 | return posix_error(); |
Martin v. Löwis | c8b2e77 | 2002-12-31 14:30:26 +0000 | [diff] [blame] | 3834 | } |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 3835 | PyOS_setsig(SIGCHLD, sig_saved); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3836 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 3837 | if (slave_name == NULL) |
| 3838 | return posix_error(); |
| 3839 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 3840 | if (slave_fd < 0) |
| 3841 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 3842 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3843 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 3844 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3845 | #ifndef __hpux |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3846 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 3847 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3848 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3849 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3850 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3851 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 3852 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3853 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 3854 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3855 | |
| 3856 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3857 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3858 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3859 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 3860 | 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] | 3861 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3862 | |
| 3863 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3864 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3865 | { |
Gregory P. Smith | 2a1c027 | 2010-03-01 17:04:45 +0000 | [diff] [blame] | 3866 | int master_fd = -1, result = 0; |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 3867 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3868 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3869 | _PyImport_AcquireLock(); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3870 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
Gregory P. Smith | 2a1c027 | 2010-03-01 17:04:45 +0000 | [diff] [blame] | 3871 | if (pid == 0) { |
| 3872 | /* child: this clobbers and resets the import lock. */ |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 3873 | PyOS_AfterFork(); |
Gregory P. Smith | 2a1c027 | 2010-03-01 17:04:45 +0000 | [diff] [blame] | 3874 | } else { |
| 3875 | /* parent: release the import lock. */ |
| 3876 | result = _PyImport_ReleaseLock(); |
| 3877 | } |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3878 | if (pid == -1) |
| 3879 | return posix_error(); |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 3880 | if (result < 0) { |
| 3881 | /* Don't clobber the OSError if the fork failed. */ |
| 3882 | PyErr_SetString(PyExc_RuntimeError, |
| 3883 | "not holding the import lock"); |
| 3884 | return NULL; |
| 3885 | } |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 3886 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 3887 | } |
| 3888 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3889 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3890 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3891 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3892 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3893 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3894 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3895 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3896 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3897 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3898 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3899 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3900 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3902 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3903 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3904 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3905 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3906 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3907 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3908 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3909 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3910 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3911 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3912 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3913 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3914 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3915 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3916 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3917 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3918 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3919 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3920 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3921 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3922 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3923 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3924 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3925 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 3926 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 3927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3929 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3930 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3931 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3933 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3934 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3935 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 3936 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3939 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3940 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3941 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3942 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3943 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3944 | |
| 3945 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3946 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3947 | { |
| 3948 | PyObject *result = NULL; |
| 3949 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3950 | #ifdef NGROUPS_MAX |
| 3951 | #define MAX_GROUPS NGROUPS_MAX |
| 3952 | #else |
| 3953 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 3954 | #define MAX_GROUPS 64 |
| 3955 | #endif |
| 3956 | gid_t grouplist[MAX_GROUPS]; |
| 3957 | int n; |
| 3958 | |
| 3959 | n = getgroups(MAX_GROUPS, grouplist); |
| 3960 | if (n < 0) |
| 3961 | posix_error(); |
| 3962 | else { |
| 3963 | result = PyList_New(n); |
| 3964 | if (result != NULL) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3965 | int i; |
| 3966 | for (i = 0; i < n; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3967 | PyObject *o = PyLong_FromLong((long)grouplist[i]); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3968 | if (o == NULL) { |
| 3969 | Py_DECREF(result); |
| 3970 | result = NULL; |
| 3971 | break; |
| 3972 | } |
| 3973 | PyList_SET_ITEM(result, i, o); |
| 3974 | } |
| 3975 | } |
| 3976 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3977 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 3978 | return result; |
| 3979 | } |
| 3980 | #endif |
| 3981 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 3982 | #ifdef HAVE_INITGROUPS |
| 3983 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 3984 | "initgroups(username, gid) -> None\n\n\ |
| 3985 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 3986 | the groups of which the specified username is a member, plus the specified\n\ |
| 3987 | group id."); |
| 3988 | |
| 3989 | static PyObject * |
| 3990 | posix_initgroups(PyObject *self, PyObject *args) |
| 3991 | { |
| 3992 | char *username; |
| 3993 | long gid; |
| 3994 | |
| 3995 | if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid)) |
| 3996 | return NULL; |
| 3997 | |
| 3998 | if (initgroups(username, (gid_t) gid) == -1) |
| 3999 | return PyErr_SetFromErrno(PyExc_OSError); |
| 4000 | |
| 4001 | Py_INCREF(Py_None); |
| 4002 | return Py_None; |
| 4003 | } |
| 4004 | #endif |
| 4005 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4006 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4007 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4008 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 4009 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4010 | |
| 4011 | static PyObject * |
| 4012 | posix_getpgid(PyObject *self, PyObject *args) |
| 4013 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4014 | pid_t pid, pgid; |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4015 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4016 | return NULL; |
| 4017 | pgid = getpgid(pid); |
| 4018 | if (pgid < 0) |
| 4019 | return posix_error(); |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4020 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 4021 | } |
| 4022 | #endif /* HAVE_GETPGID */ |
| 4023 | |
| 4024 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4025 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4026 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4027 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4028 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4029 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4030 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4031 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4032 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4033 | #ifdef GETPGRP_HAVE_ARG |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4034 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4035 | #else /* GETPGRP_HAVE_ARG */ |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4036 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4037 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4038 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4039 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 4040 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4041 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4042 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4043 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4044 | "setpgrp()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4045 | Make this process a session leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4046 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4047 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4048 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4049 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4050 | #ifdef SETPGRP_HAVE_ARG |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4051 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4052 | #else /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4053 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 4054 | #endif /* SETPGRP_HAVE_ARG */ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4055 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4056 | Py_INCREF(Py_None); |
| 4057 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4060 | #endif /* HAVE_SETPGRP */ |
| 4061 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4062 | #ifdef HAVE_GETPPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4063 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4064 | "getppid() -> ppid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4065 | Return the parent's process id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4066 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4067 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4068 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4069 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4070 | return PyLong_FromPid(getppid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4071 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4072 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4073 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4074 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4075 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4076 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4077 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4078 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4079 | |
| 4080 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4081 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4082 | { |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4083 | PyObject *result = NULL; |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4084 | char *name; |
| 4085 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4086 | |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4087 | errno = 0; |
| 4088 | name = getlogin(); |
| 4089 | if (name == NULL) { |
| 4090 | if (errno) |
| 4091 | posix_error(); |
| 4092 | else |
| 4093 | PyErr_SetString(PyExc_OSError, |
Fred Drake | e63544f | 2000-12-06 21:45:33 +0000 | [diff] [blame] | 4094 | "unable to determine login name"); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4095 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4096 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 4097 | result = PyUnicode_FromString(name); |
Fred Drake | a30680b | 2000-12-06 21:24:28 +0000 | [diff] [blame] | 4098 | errno = old_errno; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4099 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 4100 | return result; |
| 4101 | } |
| 4102 | #endif |
| 4103 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4104 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4105 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4106 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4107 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4109 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4110 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4111 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4112 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4113 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4114 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 4115 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4116 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4117 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4118 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4119 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4120 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4121 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4122 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4123 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4124 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4125 | pid_t pid; |
| 4126 | int sig; |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4127 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4128 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4129 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4130 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 4131 | APIRET rc; |
| 4132 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4133 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4134 | |
| 4135 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 4136 | APIRET rc; |
| 4137 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4138 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4139 | |
| 4140 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 4141 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4142 | #else |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4143 | if (kill(pid, sig) == -1) |
| 4144 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4145 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4146 | Py_INCREF(Py_None); |
| 4147 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4148 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4149 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4150 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4151 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4152 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4153 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4154 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4155 | |
| 4156 | static PyObject * |
| 4157 | posix_killpg(PyObject *self, PyObject *args) |
| 4158 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4159 | int sig; |
| 4160 | pid_t pgid; |
| 4161 | /* XXX some man pages make the `pgid` parameter an int, others |
| 4162 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 4163 | take the same type. Moreover, pid_t is always at least as wide as |
| 4164 | int (else compilation of this module fails), which is safe. */ |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4165 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 4166 | return NULL; |
| 4167 | if (killpg(pgid, sig) == -1) |
| 4168 | return posix_error(); |
| 4169 | Py_INCREF(Py_None); |
| 4170 | return Py_None; |
| 4171 | } |
| 4172 | #endif |
| 4173 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 4174 | #ifdef MS_WINDOWS |
| 4175 | PyDoc_STRVAR(win32_kill__doc__, |
| 4176 | "kill(pid, sig)\n\n\ |
| 4177 | Kill a process with a signal."); |
| 4178 | |
| 4179 | static PyObject * |
| 4180 | win32_kill(PyObject *self, PyObject *args) |
| 4181 | { |
| 4182 | PyObject *result, handle_obj; |
| 4183 | DWORD pid, sig, err; |
| 4184 | HANDLE handle; |
| 4185 | |
| 4186 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 4187 | return NULL; |
| 4188 | |
| 4189 | /* Console processes which share a common console can be sent CTRL+C or |
| 4190 | CTRL+BREAK events, provided they handle said events. */ |
| 4191 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 4192 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 4193 | err = GetLastError(); |
| 4194 | PyErr_SetFromWindowsErr(err); |
| 4195 | } |
| 4196 | else |
| 4197 | Py_RETURN_NONE; |
| 4198 | } |
| 4199 | |
| 4200 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 4201 | attempt to open and terminate the process. */ |
| 4202 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 4203 | if (handle == NULL) { |
| 4204 | err = GetLastError(); |
| 4205 | return PyErr_SetFromWindowsErr(err); |
| 4206 | } |
| 4207 | |
| 4208 | if (TerminateProcess(handle, sig) == 0) { |
| 4209 | err = GetLastError(); |
| 4210 | result = PyErr_SetFromWindowsErr(err); |
| 4211 | } else { |
| 4212 | Py_INCREF(Py_None); |
| 4213 | result = Py_None; |
| 4214 | } |
| 4215 | |
| 4216 | CloseHandle(handle); |
| 4217 | return result; |
| 4218 | } |
| 4219 | #endif /* MS_WINDOWS */ |
| 4220 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4221 | #ifdef HAVE_PLOCK |
| 4222 | |
| 4223 | #ifdef HAVE_SYS_LOCK_H |
| 4224 | #include <sys/lock.h> |
| 4225 | #endif |
| 4226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4227 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4228 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4229 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4230 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4231 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4232 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4233 | { |
| 4234 | int op; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4235 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4236 | return NULL; |
| 4237 | if (plock(op) == -1) |
| 4238 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4239 | Py_INCREF(Py_None); |
| 4240 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 4241 | } |
| 4242 | #endif |
| 4243 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4244 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4245 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4246 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4247 | Set the current process's user id."); |
| 4248 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4249 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4250 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4251 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4252 | long uid_arg; |
| 4253 | uid_t uid; |
| 4254 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4255 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4256 | uid = uid_arg; |
| 4257 | if (uid != uid_arg) { |
| 4258 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4259 | return NULL; |
| 4260 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4261 | if (setuid(uid) < 0) |
| 4262 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4263 | Py_INCREF(Py_None); |
| 4264 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4265 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4266 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4267 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4268 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4269 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4270 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4271 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4272 | Set the current process's effective user id."); |
| 4273 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4274 | static PyObject * |
| 4275 | posix_seteuid (PyObject *self, PyObject *args) |
| 4276 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4277 | long euid_arg; |
| 4278 | uid_t euid; |
| 4279 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4280 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4281 | euid = euid_arg; |
| 4282 | if (euid != euid_arg) { |
| 4283 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4284 | return NULL; |
| 4285 | } |
| 4286 | if (seteuid(euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4287 | return posix_error(); |
| 4288 | } else { |
| 4289 | Py_INCREF(Py_None); |
| 4290 | return Py_None; |
| 4291 | } |
| 4292 | } |
| 4293 | #endif /* HAVE_SETEUID */ |
| 4294 | |
| 4295 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4296 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4297 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4298 | Set the current process's effective group id."); |
| 4299 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4300 | static PyObject * |
| 4301 | posix_setegid (PyObject *self, PyObject *args) |
| 4302 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4303 | long egid_arg; |
| 4304 | gid_t egid; |
| 4305 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4306 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4307 | egid = egid_arg; |
| 4308 | if (egid != egid_arg) { |
| 4309 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4310 | return NULL; |
| 4311 | } |
| 4312 | if (setegid(egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4313 | return posix_error(); |
| 4314 | } else { |
| 4315 | Py_INCREF(Py_None); |
| 4316 | return Py_None; |
| 4317 | } |
| 4318 | } |
| 4319 | #endif /* HAVE_SETEGID */ |
| 4320 | |
| 4321 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4322 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4323 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4324 | Set the current process's real and effective user ids."); |
| 4325 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4326 | static PyObject * |
| 4327 | posix_setreuid (PyObject *self, PyObject *args) |
| 4328 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4329 | long ruid_arg, euid_arg; |
| 4330 | uid_t ruid, euid; |
| 4331 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4332 | return NULL; |
Gregory P. Smith | c78d79c | 2010-03-01 05:54:14 +0000 | [diff] [blame] | 4333 | if (ruid_arg == -1) |
| 4334 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 4335 | else |
| 4336 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 4337 | if (euid_arg == -1) |
| 4338 | euid = (uid_t)-1; |
| 4339 | else |
| 4340 | euid = euid_arg; |
| 4341 | if ((euid_arg != -1 && euid != euid_arg) || |
| 4342 | (ruid_arg != -1 && ruid != ruid_arg)) { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4343 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 4344 | return NULL; |
| 4345 | } |
| 4346 | if (setreuid(ruid, euid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4347 | return posix_error(); |
| 4348 | } else { |
| 4349 | Py_INCREF(Py_None); |
| 4350 | return Py_None; |
| 4351 | } |
| 4352 | } |
| 4353 | #endif /* HAVE_SETREUID */ |
| 4354 | |
| 4355 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4356 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 4357 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4358 | Set the current process's real and effective group ids."); |
| 4359 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4360 | static PyObject * |
| 4361 | posix_setregid (PyObject *self, PyObject *args) |
| 4362 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4363 | long rgid_arg, egid_arg; |
| 4364 | gid_t rgid, egid; |
| 4365 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4366 | return NULL; |
Gregory P. Smith | c78d79c | 2010-03-01 05:54:14 +0000 | [diff] [blame] | 4367 | if (rgid_arg == -1) |
| 4368 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 4369 | else |
| 4370 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 4371 | if (egid_arg == -1) |
| 4372 | egid = (gid_t)-1; |
| 4373 | else |
| 4374 | egid = egid_arg; |
| 4375 | if ((egid_arg != -1 && egid != egid_arg) || |
| 4376 | (rgid_arg != -1 && rgid != rgid_arg)) { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4377 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4378 | return NULL; |
| 4379 | } |
| 4380 | if (setregid(rgid, egid) < 0) { |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 4381 | return posix_error(); |
| 4382 | } else { |
| 4383 | Py_INCREF(Py_None); |
| 4384 | return Py_None; |
| 4385 | } |
| 4386 | } |
| 4387 | #endif /* HAVE_SETREGID */ |
| 4388 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4389 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4390 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4391 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4392 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4393 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4394 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4395 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4396 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4397 | long gid_arg; |
| 4398 | gid_t gid; |
| 4399 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4400 | return NULL; |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 4401 | gid = gid_arg; |
| 4402 | if (gid != gid_arg) { |
| 4403 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 4404 | return NULL; |
| 4405 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4406 | if (setgid(gid) < 0) |
| 4407 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4408 | Py_INCREF(Py_None); |
| 4409 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4410 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4411 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 4412 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4413 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4414 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4415 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4416 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4417 | |
| 4418 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4419 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4420 | { |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4421 | int i, len; |
| 4422 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4423 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4424 | if (!PySequence_Check(groups)) { |
| 4425 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 4426 | return NULL; |
| 4427 | } |
| 4428 | len = PySequence_Size(groups); |
| 4429 | if (len > MAX_GROUPS) { |
| 4430 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 4431 | return NULL; |
| 4432 | } |
| 4433 | for(i = 0; i < len; i++) { |
| 4434 | PyObject *elem; |
| 4435 | elem = PySequence_GetItem(groups, i); |
| 4436 | if (!elem) |
| 4437 | return NULL; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4438 | if (!PyLong_Check(elem)) { |
| 4439 | PyErr_SetString(PyExc_TypeError, |
| 4440 | "groups must be integers"); |
| 4441 | Py_DECREF(elem); |
| 4442 | return NULL; |
| 4443 | } else { |
| 4444 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 4445 | if (PyErr_Occurred()) { |
| 4446 | PyErr_SetString(PyExc_TypeError, |
| 4447 | "group id too big"); |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4448 | Py_DECREF(elem); |
| 4449 | return NULL; |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4450 | } |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4451 | grouplist[i] = x; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4452 | /* read back the value to see if it fitted in gid_t */ |
Georg Brandl | a13c244 | 2005-11-22 19:30:31 +0000 | [diff] [blame] | 4453 | if (grouplist[i] != x) { |
| 4454 | PyErr_SetString(PyExc_TypeError, |
| 4455 | "group id too big"); |
| 4456 | Py_DECREF(elem); |
| 4457 | return NULL; |
| 4458 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4459 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 4460 | Py_DECREF(elem); |
| 4461 | } |
| 4462 | |
| 4463 | if (setgroups(len, grouplist) < 0) |
| 4464 | return posix_error(); |
| 4465 | Py_INCREF(Py_None); |
| 4466 | return Py_None; |
| 4467 | } |
| 4468 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4469 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4470 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 4471 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4472 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4473 | { |
| 4474 | PyObject *result; |
| 4475 | static PyObject *struct_rusage; |
| 4476 | |
| 4477 | if (pid == -1) |
| 4478 | return posix_error(); |
| 4479 | |
| 4480 | if (struct_rusage == NULL) { |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 4481 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4482 | if (m == NULL) |
| 4483 | return NULL; |
| 4484 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 4485 | Py_DECREF(m); |
| 4486 | if (struct_rusage == NULL) |
| 4487 | return NULL; |
| 4488 | } |
| 4489 | |
| 4490 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 4491 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 4492 | if (!result) |
| 4493 | return NULL; |
| 4494 | |
| 4495 | #ifndef doubletime |
| 4496 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 4497 | #endif |
| 4498 | |
| 4499 | PyStructSequence_SET_ITEM(result, 0, |
| 4500 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 4501 | PyStructSequence_SET_ITEM(result, 1, |
| 4502 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
| 4503 | #define SET_INT(result, index, value)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4504 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4505 | SET_INT(result, 2, ru->ru_maxrss); |
| 4506 | SET_INT(result, 3, ru->ru_ixrss); |
| 4507 | SET_INT(result, 4, ru->ru_idrss); |
| 4508 | SET_INT(result, 5, ru->ru_isrss); |
| 4509 | SET_INT(result, 6, ru->ru_minflt); |
| 4510 | SET_INT(result, 7, ru->ru_majflt); |
| 4511 | SET_INT(result, 8, ru->ru_nswap); |
| 4512 | SET_INT(result, 9, ru->ru_inblock); |
| 4513 | SET_INT(result, 10, ru->ru_oublock); |
| 4514 | SET_INT(result, 11, ru->ru_msgsnd); |
| 4515 | SET_INT(result, 12, ru->ru_msgrcv); |
| 4516 | SET_INT(result, 13, ru->ru_nsignals); |
| 4517 | SET_INT(result, 14, ru->ru_nvcsw); |
| 4518 | SET_INT(result, 15, ru->ru_nivcsw); |
| 4519 | #undef SET_INT |
| 4520 | |
| 4521 | if (PyErr_Occurred()) { |
| 4522 | Py_DECREF(result); |
| 4523 | return NULL; |
| 4524 | } |
| 4525 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4526 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4527 | } |
| 4528 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 4529 | |
| 4530 | #ifdef HAVE_WAIT3 |
| 4531 | PyDoc_STRVAR(posix_wait3__doc__, |
| 4532 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 4533 | Wait for completion of a child process."); |
| 4534 | |
| 4535 | static PyObject * |
| 4536 | posix_wait3(PyObject *self, PyObject *args) |
| 4537 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4538 | pid_t pid; |
| 4539 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4540 | struct rusage ru; |
| 4541 | WAIT_TYPE status; |
| 4542 | WAIT_STATUS_INT(status) = 0; |
| 4543 | |
| 4544 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 4545 | return NULL; |
| 4546 | |
| 4547 | Py_BEGIN_ALLOW_THREADS |
| 4548 | pid = wait3(&status, options, &ru); |
| 4549 | Py_END_ALLOW_THREADS |
| 4550 | |
| 4551 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4552 | } |
| 4553 | #endif /* HAVE_WAIT3 */ |
| 4554 | |
| 4555 | #ifdef HAVE_WAIT4 |
| 4556 | PyDoc_STRVAR(posix_wait4__doc__, |
| 4557 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 4558 | Wait for completion of a given child process."); |
| 4559 | |
| 4560 | static PyObject * |
| 4561 | posix_wait4(PyObject *self, PyObject *args) |
| 4562 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4563 | pid_t pid; |
| 4564 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4565 | struct rusage ru; |
| 4566 | WAIT_TYPE status; |
| 4567 | WAIT_STATUS_INT(status) = 0; |
| 4568 | |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4569 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4570 | return NULL; |
| 4571 | |
| 4572 | Py_BEGIN_ALLOW_THREADS |
| 4573 | pid = wait4(pid, &status, options, &ru); |
| 4574 | Py_END_ALLOW_THREADS |
| 4575 | |
| 4576 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
| 4577 | } |
| 4578 | #endif /* HAVE_WAIT4 */ |
| 4579 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4580 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4581 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4582 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4583 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4584 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4585 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4586 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4587 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4588 | pid_t pid; |
| 4589 | int options; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4590 | WAIT_TYPE status; |
| 4591 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4592 | |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4593 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4594 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4595 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e6a3aa6 | 1999-02-01 16:15:30 +0000 | [diff] [blame] | 4596 | pid = waitpid(pid, &status, options); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4597 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4598 | if (pid == -1) |
| 4599 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4600 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4601 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4604 | #elif defined(HAVE_CWAIT) |
| 4605 | |
| 4606 | /* 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] | 4607 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4608 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4609 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4610 | |
| 4611 | static PyObject * |
| 4612 | posix_waitpid(PyObject *self, PyObject *args) |
| 4613 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4614 | Py_intptr_t pid; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4616 | |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4617 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4618 | return NULL; |
| 4619 | Py_BEGIN_ALLOW_THREADS |
| 4620 | pid = _cwait(&status, pid, options); |
| 4621 | Py_END_ALLOW_THREADS |
| 4622 | if (pid == -1) |
| 4623 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4624 | |
| 4625 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4626 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 4627 | } |
| 4628 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4629 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4630 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4631 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4632 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4635 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4636 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4637 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4638 | pid_t pid; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4639 | WAIT_TYPE status; |
| 4640 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4641 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 4642 | Py_BEGIN_ALLOW_THREADS |
| 4643 | pid = wait(&status); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4644 | Py_END_ALLOW_THREADS |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 4645 | if (pid == -1) |
| 4646 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4647 | |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4648 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4649 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4650 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4651 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4653 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4654 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4655 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4656 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4657 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4658 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4659 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4660 | #ifdef HAVE_LSTAT |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4661 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4662 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4663 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4664 | return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4665 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4666 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4667 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4668 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4671 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4672 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4673 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4674 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4675 | 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] | 4676 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4677 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4678 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4679 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4680 | PyObject* v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4681 | char buf[MAXPATHLEN]; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4682 | PyObject *opath; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 4683 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4684 | int n; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4685 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4686 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4687 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 4688 | PyUnicode_FSConverter, &opath)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4689 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4690 | path = bytes2str(opath, 1); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4691 | v = PySequence_GetItem(args, 0); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4692 | if (v == NULL) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4693 | release_bytes(opath); |
Neal Norwitz | cda5c06 | 2007-08-12 17:09:36 +0000 | [diff] [blame] | 4694 | return NULL; |
| 4695 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4696 | |
| 4697 | if (PyUnicode_Check(v)) { |
| 4698 | arg_is_unicode = 1; |
| 4699 | } |
| 4700 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4702 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 50e61dc | 1992-03-27 17:22:31 +0000 | [diff] [blame] | 4703 | n = readlink(path, buf, (int) sizeof buf); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4704 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4705 | if (n < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4706 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4707 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4708 | release_bytes(opath); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4709 | v = PyBytes_FromStringAndSize(buf, n); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4710 | if (arg_is_unicode) { |
| 4711 | PyObject *w; |
| 4712 | |
| 4713 | w = PyUnicode_FromEncodedObject(v, |
| 4714 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 4715 | "surrogateescape"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4716 | if (w != NULL) { |
| 4717 | Py_DECREF(v); |
| 4718 | v = w; |
| 4719 | } |
| 4720 | else { |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4721 | v = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4724 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4725 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4726 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4727 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4728 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4729 | #ifdef HAVE_SYMLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4730 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4731 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 4732 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4733 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4734 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4735 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4736 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4737 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4738 | } |
| 4739 | #endif /* HAVE_SYMLINK */ |
| 4740 | |
| 4741 | |
| 4742 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4743 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 4744 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 4745 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4746 | { |
| 4747 | ULONG value = 0; |
| 4748 | |
| 4749 | Py_BEGIN_ALLOW_THREADS |
| 4750 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 4751 | Py_END_ALLOW_THREADS |
| 4752 | |
| 4753 | return value; |
| 4754 | } |
| 4755 | |
| 4756 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4757 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4758 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 4759 | /* Currently Only Uptime is Provided -- Others Later */ |
| 4760 | return Py_BuildValue("ddddd", |
| 4761 | (double)0 /* t.tms_utime / HZ */, |
| 4762 | (double)0 /* t.tms_stime / HZ */, |
| 4763 | (double)0 /* t.tms_cutime / HZ */, |
| 4764 | (double)0 /* t.tms_cstime / HZ */, |
| 4765 | (double)system_uptime() / 1000); |
| 4766 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4767 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4768 | #define NEED_TICKS_PER_SECOND |
| 4769 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4770 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4771 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4772 | { |
| 4773 | struct tms t; |
| 4774 | clock_t c; |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4775 | errno = 0; |
| 4776 | c = times(&t); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4777 | if (c == (clock_t) -1) |
| 4778 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4779 | return Py_BuildValue("ddddd", |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 4780 | (double)t.tms_utime / ticks_per_second, |
| 4781 | (double)t.tms_stime / ticks_per_second, |
| 4782 | (double)t.tms_cutime / ticks_per_second, |
| 4783 | (double)t.tms_cstime / ticks_per_second, |
| 4784 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4785 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4786 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4787 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4788 | |
| 4789 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4790 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4791 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4792 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4793 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4794 | { |
| 4795 | FILETIME create, exit, kernel, user; |
| 4796 | HANDLE hProc; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4797 | hProc = GetCurrentProcess(); |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4798 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 4799 | /* The fields of a FILETIME structure are the hi and lo part |
| 4800 | of a 64-bit value expressed in 100 nanosecond units. |
| 4801 | 1e7 is one second in such units; 1e-7 the inverse. |
| 4802 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 4803 | */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4804 | return Py_BuildValue( |
| 4805 | "ddddd", |
Guido van Rossum | b8c3cbd | 1999-02-16 14:37:28 +0000 | [diff] [blame] | 4806 | (double)(user.dwHighDateTime*429.4967296 + |
| 4807 | user.dwLowDateTime*1e-7), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 4808 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 4809 | kernel.dwLowDateTime*1e-7), |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4810 | (double)0, |
| 4811 | (double)0, |
| 4812 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 4813 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4814 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4815 | |
| 4816 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4817 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4818 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4819 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 4820 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4821 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4822 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4823 | #ifdef HAVE_GETSID |
| 4824 | PyDoc_STRVAR(posix_getsid__doc__, |
| 4825 | "getsid(pid) -> sid\n\n\ |
| 4826 | Call the system call getsid()."); |
| 4827 | |
| 4828 | static PyObject * |
| 4829 | posix_getsid(PyObject *self, PyObject *args) |
| 4830 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4831 | pid_t pid; |
| 4832 | int sid; |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4833 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4834 | return NULL; |
| 4835 | sid = getsid(pid); |
| 4836 | if (sid < 0) |
| 4837 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4838 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 4839 | } |
| 4840 | #endif /* HAVE_GETSID */ |
| 4841 | |
| 4842 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4843 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4844 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4845 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4846 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4847 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4848 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4849 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4850 | { |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4851 | if (setsid() < 0) |
| 4852 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4853 | Py_INCREF(Py_None); |
| 4854 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4855 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4856 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4857 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4858 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4859 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4860 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4861 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4862 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4864 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4865 | { |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 4866 | pid_t pid; |
| 4867 | int pgrp; |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4868 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4869 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4870 | if (setpgid(pid, pgrp) < 0) |
| 4871 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4872 | Py_INCREF(Py_None); |
| 4873 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4874 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4875 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 4876 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4877 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4878 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4879 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4880 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4881 | 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] | 4882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4883 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4884 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4885 | { |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 4886 | int fd; |
| 4887 | pid_t pgid; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4888 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4889 | return NULL; |
| 4890 | pgid = tcgetpgrp(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4891 | if (pgid < 0) |
| 4892 | return posix_error(); |
Antoine Pitrou | c3ee166 | 2009-05-23 16:02:33 +0000 | [diff] [blame] | 4893 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4894 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4895 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4896 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4897 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4898 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4899 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4900 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4901 | 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] | 4902 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4903 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4904 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4905 | { |
Antoine Pitrou | 971e51b | 2009-05-23 16:14:27 +0000 | [diff] [blame] | 4906 | int fd; |
| 4907 | pid_t pgid; |
Gregory P. Smith | 8450857 | 2010-03-14 18:56:11 +0000 | [diff] [blame] | 4908 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4909 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4910 | if (tcsetpgrp(fd, pgid) < 0) |
| 4911 | return posix_error(); |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4912 | Py_INCREF(Py_None); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4913 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 4914 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4915 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 4916 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4917 | /* Functions acting on file descriptors */ |
| 4918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4919 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4920 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4921 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4922 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4923 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4924 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4925 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4926 | PyObject *ofile; |
| 4927 | char *file; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4928 | int flag; |
| 4929 | int mode = 0777; |
| 4930 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4931 | |
| 4932 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4933 | PyUnicodeObject *po; |
| 4934 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { |
| 4935 | Py_BEGIN_ALLOW_THREADS |
| 4936 | /* PyUnicode_AS_UNICODE OK without thread |
| 4937 | lock as it is a simple dereference. */ |
| 4938 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 4939 | Py_END_ALLOW_THREADS |
| 4940 | if (fd < 0) |
| 4941 | return posix_error(); |
| 4942 | return PyLong_FromLong((long)fd); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4943 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4944 | /* Drop the argument parsing error as narrow strings |
| 4945 | are also valid. */ |
| 4946 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4947 | #endif |
| 4948 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4949 | if (!PyArg_ParseTuple(args, "O&i|i", |
| 4950 | PyUnicode_FSConverter, &ofile, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4951 | &flag, &mode)) |
Barry Warsaw | 43d68b8 | 1996-12-19 22:10:44 +0000 | [diff] [blame] | 4952 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4953 | file = bytes2str(ofile, 1); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4954 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4955 | fd = open(file, flag, mode); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4956 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4957 | if (fd < 0) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4958 | return posix_error_with_allocated_filename(ofile); |
| 4959 | release_bytes(ofile); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4960 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4964 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4965 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4966 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4968 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4969 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4970 | { |
| 4971 | int fd, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 4972 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4973 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4974 | if (!_PyVerify_fd(fd)) |
| 4975 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4976 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4977 | res = close(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4978 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4979 | if (res < 0) |
| 4980 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4981 | Py_INCREF(Py_None); |
| 4982 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4985 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 4986 | PyDoc_STRVAR(posix_closerange__doc__, |
| 4987 | "closerange(fd_low, fd_high)\n\n\ |
| 4988 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 4989 | |
| 4990 | static PyObject * |
| 4991 | posix_closerange(PyObject *self, PyObject *args) |
| 4992 | { |
| 4993 | int fd_from, fd_to, i; |
| 4994 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 4995 | return NULL; |
| 4996 | Py_BEGIN_ALLOW_THREADS |
| 4997 | for (i = fd_from; i < fd_to; i++) |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 4998 | if (_PyVerify_fd(i)) |
| 4999 | close(i); |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 5000 | Py_END_ALLOW_THREADS |
| 5001 | Py_RETURN_NONE; |
| 5002 | } |
| 5003 | |
| 5004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5005 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5006 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5007 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5009 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5010 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5011 | { |
| 5012 | int fd; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5013 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5014 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5015 | if (!_PyVerify_fd(fd)) |
| 5016 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5017 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5018 | fd = dup(fd); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5019 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5020 | if (fd < 0) |
| 5021 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5022 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5026 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 5027 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5028 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5029 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5030 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5031 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5032 | { |
| 5033 | int fd, fd2, res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5034 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5035 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5036 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 5037 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5038 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5039 | res = dup2(fd, fd2); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5040 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5041 | if (res < 0) |
| 5042 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5043 | Py_INCREF(Py_None); |
| 5044 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5048 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5049 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5050 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5051 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5052 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5053 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5054 | { |
| 5055 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5056 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5057 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5058 | #else |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5059 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5060 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5061 | PyObject *posobj; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5062 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5063 | return NULL; |
| 5064 | #ifdef SEEK_SET |
| 5065 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 5066 | switch (how) { |
| 5067 | case 0: how = SEEK_SET; break; |
| 5068 | case 1: how = SEEK_CUR; break; |
| 5069 | case 2: how = SEEK_END; break; |
| 5070 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5071 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5072 | |
| 5073 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5074 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5075 | #else |
| 5076 | pos = PyLong_Check(posobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5077 | PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5078 | #endif |
| 5079 | if (PyErr_Occurred()) |
| 5080 | return NULL; |
| 5081 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5082 | if (!_PyVerify_fd(fd)) |
| 5083 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5084 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5085 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5086 | res = _lseeki64(fd, pos, how); |
| 5087 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5088 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5089 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5090 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5091 | if (res < 0) |
| 5092 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5093 | |
| 5094 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5095 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5096 | #else |
| 5097 | return PyLong_FromLongLong(res); |
| 5098 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5102 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5103 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5104 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5105 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5107 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5108 | { |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 5109 | int fd, size; |
| 5110 | Py_ssize_t n; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5111 | PyObject *buffer; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5112 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5113 | return NULL; |
Michael W. Hudson | 9867ced | 2005-01-31 17:01:59 +0000 | [diff] [blame] | 5114 | if (size < 0) { |
| 5115 | errno = EINVAL; |
| 5116 | return posix_error(); |
| 5117 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5118 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5119 | if (buffer == NULL) |
| 5120 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5121 | if (!_PyVerify_fd(fd)) |
| 5122 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5123 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5124 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5125 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5126 | if (n < 0) { |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5127 | Py_DECREF(buffer); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5128 | return posix_error(); |
| 5129 | } |
Guido van Rossum | 8bac546 | 1996-06-11 18:38:48 +0000 | [diff] [blame] | 5130 | if (n != size) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5131 | _PyBytes_Resize(&buffer, n); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5132 | return buffer; |
| 5133 | } |
| 5134 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5136 | PyDoc_STRVAR(posix_write__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5137 | "write(fd, string) -> byteswritten\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5138 | Write a string to a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5140 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5141 | posix_write(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5142 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5143 | Py_buffer pbuf; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5144 | int fd; |
| 5145 | Py_ssize_t size; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 5146 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 5147 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5148 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5149 | if (!_PyVerify_fd(fd)) |
| 5150 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5151 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5152 | size = write(fd, pbuf.buf, (size_t)pbuf.len); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5153 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 5154 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5155 | if (size < 0) |
| 5156 | return posix_error(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5157 | return PyLong_FromSsize_t(size); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5160 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5161 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5162 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5163 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5164 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5165 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5166 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5167 | { |
| 5168 | int fd; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5169 | STRUCT_STAT st; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5170 | int res; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5171 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5172 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 5173 | #ifdef __VMS |
| 5174 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 5175 | fsync(fd); |
| 5176 | #endif |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5177 | if (!_PyVerify_fd(fd)) |
| 5178 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5179 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5180 | res = FSTAT(fd, &st); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5181 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5182 | if (res != 0) { |
| 5183 | #ifdef MS_WINDOWS |
| 5184 | return win32_error("fstat", NULL); |
| 5185 | #else |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5186 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5187 | #endif |
| 5188 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5189 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 5190 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5193 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5194 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5195 | 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] | 5196 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5197 | |
| 5198 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 5199 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5200 | { |
| 5201 | int fd; |
| 5202 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 5203 | return NULL; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 5204 | if (!_PyVerify_fd(fd)) |
| 5205 | return PyBool_FromLong(0); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5206 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 5207 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5208 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5209 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5210 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5211 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5212 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5213 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5214 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5215 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5216 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5217 | #if defined(PYOS_OS2) |
| 5218 | HFILE read, write; |
| 5219 | APIRET rc; |
| 5220 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5221 | Py_BEGIN_ALLOW_THREADS |
| 5222 | rc = DosCreatePipe( &read, &write, 4096); |
| 5223 | Py_END_ALLOW_THREADS |
| 5224 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5225 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5226 | |
| 5227 | return Py_BuildValue("(ii)", read, write); |
| 5228 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5229 | #if !defined(MS_WINDOWS) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5230 | int fds[2]; |
| 5231 | int res; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5232 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5233 | res = pipe(fds); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5234 | Py_END_ALLOW_THREADS |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5235 | if (res != 0) |
| 5236 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5237 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5238 | #else /* MS_WINDOWS */ |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5239 | HANDLE read, write; |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5240 | int read_fd, write_fd; |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5241 | BOOL ok; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5242 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5243 | ok = CreatePipe(&read, &write, NULL, 0); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5244 | Py_END_ALLOW_THREADS |
Guido van Rossum | 794d813 | 1994-08-23 13:48:48 +0000 | [diff] [blame] | 5245 | if (!ok) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 5246 | return win32_error("CreatePipe", NULL); |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 5247 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 5248 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
Guido van Rossum | b3f9f4b | 1998-06-12 15:05:15 +0000 | [diff] [blame] | 5249 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 5250 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5251 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 5252 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5253 | #endif /* HAVE_PIPE */ |
| 5254 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5255 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5256 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5257 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5258 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5259 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5260 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5261 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5262 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5263 | { |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5264 | char *filename; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5265 | int mode = 0666; |
| 5266 | int res; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5267 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5268 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5269 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5270 | res = mkfifo(filename, mode); |
| 5271 | Py_END_ALLOW_THREADS |
| 5272 | if (res < 0) |
| 5273 | return posix_error(); |
| 5274 | Py_INCREF(Py_None); |
| 5275 | return Py_None; |
| 5276 | } |
| 5277 | #endif |
| 5278 | |
| 5279 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 5280 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5281 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 5282 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5283 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 5284 | named filename. mode specifies both the permissions to use and the\n\ |
| 5285 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 5286 | 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] | 5287 | device defines the newly created device special file (probably using\n\ |
| 5288 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5289 | |
| 5290 | |
| 5291 | static PyObject * |
| 5292 | posix_mknod(PyObject *self, PyObject *args) |
| 5293 | { |
| 5294 | char *filename; |
| 5295 | int mode = 0600; |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5296 | int device = 0; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5297 | int res; |
Martin v. Löwis | d631ebe | 2002-11-02 17:42:33 +0000 | [diff] [blame] | 5298 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 5299 | return NULL; |
| 5300 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5301 | res = mknod(filename, mode, device); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5302 | Py_END_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5303 | if (res < 0) |
| 5304 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5305 | Py_INCREF(Py_None); |
| 5306 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5307 | } |
| 5308 | #endif |
| 5309 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5310 | #ifdef HAVE_DEVICE_MACROS |
| 5311 | PyDoc_STRVAR(posix_major__doc__, |
| 5312 | "major(device) -> major number\n\ |
| 5313 | Extracts a device major number from a raw device number."); |
| 5314 | |
| 5315 | static PyObject * |
| 5316 | posix_major(PyObject *self, PyObject *args) |
| 5317 | { |
| 5318 | int device; |
| 5319 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 5320 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5321 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | PyDoc_STRVAR(posix_minor__doc__, |
| 5325 | "minor(device) -> minor number\n\ |
| 5326 | Extracts a device minor number from a raw device number."); |
| 5327 | |
| 5328 | static PyObject * |
| 5329 | posix_minor(PyObject *self, PyObject *args) |
| 5330 | { |
| 5331 | int device; |
| 5332 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 5333 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5334 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | PyDoc_STRVAR(posix_makedev__doc__, |
| 5338 | "makedev(major, minor) -> device number\n\ |
| 5339 | Composes a raw device number from the major and minor device numbers."); |
| 5340 | |
| 5341 | static PyObject * |
| 5342 | posix_makedev(PyObject *self, PyObject *args) |
| 5343 | { |
| 5344 | int major, minor; |
| 5345 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 5346 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5347 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 5348 | } |
| 5349 | #endif /* device macros */ |
| 5350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5351 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5352 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5353 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5354 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5355 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5356 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5357 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5358 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5359 | { |
| 5360 | int fd; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5361 | off_t length; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5362 | int res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5363 | PyObject *lenobj; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5364 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5365 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5366 | return NULL; |
| 5367 | |
| 5368 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5369 | length = PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5370 | #else |
| 5371 | length = PyLong_Check(lenobj) ? |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5372 | PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5373 | #endif |
| 5374 | if (PyErr_Occurred()) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5375 | return NULL; |
| 5376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5377 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5378 | res = ftruncate(fd, length); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5379 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 9053d75 | 2009-01-19 17:53:36 +0000 | [diff] [blame] | 5380 | if (res < 0) |
| 5381 | return posix_error(); |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5382 | Py_INCREF(Py_None); |
| 5383 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5384 | } |
| 5385 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 5386 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5387 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5389 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5390 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5391 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5392 | /* Save putenv() parameters as values here, so we can collect them when they |
| 5393 | * get re-set with another call for the same key. */ |
| 5394 | static PyObject *posix_putenv_garbage; |
| 5395 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5396 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5397 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5398 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5399 | #ifdef MS_WINDOWS |
| 5400 | wchar_t *s1, *s2; |
| 5401 | wchar_t *newenv; |
| 5402 | #else |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5403 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5404 | char *s1, *s2; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5405 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5406 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5407 | PyObject *newstr; |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5408 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5409 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5410 | #ifdef MS_WINDOWS |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5411 | if (!PyArg_ParseTuple(args, |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5412 | "uu:putenv", |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5413 | &s1, &s2)) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5414 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5415 | #else |
| 5416 | if (!PyArg_ParseTuple(args, |
| 5417 | "O&O&:putenv", |
| 5418 | PyUnicode_FSConverter, &os1, |
| 5419 | PyUnicode_FSConverter, &os2)) |
| 5420 | return NULL; |
| 5421 | s1 = bytes2str(os1, 1); |
| 5422 | s2 = bytes2str(os2, 1); |
| 5423 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5424 | |
| 5425 | #if defined(PYOS_OS2) |
| 5426 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 5427 | APIRET rc; |
| 5428 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5429 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
| 5430 | if (rc != NO_ERROR) |
| 5431 | return os2_error(rc); |
| 5432 | |
| 5433 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 5434 | APIRET rc; |
| 5435 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5436 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
| 5437 | if (rc != NO_ERROR) |
| 5438 | return os2_error(rc); |
| 5439 | } else { |
| 5440 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5441 | /* XXX This can leak memory -- not easy to fix :-( */ |
Tim Peters | c8996f5 | 2001-12-03 20:41:00 +0000 | [diff] [blame] | 5442 | /* len includes space for a trailing \0; the size arg to |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5443 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5444 | #ifdef MS_WINDOWS |
| 5445 | len = wcslen(s1) + wcslen(s2) + 2; |
| 5446 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
| 5447 | #else |
| 5448 | len = strlen(s1) + strlen(s2) + 2; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5449 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5450 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5451 | if (newstr == NULL) |
| 5452 | return PyErr_NoMemory(); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5453 | #ifdef MS_WINDOWS |
| 5454 | newenv = PyUnicode_AsUnicode(newstr); |
| 5455 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 5456 | if (_wputenv(newenv)) { |
| 5457 | Py_DECREF(newstr); |
| 5458 | posix_error(); |
| 5459 | return NULL; |
| 5460 | } |
| 5461 | #else |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5462 | newenv = PyBytes_AS_STRING(newstr); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5463 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 5464 | if (putenv(newenv)) { |
Neal Norwitz | 4adc9ab | 2003-02-10 03:10:43 +0000 | [diff] [blame] | 5465 | Py_DECREF(newstr); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5466 | release_bytes(os1); |
| 5467 | release_bytes(os2); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5468 | posix_error(); |
| 5469 | return NULL; |
| 5470 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 5471 | #endif |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 5472 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 5473 | * this will cause previous value to be collected. This has to |
| 5474 | * happen after the real putenv() call because the old value |
| 5475 | * was still accessible until then. */ |
| 5476 | if (PyDict_SetItem(posix_putenv_garbage, |
| 5477 | PyTuple_GET_ITEM(args, 0), newstr)) { |
| 5478 | /* really not much we can do; just leak */ |
| 5479 | PyErr_Clear(); |
| 5480 | } |
| 5481 | else { |
| 5482 | Py_DECREF(newstr); |
| 5483 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5484 | |
| 5485 | #if defined(PYOS_OS2) |
| 5486 | } |
| 5487 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5488 | #ifndef MS_WINDOWS |
| 5489 | release_bytes(os1); |
| 5490 | release_bytes(os2); |
| 5491 | #endif |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5492 | Py_INCREF(Py_None); |
| 5493 | return Py_None; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5494 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5495 | #endif /* putenv */ |
| 5496 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5497 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5498 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5499 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5500 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5501 | |
| 5502 | static PyObject * |
| 5503 | posix_unsetenv(PyObject *self, PyObject *args) |
| 5504 | { |
| 5505 | char *s1; |
| 5506 | |
| 5507 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 5508 | return NULL; |
| 5509 | |
| 5510 | unsetenv(s1); |
| 5511 | |
| 5512 | /* Remove the key from posix_putenv_garbage; |
| 5513 | * this will cause it to be collected. This has to |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5514 | * happen after the real unsetenv() call because the |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 5515 | * old value was still accessible until then. |
| 5516 | */ |
| 5517 | if (PyDict_DelItem(posix_putenv_garbage, |
| 5518 | PyTuple_GET_ITEM(args, 0))) { |
| 5519 | /* really not much we can do; just leak */ |
| 5520 | PyErr_Clear(); |
| 5521 | } |
| 5522 | |
| 5523 | Py_INCREF(Py_None); |
| 5524 | return Py_None; |
| 5525 | } |
| 5526 | #endif /* unsetenv */ |
| 5527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5528 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5529 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5530 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5531 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5532 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5533 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5534 | { |
| 5535 | int code; |
| 5536 | char *message; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5537 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5538 | return NULL; |
| 5539 | message = strerror(code); |
| 5540 | if (message == NULL) { |
| 5541 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 5542 | "strerror() argument out of range"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5543 | return NULL; |
| 5544 | } |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 5545 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5546 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 5547 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 5548 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5549 | #ifdef HAVE_SYS_WAIT_H |
| 5550 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5551 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5553 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5554 | 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] | 5555 | |
| 5556 | static PyObject * |
| 5557 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 5558 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5559 | WAIT_TYPE status; |
| 5560 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5561 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5562 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5563 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5564 | |
| 5565 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5566 | } |
| 5567 | #endif /* WCOREDUMP */ |
| 5568 | |
| 5569 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5570 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5571 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5572 | 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] | 5573 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5574 | |
| 5575 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5576 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5577 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5578 | WAIT_TYPE status; |
| 5579 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5580 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5581 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5582 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5583 | |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 5584 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5585 | } |
| 5586 | #endif /* WIFCONTINUED */ |
| 5587 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5588 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5589 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5590 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5591 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5592 | |
| 5593 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5594 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5595 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5596 | WAIT_TYPE status; |
| 5597 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5598 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5599 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5600 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5601 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5602 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5603 | } |
| 5604 | #endif /* WIFSTOPPED */ |
| 5605 | |
| 5606 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5607 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5608 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | 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] | 5610 | |
| 5611 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5612 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5613 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5614 | WAIT_TYPE status; |
| 5615 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5616 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5617 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5618 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5619 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5620 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5621 | } |
| 5622 | #endif /* WIFSIGNALED */ |
| 5623 | |
| 5624 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5625 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5626 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5627 | 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] | 5628 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5629 | |
| 5630 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5631 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5632 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5633 | WAIT_TYPE status; |
| 5634 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5635 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5636 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5637 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5638 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 5639 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5640 | } |
| 5641 | #endif /* WIFEXITED */ |
| 5642 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 5643 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5644 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5645 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5646 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5647 | |
| 5648 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5649 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5650 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5651 | WAIT_TYPE status; |
| 5652 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5653 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5654 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5655 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5656 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5657 | return Py_BuildValue("i", WEXITSTATUS(status)); |
| 5658 | } |
| 5659 | #endif /* WEXITSTATUS */ |
| 5660 | |
| 5661 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5662 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5663 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 5664 | 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] | 5665 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5666 | |
| 5667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5668 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5669 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5670 | WAIT_TYPE status; |
| 5671 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5672 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5673 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5674 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5675 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5676 | return Py_BuildValue("i", WTERMSIG(status)); |
| 5677 | } |
| 5678 | #endif /* WTERMSIG */ |
| 5679 | |
| 5680 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5681 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5682 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5683 | Return the signal that stopped the process that provided\n\ |
| 5684 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5685 | |
| 5686 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5687 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5688 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5689 | WAIT_TYPE status; |
| 5690 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5691 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5692 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5693 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5694 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 5695 | return Py_BuildValue("i", WSTOPSIG(status)); |
| 5696 | } |
| 5697 | #endif /* WSTOPSIG */ |
| 5698 | |
| 5699 | #endif /* HAVE_SYS_WAIT_H */ |
| 5700 | |
| 5701 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5702 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 5703 | #ifdef _SCO_DS |
| 5704 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 5705 | needed definitions in sys/statvfs.h */ |
| 5706 | #define _SVID3 |
| 5707 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5708 | #include <sys/statvfs.h> |
| 5709 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5710 | static PyObject* |
| 5711 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
| 5712 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 5713 | if (v == NULL) |
| 5714 | return NULL; |
| 5715 | |
| 5716 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5717 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5718 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 5719 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 5720 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 5721 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 5722 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 5723 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 5724 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 5725 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5726 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5727 | #else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5728 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 5729 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5730 | PyStructSequence_SET_ITEM(v, 2, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5731 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5732 | PyStructSequence_SET_ITEM(v, 3, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5733 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5734 | PyStructSequence_SET_ITEM(v, 4, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5735 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5736 | PyStructSequence_SET_ITEM(v, 5, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5737 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5738 | PyStructSequence_SET_ITEM(v, 6, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5739 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5740 | PyStructSequence_SET_ITEM(v, 7, |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 5741 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5742 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 5743 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5744 | #endif |
| 5745 | |
| 5746 | return v; |
| 5747 | } |
| 5748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5749 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5750 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5751 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5752 | |
| 5753 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5754 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5755 | { |
| 5756 | int fd, res; |
| 5757 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5758 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5759 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5760 | return NULL; |
| 5761 | Py_BEGIN_ALLOW_THREADS |
| 5762 | res = fstatvfs(fd, &st); |
| 5763 | Py_END_ALLOW_THREADS |
| 5764 | if (res != 0) |
| 5765 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5766 | |
| 5767 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5768 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5769 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5770 | |
| 5771 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5772 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5773 | #include <sys/statvfs.h> |
| 5774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5775 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5776 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5777 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5778 | |
| 5779 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5780 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5781 | { |
| 5782 | char *path; |
| 5783 | int res; |
| 5784 | struct statvfs st; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 5785 | if (!PyArg_ParseTuple(args, "s:statvfs", &path)) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5786 | return NULL; |
| 5787 | Py_BEGIN_ALLOW_THREADS |
| 5788 | res = statvfs(path, &st); |
| 5789 | Py_END_ALLOW_THREADS |
| 5790 | if (res != 0) |
| 5791 | return posix_error_with_filename(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5792 | |
| 5793 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 5794 | } |
| 5795 | #endif /* HAVE_STATVFS */ |
| 5796 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5797 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 5798 | * It maps strings representing configuration variable names to |
| 5799 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5800 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5801 | * rarely-used constants. There are three separate tables that use |
| 5802 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 5803 | * |
| 5804 | * This code is always included, even if none of the interfaces that |
| 5805 | * need it are included. The #if hackery needed to avoid it would be |
| 5806 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5807 | */ |
| 5808 | struct constdef { |
| 5809 | char *name; |
| 5810 | long value; |
| 5811 | }; |
| 5812 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5813 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5814 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5815 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5816 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5817 | if (PyLong_Check(arg)) { |
| 5818 | *valuep = PyLong_AS_LONG(arg); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5819 | return 1; |
| 5820 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5821 | else { |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5822 | /* look up the value in the table using a binary search */ |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5823 | size_t lo = 0; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5824 | size_t mid; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 5825 | size_t hi = tablesize; |
| 5826 | int cmp; |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5827 | const char *confname; |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5828 | if (!PyUnicode_Check(arg)) { |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5829 | PyErr_SetString(PyExc_TypeError, |
| 5830 | "configuration names must be strings or integers"); |
| 5831 | return 0; |
| 5832 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 5833 | confname = _PyUnicode_AsString(arg); |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 5834 | if (confname == NULL) |
| 5835 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5836 | while (lo < hi) { |
| 5837 | mid = (lo + hi) / 2; |
| 5838 | cmp = strcmp(confname, table[mid].name); |
| 5839 | if (cmp < 0) |
| 5840 | hi = mid; |
| 5841 | else if (cmp > 0) |
| 5842 | lo = mid + 1; |
| 5843 | else { |
| 5844 | *valuep = table[mid].value; |
| 5845 | return 1; |
| 5846 | } |
| 5847 | } |
| 5848 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 5849 | return 0; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5850 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | |
| 5854 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 5855 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5856 | #ifdef _PC_ABI_AIO_XFER_MAX |
| 5857 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
| 5858 | #endif |
| 5859 | #ifdef _PC_ABI_ASYNC_IO |
| 5860 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
| 5861 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5862 | #ifdef _PC_ASYNC_IO |
| 5863 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
| 5864 | #endif |
| 5865 | #ifdef _PC_CHOWN_RESTRICTED |
| 5866 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
| 5867 | #endif |
| 5868 | #ifdef _PC_FILESIZEBITS |
| 5869 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
| 5870 | #endif |
| 5871 | #ifdef _PC_LAST |
| 5872 | {"PC_LAST", _PC_LAST}, |
| 5873 | #endif |
| 5874 | #ifdef _PC_LINK_MAX |
| 5875 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
| 5876 | #endif |
| 5877 | #ifdef _PC_MAX_CANON |
| 5878 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
| 5879 | #endif |
| 5880 | #ifdef _PC_MAX_INPUT |
| 5881 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
| 5882 | #endif |
| 5883 | #ifdef _PC_NAME_MAX |
| 5884 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
| 5885 | #endif |
| 5886 | #ifdef _PC_NO_TRUNC |
| 5887 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
| 5888 | #endif |
| 5889 | #ifdef _PC_PATH_MAX |
| 5890 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
| 5891 | #endif |
| 5892 | #ifdef _PC_PIPE_BUF |
| 5893 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
| 5894 | #endif |
| 5895 | #ifdef _PC_PRIO_IO |
| 5896 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
| 5897 | #endif |
| 5898 | #ifdef _PC_SOCK_MAXBUF |
| 5899 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
| 5900 | #endif |
| 5901 | #ifdef _PC_SYNC_IO |
| 5902 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
| 5903 | #endif |
| 5904 | #ifdef _PC_VDISABLE |
| 5905 | {"PC_VDISABLE", _PC_VDISABLE}, |
| 5906 | #endif |
| 5907 | }; |
| 5908 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5909 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5910 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5911 | { |
| 5912 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 5913 | sizeof(posix_constants_pathconf) |
| 5914 | / sizeof(struct constdef)); |
| 5915 | } |
| 5916 | #endif |
| 5917 | |
| 5918 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5919 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5920 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5921 | 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] | 5922 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5923 | |
| 5924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5925 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5926 | { |
| 5927 | PyObject *result = NULL; |
| 5928 | int name, fd; |
| 5929 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5930 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 5931 | conv_path_confname, &name)) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5932 | long limit; |
| 5933 | |
| 5934 | errno = 0; |
| 5935 | limit = fpathconf(fd, name); |
| 5936 | if (limit == -1 && errno != 0) |
| 5937 | posix_error(); |
| 5938 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5939 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5940 | } |
| 5941 | return result; |
| 5942 | } |
| 5943 | #endif |
| 5944 | |
| 5945 | |
| 5946 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5947 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5948 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5949 | 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] | 5950 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5951 | |
| 5952 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5953 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5954 | { |
| 5955 | PyObject *result = NULL; |
| 5956 | int name; |
| 5957 | char *path; |
| 5958 | |
| 5959 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 5960 | conv_path_confname, &name)) { |
| 5961 | long limit; |
| 5962 | |
| 5963 | errno = 0; |
| 5964 | limit = pathconf(path, name); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5965 | if (limit == -1 && errno != 0) { |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5966 | if (errno == EINVAL) |
| 5967 | /* could be a path or name problem */ |
| 5968 | posix_error(); |
| 5969 | else |
| 5970 | posix_error_with_filename(path); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5971 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5972 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5973 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5974 | } |
| 5975 | return result; |
| 5976 | } |
| 5977 | #endif |
| 5978 | |
| 5979 | #ifdef HAVE_CONFSTR |
| 5980 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 5981 | #ifdef _CS_ARCHITECTURE |
| 5982 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
| 5983 | #endif |
| 5984 | #ifdef _CS_HOSTNAME |
| 5985 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
| 5986 | #endif |
| 5987 | #ifdef _CS_HW_PROVIDER |
| 5988 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
| 5989 | #endif |
| 5990 | #ifdef _CS_HW_SERIAL |
| 5991 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
| 5992 | #endif |
| 5993 | #ifdef _CS_INITTAB_NAME |
| 5994 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
| 5995 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5996 | #ifdef _CS_LFS64_CFLAGS |
| 5997 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
| 5998 | #endif |
| 5999 | #ifdef _CS_LFS64_LDFLAGS |
| 6000 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
| 6001 | #endif |
| 6002 | #ifdef _CS_LFS64_LIBS |
| 6003 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
| 6004 | #endif |
| 6005 | #ifdef _CS_LFS64_LINTFLAGS |
| 6006 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
| 6007 | #endif |
| 6008 | #ifdef _CS_LFS_CFLAGS |
| 6009 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
| 6010 | #endif |
| 6011 | #ifdef _CS_LFS_LDFLAGS |
| 6012 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
| 6013 | #endif |
| 6014 | #ifdef _CS_LFS_LIBS |
| 6015 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
| 6016 | #endif |
| 6017 | #ifdef _CS_LFS_LINTFLAGS |
| 6018 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
| 6019 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6020 | #ifdef _CS_MACHINE |
| 6021 | {"CS_MACHINE", _CS_MACHINE}, |
| 6022 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6023 | #ifdef _CS_PATH |
| 6024 | {"CS_PATH", _CS_PATH}, |
| 6025 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6026 | #ifdef _CS_RELEASE |
| 6027 | {"CS_RELEASE", _CS_RELEASE}, |
| 6028 | #endif |
| 6029 | #ifdef _CS_SRPC_DOMAIN |
| 6030 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
| 6031 | #endif |
| 6032 | #ifdef _CS_SYSNAME |
| 6033 | {"CS_SYSNAME", _CS_SYSNAME}, |
| 6034 | #endif |
| 6035 | #ifdef _CS_VERSION |
| 6036 | {"CS_VERSION", _CS_VERSION}, |
| 6037 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6038 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
| 6039 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
| 6040 | #endif |
| 6041 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
| 6042 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
| 6043 | #endif |
| 6044 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
| 6045 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
| 6046 | #endif |
| 6047 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
| 6048 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
| 6049 | #endif |
| 6050 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
| 6051 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
| 6052 | #endif |
| 6053 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
| 6054 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
| 6055 | #endif |
| 6056 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
| 6057 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
| 6058 | #endif |
| 6059 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
| 6060 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
| 6061 | #endif |
| 6062 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
| 6063 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
| 6064 | #endif |
| 6065 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
| 6066 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
| 6067 | #endif |
| 6068 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
| 6069 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
| 6070 | #endif |
| 6071 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
| 6072 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
| 6073 | #endif |
| 6074 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
| 6075 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
| 6076 | #endif |
| 6077 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
| 6078 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
| 6079 | #endif |
| 6080 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
| 6081 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
| 6082 | #endif |
| 6083 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
| 6084 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
| 6085 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6086 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
| 6087 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
| 6088 | #endif |
| 6089 | #ifdef _MIPS_CS_BASE |
| 6090 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
| 6091 | #endif |
| 6092 | #ifdef _MIPS_CS_HOSTID |
| 6093 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
| 6094 | #endif |
| 6095 | #ifdef _MIPS_CS_HW_NAME |
| 6096 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
| 6097 | #endif |
| 6098 | #ifdef _MIPS_CS_NUM_PROCESSORS |
| 6099 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
| 6100 | #endif |
| 6101 | #ifdef _MIPS_CS_OSREL_MAJ |
| 6102 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
| 6103 | #endif |
| 6104 | #ifdef _MIPS_CS_OSREL_MIN |
| 6105 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
| 6106 | #endif |
| 6107 | #ifdef _MIPS_CS_OSREL_PATCH |
| 6108 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
| 6109 | #endif |
| 6110 | #ifdef _MIPS_CS_OS_NAME |
| 6111 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
| 6112 | #endif |
| 6113 | #ifdef _MIPS_CS_OS_PROVIDER |
| 6114 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
| 6115 | #endif |
| 6116 | #ifdef _MIPS_CS_PROCESSORS |
| 6117 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
| 6118 | #endif |
| 6119 | #ifdef _MIPS_CS_SERIAL |
| 6120 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
| 6121 | #endif |
| 6122 | #ifdef _MIPS_CS_VENDOR |
| 6123 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
| 6124 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6125 | }; |
| 6126 | |
| 6127 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6128 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6129 | { |
| 6130 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 6131 | sizeof(posix_constants_confstr) |
| 6132 | / sizeof(struct constdef)); |
| 6133 | } |
| 6134 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6135 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6136 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6137 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6138 | |
| 6139 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6140 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6141 | { |
| 6142 | PyObject *result = NULL; |
| 6143 | int name; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6144 | char buffer[256]; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6145 | |
| 6146 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6147 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6148 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6149 | errno = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6150 | len = confstr(name, buffer, sizeof(buffer)); |
| 6151 | if (len == 0) { |
| 6152 | if (errno) { |
| 6153 | posix_error(); |
| 6154 | } |
| 6155 | else { |
| 6156 | result = Py_None; |
| 6157 | Py_INCREF(Py_None); |
| 6158 | } |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6159 | } |
| 6160 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6161 | if ((unsigned int)len >= sizeof(buffer)) { |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6162 | result = PyUnicode_FromStringAndSize(NULL, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6163 | if (result != NULL) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 6164 | confstr(name, _PyUnicode_AsString(result), len); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6165 | } |
| 6166 | else |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6167 | result = PyUnicode_FromStringAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6168 | } |
| 6169 | } |
| 6170 | return result; |
| 6171 | } |
| 6172 | #endif |
| 6173 | |
| 6174 | |
| 6175 | #ifdef HAVE_SYSCONF |
| 6176 | static struct constdef posix_constants_sysconf[] = { |
| 6177 | #ifdef _SC_2_CHAR_TERM |
| 6178 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
| 6179 | #endif |
| 6180 | #ifdef _SC_2_C_BIND |
| 6181 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
| 6182 | #endif |
| 6183 | #ifdef _SC_2_C_DEV |
| 6184 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
| 6185 | #endif |
| 6186 | #ifdef _SC_2_C_VERSION |
| 6187 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
| 6188 | #endif |
| 6189 | #ifdef _SC_2_FORT_DEV |
| 6190 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
| 6191 | #endif |
| 6192 | #ifdef _SC_2_FORT_RUN |
| 6193 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
| 6194 | #endif |
| 6195 | #ifdef _SC_2_LOCALEDEF |
| 6196 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
| 6197 | #endif |
| 6198 | #ifdef _SC_2_SW_DEV |
| 6199 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
| 6200 | #endif |
| 6201 | #ifdef _SC_2_UPE |
| 6202 | {"SC_2_UPE", _SC_2_UPE}, |
| 6203 | #endif |
| 6204 | #ifdef _SC_2_VERSION |
| 6205 | {"SC_2_VERSION", _SC_2_VERSION}, |
| 6206 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6207 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
| 6208 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
| 6209 | #endif |
| 6210 | #ifdef _SC_ACL |
| 6211 | {"SC_ACL", _SC_ACL}, |
| 6212 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6213 | #ifdef _SC_AIO_LISTIO_MAX |
| 6214 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
| 6215 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6216 | #ifdef _SC_AIO_MAX |
| 6217 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
| 6218 | #endif |
| 6219 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
| 6220 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
| 6221 | #endif |
| 6222 | #ifdef _SC_ARG_MAX |
| 6223 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
| 6224 | #endif |
| 6225 | #ifdef _SC_ASYNCHRONOUS_IO |
| 6226 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
| 6227 | #endif |
| 6228 | #ifdef _SC_ATEXIT_MAX |
| 6229 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
| 6230 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6231 | #ifdef _SC_AUDIT |
| 6232 | {"SC_AUDIT", _SC_AUDIT}, |
| 6233 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6234 | #ifdef _SC_AVPHYS_PAGES |
| 6235 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
| 6236 | #endif |
| 6237 | #ifdef _SC_BC_BASE_MAX |
| 6238 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
| 6239 | #endif |
| 6240 | #ifdef _SC_BC_DIM_MAX |
| 6241 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
| 6242 | #endif |
| 6243 | #ifdef _SC_BC_SCALE_MAX |
| 6244 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
| 6245 | #endif |
| 6246 | #ifdef _SC_BC_STRING_MAX |
| 6247 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
| 6248 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6249 | #ifdef _SC_CAP |
| 6250 | {"SC_CAP", _SC_CAP}, |
| 6251 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6252 | #ifdef _SC_CHARCLASS_NAME_MAX |
| 6253 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
| 6254 | #endif |
| 6255 | #ifdef _SC_CHAR_BIT |
| 6256 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
| 6257 | #endif |
| 6258 | #ifdef _SC_CHAR_MAX |
| 6259 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
| 6260 | #endif |
| 6261 | #ifdef _SC_CHAR_MIN |
| 6262 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
| 6263 | #endif |
| 6264 | #ifdef _SC_CHILD_MAX |
| 6265 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
| 6266 | #endif |
| 6267 | #ifdef _SC_CLK_TCK |
| 6268 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
| 6269 | #endif |
| 6270 | #ifdef _SC_COHER_BLKSZ |
| 6271 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
| 6272 | #endif |
| 6273 | #ifdef _SC_COLL_WEIGHTS_MAX |
| 6274 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
| 6275 | #endif |
| 6276 | #ifdef _SC_DCACHE_ASSOC |
| 6277 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
| 6278 | #endif |
| 6279 | #ifdef _SC_DCACHE_BLKSZ |
| 6280 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
| 6281 | #endif |
| 6282 | #ifdef _SC_DCACHE_LINESZ |
| 6283 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
| 6284 | #endif |
| 6285 | #ifdef _SC_DCACHE_SZ |
| 6286 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
| 6287 | #endif |
| 6288 | #ifdef _SC_DCACHE_TBLKSZ |
| 6289 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
| 6290 | #endif |
| 6291 | #ifdef _SC_DELAYTIMER_MAX |
| 6292 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
| 6293 | #endif |
| 6294 | #ifdef _SC_EQUIV_CLASS_MAX |
| 6295 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
| 6296 | #endif |
| 6297 | #ifdef _SC_EXPR_NEST_MAX |
| 6298 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
| 6299 | #endif |
| 6300 | #ifdef _SC_FSYNC |
| 6301 | {"SC_FSYNC", _SC_FSYNC}, |
| 6302 | #endif |
| 6303 | #ifdef _SC_GETGR_R_SIZE_MAX |
| 6304 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
| 6305 | #endif |
| 6306 | #ifdef _SC_GETPW_R_SIZE_MAX |
| 6307 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
| 6308 | #endif |
| 6309 | #ifdef _SC_ICACHE_ASSOC |
| 6310 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
| 6311 | #endif |
| 6312 | #ifdef _SC_ICACHE_BLKSZ |
| 6313 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
| 6314 | #endif |
| 6315 | #ifdef _SC_ICACHE_LINESZ |
| 6316 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
| 6317 | #endif |
| 6318 | #ifdef _SC_ICACHE_SZ |
| 6319 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
| 6320 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6321 | #ifdef _SC_INF |
| 6322 | {"SC_INF", _SC_INF}, |
| 6323 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6324 | #ifdef _SC_INT_MAX |
| 6325 | {"SC_INT_MAX", _SC_INT_MAX}, |
| 6326 | #endif |
| 6327 | #ifdef _SC_INT_MIN |
| 6328 | {"SC_INT_MIN", _SC_INT_MIN}, |
| 6329 | #endif |
| 6330 | #ifdef _SC_IOV_MAX |
| 6331 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
| 6332 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6333 | #ifdef _SC_IP_SECOPTS |
| 6334 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
| 6335 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6336 | #ifdef _SC_JOB_CONTROL |
| 6337 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
| 6338 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6339 | #ifdef _SC_KERN_POINTERS |
| 6340 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
| 6341 | #endif |
| 6342 | #ifdef _SC_KERN_SIM |
| 6343 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
| 6344 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6345 | #ifdef _SC_LINE_MAX |
| 6346 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
| 6347 | #endif |
| 6348 | #ifdef _SC_LOGIN_NAME_MAX |
| 6349 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
| 6350 | #endif |
| 6351 | #ifdef _SC_LOGNAME_MAX |
| 6352 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
| 6353 | #endif |
| 6354 | #ifdef _SC_LONG_BIT |
| 6355 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
| 6356 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6357 | #ifdef _SC_MAC |
| 6358 | {"SC_MAC", _SC_MAC}, |
| 6359 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6360 | #ifdef _SC_MAPPED_FILES |
| 6361 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
| 6362 | #endif |
| 6363 | #ifdef _SC_MAXPID |
| 6364 | {"SC_MAXPID", _SC_MAXPID}, |
| 6365 | #endif |
| 6366 | #ifdef _SC_MB_LEN_MAX |
| 6367 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
| 6368 | #endif |
| 6369 | #ifdef _SC_MEMLOCK |
| 6370 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
| 6371 | #endif |
| 6372 | #ifdef _SC_MEMLOCK_RANGE |
| 6373 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
| 6374 | #endif |
| 6375 | #ifdef _SC_MEMORY_PROTECTION |
| 6376 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
| 6377 | #endif |
| 6378 | #ifdef _SC_MESSAGE_PASSING |
| 6379 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
| 6380 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6381 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
| 6382 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
| 6383 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6384 | #ifdef _SC_MQ_OPEN_MAX |
| 6385 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
| 6386 | #endif |
| 6387 | #ifdef _SC_MQ_PRIO_MAX |
| 6388 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
| 6389 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6390 | #ifdef _SC_NACLS_MAX |
| 6391 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
| 6392 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6393 | #ifdef _SC_NGROUPS_MAX |
| 6394 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
| 6395 | #endif |
| 6396 | #ifdef _SC_NL_ARGMAX |
| 6397 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
| 6398 | #endif |
| 6399 | #ifdef _SC_NL_LANGMAX |
| 6400 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
| 6401 | #endif |
| 6402 | #ifdef _SC_NL_MSGMAX |
| 6403 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
| 6404 | #endif |
| 6405 | #ifdef _SC_NL_NMAX |
| 6406 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
| 6407 | #endif |
| 6408 | #ifdef _SC_NL_SETMAX |
| 6409 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
| 6410 | #endif |
| 6411 | #ifdef _SC_NL_TEXTMAX |
| 6412 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
| 6413 | #endif |
| 6414 | #ifdef _SC_NPROCESSORS_CONF |
| 6415 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
| 6416 | #endif |
| 6417 | #ifdef _SC_NPROCESSORS_ONLN |
| 6418 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
| 6419 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6420 | #ifdef _SC_NPROC_CONF |
| 6421 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
| 6422 | #endif |
| 6423 | #ifdef _SC_NPROC_ONLN |
| 6424 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
| 6425 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6426 | #ifdef _SC_NZERO |
| 6427 | {"SC_NZERO", _SC_NZERO}, |
| 6428 | #endif |
| 6429 | #ifdef _SC_OPEN_MAX |
| 6430 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
| 6431 | #endif |
| 6432 | #ifdef _SC_PAGESIZE |
| 6433 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
| 6434 | #endif |
| 6435 | #ifdef _SC_PAGE_SIZE |
| 6436 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
| 6437 | #endif |
| 6438 | #ifdef _SC_PASS_MAX |
| 6439 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
| 6440 | #endif |
| 6441 | #ifdef _SC_PHYS_PAGES |
| 6442 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
| 6443 | #endif |
| 6444 | #ifdef _SC_PII |
| 6445 | {"SC_PII", _SC_PII}, |
| 6446 | #endif |
| 6447 | #ifdef _SC_PII_INTERNET |
| 6448 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
| 6449 | #endif |
| 6450 | #ifdef _SC_PII_INTERNET_DGRAM |
| 6451 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
| 6452 | #endif |
| 6453 | #ifdef _SC_PII_INTERNET_STREAM |
| 6454 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
| 6455 | #endif |
| 6456 | #ifdef _SC_PII_OSI |
| 6457 | {"SC_PII_OSI", _SC_PII_OSI}, |
| 6458 | #endif |
| 6459 | #ifdef _SC_PII_OSI_CLTS |
| 6460 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
| 6461 | #endif |
| 6462 | #ifdef _SC_PII_OSI_COTS |
| 6463 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
| 6464 | #endif |
| 6465 | #ifdef _SC_PII_OSI_M |
| 6466 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
| 6467 | #endif |
| 6468 | #ifdef _SC_PII_SOCKET |
| 6469 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
| 6470 | #endif |
| 6471 | #ifdef _SC_PII_XTI |
| 6472 | {"SC_PII_XTI", _SC_PII_XTI}, |
| 6473 | #endif |
| 6474 | #ifdef _SC_POLL |
| 6475 | {"SC_POLL", _SC_POLL}, |
| 6476 | #endif |
| 6477 | #ifdef _SC_PRIORITIZED_IO |
| 6478 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
| 6479 | #endif |
| 6480 | #ifdef _SC_PRIORITY_SCHEDULING |
| 6481 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
| 6482 | #endif |
| 6483 | #ifdef _SC_REALTIME_SIGNALS |
| 6484 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
| 6485 | #endif |
| 6486 | #ifdef _SC_RE_DUP_MAX |
| 6487 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
| 6488 | #endif |
| 6489 | #ifdef _SC_RTSIG_MAX |
| 6490 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
| 6491 | #endif |
| 6492 | #ifdef _SC_SAVED_IDS |
| 6493 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
| 6494 | #endif |
| 6495 | #ifdef _SC_SCHAR_MAX |
| 6496 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
| 6497 | #endif |
| 6498 | #ifdef _SC_SCHAR_MIN |
| 6499 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
| 6500 | #endif |
| 6501 | #ifdef _SC_SELECT |
| 6502 | {"SC_SELECT", _SC_SELECT}, |
| 6503 | #endif |
| 6504 | #ifdef _SC_SEMAPHORES |
| 6505 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
| 6506 | #endif |
| 6507 | #ifdef _SC_SEM_NSEMS_MAX |
| 6508 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
| 6509 | #endif |
| 6510 | #ifdef _SC_SEM_VALUE_MAX |
| 6511 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
| 6512 | #endif |
| 6513 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
| 6514 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
| 6515 | #endif |
| 6516 | #ifdef _SC_SHRT_MAX |
| 6517 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
| 6518 | #endif |
| 6519 | #ifdef _SC_SHRT_MIN |
| 6520 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
| 6521 | #endif |
| 6522 | #ifdef _SC_SIGQUEUE_MAX |
| 6523 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
| 6524 | #endif |
| 6525 | #ifdef _SC_SIGRT_MAX |
| 6526 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
| 6527 | #endif |
| 6528 | #ifdef _SC_SIGRT_MIN |
| 6529 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
| 6530 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6531 | #ifdef _SC_SOFTPOWER |
| 6532 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
| 6533 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6534 | #ifdef _SC_SPLIT_CACHE |
| 6535 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
| 6536 | #endif |
| 6537 | #ifdef _SC_SSIZE_MAX |
| 6538 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
| 6539 | #endif |
| 6540 | #ifdef _SC_STACK_PROT |
| 6541 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
| 6542 | #endif |
| 6543 | #ifdef _SC_STREAM_MAX |
| 6544 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
| 6545 | #endif |
| 6546 | #ifdef _SC_SYNCHRONIZED_IO |
| 6547 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
| 6548 | #endif |
| 6549 | #ifdef _SC_THREADS |
| 6550 | {"SC_THREADS", _SC_THREADS}, |
| 6551 | #endif |
| 6552 | #ifdef _SC_THREAD_ATTR_STACKADDR |
| 6553 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
| 6554 | #endif |
| 6555 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
| 6556 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
| 6557 | #endif |
| 6558 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
| 6559 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
| 6560 | #endif |
| 6561 | #ifdef _SC_THREAD_KEYS_MAX |
| 6562 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
| 6563 | #endif |
| 6564 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
| 6565 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
| 6566 | #endif |
| 6567 | #ifdef _SC_THREAD_PRIO_INHERIT |
| 6568 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
| 6569 | #endif |
| 6570 | #ifdef _SC_THREAD_PRIO_PROTECT |
| 6571 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
| 6572 | #endif |
| 6573 | #ifdef _SC_THREAD_PROCESS_SHARED |
| 6574 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
| 6575 | #endif |
| 6576 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
| 6577 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
| 6578 | #endif |
| 6579 | #ifdef _SC_THREAD_STACK_MIN |
| 6580 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
| 6581 | #endif |
| 6582 | #ifdef _SC_THREAD_THREADS_MAX |
| 6583 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
| 6584 | #endif |
| 6585 | #ifdef _SC_TIMERS |
| 6586 | {"SC_TIMERS", _SC_TIMERS}, |
| 6587 | #endif |
| 6588 | #ifdef _SC_TIMER_MAX |
| 6589 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
| 6590 | #endif |
| 6591 | #ifdef _SC_TTY_NAME_MAX |
| 6592 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
| 6593 | #endif |
| 6594 | #ifdef _SC_TZNAME_MAX |
| 6595 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
| 6596 | #endif |
| 6597 | #ifdef _SC_T_IOV_MAX |
| 6598 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
| 6599 | #endif |
| 6600 | #ifdef _SC_UCHAR_MAX |
| 6601 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
| 6602 | #endif |
| 6603 | #ifdef _SC_UINT_MAX |
| 6604 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
| 6605 | #endif |
| 6606 | #ifdef _SC_UIO_MAXIOV |
| 6607 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
| 6608 | #endif |
| 6609 | #ifdef _SC_ULONG_MAX |
| 6610 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
| 6611 | #endif |
| 6612 | #ifdef _SC_USHRT_MAX |
| 6613 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
| 6614 | #endif |
| 6615 | #ifdef _SC_VERSION |
| 6616 | {"SC_VERSION", _SC_VERSION}, |
| 6617 | #endif |
| 6618 | #ifdef _SC_WORD_BIT |
| 6619 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
| 6620 | #endif |
| 6621 | #ifdef _SC_XBS5_ILP32_OFF32 |
| 6622 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
| 6623 | #endif |
| 6624 | #ifdef _SC_XBS5_ILP32_OFFBIG |
| 6625 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
| 6626 | #endif |
| 6627 | #ifdef _SC_XBS5_LP64_OFF64 |
| 6628 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
| 6629 | #endif |
| 6630 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
| 6631 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
| 6632 | #endif |
| 6633 | #ifdef _SC_XOPEN_CRYPT |
| 6634 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
| 6635 | #endif |
| 6636 | #ifdef _SC_XOPEN_ENH_I18N |
| 6637 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
| 6638 | #endif |
| 6639 | #ifdef _SC_XOPEN_LEGACY |
| 6640 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
| 6641 | #endif |
| 6642 | #ifdef _SC_XOPEN_REALTIME |
| 6643 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
| 6644 | #endif |
| 6645 | #ifdef _SC_XOPEN_REALTIME_THREADS |
| 6646 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
| 6647 | #endif |
| 6648 | #ifdef _SC_XOPEN_SHM |
| 6649 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
| 6650 | #endif |
| 6651 | #ifdef _SC_XOPEN_UNIX |
| 6652 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
| 6653 | #endif |
| 6654 | #ifdef _SC_XOPEN_VERSION |
| 6655 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
| 6656 | #endif |
| 6657 | #ifdef _SC_XOPEN_XCU_VERSION |
| 6658 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
| 6659 | #endif |
| 6660 | #ifdef _SC_XOPEN_XPG2 |
| 6661 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
| 6662 | #endif |
| 6663 | #ifdef _SC_XOPEN_XPG3 |
| 6664 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
| 6665 | #endif |
| 6666 | #ifdef _SC_XOPEN_XPG4 |
| 6667 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
| 6668 | #endif |
| 6669 | }; |
| 6670 | |
| 6671 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6672 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6673 | { |
| 6674 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 6675 | sizeof(posix_constants_sysconf) |
| 6676 | / sizeof(struct constdef)); |
| 6677 | } |
| 6678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6680 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6681 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6682 | |
| 6683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6684 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6685 | { |
| 6686 | PyObject *result = NULL; |
| 6687 | int name; |
| 6688 | |
| 6689 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 6690 | int value; |
| 6691 | |
| 6692 | errno = 0; |
| 6693 | value = sysconf(name); |
| 6694 | if (value == -1 && errno != 0) |
| 6695 | posix_error(); |
| 6696 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6697 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6698 | } |
| 6699 | return result; |
| 6700 | } |
| 6701 | #endif |
| 6702 | |
| 6703 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6704 | /* This code is used to ensure that the tables of configuration value names |
| 6705 | * are in sorted order as required by conv_confname(), and also to build the |
| 6706 | * the exported dictionaries that are used to publish information about the |
| 6707 | * names available on the host platform. |
| 6708 | * |
| 6709 | * Sorting the table at runtime ensures that the table is properly ordered |
| 6710 | * when used, even for platforms we're not able to test on. It also makes |
| 6711 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6712 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6713 | |
| 6714 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6715 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6716 | { |
| 6717 | const struct constdef *c1 = |
| 6718 | (const struct constdef *) v1; |
| 6719 | const struct constdef *c2 = |
| 6720 | (const struct constdef *) v2; |
| 6721 | |
| 6722 | return strcmp(c1->name, c2->name); |
| 6723 | } |
| 6724 | |
| 6725 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6726 | setup_confname_table(struct constdef *table, size_t tablesize, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6727 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6728 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6729 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6730 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6731 | |
| 6732 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 6733 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6734 | if (d == NULL) |
| 6735 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6736 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6737 | for (i=0; i < tablesize; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6738 | PyObject *o = PyLong_FromLong(table[i].value); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6739 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 6740 | Py_XDECREF(o); |
| 6741 | Py_DECREF(d); |
| 6742 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6743 | } |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 6744 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6745 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6746 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6747 | } |
| 6748 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6749 | /* Return -1 on failure, 0 on success. */ |
| 6750 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6751 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6752 | { |
| 6753 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6754 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6755 | sizeof(posix_constants_pathconf) |
| 6756 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6757 | "pathconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6758 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6759 | #endif |
| 6760 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6761 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6762 | sizeof(posix_constants_confstr) |
| 6763 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6764 | "confstr_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6765 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6766 | #endif |
| 6767 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6768 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6769 | sizeof(posix_constants_sysconf) |
| 6770 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 6771 | "sysconf_names", module)) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6772 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6773 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6774 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6775 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 6776 | |
| 6777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6778 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6779 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6780 | 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] | 6781 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6782 | |
| 6783 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6784 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6785 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6786 | abort(); |
| 6787 | /*NOTREACHED*/ |
| 6788 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 6789 | return NULL; |
| 6790 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 6791 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6792 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6793 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6794 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 6795 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6796 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6797 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 6798 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 6799 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 6800 | application (if any) its extension is associated.\n\ |
| 6801 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 6802 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6803 | \n\ |
| 6804 | startfile returns as soon as the associated application is launched.\n\ |
| 6805 | There is no option to wait for the application to close, and no way\n\ |
| 6806 | to retrieve the application's exit status.\n\ |
| 6807 | \n\ |
| 6808 | The filepath is relative to the current directory. If you want to use\n\ |
| 6809 | 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] | 6810 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6811 | |
| 6812 | static PyObject * |
| 6813 | win32_startfile(PyObject *self, PyObject *args) |
| 6814 | { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6815 | PyObject *ofilepath; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6816 | char *filepath; |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6817 | char *operation = NULL; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6818 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6819 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6820 | PyObject *unipath, *woperation = NULL; |
| 6821 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 6822 | &unipath, &operation)) { |
| 6823 | PyErr_Clear(); |
| 6824 | goto normal; |
| 6825 | } |
| 6826 | |
| 6827 | if (operation) { |
| 6828 | woperation = PyUnicode_DecodeASCII(operation, |
| 6829 | strlen(operation), NULL); |
| 6830 | if (!woperation) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6831 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6832 | operation = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6833 | goto normal; |
| 6834 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6835 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 6836 | |
| 6837 | Py_BEGIN_ALLOW_THREADS |
| 6838 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 6839 | PyUnicode_AS_UNICODE(unipath), |
| 6840 | NULL, NULL, SW_SHOWNORMAL); |
| 6841 | Py_END_ALLOW_THREADS |
| 6842 | |
| 6843 | Py_XDECREF(woperation); |
| 6844 | if (rc <= (HINSTANCE)32) { |
| 6845 | PyObject *errval = win32_error_unicode("startfile", |
| 6846 | PyUnicode_AS_UNICODE(unipath)); |
| 6847 | return errval; |
| 6848 | } |
| 6849 | Py_INCREF(Py_None); |
| 6850 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6851 | |
| 6852 | normal: |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6853 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 6854 | PyUnicode_FSConverter, &ofilepath, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6855 | &operation)) |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6856 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6857 | filepath = bytes2str(ofilepath, 1); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6858 | Py_BEGIN_ALLOW_THREADS |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 6859 | rc = ShellExecute((HWND)0, operation, filepath, |
| 6860 | NULL, NULL, SW_SHOWNORMAL); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6861 | Py_END_ALLOW_THREADS |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6862 | if (rc <= (HINSTANCE)32) { |
| 6863 | PyObject *errval = win32_error("startfile", filepath); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6864 | release_bytes(ofilepath); |
Georg Brandl | e9f8ec9 | 2005-09-25 06:16:40 +0000 | [diff] [blame] | 6865 | return errval; |
| 6866 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6867 | release_bytes(ofilepath); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 6868 | Py_INCREF(Py_None); |
| 6869 | return Py_None; |
| 6870 | } |
| 6871 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 6872 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6873 | #ifdef HAVE_GETLOADAVG |
| 6874 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 6875 | "getloadavg() -> (float, float, float)\n\n\ |
| 6876 | Return the number of processes in the system run queue averaged over\n\ |
| 6877 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 6878 | was unobtainable"); |
| 6879 | |
| 6880 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6881 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6882 | { |
| 6883 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6884 | if (getloadavg(loadavg, 3)!=3) { |
| 6885 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 6886 | return NULL; |
| 6887 | } else |
| 6888 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
| 6889 | } |
| 6890 | #endif |
| 6891 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6892 | #ifdef MS_WINDOWS |
| 6893 | |
| 6894 | PyDoc_STRVAR(win32_urandom__doc__, |
| 6895 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 6896 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6897 | |
| 6898 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 6899 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 6900 | DWORD dwFlags ); |
| 6901 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 6902 | BYTE *pbBuffer ); |
| 6903 | |
| 6904 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 6905 | /* This handle is never explicitly released. Instead, the operating |
| 6906 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6907 | static HCRYPTPROV hCryptProv = 0; |
| 6908 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6909 | static PyObject* |
| 6910 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6911 | { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6912 | int howMany; |
| 6913 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6914 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6915 | /* Read arguments */ |
Tim Peters | 9b279a8 | 2004-08-30 17:10:53 +0000 | [diff] [blame] | 6916 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6917 | return NULL; |
Tim Peters | 51eba61 | 2004-08-30 17:08:02 +0000 | [diff] [blame] | 6918 | if (howMany < 0) |
| 6919 | return PyErr_Format(PyExc_ValueError, |
| 6920 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6921 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6922 | if (hCryptProv == 0) { |
| 6923 | HINSTANCE hAdvAPI32 = NULL; |
| 6924 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6925 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6926 | /* Obtain handle to the DLL containing CryptoAPI |
| 6927 | This should not fail */ |
| 6928 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 6929 | if(hAdvAPI32 == NULL) |
| 6930 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6931 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6932 | /* Obtain pointers to the CryptoAPI functions |
| 6933 | This will fail on some early versions of Win95 */ |
| 6934 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 6935 | hAdvAPI32, |
| 6936 | "CryptAcquireContextA"); |
| 6937 | if (pCryptAcquireContext == NULL) |
| 6938 | return PyErr_Format(PyExc_NotImplementedError, |
| 6939 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6940 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6941 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 6942 | hAdvAPI32, "CryptGenRandom"); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6943 | if (pCryptGenRandom == NULL) |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6944 | return PyErr_Format(PyExc_NotImplementedError, |
| 6945 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6946 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6947 | /* Acquire context */ |
| 6948 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 6949 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 6950 | return win32_error("CryptAcquireContext", NULL); |
| 6951 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6952 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6953 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6954 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6955 | if (result != NULL) { |
| 6956 | /* Get random data */ |
Amaury Forgeot d'Arc | a05ada3 | 2008-07-21 21:13:14 +0000 | [diff] [blame] | 6957 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6958 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6959 | PyBytes_AS_STRING(result))) { |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6960 | Py_DECREF(result); |
| 6961 | return win32_error("CryptGenRandom", NULL); |
| 6962 | } |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 6963 | } |
Tim Peters | d311538 | 2004-08-30 17:36:46 +0000 | [diff] [blame] | 6964 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 6965 | } |
| 6966 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 6967 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6968 | PyDoc_STRVAR(device_encoding__doc__, |
| 6969 | "device_encoding(fd) -> str\n\n\ |
| 6970 | Return a string describing the encoding of the device\n\ |
| 6971 | if the output is a terminal; else return None."); |
| 6972 | |
| 6973 | static PyObject * |
| 6974 | device_encoding(PyObject *self, PyObject *args) |
| 6975 | { |
| 6976 | int fd; |
| 6977 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 6978 | return NULL; |
Kristján Valur Jónsson | 649170b | 2009-03-24 14:15:49 +0000 | [diff] [blame] | 6979 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6980 | Py_INCREF(Py_None); |
| 6981 | return Py_None; |
| 6982 | } |
| 6983 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 6984 | if (fd == 0) { |
| 6985 | char buf[100]; |
| 6986 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 6987 | return PyUnicode_FromString(buf); |
| 6988 | } |
| 6989 | if (fd == 1 || fd == 2) { |
| 6990 | char buf[100]; |
| 6991 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 6992 | return PyUnicode_FromString(buf); |
| 6993 | } |
| 6994 | #elif defined(CODESET) |
| 6995 | { |
| 6996 | char *codeset = nl_langinfo(CODESET); |
Mark Dickinson | da2706b | 2008-12-11 18:03:03 +0000 | [diff] [blame] | 6997 | if (codeset != NULL && codeset[0] != 0) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 6998 | return PyUnicode_FromString(codeset); |
| 6999 | } |
| 7000 | #endif |
| 7001 | Py_INCREF(Py_None); |
| 7002 | return Py_None; |
| 7003 | } |
| 7004 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7005 | #ifdef __VMS |
| 7006 | /* Use openssl random routine */ |
| 7007 | #include <openssl/rand.h> |
| 7008 | PyDoc_STRVAR(vms_urandom__doc__, |
| 7009 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 7010 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7011 | |
| 7012 | static PyObject* |
| 7013 | vms_urandom(PyObject *self, PyObject *args) |
| 7014 | { |
| 7015 | int howMany; |
| 7016 | PyObject* result; |
| 7017 | |
| 7018 | /* Read arguments */ |
| 7019 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 7020 | return NULL; |
| 7021 | if (howMany < 0) |
| 7022 | return PyErr_Format(PyExc_ValueError, |
| 7023 | "negative argument not allowed"); |
| 7024 | |
| 7025 | /* Allocate bytes */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7026 | result = PyBytes_FromStringAndSize(NULL, howMany); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | if (result != NULL) { |
| 7028 | /* Get random data */ |
| 7029 | if (RAND_pseudo_bytes((unsigned char*) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7030 | PyBytes_AS_STRING(result), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7031 | howMany) < 0) { |
| 7032 | Py_DECREF(result); |
| 7033 | return PyErr_Format(PyExc_ValueError, |
| 7034 | "RAND_pseudo_bytes"); |
| 7035 | } |
| 7036 | } |
| 7037 | return result; |
| 7038 | } |
| 7039 | #endif |
| 7040 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7041 | #ifdef HAVE_SETRESUID |
| 7042 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 7043 | "setresuid(ruid, euid, suid)\n\n\ |
| 7044 | Set the current process's real, effective, and saved user ids."); |
| 7045 | |
| 7046 | static PyObject* |
| 7047 | posix_setresuid (PyObject *self, PyObject *args) |
| 7048 | { |
| 7049 | /* We assume uid_t is no larger than a long. */ |
| 7050 | long ruid, euid, suid; |
| 7051 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 7052 | return NULL; |
| 7053 | if (setresuid(ruid, euid, suid) < 0) |
| 7054 | return posix_error(); |
| 7055 | Py_RETURN_NONE; |
| 7056 | } |
| 7057 | #endif |
| 7058 | |
| 7059 | #ifdef HAVE_SETRESGID |
| 7060 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 7061 | "setresgid(rgid, egid, sgid)\n\n\ |
| 7062 | Set the current process's real, effective, and saved group ids."); |
| 7063 | |
| 7064 | static PyObject* |
| 7065 | posix_setresgid (PyObject *self, PyObject *args) |
| 7066 | { |
| 7067 | /* We assume uid_t is no larger than a long. */ |
| 7068 | long rgid, egid, sgid; |
| 7069 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 7070 | return NULL; |
| 7071 | if (setresgid(rgid, egid, sgid) < 0) |
| 7072 | return posix_error(); |
| 7073 | Py_RETURN_NONE; |
| 7074 | } |
| 7075 | #endif |
| 7076 | |
| 7077 | #ifdef HAVE_GETRESUID |
| 7078 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 7079 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 7080 | Get tuple of the current process's real, effective, and saved user ids."); |
| 7081 | |
| 7082 | static PyObject* |
| 7083 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 7084 | { |
| 7085 | uid_t ruid, euid, suid; |
| 7086 | long l_ruid, l_euid, l_suid; |
| 7087 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 7088 | return posix_error(); |
| 7089 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 7090 | l_ruid = ruid; |
| 7091 | l_euid = euid; |
| 7092 | l_suid = suid; |
| 7093 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
| 7094 | } |
| 7095 | #endif |
| 7096 | |
| 7097 | #ifdef HAVE_GETRESGID |
| 7098 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 7099 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
| 7100 | Get tuple of the current process's real, effective, and saved user ids."); |
| 7101 | |
| 7102 | static PyObject* |
| 7103 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 7104 | { |
| 7105 | uid_t rgid, egid, sgid; |
| 7106 | long l_rgid, l_egid, l_sgid; |
| 7107 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 7108 | return posix_error(); |
| 7109 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 7110 | l_rgid = rgid; |
| 7111 | l_egid = egid; |
| 7112 | l_sgid = sgid; |
| 7113 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
| 7114 | } |
| 7115 | #endif |
| 7116 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7117 | static PyMethodDef posix_methods[] = { |
| 7118 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
| 7119 | #ifdef HAVE_TTYNAME |
| 7120 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
| 7121 | #endif |
| 7122 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7123 | #ifdef HAVE_CHFLAGS |
| 7124 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
| 7125 | #endif /* HAVE_CHFLAGS */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7126 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_FCHMOD |
| 7128 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
| 7129 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7130 | #ifdef HAVE_CHOWN |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7131 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7132 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 7133 | #ifdef HAVE_LCHMOD |
| 7134 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
| 7135 | #endif /* HAVE_LCHMOD */ |
| 7136 | #ifdef HAVE_FCHOWN |
| 7137 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
| 7138 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 7139 | #ifdef HAVE_LCHFLAGS |
| 7140 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
| 7141 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_LCHOWN |
| 7143 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
| 7144 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 7145 | #ifdef HAVE_CHROOT |
| 7146 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
| 7147 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7148 | #ifdef HAVE_CTERMID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7149 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7150 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7151 | #ifdef HAVE_GETCWD |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 7152 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 7153 | METH_NOARGS, posix_getcwd__doc__}, |
| 7154 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 7155 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 7156 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7157 | #ifdef HAVE_LINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7158 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7159 | #endif /* HAVE_LINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7160 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
| 7161 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 7162 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7163 | #ifdef HAVE_NICE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7164 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7165 | #endif /* HAVE_NICE */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7166 | #ifdef HAVE_READLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7167 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7168 | #endif /* HAVE_READLINK */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7169 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 7170 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 7171 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 7172 | {"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] | 7173 | #ifdef HAVE_SYMLINK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7174 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7175 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7176 | #ifdef HAVE_SYSTEM |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7177 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7178 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7179 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7180 | #ifdef HAVE_UNAME |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7181 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7182 | #endif /* HAVE_UNAME */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7183 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 7184 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 7185 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7186 | #ifdef HAVE_TIMES |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7187 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7188 | #endif /* HAVE_TIMES */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7189 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7190 | #ifdef HAVE_EXECV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7191 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 7192 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7193 | #endif /* HAVE_EXECV */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7194 | #ifdef HAVE_SPAWNV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7195 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 7196 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 7197 | #if defined(PYOS_OS2) |
| 7198 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 7199 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
| 7200 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 7201 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7202 | #ifdef HAVE_FORK1 |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7203 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 7204 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7205 | #ifdef HAVE_FORK |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7206 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7207 | #endif /* HAVE_FORK */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7208 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7209 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7210 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7211 | #ifdef HAVE_FORKPTY |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7212 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7213 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7214 | #ifdef HAVE_GETEGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7215 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7216 | #endif /* HAVE_GETEGID */ |
| 7217 | #ifdef HAVE_GETEUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7218 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7219 | #endif /* HAVE_GETEUID */ |
| 7220 | #ifdef HAVE_GETGID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7221 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7222 | #endif /* HAVE_GETGID */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7223 | #ifdef HAVE_GETGROUPS |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7224 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7225 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7226 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7227 | #ifdef HAVE_GETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7228 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7229 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7230 | #ifdef HAVE_GETPPID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7231 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7232 | #endif /* HAVE_GETPPID */ |
| 7233 | #ifdef HAVE_GETUID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7234 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7235 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7236 | #ifdef HAVE_GETLOGIN |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7237 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7238 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7239 | #ifdef HAVE_KILL |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7240 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7241 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 7242 | #ifdef HAVE_KILLPG |
| 7243 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
| 7244 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7245 | #ifdef HAVE_PLOCK |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7246 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7247 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7248 | #ifdef MS_WINDOWS |
| 7249 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7250 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 7251 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7252 | #ifdef HAVE_SETUID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7253 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7254 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7255 | #ifdef HAVE_SETEUID |
| 7256 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
| 7257 | #endif /* HAVE_SETEUID */ |
| 7258 | #ifdef HAVE_SETEGID |
| 7259 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
| 7260 | #endif /* HAVE_SETEGID */ |
| 7261 | #ifdef HAVE_SETREUID |
| 7262 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
| 7263 | #endif /* HAVE_SETREUID */ |
| 7264 | #ifdef HAVE_SETREGID |
| 7265 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
| 7266 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7267 | #ifdef HAVE_SETGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7268 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7269 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7270 | #ifdef HAVE_SETGROUPS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 7271 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7272 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7273 | #ifdef HAVE_INITGROUPS |
| 7274 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
| 7275 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7276 | #ifdef HAVE_GETPGID |
| 7277 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
| 7278 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7279 | #ifdef HAVE_SETPGRP |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7280 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7281 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7282 | #ifdef HAVE_WAIT |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7283 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7284 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7285 | #ifdef HAVE_WAIT3 |
| 7286 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
| 7287 | #endif /* HAVE_WAIT3 */ |
| 7288 | #ifdef HAVE_WAIT4 |
| 7289 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
| 7290 | #endif /* HAVE_WAIT4 */ |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7291 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7292 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7293 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 7294 | #ifdef HAVE_GETSID |
| 7295 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
| 7296 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7297 | #ifdef HAVE_SETSID |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7298 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7299 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7300 | #ifdef HAVE_SETPGID |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7301 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7302 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7303 | #ifdef HAVE_TCGETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7304 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7305 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7306 | #ifdef HAVE_TCSETPGRP |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7307 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7308 | #endif /* HAVE_TCSETPGRP */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7309 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 7310 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 7311 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 7312 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7313 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 7314 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
| 7315 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 7316 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
| 7317 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
| 7318 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7319 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7320 | #ifdef HAVE_PIPE |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7321 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7322 | #endif |
| 7323 | #ifdef HAVE_MKFIFO |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7324 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7325 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7326 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7327 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
| 7328 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7329 | #ifdef HAVE_DEVICE_MACROS |
| 7330 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 7331 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 7332 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
| 7333 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7334 | #ifdef HAVE_FTRUNCATE |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7335 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7336 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7337 | #ifdef HAVE_PUTENV |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7338 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7339 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7340 | #ifdef HAVE_UNSETENV |
| 7341 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
| 7342 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7343 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7344 | #ifdef HAVE_FCHDIR |
| 7345 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
| 7346 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7347 | #ifdef HAVE_FSYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7348 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7349 | #endif |
| 7350 | #ifdef HAVE_FDATASYNC |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7351 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 7352 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7353 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7354 | #ifdef WCOREDUMP |
| 7355 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
| 7356 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7357 | #ifdef WIFCONTINUED |
| 7358 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
| 7359 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7360 | #ifdef WIFSTOPPED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7361 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7362 | #endif /* WIFSTOPPED */ |
| 7363 | #ifdef WIFSIGNALED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7364 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7365 | #endif /* WIFSIGNALED */ |
| 7366 | #ifdef WIFEXITED |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7367 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7368 | #endif /* WIFEXITED */ |
| 7369 | #ifdef WEXITSTATUS |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7370 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7371 | #endif /* WEXITSTATUS */ |
| 7372 | #ifdef WTERMSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7373 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7374 | #endif /* WTERMSIG */ |
| 7375 | #ifdef WSTOPSIG |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7376 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7377 | #endif /* WSTOPSIG */ |
| 7378 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7379 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7380 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7381 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7382 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7383 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7384 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7385 | #ifdef HAVE_CONFSTR |
| 7386 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
| 7387 | #endif |
| 7388 | #ifdef HAVE_SYSCONF |
| 7389 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
| 7390 | #endif |
| 7391 | #ifdef HAVE_FPATHCONF |
| 7392 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
| 7393 | #endif |
| 7394 | #ifdef HAVE_PATHCONF |
| 7395 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
| 7396 | #endif |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7397 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7398 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 7399 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
| 7400 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7401 | #ifdef HAVE_GETLOADAVG |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7402 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 7403 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 7404 | #ifdef MS_WINDOWS |
| 7405 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
| 7406 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7407 | #ifdef __VMS |
| 7408 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
| 7409 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 7410 | #ifdef HAVE_SETRESUID |
| 7411 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
| 7412 | #endif |
| 7413 | #ifdef HAVE_SETRESGID |
| 7414 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
| 7415 | #endif |
| 7416 | #ifdef HAVE_GETRESUID |
| 7417 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
| 7418 | #endif |
| 7419 | #ifdef HAVE_GETRESGID |
| 7420 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
| 7421 | #endif |
| 7422 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7423 | {NULL, NULL} /* Sentinel */ |
| 7424 | }; |
| 7425 | |
| 7426 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7427 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7428 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7429 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7430 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7433 | #if defined(PYOS_OS2) |
| 7434 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7435 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7436 | { |
| 7437 | APIRET rc; |
| 7438 | ULONG values[QSV_MAX+1]; |
| 7439 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 7440 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7441 | |
| 7442 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 7443 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7444 | Py_END_ALLOW_THREADS |
| 7445 | |
| 7446 | if (rc != NO_ERROR) { |
| 7447 | os2_error(rc); |
| 7448 | return -1; |
| 7449 | } |
| 7450 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7451 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 7452 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 7453 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 7454 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 7455 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 7456 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 7457 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7458 | |
| 7459 | switch (values[QSV_VERSION_MINOR]) { |
| 7460 | case 0: ver = "2.00"; break; |
| 7461 | case 10: ver = "2.10"; break; |
| 7462 | case 11: ver = "2.11"; break; |
| 7463 | case 30: ver = "3.00"; break; |
| 7464 | case 40: ver = "4.00"; break; |
| 7465 | case 50: ver = "5.00"; break; |
| 7466 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 7467 | PyOS_snprintf(tmp, sizeof(tmp), |
| 7468 | "%d-%d", values[QSV_VERSION_MAJOR], |
| 7469 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7470 | ver = &tmp[0]; |
| 7471 | } |
| 7472 | |
| 7473 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7474 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7475 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7476 | |
| 7477 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 7478 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 7479 | tmp[1] = ':'; |
| 7480 | tmp[2] = '\0'; |
| 7481 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7482 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7483 | } |
| 7484 | #endif |
| 7485 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7486 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7487 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7488 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7489 | #ifdef F_OK |
| 7490 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7491 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7492 | #ifdef R_OK |
| 7493 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7494 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7495 | #ifdef W_OK |
| 7496 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7497 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7498 | #ifdef X_OK |
| 7499 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7500 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7501 | #ifdef NGROUPS_MAX |
| 7502 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
| 7503 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 7504 | #ifdef TMP_MAX |
| 7505 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
| 7506 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7507 | #ifdef WCONTINUED |
| 7508 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
| 7509 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7510 | #ifdef WNOHANG |
| 7511 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7512 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7513 | #ifdef WUNTRACED |
| 7514 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
| 7515 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7516 | #ifdef O_RDONLY |
| 7517 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
| 7518 | #endif |
| 7519 | #ifdef O_WRONLY |
| 7520 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
| 7521 | #endif |
| 7522 | #ifdef O_RDWR |
| 7523 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
| 7524 | #endif |
| 7525 | #ifdef O_NDELAY |
| 7526 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
| 7527 | #endif |
| 7528 | #ifdef O_NONBLOCK |
| 7529 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
| 7530 | #endif |
| 7531 | #ifdef O_APPEND |
| 7532 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
| 7533 | #endif |
| 7534 | #ifdef O_DSYNC |
| 7535 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
| 7536 | #endif |
| 7537 | #ifdef O_RSYNC |
| 7538 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
| 7539 | #endif |
| 7540 | #ifdef O_SYNC |
| 7541 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
| 7542 | #endif |
| 7543 | #ifdef O_NOCTTY |
| 7544 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
| 7545 | #endif |
| 7546 | #ifdef O_CREAT |
| 7547 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
| 7548 | #endif |
| 7549 | #ifdef O_EXCL |
| 7550 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
| 7551 | #endif |
| 7552 | #ifdef O_TRUNC |
| 7553 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
| 7554 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 7555 | #ifdef O_BINARY |
| 7556 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
| 7557 | #endif |
| 7558 | #ifdef O_TEXT |
| 7559 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
| 7560 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7561 | #ifdef O_LARGEFILE |
| 7562 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
| 7563 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 7564 | #ifdef O_SHLOCK |
| 7565 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
| 7566 | #endif |
| 7567 | #ifdef O_EXLOCK |
| 7568 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
| 7569 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7570 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7571 | /* MS Windows */ |
| 7572 | #ifdef O_NOINHERIT |
| 7573 | /* Don't inherit in child processes. */ |
| 7574 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
| 7575 | #endif |
| 7576 | #ifdef _O_SHORT_LIVED |
| 7577 | /* Optimize for short life (keep in memory). */ |
| 7578 | /* MS forgot to define this one with a non-underscore form too. */ |
| 7579 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
| 7580 | #endif |
| 7581 | #ifdef O_TEMPORARY |
| 7582 | /* Automatically delete when last handle is closed. */ |
| 7583 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
| 7584 | #endif |
| 7585 | #ifdef O_RANDOM |
| 7586 | /* Optimize for random access. */ |
| 7587 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
| 7588 | #endif |
| 7589 | #ifdef O_SEQUENTIAL |
| 7590 | /* Optimize for sequential access. */ |
| 7591 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
| 7592 | #endif |
| 7593 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7594 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 7595 | #ifdef O_ASYNC |
| 7596 | /* Send a SIGIO signal whenever input or output |
| 7597 | becomes available on file descriptor */ |
| 7598 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
| 7599 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 7600 | #ifdef O_DIRECT |
| 7601 | /* Direct disk access. */ |
| 7602 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
| 7603 | #endif |
| 7604 | #ifdef O_DIRECTORY |
| 7605 | /* Must be a directory. */ |
| 7606 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
| 7607 | #endif |
| 7608 | #ifdef O_NOFOLLOW |
| 7609 | /* Do not follow links. */ |
| 7610 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
| 7611 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 7612 | #ifdef O_NOATIME |
| 7613 | /* Do not update the access time. */ |
| 7614 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
| 7615 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7616 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7617 | /* These come from sysexits.h */ |
| 7618 | #ifdef EX_OK |
| 7619 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7620 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7621 | #ifdef EX_USAGE |
| 7622 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7623 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7624 | #ifdef EX_DATAERR |
| 7625 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7626 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7627 | #ifdef EX_NOINPUT |
| 7628 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7629 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7630 | #ifdef EX_NOUSER |
| 7631 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7632 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7633 | #ifdef EX_NOHOST |
| 7634 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7635 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7636 | #ifdef EX_UNAVAILABLE |
| 7637 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7638 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7639 | #ifdef EX_SOFTWARE |
| 7640 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7641 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7642 | #ifdef EX_OSERR |
| 7643 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7644 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7645 | #ifdef EX_OSFILE |
| 7646 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7647 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7648 | #ifdef EX_CANTCREAT |
| 7649 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7650 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7651 | #ifdef EX_IOERR |
| 7652 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7653 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7654 | #ifdef EX_TEMPFAIL |
| 7655 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7656 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7657 | #ifdef EX_PROTOCOL |
| 7658 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7659 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7660 | #ifdef EX_NOPERM |
| 7661 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7662 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7663 | #ifdef EX_CONFIG |
| 7664 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7665 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7666 | #ifdef EX_NOTFOUND |
| 7667 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 7668 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 7669 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7670 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7671 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 7672 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 7673 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 7674 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 7675 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 7676 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 7677 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 7678 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 7679 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 7680 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 7681 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 7682 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 7683 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 7684 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 7685 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 7686 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 7687 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 7688 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 7689 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 7690 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 7691 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
| 7692 | #else |
Guido van Rossum | 7d38529 | 1999-02-16 19:38:04 +0000 | [diff] [blame] | 7693 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 7694 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 7695 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 7696 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 7697 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7698 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 7699 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 7700 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7701 | #if defined(PYOS_OS2) |
| 7702 | if (insertvalues(d)) return -1; |
| 7703 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7704 | return 0; |
| 7705 | } |
| 7706 | |
| 7707 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7708 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7709 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7710 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7711 | |
| 7712 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7713 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7714 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 7715 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7716 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7717 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7718 | #define MODNAME "posix" |
| 7719 | #endif |
| 7720 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7721 | static struct PyModuleDef posixmodule = { |
| 7722 | PyModuleDef_HEAD_INIT, |
| 7723 | MODNAME, |
| 7724 | posix__doc__, |
| 7725 | -1, |
| 7726 | posix_methods, |
| 7727 | NULL, |
| 7728 | NULL, |
| 7729 | NULL, |
| 7730 | NULL |
| 7731 | }; |
| 7732 | |
| 7733 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 7734 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 7735 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7736 | { |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7737 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7738 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7739 | m = PyModule_Create(&posixmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 7740 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7741 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7742 | |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 7743 | /* Initialize environ dictionary */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7744 | v = convertenviron(); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7745 | Py_XINCREF(v); |
| 7746 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7747 | return NULL; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7748 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7749 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7750 | if (all_ins(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7751 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 7752 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7753 | if (setup_confname_tables(m)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7754 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 7755 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7756 | Py_INCREF(PyExc_OSError); |
| 7757 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7758 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7759 | #ifdef HAVE_PUTENV |
Neil Schemenauer | 19030a0 | 2001-01-16 04:27:47 +0000 | [diff] [blame] | 7760 | if (posix_putenv_garbage == NULL) |
| 7761 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 7762 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 7763 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7764 | if (!initialized) { |
| 7765 | stat_result_desc.name = MODNAME ".stat_result"; |
| 7766 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 7767 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 7768 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 7769 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 7770 | structseq_new = StatResultType.tp_new; |
| 7771 | StatResultType.tp_new = statresult_new; |
| 7772 | |
| 7773 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 7774 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 7775 | #ifdef NEED_TICKS_PER_SECOND |
| 7776 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
| 7777 | ticks_per_second = sysconf(_SC_CLK_TCK); |
| 7778 | # elif defined(HZ) |
| 7779 | ticks_per_second = HZ; |
| 7780 | # else |
| 7781 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
| 7782 | # endif |
| 7783 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7784 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7785 | Py_INCREF((PyObject*) &StatResultType); |
| 7786 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 7787 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 7788 | PyModule_AddObject(m, "statvfs_result", |
| 7789 | (PyObject*) &StatVFSResultType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7790 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7791 | |
| 7792 | #ifdef __APPLE__ |
| 7793 | /* |
| 7794 | * Step 2 of weak-linking support on Mac OS X. |
| 7795 | * |
| 7796 | * The code below removes functions that are not available on the |
| 7797 | * currently active platform. |
| 7798 | * |
| 7799 | * This block allow one to use a python binary that was build on |
| 7800 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 7801 | * OSX 10.4. |
| 7802 | */ |
| 7803 | #ifdef HAVE_FSTATVFS |
| 7804 | if (fstatvfs == NULL) { |
| 7805 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7806 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7807 | } |
| 7808 | } |
| 7809 | #endif /* HAVE_FSTATVFS */ |
| 7810 | |
| 7811 | #ifdef HAVE_STATVFS |
| 7812 | if (statvfs == NULL) { |
| 7813 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7814 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7815 | } |
| 7816 | } |
| 7817 | #endif /* HAVE_STATVFS */ |
| 7818 | |
| 7819 | # ifdef HAVE_LCHOWN |
| 7820 | if (lchown == NULL) { |
| 7821 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7822 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7823 | } |
| 7824 | } |
| 7825 | #endif /* HAVE_LCHOWN */ |
| 7826 | |
| 7827 | |
| 7828 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7829 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7830 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7831 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7832 | |
| 7833 | #ifdef __cplusplus |
| 7834 | } |
| 7835 | #endif |