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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 31 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #endif /* defined(__VMS) */ |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 47 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #define INCL_DOS |
| 49 | #define INCL_DOSERRORS |
| 50 | #define INCL_DOSPROCESS |
| 51 | #define INCL_NOPMAPI |
| 52 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 53 | #if defined(PYCC_GCC) |
| 54 | #include <ctype.h> |
| 55 | #include <io.h> |
| 56 | #include <stdio.h> |
| 57 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 59 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 62 | #ifdef HAVE_SYS_UIO_H |
| 63 | #include <sys/uio.h> |
| 64 | #endif |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_TYPES_H */ |
| 69 | |
| 70 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 75 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #ifdef HAVE_FCNTL_H |
| 83 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 84 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #endif |
| 89 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYSEXITS_H |
| 91 | #include <sysexits.h> |
| 92 | #endif /* HAVE_SYSEXITS_H */ |
| 93 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_LOADAVG_H |
| 95 | #include <sys/loadavg.h> |
| 96 | #endif |
| 97 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 98 | #ifdef HAVE_LANGINFO_H |
| 99 | #include <langinfo.h> |
| 100 | #endif |
| 101 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 102 | #ifdef HAVE_SYS_SENDFILE_H |
| 103 | #include <sys/sendfile.h> |
| 104 | #endif |
| 105 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 106 | #ifdef HAVE_SCHED_H |
| 107 | #include <sched.h> |
| 108 | #endif |
| 109 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 110 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 111 | #define USE_XATTRS |
| 112 | #endif |
| 113 | |
| 114 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 115 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 116 | #endif |
| 117 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 118 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 119 | #ifdef HAVE_SYS_SOCKET_H |
| 120 | #include <sys/socket.h> |
| 121 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 124 | #ifdef HAVE_DLFCN_H |
| 125 | #include <dlfcn.h> |
| 126 | #endif |
| 127 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 129 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 130 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 131 | #include <process.h> |
| 132 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 133 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_GETCWD 1 |
| 135 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 136 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #if defined(__OS2__) |
| 138 | #define HAVE_EXECV 1 |
| 139 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #include <process.h> |
| 142 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 143 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #define HAVE_EXECV 1 |
| 145 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_OPENDIR 1 |
| 147 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #define HAVE_WAIT 1 |
| 150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 152 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 153 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 154 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 155 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | #define HAVE_EXECV 1 |
| 157 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 158 | #define HAVE_SYSTEM 1 |
| 159 | #define HAVE_CWAIT 1 |
| 160 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 161 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 162 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 163 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 164 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 166 | /* Unix functions that the configure script doesn't check for */ |
| 167 | #define HAVE_EXECV 1 |
| 168 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 169 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 170 | #define HAVE_FORK1 1 |
| 171 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 172 | #define HAVE_GETCWD 1 |
| 173 | #define HAVE_GETEGID 1 |
| 174 | #define HAVE_GETEUID 1 |
| 175 | #define HAVE_GETGID 1 |
| 176 | #define HAVE_GETPPID 1 |
| 177 | #define HAVE_GETUID 1 |
| 178 | #define HAVE_KILL 1 |
| 179 | #define HAVE_OPENDIR 1 |
| 180 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 181 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 183 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 184 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #endif /* _MSC_VER */ |
| 186 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 187 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 188 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 191 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 192 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 193 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 194 | (default) */ |
| 195 | extern char *ctermid_r(char *); |
| 196 | #endif |
| 197 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 198 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 201 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 202 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 205 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 206 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #endif |
| 208 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 209 | extern int chdir(char *); |
| 210 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int chdir(const char *); |
| 213 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 214 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 215 | #ifdef __BORLANDC__ |
| 216 | extern int chmod(const char *, int); |
| 217 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 218 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 219 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 220 | /*#ifdef HAVE_FCHMOD |
| 221 | extern int fchmod(int, mode_t); |
| 222 | #endif*/ |
| 223 | /*#ifdef HAVE_LCHMOD |
| 224 | extern int lchmod(const char *, mode_t); |
| 225 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 226 | extern int chown(const char *, uid_t, gid_t); |
| 227 | extern char *getcwd(char *, int); |
| 228 | extern char *strerror(int); |
| 229 | extern int link(const char *, const char *); |
| 230 | extern int rename(const char *, const char *); |
| 231 | extern int stat(const char *, struct stat *); |
| 232 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 234 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 235 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 237 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 238 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 241 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_UTIME_H |
| 244 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 247 | #ifdef HAVE_SYS_UTIME_H |
| 248 | #include <sys/utime.h> |
| 249 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 250 | #endif /* HAVE_SYS_UTIME_H */ |
| 251 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #ifdef HAVE_SYS_TIMES_H |
| 253 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
| 256 | #ifdef HAVE_SYS_PARAM_H |
| 257 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 258 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
| 260 | #ifdef HAVE_SYS_UTSNAME_H |
| 261 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 267 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 268 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #include <direct.h> |
| 270 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 271 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 274 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 277 | #endif |
| 278 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 286 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 296 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 297 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 300 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 301 | #endif |
| 302 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 303 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 304 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 305 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 306 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 308 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 309 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 310 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 311 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 312 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 313 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 314 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 316 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 317 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 318 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 322 | #define MAXPATHLEN PATH_MAX |
| 323 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 326 | #endif /* MAXPATHLEN */ |
| 327 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 328 | #ifdef UNION_WAIT |
| 329 | /* Emulate some macros on systems that have a union instead of macros */ |
| 330 | |
| 331 | #ifndef WIFEXITED |
| 332 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WEXITSTATUS |
| 336 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WTERMSIG |
| 340 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 341 | #endif |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | #define WAIT_TYPE union wait |
| 344 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 345 | |
| 346 | #else /* !UNION_WAIT */ |
| 347 | #define WAIT_TYPE int |
| 348 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 349 | #endif /* UNION_WAIT */ |
| 350 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 351 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 352 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 353 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 354 | #define USE_CTERMID_R |
| 355 | #endif |
| 356 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 358 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 359 | #undef FSTAT |
| 360 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 361 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT win32_stat |
| 363 | # define FSTAT win32_fstat |
| 364 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT stat |
| 367 | # define FSTAT fstat |
| 368 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 369 | #endif |
| 370 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 371 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #include <sys/mkdev.h> |
| 373 | #else |
| 374 | #if defined(MAJOR_IN_SYSMACROS) |
| 375 | #include <sys/sysmacros.h> |
| 376 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 377 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 378 | #include <sys/mkdev.h> |
| 379 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 380 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 381 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 382 | /* A helper used by a number of POSIX-only functions */ |
| 383 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 384 | static int |
| 385 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 386 | { |
| 387 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 388 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 389 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 390 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 391 | #endif |
| 392 | if (PyErr_Occurred()) |
| 393 | return 0; |
| 394 | return 1; |
| 395 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 396 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 397 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 398 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 399 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 400 | * valid and throw an assertion if it isn't. |
| 401 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 402 | * an assertion can be useful, but it does contradict the POSIX standard |
| 403 | * which for write(2) states: |
| 404 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 405 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 406 | * writing." |
| 407 | * Furthermore, python allows the user to enter any old integer |
| 408 | * as a fd and should merely raise a python exception on error. |
| 409 | * The Microsoft CRT doesn't provide an official way to check for the |
| 410 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 411 | * by using the exported __pinfo data member and knowledge of the |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 412 | * internal structures involved. |
| 413 | * The structures below must be updated for each version of visual studio |
| 414 | * according to the file internal.h in the CRT source, until MS comes |
| 415 | * up with a less hacky way to do this. |
| 416 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 417 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 418 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 419 | /* The actual size of the structure is determined at runtime. |
| 420 | * Only the first items must be present. |
| 421 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 422 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 423 | intptr_t osfhnd; |
| 424 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 425 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 426 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 427 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 428 | #define IOINFO_L2E 5 |
| 429 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 430 | #define IOINFO_ARRAYS 64 |
| 431 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 432 | #define FOPEN 0x01 |
| 433 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 434 | |
| 435 | /* This function emulates what the windows CRT does to validate file handles */ |
| 436 | int |
| 437 | _PyVerify_fd(int fd) |
| 438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 439 | const int i1 = fd >> IOINFO_L2E; |
| 440 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 442 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 444 | /* Determine the actual size of the ioinfo structure, |
| 445 | * as used by the CRT loaded in memory |
| 446 | */ |
| 447 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 448 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 449 | } |
| 450 | if (sizeof_ioinfo == 0) { |
| 451 | /* This should not happen... */ |
| 452 | goto fail; |
| 453 | } |
| 454 | |
| 455 | /* See that it isn't a special CLEAR fileno */ |
| 456 | if (fd != _NO_CONSOLE_FILENO) { |
| 457 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 458 | * we check pointer validity and other info |
| 459 | */ |
| 460 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 461 | /* finally, check that the file is open */ |
| 462 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 463 | if (info->osfile & FOPEN) { |
| 464 | return 1; |
| 465 | } |
| 466 | } |
| 467 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 468 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 469 | errno = EBADF; |
| 470 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 474 | static int |
| 475 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 476 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 477 | if (!_PyVerify_fd(fd1)) |
| 478 | return 0; |
| 479 | if (fd2 == _NO_CONSOLE_FILENO) |
| 480 | return 0; |
| 481 | if ((unsigned)fd2 < _NHANDLE_) |
| 482 | return 1; |
| 483 | else |
| 484 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 485 | } |
| 486 | #else |
| 487 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 488 | #define _PyVerify_fd_dup2(A, B) (1) |
| 489 | #endif |
| 490 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 491 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 492 | /* The following structure was copied from |
| 493 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 494 | include doesn't seem to be present in the Windows SDK (at least as included |
| 495 | with Visual Studio Express). */ |
| 496 | typedef struct _REPARSE_DATA_BUFFER { |
| 497 | ULONG ReparseTag; |
| 498 | USHORT ReparseDataLength; |
| 499 | USHORT Reserved; |
| 500 | union { |
| 501 | struct { |
| 502 | USHORT SubstituteNameOffset; |
| 503 | USHORT SubstituteNameLength; |
| 504 | USHORT PrintNameOffset; |
| 505 | USHORT PrintNameLength; |
| 506 | ULONG Flags; |
| 507 | WCHAR PathBuffer[1]; |
| 508 | } SymbolicLinkReparseBuffer; |
| 509 | |
| 510 | struct { |
| 511 | USHORT SubstituteNameOffset; |
| 512 | USHORT SubstituteNameLength; |
| 513 | USHORT PrintNameOffset; |
| 514 | USHORT PrintNameLength; |
| 515 | WCHAR PathBuffer[1]; |
| 516 | } MountPointReparseBuffer; |
| 517 | |
| 518 | struct { |
| 519 | UCHAR DataBuffer[1]; |
| 520 | } GenericReparseBuffer; |
| 521 | }; |
| 522 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 523 | |
| 524 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 525 | GenericReparseBuffer) |
| 526 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 527 | |
| 528 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 529 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 530 | { |
| 531 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 532 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 533 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 534 | |
| 535 | if (0 == DeviceIoControl( |
| 536 | reparse_point_handle, |
| 537 | FSCTL_GET_REPARSE_POINT, |
| 538 | NULL, 0, /* in buffer */ |
| 539 | target_buffer, sizeof(target_buffer), |
| 540 | &n_bytes_returned, |
| 541 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 542 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 543 | |
| 544 | if (reparse_tag) |
| 545 | *reparse_tag = rdb->ReparseTag; |
| 546 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 547 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 548 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 549 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 552 | #ifdef WITH_NEXT_FRAMEWORK |
| 553 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 554 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 555 | */ |
| 556 | #include <crt_externs.h> |
| 557 | static char **environ; |
| 558 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 559 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 560 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 561 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 562 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 563 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 565 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 566 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 567 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 568 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 569 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 570 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 571 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 572 | APIRET rc; |
| 573 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 574 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 575 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 576 | d = PyDict_New(); |
| 577 | if (d == NULL) |
| 578 | return NULL; |
| 579 | #ifdef WITH_NEXT_FRAMEWORK |
| 580 | if (environ == NULL) |
| 581 | environ = *_NSGetEnviron(); |
| 582 | #endif |
| 583 | #ifdef MS_WINDOWS |
| 584 | /* _wenviron must be initialized in this way if the program is started |
| 585 | through main() instead of wmain(). */ |
| 586 | _wgetenv(L""); |
| 587 | if (_wenviron == NULL) |
| 588 | return d; |
| 589 | /* This part ignores errors */ |
| 590 | for (e = _wenviron; *e != NULL; e++) { |
| 591 | PyObject *k; |
| 592 | PyObject *v; |
| 593 | wchar_t *p = wcschr(*e, L'='); |
| 594 | if (p == NULL) |
| 595 | continue; |
| 596 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 597 | if (k == NULL) { |
| 598 | PyErr_Clear(); |
| 599 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 600 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 601 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 602 | if (v == NULL) { |
| 603 | PyErr_Clear(); |
| 604 | Py_DECREF(k); |
| 605 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 606 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 607 | if (PyDict_GetItem(d, k) == NULL) { |
| 608 | if (PyDict_SetItem(d, k, v) != 0) |
| 609 | PyErr_Clear(); |
| 610 | } |
| 611 | Py_DECREF(k); |
| 612 | Py_DECREF(v); |
| 613 | } |
| 614 | #else |
| 615 | if (environ == NULL) |
| 616 | return d; |
| 617 | /* This part ignores errors */ |
| 618 | for (e = environ; *e != NULL; e++) { |
| 619 | PyObject *k; |
| 620 | PyObject *v; |
| 621 | char *p = strchr(*e, '='); |
| 622 | if (p == NULL) |
| 623 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 624 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 625 | if (k == NULL) { |
| 626 | PyErr_Clear(); |
| 627 | continue; |
| 628 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 629 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 630 | if (v == NULL) { |
| 631 | PyErr_Clear(); |
| 632 | Py_DECREF(k); |
| 633 | continue; |
| 634 | } |
| 635 | if (PyDict_GetItem(d, k) == NULL) { |
| 636 | if (PyDict_SetItem(d, k, v) != 0) |
| 637 | PyErr_Clear(); |
| 638 | } |
| 639 | Py_DECREF(k); |
| 640 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 641 | } |
| 642 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 643 | #if defined(PYOS_OS2) |
| 644 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 645 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 646 | PyObject *v = PyBytes_FromString(buffer); |
| 647 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 648 | Py_DECREF(v); |
| 649 | } |
| 650 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 651 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 652 | PyObject *v = PyBytes_FromString(buffer); |
| 653 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 654 | Py_DECREF(v); |
| 655 | } |
| 656 | #endif |
| 657 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 660 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 661 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 662 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 663 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 665 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 667 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 668 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 670 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 673 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 674 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 675 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 676 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 677 | PyObject *name_str, *rc; |
| 678 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 679 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 680 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 681 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 682 | name_str); |
| 683 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 684 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 687 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 688 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 689 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 691 | /* XXX We should pass the function name along in the future. |
| 692 | (winreg.c also wants to pass the function name.) |
| 693 | This would however require an additional param to the |
| 694 | Windows error object, which is non-trivial. |
| 695 | */ |
| 696 | errno = GetLastError(); |
| 697 | if (filename) |
| 698 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 699 | else |
| 700 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 701 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 702 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 703 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 704 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 706 | /* XXX - see win32_error for comments on 'function' */ |
| 707 | errno = GetLastError(); |
| 708 | if (filename) |
| 709 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 710 | else |
| 711 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 714 | static PyObject * |
| 715 | win32_error_object(char* function, PyObject* filename) |
| 716 | { |
| 717 | /* XXX - see win32_error for comments on 'function' */ |
| 718 | errno = GetLastError(); |
| 719 | if (filename) |
| 720 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 721 | PyExc_WindowsError, |
| 722 | errno, |
| 723 | filename); |
| 724 | else |
| 725 | return PyErr_SetFromWindowsErr(errno); |
| 726 | } |
| 727 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 728 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 729 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 731 | if (PyUnicode_CheckExact(*param)) |
| 732 | Py_INCREF(*param); |
| 733 | else if (PyUnicode_Check(*param)) |
| 734 | /* For a Unicode subtype that's not a Unicode object, |
| 735 | return a true Unicode object with the same data. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 736 | *param = PyUnicode_Copy(*param); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 737 | else |
| 738 | *param = PyUnicode_FromEncodedObject(*param, |
| 739 | Py_FileSystemDefaultEncoding, |
| 740 | "strict"); |
| 741 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 744 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 746 | #if defined(PYOS_OS2) |
| 747 | /********************************************************************** |
| 748 | * Helper Function to Trim and Format OS/2 Messages |
| 749 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 750 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 751 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 752 | { |
| 753 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 754 | |
| 755 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 756 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 757 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 758 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 759 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 760 | } |
| 761 | |
| 762 | /* Add Optional Reason Text */ |
| 763 | if (reason) { |
| 764 | strcat(msgbuf, " : "); |
| 765 | strcat(msgbuf, reason); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /********************************************************************** |
| 770 | * Decode an OS/2 Operating System Error Code |
| 771 | * |
| 772 | * A convenience function to lookup an OS/2 error code and return a |
| 773 | * text message we can use to raise a Python exception. |
| 774 | * |
| 775 | * Notes: |
| 776 | * The messages for errors returned from the OS/2 kernel reside in |
| 777 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 778 | * |
| 779 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 780 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 781 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 782 | { |
| 783 | APIRET rc; |
| 784 | ULONG msglen; |
| 785 | |
| 786 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 787 | Py_BEGIN_ALLOW_THREADS |
| 788 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 789 | errorcode, "oso001.msg", &msglen); |
| 790 | Py_END_ALLOW_THREADS |
| 791 | |
| 792 | if (rc == NO_ERROR) |
| 793 | os2_formatmsg(msgbuf, msglen, reason); |
| 794 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 795 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 796 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 797 | |
| 798 | return msgbuf; |
| 799 | } |
| 800 | |
| 801 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 802 | errors are not in a global variable e.g. 'errno' nor are |
| 803 | they congruent with posix error numbers. */ |
| 804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 805 | static PyObject * |
| 806 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 807 | { |
| 808 | char text[1024]; |
| 809 | PyObject *v; |
| 810 | |
| 811 | os2_strerror(text, sizeof(text), code, ""); |
| 812 | |
| 813 | v = Py_BuildValue("(is)", code, text); |
| 814 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 815 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 816 | Py_DECREF(v); |
| 817 | } |
| 818 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 819 | } |
| 820 | |
| 821 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | |
| 823 | /* POSIX generic methods */ |
| 824 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 825 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 826 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 827 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 828 | int fd; |
| 829 | int res; |
| 830 | fd = PyObject_AsFileDescriptor(fdobj); |
| 831 | if (fd < 0) |
| 832 | return NULL; |
| 833 | if (!_PyVerify_fd(fd)) |
| 834 | return posix_error(); |
| 835 | Py_BEGIN_ALLOW_THREADS |
| 836 | res = (*func)(fd); |
| 837 | Py_END_ALLOW_THREADS |
| 838 | if (res < 0) |
| 839 | return posix_error(); |
| 840 | Py_INCREF(Py_None); |
| 841 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 842 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 843 | |
| 844 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 845 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 846 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 847 | PyObject *opath1 = NULL; |
| 848 | char *path1; |
| 849 | int res; |
| 850 | if (!PyArg_ParseTuple(args, format, |
| 851 | PyUnicode_FSConverter, &opath1)) |
| 852 | return NULL; |
| 853 | path1 = PyBytes_AsString(opath1); |
| 854 | Py_BEGIN_ALLOW_THREADS |
| 855 | res = (*func)(path1); |
| 856 | Py_END_ALLOW_THREADS |
| 857 | if (res < 0) |
| 858 | return posix_error_with_allocated_filename(opath1); |
| 859 | Py_DECREF(opath1); |
| 860 | Py_INCREF(Py_None); |
| 861 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 864 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 865 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 866 | char *format, |
| 867 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 868 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 869 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 870 | char *path1, *path2; |
| 871 | int res; |
| 872 | if (!PyArg_ParseTuple(args, format, |
| 873 | PyUnicode_FSConverter, &opath1, |
| 874 | PyUnicode_FSConverter, &opath2)) { |
| 875 | return NULL; |
| 876 | } |
| 877 | path1 = PyBytes_AsString(opath1); |
| 878 | path2 = PyBytes_AsString(opath2); |
| 879 | Py_BEGIN_ALLOW_THREADS |
| 880 | res = (*func)(path1, path2); |
| 881 | Py_END_ALLOW_THREADS |
| 882 | Py_DECREF(opath1); |
| 883 | Py_DECREF(opath2); |
| 884 | if (res != 0) |
| 885 | /* XXX how to report both path1 and path2??? */ |
| 886 | return posix_error(); |
| 887 | Py_INCREF(Py_None); |
| 888 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 891 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 892 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 893 | win32_1str(PyObject* args, char* func, |
| 894 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 895 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 897 | PyObject *uni; |
| 898 | char *ansi; |
| 899 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 900 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 901 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 902 | { |
| 903 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 904 | if (wstr == NULL) |
| 905 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 906 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 907 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 908 | Py_END_ALLOW_THREADS |
| 909 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 910 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
| 913 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 914 | PyErr_Clear(); |
| 915 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 916 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 917 | return NULL; |
| 918 | Py_BEGIN_ALLOW_THREADS |
| 919 | result = funcA(ansi); |
| 920 | Py_END_ALLOW_THREADS |
| 921 | if (!result) |
| 922 | return win32_error(func, ansi); |
| 923 | Py_INCREF(Py_None); |
| 924 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 925 | |
| 926 | } |
| 927 | |
| 928 | /* This is a reimplementation of the C library's chdir function, |
| 929 | but one that produces Win32 errors instead of DOS error codes. |
| 930 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 931 | it also needs to set "magic" environment variables indicating |
| 932 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 933 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 934 | win32_chdir(LPCSTR path) |
| 935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 936 | char new_path[MAX_PATH+1]; |
| 937 | int result; |
| 938 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 940 | if(!SetCurrentDirectoryA(path)) |
| 941 | return FALSE; |
| 942 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 943 | if (!result) |
| 944 | return FALSE; |
| 945 | /* In the ANSI API, there should not be any paths longer |
| 946 | than MAX_PATH. */ |
| 947 | assert(result <= MAX_PATH+1); |
| 948 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 949 | strncmp(new_path, "//", 2) == 0) |
| 950 | /* UNC path, nothing to do. */ |
| 951 | return TRUE; |
| 952 | env[1] = new_path[0]; |
| 953 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* The Unicode version differs from the ANSI version |
| 957 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 958 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 959 | win32_wchdir(LPCWSTR path) |
| 960 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 961 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 962 | int result; |
| 963 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 965 | if(!SetCurrentDirectoryW(path)) |
| 966 | return FALSE; |
| 967 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 968 | if (!result) |
| 969 | return FALSE; |
| 970 | if (result > MAX_PATH+1) { |
| 971 | new_path = malloc(result * sizeof(wchar_t)); |
| 972 | if (!new_path) { |
| 973 | SetLastError(ERROR_OUTOFMEMORY); |
| 974 | return FALSE; |
| 975 | } |
| 976 | result = GetCurrentDirectoryW(result, new_path); |
| 977 | if (!result) { |
| 978 | free(new_path); |
| 979 | return FALSE; |
| 980 | } |
| 981 | } |
| 982 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 983 | wcsncmp(new_path, L"//", 2) == 0) |
| 984 | /* UNC path, nothing to do. */ |
| 985 | return TRUE; |
| 986 | env[1] = new_path[0]; |
| 987 | result = SetEnvironmentVariableW(env, new_path); |
| 988 | if (new_path != _new_path) |
| 989 | free(new_path); |
| 990 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 991 | } |
| 992 | #endif |
| 993 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 994 | #ifdef MS_WINDOWS |
| 995 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 996 | - time stamps are restricted to second resolution |
| 997 | - file modification times suffer from forth-and-back conversions between |
| 998 | UTC and local time |
| 999 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1000 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1001 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1002 | |
| 1003 | struct win32_stat{ |
| 1004 | int st_dev; |
| 1005 | __int64 st_ino; |
| 1006 | unsigned short st_mode; |
| 1007 | int st_nlink; |
| 1008 | int st_uid; |
| 1009 | int st_gid; |
| 1010 | int st_rdev; |
| 1011 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1012 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1013 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1014 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1015 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1016 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1017 | int st_ctime_nsec; |
| 1018 | }; |
| 1019 | |
| 1020 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1021 | |
| 1022 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1023 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1025 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1026 | /* Cannot simply cast and dereference in_ptr, |
| 1027 | since it might not be aligned properly */ |
| 1028 | __int64 in; |
| 1029 | memcpy(&in, in_ptr, sizeof(in)); |
| 1030 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1031 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1034 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1035 | time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1036 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1037 | /* XXX endianness */ |
| 1038 | __int64 out; |
| 1039 | out = time_in + secs_between_epochs; |
| 1040 | out = out * 10000000 + nsec_in / 100; |
| 1041 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1044 | /* Below, we *know* that ugo+r is 0444 */ |
| 1045 | #if _S_IREAD != 0400 |
| 1046 | #error Unsupported C library |
| 1047 | #endif |
| 1048 | static int |
| 1049 | attributes_to_mode(DWORD attr) |
| 1050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1051 | int m = 0; |
| 1052 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1053 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1054 | else |
| 1055 | m |= _S_IFREG; |
| 1056 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1057 | m |= 0444; |
| 1058 | else |
| 1059 | m |= 0666; |
| 1060 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1064 | attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1065 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1066 | memset(result, 0, sizeof(*result)); |
| 1067 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1068 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1069 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1070 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1071 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1072 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1073 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1074 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1075 | /* first clear the S_IFMT bits */ |
| 1076 | result->st_mode ^= (result->st_mode & 0170000); |
| 1077 | /* now set the bits that make this a symlink */ |
| 1078 | result->st_mode |= 0120000; |
| 1079 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1081 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1084 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1085 | attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1086 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1087 | HANDLE hFindFile; |
| 1088 | WIN32_FIND_DATAA FileData; |
| 1089 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1090 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1091 | return FALSE; |
| 1092 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1093 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1094 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1095 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1096 | info->ftCreationTime = FileData.ftCreationTime; |
| 1097 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1098 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1099 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1100 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1101 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1102 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1103 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1104 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1108 | attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1109 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1110 | HANDLE hFindFile; |
| 1111 | WIN32_FIND_DATAW FileData; |
| 1112 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1113 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1114 | return FALSE; |
| 1115 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1116 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1117 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1118 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1119 | info->ftCreationTime = FileData.ftCreationTime; |
| 1120 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1121 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1122 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1123 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1124 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1125 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1126 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1127 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1130 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1131 | static int has_GetFinalPathNameByHandle = 0; |
| 1132 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1133 | DWORD); |
| 1134 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1135 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1136 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1137 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1138 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1139 | HINSTANCE hKernel32; |
| 1140 | /* only recheck */ |
| 1141 | if (!has_GetFinalPathNameByHandle) |
| 1142 | { |
| 1143 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1144 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1145 | "GetFinalPathNameByHandleA"); |
| 1146 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1147 | "GetFinalPathNameByHandleW"); |
| 1148 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1149 | Py_GetFinalPathNameByHandleW; |
| 1150 | } |
| 1151 | return has_GetFinalPathNameByHandle; |
| 1152 | } |
| 1153 | |
| 1154 | static BOOL |
| 1155 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1156 | { |
| 1157 | int buf_size, result_length; |
| 1158 | wchar_t *buf; |
| 1159 | |
| 1160 | /* We have a good handle to the target, use it to determine |
| 1161 | the target path name (then we'll call lstat on it). */ |
| 1162 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1163 | VOLUME_NAME_DOS); |
| 1164 | if(!buf_size) |
| 1165 | return FALSE; |
| 1166 | |
| 1167 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1168 | if (!buf) { |
| 1169 | SetLastError(ERROR_OUTOFMEMORY); |
| 1170 | return FALSE; |
| 1171 | } |
| 1172 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1173 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1174 | buf, buf_size, VOLUME_NAME_DOS); |
| 1175 | |
| 1176 | if(!result_length) { |
| 1177 | free(buf); |
| 1178 | return FALSE; |
| 1179 | } |
| 1180 | |
| 1181 | if(!CloseHandle(hdl)) { |
| 1182 | free(buf); |
| 1183 | return FALSE; |
| 1184 | } |
| 1185 | |
| 1186 | buf[result_length] = 0; |
| 1187 | |
| 1188 | *target_path = buf; |
| 1189 | return TRUE; |
| 1190 | } |
| 1191 | |
| 1192 | static int |
| 1193 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1194 | BOOL traverse); |
| 1195 | static int |
| 1196 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1197 | BOOL traverse) |
| 1198 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1199 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1200 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1201 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1202 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1203 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1204 | const char *dot; |
| 1205 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1206 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1207 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1208 | traverse reparse point. */ |
| 1209 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1212 | hFile = CreateFileA( |
| 1213 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1214 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1215 | 0, /* share mode */ |
| 1216 | NULL, /* security attributes */ |
| 1217 | OPEN_EXISTING, |
| 1218 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1219 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1220 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1221 | the symlink path agin and not the actual final path. */ |
| 1222 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1223 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1224 | NULL); |
| 1225 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1226 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1227 | /* Either the target doesn't exist, or we don't have access to |
| 1228 | get a handle to it. If the former, we need to return an error. |
| 1229 | If the latter, we can use attributes_from_dir. */ |
| 1230 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1231 | return -1; |
| 1232 | /* Could not get attributes on open file. Fall back to |
| 1233 | reading the directory. */ |
| 1234 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1235 | /* Very strange. This should not fail now */ |
| 1236 | return -1; |
| 1237 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1238 | if (traverse) { |
| 1239 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1240 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1241 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1242 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1243 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1244 | } else { |
| 1245 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1246 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1247 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1248 | } |
| 1249 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1250 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1251 | return -1; |
| 1252 | |
| 1253 | /* Close the outer open file handle now that we're about to |
| 1254 | reopen it with different flags. */ |
| 1255 | if (!CloseHandle(hFile)) |
| 1256 | return -1; |
| 1257 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1258 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1259 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1260 | the file without the reparse handling flag set. */ |
| 1261 | hFile2 = CreateFileA( |
| 1262 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1263 | NULL, OPEN_EXISTING, |
| 1264 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1265 | NULL); |
| 1266 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1267 | return -1; |
| 1268 | |
| 1269 | if (!get_target_path(hFile2, &target_path)) |
| 1270 | return -1; |
| 1271 | |
| 1272 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1273 | free(target_path); |
| 1274 | return code; |
| 1275 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1276 | } else |
| 1277 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1278 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1279 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1280 | |
| 1281 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1282 | dot = strrchr(path, '.'); |
| 1283 | if (dot) { |
| 1284 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1285 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1286 | result->st_mode |= 0111; |
| 1287 | } |
| 1288 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1292 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1293 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1294 | { |
| 1295 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1296 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1297 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1298 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1299 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1300 | const wchar_t *dot; |
| 1301 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1302 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1303 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1304 | traverse reparse point. */ |
| 1305 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1308 | hFile = CreateFileW( |
| 1309 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1310 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1311 | 0, /* share mode */ |
| 1312 | NULL, /* security attributes */ |
| 1313 | OPEN_EXISTING, |
| 1314 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1315 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1316 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1317 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1318 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1319 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1320 | NULL); |
| 1321 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1322 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1323 | /* Either the target doesn't exist, or we don't have access to |
| 1324 | get a handle to it. If the former, we need to return an error. |
| 1325 | If the latter, we can use attributes_from_dir. */ |
| 1326 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1327 | return -1; |
| 1328 | /* Could not get attributes on open file. Fall back to |
| 1329 | reading the directory. */ |
| 1330 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1331 | /* Very strange. This should not fail now */ |
| 1332 | return -1; |
| 1333 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1334 | if (traverse) { |
| 1335 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1336 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1337 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1338 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1339 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1340 | } else { |
| 1341 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1342 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1343 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1344 | } |
| 1345 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1346 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1347 | return -1; |
| 1348 | |
| 1349 | /* Close the outer open file handle now that we're about to |
| 1350 | reopen it with different flags. */ |
| 1351 | if (!CloseHandle(hFile)) |
| 1352 | return -1; |
| 1353 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1354 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1355 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1356 | the file without the reparse handling flag set. */ |
| 1357 | hFile2 = CreateFileW( |
| 1358 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1359 | NULL, OPEN_EXISTING, |
| 1360 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1361 | NULL); |
| 1362 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1363 | return -1; |
| 1364 | |
| 1365 | if (!get_target_path(hFile2, &target_path)) |
| 1366 | return -1; |
| 1367 | |
| 1368 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1369 | free(target_path); |
| 1370 | return code; |
| 1371 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1372 | } else |
| 1373 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1374 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1375 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1378 | dot = wcsrchr(path, '.'); |
| 1379 | if (dot) { |
| 1380 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1381 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1382 | result->st_mode |= 0111; |
| 1383 | } |
| 1384 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1388 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1389 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1390 | /* Protocol violation: we explicitly clear errno, instead of |
| 1391 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1392 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1393 | errno = 0; |
| 1394 | return code; |
| 1395 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1396 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1397 | static int |
| 1398 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1399 | { |
| 1400 | /* Protocol violation: we explicitly clear errno, instead of |
| 1401 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1402 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1403 | errno = 0; |
| 1404 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1405 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1406 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1407 | |
| 1408 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1409 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1410 | default does not traverse symlinks and instead returns attributes for |
| 1411 | the symlink. |
| 1412 | |
| 1413 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1414 | win32_stat will first explicitly resolve the symlink target and then will |
| 1415 | call win32_lstat on that result. |
| 1416 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1417 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1418 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1419 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1420 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1421 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1422 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1425 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1426 | win32_lstat_w(const wchar_t* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1427 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1428 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | static int |
| 1432 | win32_stat(const char* path, struct win32_stat *result) |
| 1433 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1434 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1437 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1438 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1439 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1440 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static int |
| 1444 | win32_fstat(int file_number, struct win32_stat *result) |
| 1445 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | BY_HANDLE_FILE_INFORMATION info; |
| 1447 | HANDLE h; |
| 1448 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1449 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1450 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1452 | /* Protocol violation: we explicitly clear errno, instead of |
| 1453 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1454 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1455 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1456 | if (h == INVALID_HANDLE_VALUE) { |
| 1457 | /* This is really a C library error (invalid file handle). |
| 1458 | We set the Win32 error to the closes one matching. */ |
| 1459 | SetLastError(ERROR_INVALID_HANDLE); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1463 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1464 | type = GetFileType(h); |
| 1465 | if (type == FILE_TYPE_UNKNOWN) { |
| 1466 | DWORD error = GetLastError(); |
| 1467 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1468 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1469 | } |
| 1470 | /* else: valid but unknown file */ |
| 1471 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1472 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | if (type != FILE_TYPE_DISK) { |
| 1474 | if (type == FILE_TYPE_CHAR) |
| 1475 | result->st_mode = _S_IFCHR; |
| 1476 | else if (type == FILE_TYPE_PIPE) |
| 1477 | result->st_mode = _S_IFIFO; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | if (!GetFileInformationByHandle(h, &info)) { |
| 1482 | return -1; |
| 1483 | } |
| 1484 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1485 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1486 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1487 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1488 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | #endif /* MS_WINDOWS */ |
| 1492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1493 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1494 | "stat_result: Result from stat or lstat.\n\n\ |
| 1495 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1496 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1497 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1498 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1499 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1500 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1501 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1502 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1503 | |
| 1504 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1505 | {"st_mode", "protection bits"}, |
| 1506 | {"st_ino", "inode"}, |
| 1507 | {"st_dev", "device"}, |
| 1508 | {"st_nlink", "number of hard links"}, |
| 1509 | {"st_uid", "user ID of owner"}, |
| 1510 | {"st_gid", "group ID of owner"}, |
| 1511 | {"st_size", "total size, in bytes"}, |
| 1512 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1513 | {NULL, "integer time of last access"}, |
| 1514 | {NULL, "integer time of last modification"}, |
| 1515 | {NULL, "integer time of last change"}, |
| 1516 | {"st_atime", "time of last access"}, |
| 1517 | {"st_mtime", "time of last modification"}, |
| 1518 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1519 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1520 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1521 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1522 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1523 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1524 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1525 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1526 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1527 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1528 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1529 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1530 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1531 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1532 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1533 | #endif |
| 1534 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1535 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1536 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1537 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1538 | }; |
| 1539 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1540 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1541 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1542 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1543 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1544 | #endif |
| 1545 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1546 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1547 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1548 | #else |
| 1549 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1550 | #endif |
| 1551 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1552 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1553 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1554 | #else |
| 1555 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1556 | #endif |
| 1557 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1558 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1559 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1560 | #else |
| 1561 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1562 | #endif |
| 1563 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1564 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1565 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1566 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1567 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1568 | #endif |
| 1569 | |
| 1570 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1571 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1572 | #else |
| 1573 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1574 | #endif |
| 1575 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1576 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1577 | "stat_result", /* name */ |
| 1578 | stat_result__doc__, /* doc */ |
| 1579 | stat_result_fields, |
| 1580 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1581 | }; |
| 1582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1583 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1584 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1585 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1586 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1587 | 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] | 1588 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1589 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1590 | |
| 1591 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1592 | {"f_bsize", }, |
| 1593 | {"f_frsize", }, |
| 1594 | {"f_blocks", }, |
| 1595 | {"f_bfree", }, |
| 1596 | {"f_bavail", }, |
| 1597 | {"f_files", }, |
| 1598 | {"f_ffree", }, |
| 1599 | {"f_favail", }, |
| 1600 | {"f_flag", }, |
| 1601 | {"f_namemax",}, |
| 1602 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1603 | }; |
| 1604 | |
| 1605 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1606 | "statvfs_result", /* name */ |
| 1607 | statvfs_result__doc__, /* doc */ |
| 1608 | statvfs_result_fields, |
| 1609 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1610 | }; |
| 1611 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1612 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1613 | PyDoc_STRVAR(waitid_result__doc__, |
| 1614 | "waitid_result: Result from waitid.\n\n\ |
| 1615 | This object may be accessed either as a tuple of\n\ |
| 1616 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1617 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1618 | \n\ |
| 1619 | See os.waitid for more information."); |
| 1620 | |
| 1621 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1622 | {"si_pid", }, |
| 1623 | {"si_uid", }, |
| 1624 | {"si_signo", }, |
| 1625 | {"si_status", }, |
| 1626 | {"si_code", }, |
| 1627 | {0} |
| 1628 | }; |
| 1629 | |
| 1630 | static PyStructSequence_Desc waitid_result_desc = { |
| 1631 | "waitid_result", /* name */ |
| 1632 | waitid_result__doc__, /* doc */ |
| 1633 | waitid_result_fields, |
| 1634 | 5 |
| 1635 | }; |
| 1636 | static PyTypeObject WaitidResultType; |
| 1637 | #endif |
| 1638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1639 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1640 | static PyTypeObject StatResultType; |
| 1641 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1642 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1643 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1644 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1645 | static newfunc structseq_new; |
| 1646 | |
| 1647 | static PyObject * |
| 1648 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1649 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1650 | PyStructSequence *result; |
| 1651 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1652 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1653 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1654 | if (!result) |
| 1655 | return NULL; |
| 1656 | /* If we have been initialized from a tuple, |
| 1657 | st_?time might be set to None. Initialize it |
| 1658 | from the int slots. */ |
| 1659 | for (i = 7; i <= 9; i++) { |
| 1660 | if (result->ob_item[i+3] == Py_None) { |
| 1661 | Py_DECREF(Py_None); |
| 1662 | Py_INCREF(result->ob_item[i]); |
| 1663 | result->ob_item[i+3] = result->ob_item[i]; |
| 1664 | } |
| 1665 | } |
| 1666 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1672 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1673 | |
| 1674 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1675 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1676 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1677 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1678 | future calls return ints. \n\ |
| 1679 | If newval is omitted, return the current setting.\n"); |
| 1680 | |
| 1681 | static PyObject* |
| 1682 | stat_float_times(PyObject* self, PyObject *args) |
| 1683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1684 | int newval = -1; |
| 1685 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1686 | return NULL; |
| 1687 | if (newval == -1) |
| 1688 | /* Return old value */ |
| 1689 | return PyBool_FromLong(_stat_float_times); |
| 1690 | _stat_float_times = newval; |
| 1691 | Py_INCREF(Py_None); |
| 1692 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1693 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1694 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1695 | static void |
| 1696 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1698 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1699 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1700 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1701 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1702 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1703 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1704 | if (!ival) |
| 1705 | return; |
| 1706 | if (_stat_float_times) { |
| 1707 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1708 | } else { |
| 1709 | fval = ival; |
| 1710 | Py_INCREF(fval); |
| 1711 | } |
| 1712 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1713 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1716 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1717 | (used by posix_stat() and posix_fstat()) */ |
| 1718 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1719 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1720 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1721 | unsigned long ansec, mnsec, cnsec; |
| 1722 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1723 | if (v == NULL) |
| 1724 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1725 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1726 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1727 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1728 | PyStructSequence_SET_ITEM(v, 1, |
| 1729 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1730 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1731 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1732 | #endif |
| 1733 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1734 | PyStructSequence_SET_ITEM(v, 2, |
| 1735 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1736 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1738 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1739 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1740 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1741 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1742 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1743 | PyStructSequence_SET_ITEM(v, 6, |
| 1744 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1745 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1747 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1748 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1749 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1750 | ansec = st->st_atim.tv_nsec; |
| 1751 | mnsec = st->st_mtim.tv_nsec; |
| 1752 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1753 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1754 | ansec = st->st_atimespec.tv_nsec; |
| 1755 | mnsec = st->st_mtimespec.tv_nsec; |
| 1756 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1757 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | ansec = st->st_atime_nsec; |
| 1759 | mnsec = st->st_mtime_nsec; |
| 1760 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1761 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1762 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1763 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | fill_time(v, 7, st->st_atime, ansec); |
| 1765 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1766 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1767 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1768 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1770 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1771 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1772 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1773 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1774 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1775 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1776 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1778 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1779 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1780 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1781 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1782 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1783 | #endif |
| 1784 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1785 | { |
| 1786 | PyObject *val; |
| 1787 | unsigned long bsec,bnsec; |
| 1788 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1789 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1790 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1791 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1793 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1794 | if (_stat_float_times) { |
| 1795 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1796 | } else { |
| 1797 | val = PyLong_FromLong((long)bsec); |
| 1798 | } |
| 1799 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1800 | val); |
| 1801 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1802 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1803 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1804 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1805 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1806 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1807 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1808 | if (PyErr_Occurred()) { |
| 1809 | Py_DECREF(v); |
| 1810 | return NULL; |
| 1811 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1812 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1813 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1816 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1817 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1818 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1819 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1820 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1821 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1822 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1823 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1824 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1825 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1826 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1827 | STRUCT_STAT st; |
| 1828 | PyObject *opath; |
| 1829 | char *path; |
| 1830 | int res; |
| 1831 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1832 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1833 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1834 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1835 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1836 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 1837 | if (wpath == NULL) |
| 1838 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1841 | res = wstatfunc(wpath, &st); |
| 1842 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1844 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1845 | return win32_error_object("stat", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | return _pystat_fromstructstat(&st); |
| 1847 | } |
| 1848 | /* Drop the argument parsing error as narrow strings |
| 1849 | are also valid. */ |
| 1850 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1851 | #endif |
| 1852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1853 | if (!PyArg_ParseTuple(args, format, |
| 1854 | PyUnicode_FSConverter, &opath)) |
| 1855 | return NULL; |
| 1856 | path = PyBytes_AsString(opath); |
| 1857 | Py_BEGIN_ALLOW_THREADS |
| 1858 | res = (*statfunc)(path, &st); |
| 1859 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1862 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1863 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1864 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1866 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | } |
| 1868 | else |
| 1869 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1870 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1871 | Py_DECREF(opath); |
| 1872 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1875 | /* POSIX methods */ |
| 1876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1877 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1878 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1879 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1880 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1881 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1882 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1883 | 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] | 1884 | |
| 1885 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1886 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1887 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | PyObject *opath; |
| 1889 | char *path; |
| 1890 | int mode; |
| 1891 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1892 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1893 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1894 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1895 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1896 | wchar_t* wpath = PyUnicode_AsUnicode(po); |
| 1897 | if (wpath == NULL) |
| 1898 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1899 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1900 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1901 | Py_END_ALLOW_THREADS |
| 1902 | goto finish; |
| 1903 | } |
| 1904 | /* Drop the argument parsing error as narrow strings |
| 1905 | are also valid. */ |
| 1906 | PyErr_Clear(); |
| 1907 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1908 | PyUnicode_FSConverter, &opath, &mode)) |
| 1909 | return NULL; |
| 1910 | path = PyBytes_AsString(opath); |
| 1911 | Py_BEGIN_ALLOW_THREADS |
| 1912 | attr = GetFileAttributesA(path); |
| 1913 | Py_END_ALLOW_THREADS |
| 1914 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1915 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1916 | if (attr == 0xFFFFFFFF) |
| 1917 | /* File does not exist, or cannot read attributes */ |
| 1918 | return PyBool_FromLong(0); |
| 1919 | /* Access is possible if either write access wasn't requested, or |
| 1920 | the file isn't read-only, or if it's a directory, as there are |
| 1921 | no read-only directories on Windows. */ |
| 1922 | return PyBool_FromLong(!(mode & 2) |
| 1923 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1924 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1925 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1926 | int res; |
| 1927 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1928 | PyUnicode_FSConverter, &opath, &mode)) |
| 1929 | return NULL; |
| 1930 | path = PyBytes_AsString(opath); |
| 1931 | Py_BEGIN_ALLOW_THREADS |
| 1932 | res = access(path, mode); |
| 1933 | Py_END_ALLOW_THREADS |
| 1934 | Py_DECREF(opath); |
| 1935 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1936 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1939 | #ifndef F_OK |
| 1940 | #define F_OK 0 |
| 1941 | #endif |
| 1942 | #ifndef R_OK |
| 1943 | #define R_OK 4 |
| 1944 | #endif |
| 1945 | #ifndef W_OK |
| 1946 | #define W_OK 2 |
| 1947 | #endif |
| 1948 | #ifndef X_OK |
| 1949 | #define X_OK 1 |
| 1950 | #endif |
| 1951 | |
| 1952 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1953 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1954 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1956 | |
| 1957 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1958 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1959 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1960 | int id; |
| 1961 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1962 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1963 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1964 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1965 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1966 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1967 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1968 | if (id == 0) { |
| 1969 | ret = ttyname(); |
| 1970 | } |
| 1971 | else { |
| 1972 | ret = NULL; |
| 1973 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1974 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1975 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1976 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | if (ret == NULL) |
| 1978 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1979 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1980 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1981 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1982 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1983 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1984 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1985 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1986 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1987 | |
| 1988 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1989 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1990 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | char *ret; |
| 1992 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1993 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1994 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1995 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1996 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1998 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1999 | if (ret == NULL) |
| 2000 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2001 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2002 | } |
| 2003 | #endif |
| 2004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2005 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2006 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2007 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2008 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2009 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2010 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2011 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2012 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | 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] | 2014 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2016 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2017 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2018 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2020 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2023 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2024 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2025 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2026 | 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] | 2027 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2028 | |
| 2029 | static PyObject * |
| 2030 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2031 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2032 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2033 | } |
| 2034 | #endif /* HAVE_FCHDIR */ |
| 2035 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2037 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2038 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2039 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2040 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2042 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2044 | PyObject *opath = NULL; |
| 2045 | char *path = NULL; |
| 2046 | int i; |
| 2047 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2048 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2049 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2050 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2051 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2052 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2053 | if (wpath == NULL) |
| 2054 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2056 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2057 | if (attr != 0xFFFFFFFF) { |
| 2058 | if (i & _S_IWRITE) |
| 2059 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2060 | else |
| 2061 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2062 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | } |
| 2064 | else |
| 2065 | res = 0; |
| 2066 | Py_END_ALLOW_THREADS |
| 2067 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2068 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | Py_INCREF(Py_None); |
| 2070 | return Py_None; |
| 2071 | } |
| 2072 | /* Drop the argument parsing error as narrow strings |
| 2073 | are also valid. */ |
| 2074 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2076 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2077 | &opath, &i)) |
| 2078 | return NULL; |
| 2079 | path = PyBytes_AsString(opath); |
| 2080 | Py_BEGIN_ALLOW_THREADS |
| 2081 | attr = GetFileAttributesA(path); |
| 2082 | if (attr != 0xFFFFFFFF) { |
| 2083 | if (i & _S_IWRITE) |
| 2084 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2085 | else |
| 2086 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2087 | res = SetFileAttributesA(path, attr); |
| 2088 | } |
| 2089 | else |
| 2090 | res = 0; |
| 2091 | Py_END_ALLOW_THREADS |
| 2092 | if (!res) { |
| 2093 | win32_error("chmod", path); |
| 2094 | Py_DECREF(opath); |
| 2095 | return NULL; |
| 2096 | } |
| 2097 | Py_DECREF(opath); |
| 2098 | Py_INCREF(Py_None); |
| 2099 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2100 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2101 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2102 | &opath, &i)) |
| 2103 | return NULL; |
| 2104 | path = PyBytes_AsString(opath); |
| 2105 | Py_BEGIN_ALLOW_THREADS |
| 2106 | res = chmod(path, i); |
| 2107 | Py_END_ALLOW_THREADS |
| 2108 | if (res < 0) |
| 2109 | return posix_error_with_allocated_filename(opath); |
| 2110 | Py_DECREF(opath); |
| 2111 | Py_INCREF(Py_None); |
| 2112 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2113 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2116 | #ifdef HAVE_FCHMOD |
| 2117 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2118 | "fchmod(fd, mode)\n\n\ |
| 2119 | Change the access permissions of the file given by file\n\ |
| 2120 | descriptor fd."); |
| 2121 | |
| 2122 | static PyObject * |
| 2123 | posix_fchmod(PyObject *self, PyObject *args) |
| 2124 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2125 | int fd, mode, res; |
| 2126 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2127 | return NULL; |
| 2128 | Py_BEGIN_ALLOW_THREADS |
| 2129 | res = fchmod(fd, mode); |
| 2130 | Py_END_ALLOW_THREADS |
| 2131 | if (res < 0) |
| 2132 | return posix_error(); |
| 2133 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2134 | } |
| 2135 | #endif /* HAVE_FCHMOD */ |
| 2136 | |
| 2137 | #ifdef HAVE_LCHMOD |
| 2138 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2139 | "lchmod(path, mode)\n\n\ |
| 2140 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2141 | affects the link itself rather than the target."); |
| 2142 | |
| 2143 | static PyObject * |
| 2144 | posix_lchmod(PyObject *self, PyObject *args) |
| 2145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2146 | PyObject *opath; |
| 2147 | char *path; |
| 2148 | int i; |
| 2149 | int res; |
| 2150 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2151 | &opath, &i)) |
| 2152 | return NULL; |
| 2153 | path = PyBytes_AsString(opath); |
| 2154 | Py_BEGIN_ALLOW_THREADS |
| 2155 | res = lchmod(path, i); |
| 2156 | Py_END_ALLOW_THREADS |
| 2157 | if (res < 0) |
| 2158 | return posix_error_with_allocated_filename(opath); |
| 2159 | Py_DECREF(opath); |
| 2160 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2161 | } |
| 2162 | #endif /* HAVE_LCHMOD */ |
| 2163 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2164 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2165 | #ifdef HAVE_CHFLAGS |
| 2166 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2167 | "chflags(path, flags)\n\n\ |
| 2168 | Set file flags."); |
| 2169 | |
| 2170 | static PyObject * |
| 2171 | posix_chflags(PyObject *self, PyObject *args) |
| 2172 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2173 | PyObject *opath; |
| 2174 | char *path; |
| 2175 | unsigned long flags; |
| 2176 | int res; |
| 2177 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2178 | PyUnicode_FSConverter, &opath, &flags)) |
| 2179 | return NULL; |
| 2180 | path = PyBytes_AsString(opath); |
| 2181 | Py_BEGIN_ALLOW_THREADS |
| 2182 | res = chflags(path, flags); |
| 2183 | Py_END_ALLOW_THREADS |
| 2184 | if (res < 0) |
| 2185 | return posix_error_with_allocated_filename(opath); |
| 2186 | Py_DECREF(opath); |
| 2187 | Py_INCREF(Py_None); |
| 2188 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2189 | } |
| 2190 | #endif /* HAVE_CHFLAGS */ |
| 2191 | |
| 2192 | #ifdef HAVE_LCHFLAGS |
| 2193 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2194 | "lchflags(path, flags)\n\n\ |
| 2195 | Set file flags.\n\ |
| 2196 | This function will not follow symbolic links."); |
| 2197 | |
| 2198 | static PyObject * |
| 2199 | posix_lchflags(PyObject *self, PyObject *args) |
| 2200 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2201 | PyObject *opath; |
| 2202 | char *path; |
| 2203 | unsigned long flags; |
| 2204 | int res; |
| 2205 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2206 | PyUnicode_FSConverter, &opath, &flags)) |
| 2207 | return NULL; |
| 2208 | path = PyBytes_AsString(opath); |
| 2209 | Py_BEGIN_ALLOW_THREADS |
| 2210 | res = lchflags(path, flags); |
| 2211 | Py_END_ALLOW_THREADS |
| 2212 | if (res < 0) |
| 2213 | return posix_error_with_allocated_filename(opath); |
| 2214 | Py_DECREF(opath); |
| 2215 | Py_INCREF(Py_None); |
| 2216 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2217 | } |
| 2218 | #endif /* HAVE_LCHFLAGS */ |
| 2219 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2220 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2221 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2222 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2223 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2224 | |
| 2225 | static PyObject * |
| 2226 | posix_chroot(PyObject *self, PyObject *args) |
| 2227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2228 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2229 | } |
| 2230 | #endif |
| 2231 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2232 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2233 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2234 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2235 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2236 | |
| 2237 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2238 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2239 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2240 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2241 | } |
| 2242 | #endif /* HAVE_FSYNC */ |
| 2243 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2244 | #ifdef HAVE_SYNC |
| 2245 | PyDoc_STRVAR(posix_sync__doc__, |
| 2246 | "sync()\n\n\ |
| 2247 | Force write of everything to disk."); |
| 2248 | |
| 2249 | static PyObject * |
| 2250 | posix_sync(PyObject *self, PyObject *noargs) |
| 2251 | { |
| 2252 | Py_BEGIN_ALLOW_THREADS |
| 2253 | sync(); |
| 2254 | Py_END_ALLOW_THREADS |
| 2255 | Py_RETURN_NONE; |
| 2256 | } |
| 2257 | #endif |
| 2258 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2259 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2260 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2261 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2262 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2263 | #endif |
| 2264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2265 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2266 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2267 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2268 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2269 | |
| 2270 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2271 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2272 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2273 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2274 | } |
| 2275 | #endif /* HAVE_FDATASYNC */ |
| 2276 | |
| 2277 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2278 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2279 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2280 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2281 | 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] | 2282 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2283 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2284 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2285 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2286 | PyObject *opath; |
| 2287 | char *path; |
| 2288 | long uid, gid; |
| 2289 | int res; |
| 2290 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2291 | PyUnicode_FSConverter, &opath, |
| 2292 | &uid, &gid)) |
| 2293 | return NULL; |
| 2294 | path = PyBytes_AsString(opath); |
| 2295 | Py_BEGIN_ALLOW_THREADS |
| 2296 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2297 | Py_END_ALLOW_THREADS |
| 2298 | if (res < 0) |
| 2299 | return posix_error_with_allocated_filename(opath); |
| 2300 | Py_DECREF(opath); |
| 2301 | Py_INCREF(Py_None); |
| 2302 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2303 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2304 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2305 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2306 | #ifdef HAVE_FCHOWN |
| 2307 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2308 | "fchown(fd, uid, gid)\n\n\ |
| 2309 | Change the owner and group id of the file given by file descriptor\n\ |
| 2310 | fd to the numeric uid and gid."); |
| 2311 | |
| 2312 | static PyObject * |
| 2313 | posix_fchown(PyObject *self, PyObject *args) |
| 2314 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2315 | int fd; |
| 2316 | long uid, gid; |
| 2317 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2318 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2319 | return NULL; |
| 2320 | Py_BEGIN_ALLOW_THREADS |
| 2321 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2322 | Py_END_ALLOW_THREADS |
| 2323 | if (res < 0) |
| 2324 | return posix_error(); |
| 2325 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2326 | } |
| 2327 | #endif /* HAVE_FCHOWN */ |
| 2328 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2329 | #ifdef HAVE_LCHOWN |
| 2330 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2331 | "lchown(path, uid, gid)\n\n\ |
| 2332 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2333 | This function will not follow symbolic links."); |
| 2334 | |
| 2335 | static PyObject * |
| 2336 | posix_lchown(PyObject *self, PyObject *args) |
| 2337 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2338 | PyObject *opath; |
| 2339 | char *path; |
| 2340 | long uid, gid; |
| 2341 | int res; |
| 2342 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2343 | PyUnicode_FSConverter, &opath, |
| 2344 | &uid, &gid)) |
| 2345 | return NULL; |
| 2346 | path = PyBytes_AsString(opath); |
| 2347 | Py_BEGIN_ALLOW_THREADS |
| 2348 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2349 | Py_END_ALLOW_THREADS |
| 2350 | if (res < 0) |
| 2351 | return posix_error_with_allocated_filename(opath); |
| 2352 | Py_DECREF(opath); |
| 2353 | Py_INCREF(Py_None); |
| 2354 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2355 | } |
| 2356 | #endif /* HAVE_LCHOWN */ |
| 2357 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2358 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2359 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2360 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2361 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2363 | char buf[1026]; |
| 2364 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2365 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2366 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2367 | if (!use_bytes) { |
| 2368 | wchar_t wbuf[1026]; |
| 2369 | wchar_t *wbuf2 = wbuf; |
| 2370 | PyObject *resobj; |
| 2371 | DWORD len; |
| 2372 | Py_BEGIN_ALLOW_THREADS |
| 2373 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2374 | /* If the buffer is large enough, len does not include the |
| 2375 | terminating \0. If the buffer is too small, len includes |
| 2376 | the space needed for the terminator. */ |
| 2377 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2378 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2379 | if (wbuf2) |
| 2380 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2381 | } |
| 2382 | Py_END_ALLOW_THREADS |
| 2383 | if (!wbuf2) { |
| 2384 | PyErr_NoMemory(); |
| 2385 | return NULL; |
| 2386 | } |
| 2387 | if (!len) { |
| 2388 | if (wbuf2 != wbuf) free(wbuf2); |
| 2389 | return win32_error("getcwdu", NULL); |
| 2390 | } |
| 2391 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2392 | if (wbuf2 != wbuf) free(wbuf2); |
| 2393 | return resobj; |
| 2394 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2395 | #endif |
| 2396 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2397 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2398 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2399 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2400 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2401 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2402 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2403 | Py_END_ALLOW_THREADS |
| 2404 | if (res == NULL) |
| 2405 | return posix_error(); |
| 2406 | if (use_bytes) |
| 2407 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2408 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2409 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2410 | |
| 2411 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2412 | "getcwd() -> path\n\n\ |
| 2413 | Return a unicode string representing the current working directory."); |
| 2414 | |
| 2415 | static PyObject * |
| 2416 | posix_getcwd_unicode(PyObject *self) |
| 2417 | { |
| 2418 | return posix_getcwd(0); |
| 2419 | } |
| 2420 | |
| 2421 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2422 | "getcwdb() -> path\n\n\ |
| 2423 | Return a bytes string representing the current working directory."); |
| 2424 | |
| 2425 | static PyObject * |
| 2426 | posix_getcwd_bytes(PyObject *self) |
| 2427 | { |
| 2428 | return posix_getcwd(1); |
| 2429 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2430 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2431 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2432 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2433 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2434 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2435 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2436 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2437 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2438 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2439 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2440 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2441 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2442 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2443 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2444 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2445 | #ifdef MS_WINDOWS |
| 2446 | PyDoc_STRVAR(win32_link__doc__, |
| 2447 | "link(src, dst)\n\n\ |
| 2448 | Create a hard link to a file."); |
| 2449 | |
| 2450 | static PyObject * |
| 2451 | win32_link(PyObject *self, PyObject *args) |
| 2452 | { |
| 2453 | PyObject *osrc, *odst; |
| 2454 | char *src, *dst; |
| 2455 | BOOL rslt; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2456 | PyObject *usrc, *udst; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2457 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2458 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) |
| 2459 | { |
| 2460 | wchar_t *wsrc, *wdst; |
| 2461 | wsrc = PyUnicode_AsUnicode(usrc); |
| 2462 | if (wsrc == NULL) |
| 2463 | return NULL; |
| 2464 | wdst = PyUnicode_AsUnicode(udst); |
| 2465 | if (wdst == NULL) |
| 2466 | return NULL; |
| 2467 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2468 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2469 | rslt = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2470 | Py_END_ALLOW_THREADS |
| 2471 | |
| 2472 | if (rslt == 0) |
| 2473 | return win32_error("link", NULL); |
| 2474 | |
| 2475 | Py_RETURN_NONE; |
| 2476 | } |
| 2477 | |
| 2478 | /* Narrow strings also valid. */ |
| 2479 | PyErr_Clear(); |
| 2480 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2481 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2482 | PyUnicode_FSConverter, &odst)) |
| 2483 | return NULL; |
| 2484 | |
| 2485 | src = PyBytes_AsString(osrc); |
| 2486 | dst = PyBytes_AsString(odst); |
| 2487 | |
| 2488 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2489 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2490 | Py_END_ALLOW_THREADS |
| 2491 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2492 | Py_DECREF(osrc); |
| 2493 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2494 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2495 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2496 | |
| 2497 | Py_RETURN_NONE; |
| 2498 | } |
| 2499 | #endif /* MS_WINDOWS */ |
| 2500 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2502 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2503 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2504 | Return a list containing the names of the entries in the directory.\n\ |
| 2505 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2506 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2507 | \n\ |
| 2508 | 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] | 2509 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2510 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2511 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2512 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2513 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2514 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2515 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2516 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2518 | PyObject *d, *v; |
| 2519 | HANDLE hFindFile; |
| 2520 | BOOL result; |
| 2521 | WIN32_FIND_DATA FileData; |
| 2522 | PyObject *opath; |
| 2523 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2524 | char *bufptr = namebuf; |
| 2525 | 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] | 2526 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2527 | PyObject *po = NULL; |
| 2528 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2529 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2530 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2531 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2532 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2533 | po_wchars = L"."; |
| 2534 | len = 1; |
| 2535 | } else { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 2536 | po_wchars = PyUnicode_AsUnicodeAndSize(po, &len); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2537 | if (po_wchars == NULL) |
| 2538 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2539 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2540 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2541 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2542 | if (!wnamebuf) { |
| 2543 | PyErr_NoMemory(); |
| 2544 | return NULL; |
| 2545 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2546 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2547 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2548 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2549 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2550 | wnamebuf[len++] = L'\\'; |
| 2551 | wcscpy(wnamebuf + len, L"*.*"); |
| 2552 | } |
| 2553 | if ((d = PyList_New(0)) == NULL) { |
| 2554 | free(wnamebuf); |
| 2555 | return NULL; |
| 2556 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2557 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2558 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2559 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2560 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2561 | int error = GetLastError(); |
| 2562 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2563 | free(wnamebuf); |
| 2564 | return d; |
| 2565 | } |
| 2566 | Py_DECREF(d); |
| 2567 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2568 | free(wnamebuf); |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | do { |
| 2572 | /* Skip over . and .. */ |
| 2573 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2574 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2575 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2576 | if (v == NULL) { |
| 2577 | Py_DECREF(d); |
| 2578 | d = NULL; |
| 2579 | break; |
| 2580 | } |
| 2581 | if (PyList_Append(d, v) != 0) { |
| 2582 | Py_DECREF(v); |
| 2583 | Py_DECREF(d); |
| 2584 | d = NULL; |
| 2585 | break; |
| 2586 | } |
| 2587 | Py_DECREF(v); |
| 2588 | } |
| 2589 | Py_BEGIN_ALLOW_THREADS |
| 2590 | result = FindNextFileW(hFindFile, &wFileData); |
| 2591 | Py_END_ALLOW_THREADS |
| 2592 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2593 | it got to the end of the directory. */ |
| 2594 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2595 | Py_DECREF(d); |
| 2596 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2597 | FindClose(hFindFile); |
| 2598 | free(wnamebuf); |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2602 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2603 | if (FindClose(hFindFile) == FALSE) { |
| 2604 | Py_DECREF(d); |
| 2605 | win32_error_unicode("FindClose", wnamebuf); |
| 2606 | free(wnamebuf); |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | free(wnamebuf); |
| 2610 | return d; |
| 2611 | } |
| 2612 | /* Drop the argument parsing error as narrow strings |
| 2613 | are also valid. */ |
| 2614 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2615 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2616 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2617 | PyUnicode_FSConverter, &opath)) |
| 2618 | return NULL; |
| 2619 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2620 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2621 | Py_DECREF(opath); |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2625 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2626 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2627 | if (len > 0) { |
| 2628 | char ch = namebuf[len-1]; |
| 2629 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2630 | namebuf[len++] = '/'; |
| 2631 | strcpy(namebuf + len, "*.*"); |
| 2632 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2633 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2634 | if ((d = PyList_New(0)) == NULL) |
| 2635 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2636 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2637 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2638 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2639 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2640 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2641 | int error = GetLastError(); |
| 2642 | if (error == ERROR_FILE_NOT_FOUND) |
| 2643 | return d; |
| 2644 | Py_DECREF(d); |
| 2645 | return win32_error("FindFirstFile", namebuf); |
| 2646 | } |
| 2647 | do { |
| 2648 | /* Skip over . and .. */ |
| 2649 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2650 | strcmp(FileData.cFileName, "..") != 0) { |
| 2651 | v = PyBytes_FromString(FileData.cFileName); |
| 2652 | if (v == NULL) { |
| 2653 | Py_DECREF(d); |
| 2654 | d = NULL; |
| 2655 | break; |
| 2656 | } |
| 2657 | if (PyList_Append(d, v) != 0) { |
| 2658 | Py_DECREF(v); |
| 2659 | Py_DECREF(d); |
| 2660 | d = NULL; |
| 2661 | break; |
| 2662 | } |
| 2663 | Py_DECREF(v); |
| 2664 | } |
| 2665 | Py_BEGIN_ALLOW_THREADS |
| 2666 | result = FindNextFile(hFindFile, &FileData); |
| 2667 | Py_END_ALLOW_THREADS |
| 2668 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2669 | it got to the end of the directory. */ |
| 2670 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2671 | Py_DECREF(d); |
| 2672 | win32_error("FindNextFile", namebuf); |
| 2673 | FindClose(hFindFile); |
| 2674 | return NULL; |
| 2675 | } |
| 2676 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2678 | if (FindClose(hFindFile) == FALSE) { |
| 2679 | Py_DECREF(d); |
| 2680 | return win32_error("FindClose", namebuf); |
| 2681 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2682 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2683 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2684 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2685 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2686 | |
| 2687 | #ifndef MAX_PATH |
| 2688 | #define MAX_PATH CCHMAXPATH |
| 2689 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2690 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2691 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2692 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2693 | PyObject *d, *v; |
| 2694 | char namebuf[MAX_PATH+5]; |
| 2695 | HDIR hdir = 1; |
| 2696 | ULONG srchcnt = 1; |
| 2697 | FILEFINDBUF3 ep; |
| 2698 | APIRET rc; |
| 2699 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2700 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2701 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2702 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2703 | name = PyBytes_AsString(oname); |
| 2704 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2705 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2706 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2707 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2708 | return NULL; |
| 2709 | } |
| 2710 | strcpy(namebuf, name); |
| 2711 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2712 | if (*pt == ALTSEP) |
| 2713 | *pt = SEP; |
| 2714 | if (namebuf[len-1] != SEP) |
| 2715 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2716 | strcpy(namebuf + len, "*.*"); |
| 2717 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2718 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2719 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2720 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2721 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2722 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2723 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2724 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2725 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2726 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2727 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2728 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2729 | |
| 2730 | if (rc != NO_ERROR) { |
| 2731 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2732 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2735 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2736 | do { |
| 2737 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2738 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2739 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2740 | |
| 2741 | strcpy(namebuf, ep.achName); |
| 2742 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2743 | /* Leave Case of Name Alone -- In Native Form */ |
| 2744 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2745 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2746 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2747 | if (v == NULL) { |
| 2748 | Py_DECREF(d); |
| 2749 | d = NULL; |
| 2750 | break; |
| 2751 | } |
| 2752 | if (PyList_Append(d, v) != 0) { |
| 2753 | Py_DECREF(v); |
| 2754 | Py_DECREF(d); |
| 2755 | d = NULL; |
| 2756 | break; |
| 2757 | } |
| 2758 | Py_DECREF(v); |
| 2759 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2760 | } |
| 2761 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2762 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2763 | return d; |
| 2764 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2765 | PyObject *oname; |
| 2766 | char *name; |
| 2767 | PyObject *d, *v; |
| 2768 | DIR *dirp; |
| 2769 | struct dirent *ep; |
| 2770 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2772 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2773 | /* v is never read, so it does not need to be initialized yet. */ |
| 2774 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2775 | arg_is_unicode = 0; |
| 2776 | PyErr_Clear(); |
| 2777 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2778 | oname = NULL; |
| 2779 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2780 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2781 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2782 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2783 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2784 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2785 | Py_BEGIN_ALLOW_THREADS |
| 2786 | dirp = opendir(name); |
| 2787 | Py_END_ALLOW_THREADS |
| 2788 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | return posix_error_with_allocated_filename(oname); |
| 2790 | } |
| 2791 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2792 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2793 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2794 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2795 | Py_DECREF(oname); |
| 2796 | return NULL; |
| 2797 | } |
| 2798 | for (;;) { |
| 2799 | errno = 0; |
| 2800 | Py_BEGIN_ALLOW_THREADS |
| 2801 | ep = readdir(dirp); |
| 2802 | Py_END_ALLOW_THREADS |
| 2803 | if (ep == NULL) { |
| 2804 | if (errno == 0) { |
| 2805 | break; |
| 2806 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2807 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2808 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2809 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2810 | Py_DECREF(d); |
| 2811 | return posix_error_with_allocated_filename(oname); |
| 2812 | } |
| 2813 | } |
| 2814 | if (ep->d_name[0] == '.' && |
| 2815 | (NAMLEN(ep) == 1 || |
| 2816 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2817 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2818 | if (arg_is_unicode) |
| 2819 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2820 | else |
| 2821 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2822 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2823 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2824 | break; |
| 2825 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2826 | if (PyList_Append(d, v) != 0) { |
| 2827 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2828 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2829 | break; |
| 2830 | } |
| 2831 | Py_DECREF(v); |
| 2832 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2833 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2834 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2835 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2836 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2838 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2839 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2840 | #endif /* which OS */ |
| 2841 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2843 | #ifdef HAVE_FDOPENDIR |
| 2844 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2845 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2846 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2847 | After succesful execution of this function, fd will be closed."); |
| 2848 | |
| 2849 | static PyObject * |
| 2850 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2851 | { |
| 2852 | PyObject *d, *v; |
| 2853 | DIR *dirp; |
| 2854 | struct dirent *ep; |
| 2855 | int fd; |
| 2856 | |
| 2857 | errno = 0; |
| 2858 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2859 | return NULL; |
| 2860 | Py_BEGIN_ALLOW_THREADS |
| 2861 | dirp = fdopendir(fd); |
| 2862 | Py_END_ALLOW_THREADS |
| 2863 | if (dirp == NULL) { |
| 2864 | close(fd); |
| 2865 | return posix_error(); |
| 2866 | } |
| 2867 | if ((d = PyList_New(0)) == NULL) { |
| 2868 | Py_BEGIN_ALLOW_THREADS |
| 2869 | closedir(dirp); |
| 2870 | Py_END_ALLOW_THREADS |
| 2871 | return NULL; |
| 2872 | } |
| 2873 | for (;;) { |
| 2874 | errno = 0; |
| 2875 | Py_BEGIN_ALLOW_THREADS |
| 2876 | ep = readdir(dirp); |
| 2877 | Py_END_ALLOW_THREADS |
| 2878 | if (ep == NULL) { |
| 2879 | if (errno == 0) { |
| 2880 | break; |
| 2881 | } else { |
| 2882 | Py_BEGIN_ALLOW_THREADS |
| 2883 | closedir(dirp); |
| 2884 | Py_END_ALLOW_THREADS |
| 2885 | Py_DECREF(d); |
| 2886 | return posix_error(); |
| 2887 | } |
| 2888 | } |
| 2889 | if (ep->d_name[0] == '.' && |
| 2890 | (NAMLEN(ep) == 1 || |
| 2891 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2892 | continue; |
| 2893 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2894 | if (v == NULL) { |
| 2895 | Py_CLEAR(d); |
| 2896 | break; |
| 2897 | } |
| 2898 | if (PyList_Append(d, v) != 0) { |
| 2899 | Py_DECREF(v); |
| 2900 | Py_CLEAR(d); |
| 2901 | break; |
| 2902 | } |
| 2903 | Py_DECREF(v); |
| 2904 | } |
| 2905 | Py_BEGIN_ALLOW_THREADS |
| 2906 | closedir(dirp); |
| 2907 | Py_END_ALLOW_THREADS |
| 2908 | |
| 2909 | return d; |
| 2910 | } |
| 2911 | #endif |
| 2912 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2913 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2914 | /* A helper function for abspath on win32 */ |
| 2915 | static PyObject * |
| 2916 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2917 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2918 | PyObject *opath; |
| 2919 | char *path; |
| 2920 | char outbuf[MAX_PATH*2]; |
| 2921 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2922 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2923 | PyObject *po; |
| 2924 | |
| 2925 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 2926 | { |
| 2927 | wchar_t *wpath; |
| 2928 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2929 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2930 | DWORD result; |
| 2931 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2932 | |
| 2933 | wpath = PyUnicode_AsUnicode(po); |
| 2934 | if (wpath == NULL) |
| 2935 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2936 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2937 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2939 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2940 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2941 | if (!woutbufp) |
| 2942 | return PyErr_NoMemory(); |
| 2943 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2944 | } |
| 2945 | if (result) |
| 2946 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2947 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2948 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2949 | if (woutbufp != woutbuf) |
| 2950 | free(woutbufp); |
| 2951 | return v; |
| 2952 | } |
| 2953 | /* Drop the argument parsing error as narrow strings |
| 2954 | are also valid. */ |
| 2955 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2956 | #endif |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2958 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2959 | PyUnicode_FSConverter, &opath)) |
| 2960 | return NULL; |
| 2961 | path = PyBytes_AsString(opath); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2962 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2963 | outbuf, &temp)) { |
| 2964 | win32_error("GetFullPathName", path); |
| 2965 | Py_DECREF(opath); |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | Py_DECREF(opath); |
| 2969 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2970 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2971 | Py_FileSystemDefaultEncoding, NULL); |
| 2972 | } |
| 2973 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2974 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2975 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2976 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2977 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2978 | /* A helper function for samepath on windows */ |
| 2979 | static PyObject * |
| 2980 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2981 | { |
| 2982 | HANDLE hFile; |
| 2983 | int buf_size; |
| 2984 | wchar_t *target_path; |
| 2985 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2986 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2987 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2988 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2989 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2990 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2991 | path = PyUnicode_AsUnicode(po); |
| 2992 | if (path == NULL) |
| 2993 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2994 | |
| 2995 | if(!check_GetFinalPathNameByHandle()) { |
| 2996 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2997 | NotImplementedError. */ |
| 2998 | return PyErr_Format(PyExc_NotImplementedError, |
| 2999 | "GetFinalPathNameByHandle not available on this platform"); |
| 3000 | } |
| 3001 | |
| 3002 | hFile = CreateFileW( |
| 3003 | path, |
| 3004 | 0, /* desired access */ |
| 3005 | 0, /* share mode */ |
| 3006 | NULL, /* security attributes */ |
| 3007 | OPEN_EXISTING, |
| 3008 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3009 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3010 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3011 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3012 | if(hFile == INVALID_HANDLE_VALUE) |
| 3013 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* We have a good handle to the target, use it to determine the |
| 3016 | target path name. */ |
| 3017 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3018 | |
| 3019 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3020 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3021 | |
| 3022 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3023 | if(!target_path) |
| 3024 | return PyErr_NoMemory(); |
| 3025 | |
| 3026 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3027 | buf_size, VOLUME_NAME_DOS); |
| 3028 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3029 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3030 | |
| 3031 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3032 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3033 | |
| 3034 | target_path[result_length] = 0; |
| 3035 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 3036 | free(target_path); |
| 3037 | return result; |
| 3038 | |
| 3039 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3040 | |
| 3041 | static PyObject * |
| 3042 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3043 | { |
| 3044 | HANDLE hFile; |
| 3045 | BY_HANDLE_FILE_INFORMATION info; |
| 3046 | int fd; |
| 3047 | |
| 3048 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3049 | return NULL; |
| 3050 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3051 | if (!_PyVerify_fd(fd)) |
| 3052 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3053 | |
| 3054 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3055 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3056 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3057 | |
| 3058 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3059 | return win32_error("_getfileinformation", NULL); |
| 3060 | |
| 3061 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3062 | info.nFileIndexHigh, |
| 3063 | info.nFileIndexLow); |
| 3064 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3065 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3066 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3067 | "Return true if the pathname refers to an existing directory."); |
| 3068 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3069 | static PyObject * |
| 3070 | posix__isdir(PyObject *self, PyObject *args) |
| 3071 | { |
| 3072 | PyObject *opath; |
| 3073 | char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3074 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3075 | DWORD attributes; |
| 3076 | |
| 3077 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3078 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3079 | if (wpath == NULL) |
| 3080 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3081 | |
| 3082 | attributes = GetFileAttributesW(wpath); |
| 3083 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3084 | Py_RETURN_FALSE; |
| 3085 | goto check; |
| 3086 | } |
| 3087 | /* Drop the argument parsing error as narrow strings |
| 3088 | are also valid. */ |
| 3089 | PyErr_Clear(); |
| 3090 | |
| 3091 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 3092 | PyUnicode_FSConverter, &opath)) |
| 3093 | return NULL; |
| 3094 | |
| 3095 | path = PyBytes_AsString(opath); |
| 3096 | attributes = GetFileAttributesA(path); |
| 3097 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3098 | Py_RETURN_FALSE; |
| 3099 | |
| 3100 | check: |
| 3101 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3102 | Py_RETURN_TRUE; |
| 3103 | else |
| 3104 | Py_RETURN_FALSE; |
| 3105 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3106 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3108 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3109 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3110 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3112 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3113 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3114 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3115 | int res; |
| 3116 | PyObject *opath; |
| 3117 | char *path; |
| 3118 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3119 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3120 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3121 | PyObject *po; |
| 3122 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3123 | { |
| 3124 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3125 | if (wpath == NULL) |
| 3126 | return NULL; |
| 3127 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3128 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3129 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3130 | Py_END_ALLOW_THREADS |
| 3131 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3132 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3133 | Py_INCREF(Py_None); |
| 3134 | return Py_None; |
| 3135 | } |
| 3136 | /* Drop the argument parsing error as narrow strings |
| 3137 | are also valid. */ |
| 3138 | PyErr_Clear(); |
| 3139 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3140 | PyUnicode_FSConverter, &opath, &mode)) |
| 3141 | return NULL; |
| 3142 | path = PyBytes_AsString(opath); |
| 3143 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3144 | res = CreateDirectoryA(path, NULL); |
| 3145 | Py_END_ALLOW_THREADS |
| 3146 | if (!res) { |
| 3147 | win32_error("mkdir", path); |
| 3148 | Py_DECREF(opath); |
| 3149 | return NULL; |
| 3150 | } |
| 3151 | Py_DECREF(opath); |
| 3152 | Py_INCREF(Py_None); |
| 3153 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3154 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3155 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3157 | PyUnicode_FSConverter, &opath, &mode)) |
| 3158 | return NULL; |
| 3159 | path = PyBytes_AsString(opath); |
| 3160 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3161 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3162 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3163 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3164 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3165 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | Py_END_ALLOW_THREADS |
| 3167 | if (res < 0) |
| 3168 | return posix_error_with_allocated_filename(opath); |
| 3169 | Py_DECREF(opath); |
| 3170 | Py_INCREF(Py_None); |
| 3171 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3172 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3175 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3176 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3177 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3178 | #include <sys/resource.h> |
| 3179 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3180 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3181 | |
| 3182 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3183 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3184 | "nice(inc) -> new_priority\n\n\ |
| 3185 | 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] | 3186 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3187 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3188 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3189 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3190 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3192 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3193 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3194 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3195 | /* There are two flavours of 'nice': one that returns the new |
| 3196 | priority (as required by almost all standards out there) and the |
| 3197 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3198 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3200 | If we are of the nice family that returns the new priority, we |
| 3201 | need to clear errno before the call, and check if errno is filled |
| 3202 | before calling posix_error() on a returnvalue of -1, because the |
| 3203 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3204 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3205 | errno = 0; |
| 3206 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3207 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3208 | if (value == 0) |
| 3209 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3210 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3211 | if (value == -1 && errno != 0) |
| 3212 | /* either nice() or getpriority() returned an error */ |
| 3213 | return posix_error(); |
| 3214 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3215 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3216 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3217 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3218 | |
| 3219 | #ifdef HAVE_GETPRIORITY |
| 3220 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3221 | "getpriority(which, who) -> current_priority\n\n\ |
| 3222 | Get program scheduling priority."); |
| 3223 | |
| 3224 | static PyObject * |
| 3225 | posix_getpriority(PyObject *self, PyObject *args) |
| 3226 | { |
| 3227 | int which, who, retval; |
| 3228 | |
| 3229 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3230 | return NULL; |
| 3231 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3232 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3233 | if (errno != 0) |
| 3234 | return posix_error(); |
| 3235 | return PyLong_FromLong((long)retval); |
| 3236 | } |
| 3237 | #endif /* HAVE_GETPRIORITY */ |
| 3238 | |
| 3239 | |
| 3240 | #ifdef HAVE_SETPRIORITY |
| 3241 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3242 | "setpriority(which, who, prio) -> None\n\n\ |
| 3243 | Set program scheduling priority."); |
| 3244 | |
| 3245 | static PyObject * |
| 3246 | posix_setpriority(PyObject *self, PyObject *args) |
| 3247 | { |
| 3248 | int which, who, prio, retval; |
| 3249 | |
| 3250 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3251 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3252 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3253 | if (retval == -1) |
| 3254 | return posix_error(); |
| 3255 | Py_RETURN_NONE; |
| 3256 | } |
| 3257 | #endif /* HAVE_SETPRIORITY */ |
| 3258 | |
| 3259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3260 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3261 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3262 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3263 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3264 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3265 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3266 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3267 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3268 | PyObject *o1, *o2; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3269 | wchar_t *w1, *w2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3270 | char *p1, *p2; |
| 3271 | BOOL result; |
| 3272 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3273 | goto error; |
| 3274 | if (!convert_to_unicode(&o1)) |
| 3275 | goto error; |
| 3276 | if (!convert_to_unicode(&o2)) { |
| 3277 | Py_DECREF(o1); |
| 3278 | goto error; |
| 3279 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3280 | w1 = PyUnicode_AsUnicode(o1); |
| 3281 | if (w1 == NULL) |
| 3282 | goto error; |
| 3283 | w2 = PyUnicode_AsUnicode(o2); |
| 3284 | if (w2 == NULL) |
| 3285 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3286 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3287 | result = MoveFileW(w1, w2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3288 | Py_END_ALLOW_THREADS |
| 3289 | Py_DECREF(o1); |
| 3290 | Py_DECREF(o2); |
| 3291 | if (!result) |
| 3292 | return win32_error("rename", NULL); |
| 3293 | Py_INCREF(Py_None); |
| 3294 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3295 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3296 | PyErr_Clear(); |
| 3297 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3298 | return NULL; |
| 3299 | Py_BEGIN_ALLOW_THREADS |
| 3300 | result = MoveFileA(p1, p2); |
| 3301 | Py_END_ALLOW_THREADS |
| 3302 | if (!result) |
| 3303 | return win32_error("rename", NULL); |
| 3304 | Py_INCREF(Py_None); |
| 3305 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3306 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3307 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3308 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3312 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3313 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3314 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3315 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3316 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3317 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3318 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3319 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3320 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3321 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3322 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3323 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3326 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3327 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3328 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3329 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3330 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3331 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3332 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3333 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3334 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3335 | return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3336 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3337 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3338 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3341 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3342 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3343 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3344 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3345 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3346 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3347 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3348 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3350 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3351 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3352 | wchar_t *command; |
| 3353 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3354 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3356 | Py_BEGIN_ALLOW_THREADS |
| 3357 | sts = _wsystem(command); |
| 3358 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3359 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3360 | PyObject *command_obj; |
| 3361 | char *command; |
| 3362 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3363 | PyUnicode_FSConverter, &command_obj)) |
| 3364 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3366 | command = PyBytes_AsString(command_obj); |
| 3367 | Py_BEGIN_ALLOW_THREADS |
| 3368 | sts = system(command); |
| 3369 | Py_END_ALLOW_THREADS |
| 3370 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3371 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3372 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3373 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3374 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3375 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3377 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3378 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3379 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3380 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3381 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3382 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3384 | int i; |
| 3385 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3386 | return NULL; |
| 3387 | i = (int)umask(i); |
| 3388 | if (i < 0) |
| 3389 | return posix_error(); |
| 3390 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3393 | #ifdef MS_WINDOWS |
| 3394 | |
| 3395 | /* override the default DeleteFileW behavior so that directory |
| 3396 | symlinks can be removed with this function, the same as with |
| 3397 | Unix symlinks */ |
| 3398 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3399 | { |
| 3400 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3401 | WIN32_FIND_DATAW find_data; |
| 3402 | HANDLE find_data_handle; |
| 3403 | int is_directory = 0; |
| 3404 | int is_link = 0; |
| 3405 | |
| 3406 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3407 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3408 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3409 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3410 | it is a symlink */ |
| 3411 | if(is_directory && |
| 3412 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3413 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3414 | |
| 3415 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3416 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3417 | FindClose(find_data_handle); |
| 3418 | } |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | if (is_directory && is_link) |
| 3423 | return RemoveDirectoryW(lpFileName); |
| 3424 | |
| 3425 | return DeleteFileW(lpFileName); |
| 3426 | } |
| 3427 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3429 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3430 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3433 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3434 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3435 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3436 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3437 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3438 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3439 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3440 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3441 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3442 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3443 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3444 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3445 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3448 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3449 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3450 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3451 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3452 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3454 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3455 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3456 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3457 | struct utsname u; |
| 3458 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3460 | Py_BEGIN_ALLOW_THREADS |
| 3461 | res = uname(&u); |
| 3462 | Py_END_ALLOW_THREADS |
| 3463 | if (res < 0) |
| 3464 | return posix_error(); |
| 3465 | return Py_BuildValue("(sssss)", |
| 3466 | u.sysname, |
| 3467 | u.nodename, |
| 3468 | u.release, |
| 3469 | u.version, |
| 3470 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3471 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3472 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3473 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3474 | |
| 3475 | /* |
| 3476 | * Classic POSIX utime functions supported microseconds (1m/sec). |
| 3477 | * Newer POSIX functions support nanoseconds (1 billion per sec). |
| 3478 | * posixmodule now uses the new functions where possible. |
| 3479 | * This improves accuracy in many situations, for example shutil.copy2(). |
| 3480 | * |
| 3481 | * The implementation isn't currently sophisticated enough to handle |
| 3482 | * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false. |
| 3483 | * Specifically, posix_futimes() would break. |
| 3484 | * |
| 3485 | * Supporting such a platform wouldn't be impossible; you'd need two |
| 3486 | * extract_time() functions, or make its precision a parameter. |
| 3487 | * Since such a platform seems unlikely we haven't bothered. |
| 3488 | */ |
| 3489 | #if defined(HAVE_UTIMENSAT) |
| 3490 | #define EXTRACT_TIME_PRECISION (1e9) |
| 3491 | #if !defined(HAVE_FUTIMENS) |
| 3492 | #error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment. |
| 3493 | #endif |
| 3494 | #else |
| 3495 | #define EXTRACT_TIME_PRECISION (1e6) |
| 3496 | #endif |
| 3497 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3498 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3499 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3500 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3501 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3502 | if (PyFloat_Check(t)) { |
| 3503 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3504 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3505 | if (!intobj) |
| 3506 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3507 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3508 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3509 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3510 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3511 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3512 | Py_DECREF(intobj); |
| 3513 | if (intval == -1 && PyErr_Occurred()) |
| 3514 | return -1; |
| 3515 | *sec = intval; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3516 | |
| 3517 | *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3518 | if (*usec < 0) |
| 3519 | /* If rounding gave us a negative number, |
| 3520 | truncate. */ |
| 3521 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3522 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3523 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3524 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3525 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3526 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3527 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3528 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3529 | if (intval == -1 && PyErr_Occurred()) |
| 3530 | return -1; |
| 3531 | *sec = intval; |
| 3532 | *usec = 0; |
| 3533 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3534 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3536 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3537 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3538 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3539 | 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] | 3540 | 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] | 3541 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3542 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3543 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3544 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3545 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3546 | PyObject *arg; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3547 | PyObject *obwpath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | wchar_t *wpath = NULL; |
| 3549 | PyObject *oapath; |
| 3550 | char *apath; |
| 3551 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3552 | time_t atimesec, mtimesec; |
| 3553 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | FILETIME atime, mtime; |
| 3555 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3558 | wpath = PyUnicode_AsUnicode(obwpath); |
| 3559 | if (wpath == NULL) |
| 3560 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3561 | Py_BEGIN_ALLOW_THREADS |
| 3562 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3563 | NULL, OPEN_EXISTING, |
| 3564 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3565 | Py_END_ALLOW_THREADS |
| 3566 | if (hFile == INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3567 | return win32_error_object("utime", obwpath); |
| 3568 | } |
| 3569 | else { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | /* Drop the argument parsing error as narrow strings |
| 3571 | are also valid. */ |
| 3572 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3573 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3574 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3575 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3576 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3577 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3578 | apath = PyBytes_AsString(oapath); |
| 3579 | Py_BEGIN_ALLOW_THREADS |
| 3580 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3581 | NULL, OPEN_EXISTING, |
| 3582 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3583 | Py_END_ALLOW_THREADS |
| 3584 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3585 | win32_error("utime", apath); |
| 3586 | Py_DECREF(oapath); |
| 3587 | return NULL; |
| 3588 | } |
| 3589 | Py_DECREF(oapath); |
| 3590 | } |
| 3591 | |
| 3592 | if (arg == Py_None) { |
| 3593 | SYSTEMTIME now; |
| 3594 | GetSystemTime(&now); |
| 3595 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3596 | !SystemTimeToFileTime(&now, &atime)) { |
| 3597 | win32_error("utime", NULL); |
| 3598 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3599 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3600 | } |
| 3601 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3602 | PyErr_SetString(PyExc_TypeError, |
| 3603 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3604 | goto done; |
| 3605 | } |
| 3606 | else { |
| 3607 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3608 | &atimesec, &ausec) == -1) |
| 3609 | goto done; |
| 3610 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3611 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3612 | &mtimesec, &musec) == -1) |
| 3613 | goto done; |
| 3614 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3615 | } |
| 3616 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3617 | /* Avoid putting the file name into the error here, |
| 3618 | as that may confuse the user into believing that |
| 3619 | something is wrong with the file, when it also |
| 3620 | could be the time stamp that gives a problem. */ |
| 3621 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3622 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3623 | } |
| 3624 | Py_INCREF(Py_None); |
| 3625 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3626 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3627 | CloseHandle(hFile); |
| 3628 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3629 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3630 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3631 | PyObject *opath; |
| 3632 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3633 | time_t atime, mtime; |
| 3634 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3635 | int res; |
| 3636 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3637 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3638 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3639 | PyUnicode_FSConverter, &opath, &arg)) |
| 3640 | return NULL; |
| 3641 | path = PyBytes_AsString(opath); |
| 3642 | if (arg == Py_None) { |
| 3643 | /* optional time values not given */ |
| 3644 | Py_BEGIN_ALLOW_THREADS |
| 3645 | res = utime(path, NULL); |
| 3646 | Py_END_ALLOW_THREADS |
| 3647 | } |
| 3648 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3649 | PyErr_SetString(PyExc_TypeError, |
| 3650 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3651 | Py_DECREF(opath); |
| 3652 | return NULL; |
| 3653 | } |
| 3654 | else { |
| 3655 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3656 | &atime, &ausec) == -1) { |
| 3657 | Py_DECREF(opath); |
| 3658 | return NULL; |
| 3659 | } |
| 3660 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3661 | &mtime, &musec) == -1) { |
| 3662 | Py_DECREF(opath); |
| 3663 | return NULL; |
| 3664 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3665 | |
| 3666 | Py_BEGIN_ALLOW_THREADS |
| 3667 | { |
| 3668 | #ifdef HAVE_UTIMENSAT |
| 3669 | struct timespec buf[2]; |
| 3670 | buf[0].tv_sec = atime; |
| 3671 | buf[0].tv_nsec = ausec; |
| 3672 | buf[1].tv_sec = mtime; |
| 3673 | buf[1].tv_nsec = musec; |
| 3674 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3675 | #elif defined(HAVE_UTIMES) |
| 3676 | struct timeval buf[2]; |
| 3677 | buf[0].tv_sec = atime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3678 | buf[0].tv_usec = ausec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3679 | buf[1].tv_sec = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3680 | buf[1].tv_usec = musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3681 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3682 | #elif defined(HAVE_UTIME_H) |
| 3683 | /* XXX should define struct utimbuf instead, above */ |
| 3684 | struct utimbuf buf; |
| 3685 | buf.actime = atime; |
| 3686 | buf.modtime = mtime; |
| 3687 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3688 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3689 | time_t buf[2]; |
| 3690 | buf[0] = atime; |
| 3691 | buf[1] = mtime; |
| 3692 | res = utime(path, buf); |
| 3693 | #endif |
| 3694 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3695 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3696 | } |
| 3697 | if (res < 0) { |
| 3698 | return posix_error_with_allocated_filename(opath); |
| 3699 | } |
| 3700 | Py_DECREF(opath); |
| 3701 | Py_INCREF(Py_None); |
| 3702 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3703 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3704 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3707 | #ifdef HAVE_FUTIMES |
| 3708 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3709 | "futimes(fd, (atime, mtime))\n\ |
| 3710 | futimes(fd, None)\n\n\ |
| 3711 | Set the access and modified time of the file specified by the file\n\ |
| 3712 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3713 | access and modified times to the current time."); |
| 3714 | |
| 3715 | static PyObject * |
| 3716 | posix_futimes(PyObject *self, PyObject *args) |
| 3717 | { |
| 3718 | int res, fd; |
| 3719 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3720 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3721 | long ausec, musec; |
| 3722 | |
| 3723 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3724 | return NULL; |
| 3725 | |
| 3726 | if (arg == Py_None) { |
| 3727 | /* optional time values not given */ |
| 3728 | Py_BEGIN_ALLOW_THREADS |
| 3729 | res = futimes(fd, NULL); |
| 3730 | Py_END_ALLOW_THREADS |
| 3731 | } |
| 3732 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3733 | PyErr_SetString(PyExc_TypeError, |
| 3734 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3735 | return NULL; |
| 3736 | } |
| 3737 | else { |
| 3738 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3739 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3740 | return NULL; |
| 3741 | } |
| 3742 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3743 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3744 | return NULL; |
| 3745 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3746 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3747 | { |
| 3748 | #ifdef HAVE_FUTIMENS |
| 3749 | struct timespec buf[2]; |
| 3750 | buf[0].tv_sec = atime; |
| 3751 | buf[0].tv_nsec = ausec; |
| 3752 | buf[1].tv_sec = mtime; |
| 3753 | buf[1].tv_nsec = musec; |
| 3754 | res = futimens(fd, buf); |
| 3755 | #else |
| 3756 | struct timeval buf[2]; |
| 3757 | buf[0].tv_sec = atime; |
| 3758 | buf[0].tv_usec = ausec; |
| 3759 | buf[1].tv_sec = mtime; |
| 3760 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3761 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3762 | #endif |
| 3763 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3764 | Py_END_ALLOW_THREADS |
| 3765 | } |
| 3766 | if (res < 0) |
| 3767 | return posix_error(); |
| 3768 | Py_RETURN_NONE; |
| 3769 | } |
| 3770 | #endif |
| 3771 | |
| 3772 | #ifdef HAVE_LUTIMES |
| 3773 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3774 | "lutimes(path, (atime, mtime))\n\ |
| 3775 | lutimes(path, None)\n\n\ |
| 3776 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3777 | |
| 3778 | static PyObject * |
| 3779 | posix_lutimes(PyObject *self, PyObject *args) |
| 3780 | { |
| 3781 | PyObject *opath, *arg; |
| 3782 | const char *path; |
| 3783 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3784 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3785 | long ausec, musec; |
| 3786 | |
| 3787 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3788 | PyUnicode_FSConverter, &opath, &arg)) |
| 3789 | return NULL; |
| 3790 | path = PyBytes_AsString(opath); |
| 3791 | if (arg == Py_None) { |
| 3792 | /* optional time values not given */ |
| 3793 | Py_BEGIN_ALLOW_THREADS |
| 3794 | res = lutimes(path, NULL); |
| 3795 | Py_END_ALLOW_THREADS |
| 3796 | } |
| 3797 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3798 | PyErr_SetString(PyExc_TypeError, |
| 3799 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3800 | Py_DECREF(opath); |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | else { |
| 3804 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3805 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3806 | Py_DECREF(opath); |
| 3807 | return NULL; |
| 3808 | } |
| 3809 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3810 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3811 | Py_DECREF(opath); |
| 3812 | return NULL; |
| 3813 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3814 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3815 | { |
| 3816 | #ifdef HAVE_UTIMENSAT |
| 3817 | struct timespec buf[2]; |
| 3818 | buf[0].tv_sec = atime; |
| 3819 | buf[0].tv_nsec = ausec; |
| 3820 | buf[1].tv_sec = mtime; |
| 3821 | buf[1].tv_nsec = musec; |
| 3822 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3823 | #else |
| 3824 | struct timeval buf[2]; |
| 3825 | buf[0].tv_sec = atime; |
| 3826 | buf[0].tv_usec = ausec; |
| 3827 | buf[1].tv_sec = mtime; |
| 3828 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3829 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3830 | #endif |
| 3831 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3832 | Py_END_ALLOW_THREADS |
| 3833 | } |
| 3834 | Py_DECREF(opath); |
| 3835 | if (res < 0) |
| 3836 | return posix_error(); |
| 3837 | Py_RETURN_NONE; |
| 3838 | } |
| 3839 | #endif |
| 3840 | |
| 3841 | #ifdef HAVE_FUTIMENS |
| 3842 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3843 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3844 | futimens(fd, None, None)\n\n\ |
| 3845 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3846 | nanosecond precision.\n\ |
| 3847 | The second form sets atime and mtime to the current time.\n\ |
| 3848 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3849 | current time.\n\ |
| 3850 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3851 | |
| 3852 | static PyObject * |
| 3853 | posix_futimens(PyObject *self, PyObject *args) |
| 3854 | { |
| 3855 | int res, fd; |
| 3856 | PyObject *atime, *mtime; |
| 3857 | struct timespec buf[2]; |
| 3858 | |
| 3859 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3860 | &fd, &atime, &mtime)) |
| 3861 | return NULL; |
| 3862 | if (atime == Py_None && mtime == Py_None) { |
| 3863 | /* optional time values not given */ |
| 3864 | Py_BEGIN_ALLOW_THREADS |
| 3865 | res = futimens(fd, NULL); |
| 3866 | Py_END_ALLOW_THREADS |
| 3867 | } |
| 3868 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3869 | PyErr_SetString(PyExc_TypeError, |
| 3870 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3871 | return NULL; |
| 3872 | } |
| 3873 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3874 | PyErr_SetString(PyExc_TypeError, |
| 3875 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3876 | return NULL; |
| 3877 | } |
| 3878 | else { |
| 3879 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3880 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3881 | return NULL; |
| 3882 | } |
| 3883 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3884 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3885 | return NULL; |
| 3886 | } |
| 3887 | Py_BEGIN_ALLOW_THREADS |
| 3888 | res = futimens(fd, buf); |
| 3889 | Py_END_ALLOW_THREADS |
| 3890 | } |
| 3891 | if (res < 0) |
| 3892 | return posix_error(); |
| 3893 | Py_RETURN_NONE; |
| 3894 | } |
| 3895 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3896 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3897 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3898 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3899 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3900 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3901 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3902 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3903 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3904 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3905 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3906 | int sts; |
| 3907 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3908 | return NULL; |
| 3909 | _exit(sts); |
| 3910 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3913 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3914 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3915 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3916 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3917 | Py_ssize_t i; |
| 3918 | for (i = 0; i < count; i++) |
| 3919 | PyMem_Free(array[i]); |
| 3920 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3921 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3922 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3923 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3924 | int fsconvert_strdup(PyObject *o, char**out) |
| 3925 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3926 | PyObject *bytes; |
| 3927 | Py_ssize_t size; |
| 3928 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3929 | return 0; |
| 3930 | size = PyBytes_GET_SIZE(bytes); |
| 3931 | *out = PyMem_Malloc(size+1); |
| 3932 | if (!*out) |
| 3933 | return 0; |
| 3934 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3935 | Py_DECREF(bytes); |
| 3936 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3937 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3938 | #endif |
| 3939 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3940 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3941 | static char** |
| 3942 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3943 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3944 | char **envlist; |
| 3945 | Py_ssize_t i, pos, envc; |
| 3946 | PyObject *keys=NULL, *vals=NULL; |
| 3947 | PyObject *key, *val, *key2, *val2; |
| 3948 | char *p, *k, *v; |
| 3949 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3950 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3951 | i = PyMapping_Size(env); |
| 3952 | if (i < 0) |
| 3953 | return NULL; |
| 3954 | envlist = PyMem_NEW(char *, i + 1); |
| 3955 | if (envlist == NULL) { |
| 3956 | PyErr_NoMemory(); |
| 3957 | return NULL; |
| 3958 | } |
| 3959 | envc = 0; |
| 3960 | keys = PyMapping_Keys(env); |
| 3961 | vals = PyMapping_Values(env); |
| 3962 | if (!keys || !vals) |
| 3963 | goto error; |
| 3964 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3965 | PyErr_Format(PyExc_TypeError, |
| 3966 | "env.keys() or env.values() is not a list"); |
| 3967 | goto error; |
| 3968 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3969 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3970 | for (pos = 0; pos < i; pos++) { |
| 3971 | key = PyList_GetItem(keys, pos); |
| 3972 | val = PyList_GetItem(vals, pos); |
| 3973 | if (!key || !val) |
| 3974 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3976 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3977 | goto error; |
| 3978 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3979 | Py_DECREF(key2); |
| 3980 | goto error; |
| 3981 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3982 | |
| 3983 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3984 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3985 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3986 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3987 | k = PyBytes_AsString(key2); |
| 3988 | v = PyBytes_AsString(val2); |
| 3989 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3991 | p = PyMem_NEW(char, len); |
| 3992 | if (p == NULL) { |
| 3993 | PyErr_NoMemory(); |
| 3994 | Py_DECREF(key2); |
| 3995 | Py_DECREF(val2); |
| 3996 | goto error; |
| 3997 | } |
| 3998 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3999 | envlist[envc++] = p; |
| 4000 | Py_DECREF(key2); |
| 4001 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4002 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4003 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4004 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | } |
| 4006 | Py_DECREF(vals); |
| 4007 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4009 | envlist[envc] = 0; |
| 4010 | *envc_ptr = envc; |
| 4011 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4012 | |
| 4013 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4014 | Py_XDECREF(keys); |
| 4015 | Py_XDECREF(vals); |
| 4016 | while (--envc >= 0) |
| 4017 | PyMem_DEL(envlist[envc]); |
| 4018 | PyMem_DEL(envlist); |
| 4019 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4020 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4021 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4022 | static char** |
| 4023 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4024 | { |
| 4025 | int i; |
| 4026 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4027 | if (argvlist == NULL) { |
| 4028 | PyErr_NoMemory(); |
| 4029 | return NULL; |
| 4030 | } |
| 4031 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4032 | PyObject* item = PySequence_ITEM(argv, i); |
| 4033 | if (item == NULL) |
| 4034 | goto fail; |
| 4035 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4036 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4037 | goto fail; |
| 4038 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4039 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4040 | } |
| 4041 | argvlist[*argc] = NULL; |
| 4042 | return argvlist; |
| 4043 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4044 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4045 | free_string_array(argvlist, *argc); |
| 4046 | return NULL; |
| 4047 | } |
| 4048 | #endif |
| 4049 | |
| 4050 | #ifdef HAVE_EXECV |
| 4051 | PyDoc_STRVAR(posix_execv__doc__, |
| 4052 | "execv(path, args)\n\n\ |
| 4053 | Execute an executable path with arguments, replacing current process.\n\ |
| 4054 | \n\ |
| 4055 | path: path of executable file\n\ |
| 4056 | args: tuple or list of strings"); |
| 4057 | |
| 4058 | static PyObject * |
| 4059 | posix_execv(PyObject *self, PyObject *args) |
| 4060 | { |
| 4061 | PyObject *opath; |
| 4062 | char *path; |
| 4063 | PyObject *argv; |
| 4064 | char **argvlist; |
| 4065 | Py_ssize_t argc; |
| 4066 | |
| 4067 | /* execv has two arguments: (path, argv), where |
| 4068 | argv is a list or tuple of strings. */ |
| 4069 | |
| 4070 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4071 | PyUnicode_FSConverter, |
| 4072 | &opath, &argv)) |
| 4073 | return NULL; |
| 4074 | path = PyBytes_AsString(opath); |
| 4075 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4076 | PyErr_SetString(PyExc_TypeError, |
| 4077 | "execv() arg 2 must be a tuple or list"); |
| 4078 | Py_DECREF(opath); |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | argc = PySequence_Size(argv); |
| 4082 | if (argc < 1) { |
| 4083 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4084 | Py_DECREF(opath); |
| 4085 | return NULL; |
| 4086 | } |
| 4087 | |
| 4088 | argvlist = parse_arglist(argv, &argc); |
| 4089 | if (argvlist == NULL) { |
| 4090 | Py_DECREF(opath); |
| 4091 | return NULL; |
| 4092 | } |
| 4093 | |
| 4094 | execv(path, argvlist); |
| 4095 | |
| 4096 | /* If we get here it's definitely an error */ |
| 4097 | |
| 4098 | free_string_array(argvlist, argc); |
| 4099 | Py_DECREF(opath); |
| 4100 | return posix_error(); |
| 4101 | } |
| 4102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4103 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4104 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4105 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4106 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4107 | path: path of executable file\n\ |
| 4108 | args: tuple or list of arguments\n\ |
| 4109 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4110 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4111 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4112 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4113 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4114 | PyObject *opath; |
| 4115 | char *path; |
| 4116 | PyObject *argv, *env; |
| 4117 | char **argvlist; |
| 4118 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4119 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4120 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4121 | /* execve has three arguments: (path, argv, env), where |
| 4122 | argv is a list or tuple of strings and env is a dictionary |
| 4123 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4125 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4126 | PyUnicode_FSConverter, |
| 4127 | &opath, &argv, &env)) |
| 4128 | return NULL; |
| 4129 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4130 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4131 | PyErr_SetString(PyExc_TypeError, |
| 4132 | "execve() arg 2 must be a tuple or list"); |
| 4133 | goto fail_0; |
| 4134 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4135 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4136 | if (!PyMapping_Check(env)) { |
| 4137 | PyErr_SetString(PyExc_TypeError, |
| 4138 | "execve() arg 3 must be a mapping object"); |
| 4139 | goto fail_0; |
| 4140 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4141 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4142 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4143 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4144 | goto fail_0; |
| 4145 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4147 | envlist = parse_envlist(env, &envc); |
| 4148 | if (envlist == NULL) |
| 4149 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4151 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4153 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4155 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4156 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4157 | while (--envc >= 0) |
| 4158 | PyMem_DEL(envlist[envc]); |
| 4159 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4160 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4161 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4162 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4163 | Py_DECREF(opath); |
| 4164 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4165 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4166 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4167 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4168 | #ifdef HAVE_FEXECVE |
| 4169 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4170 | "fexecve(fd, args, env)\n\n\ |
| 4171 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4172 | environment, replacing the current process.\n\ |
| 4173 | \n\ |
| 4174 | fd: file descriptor of executable\n\ |
| 4175 | args: tuple or list of arguments\n\ |
| 4176 | env: dictionary of strings mapping to strings"); |
| 4177 | |
| 4178 | static PyObject * |
| 4179 | posix_fexecve(PyObject *self, PyObject *args) |
| 4180 | { |
| 4181 | int fd; |
| 4182 | PyObject *argv, *env; |
| 4183 | char **argvlist; |
| 4184 | char **envlist; |
| 4185 | Py_ssize_t argc, envc; |
| 4186 | |
| 4187 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4188 | &fd, &argv, &env)) |
| 4189 | return NULL; |
| 4190 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4191 | PyErr_SetString(PyExc_TypeError, |
| 4192 | "fexecve() arg 2 must be a tuple or list"); |
| 4193 | return NULL; |
| 4194 | } |
| 4195 | argc = PySequence_Size(argv); |
| 4196 | if (!PyMapping_Check(env)) { |
| 4197 | PyErr_SetString(PyExc_TypeError, |
| 4198 | "fexecve() arg 3 must be a mapping object"); |
| 4199 | return NULL; |
| 4200 | } |
| 4201 | |
| 4202 | argvlist = parse_arglist(argv, &argc); |
| 4203 | if (argvlist == NULL) |
| 4204 | return NULL; |
| 4205 | |
| 4206 | envlist = parse_envlist(env, &envc); |
| 4207 | if (envlist == NULL) |
| 4208 | goto fail; |
| 4209 | |
| 4210 | fexecve(fd, argvlist, envlist); |
| 4211 | |
| 4212 | /* If we get here it's definitely an error */ |
| 4213 | |
| 4214 | (void) posix_error(); |
| 4215 | |
| 4216 | while (--envc >= 0) |
| 4217 | PyMem_DEL(envlist[envc]); |
| 4218 | PyMem_DEL(envlist); |
| 4219 | fail: |
| 4220 | free_string_array(argvlist, argc); |
| 4221 | return NULL; |
| 4222 | } |
| 4223 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4224 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4225 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4226 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4227 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4228 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4229 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4230 | mode: mode of process creation\n\ |
| 4231 | path: path of executable file\n\ |
| 4232 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4233 | |
| 4234 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4235 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4236 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4237 | PyObject *opath; |
| 4238 | char *path; |
| 4239 | PyObject *argv; |
| 4240 | char **argvlist; |
| 4241 | int mode, i; |
| 4242 | Py_ssize_t argc; |
| 4243 | Py_intptr_t spawnval; |
| 4244 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4246 | /* spawnv has three arguments: (mode, path, argv), where |
| 4247 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4248 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4249 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4250 | PyUnicode_FSConverter, |
| 4251 | &opath, &argv)) |
| 4252 | return NULL; |
| 4253 | path = PyBytes_AsString(opath); |
| 4254 | if (PyList_Check(argv)) { |
| 4255 | argc = PyList_Size(argv); |
| 4256 | getitem = PyList_GetItem; |
| 4257 | } |
| 4258 | else if (PyTuple_Check(argv)) { |
| 4259 | argc = PyTuple_Size(argv); |
| 4260 | getitem = PyTuple_GetItem; |
| 4261 | } |
| 4262 | else { |
| 4263 | PyErr_SetString(PyExc_TypeError, |
| 4264 | "spawnv() arg 2 must be a tuple or list"); |
| 4265 | Py_DECREF(opath); |
| 4266 | return NULL; |
| 4267 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4268 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4269 | argvlist = PyMem_NEW(char *, argc+1); |
| 4270 | if (argvlist == NULL) { |
| 4271 | Py_DECREF(opath); |
| 4272 | return PyErr_NoMemory(); |
| 4273 | } |
| 4274 | for (i = 0; i < argc; i++) { |
| 4275 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4276 | &argvlist[i])) { |
| 4277 | free_string_array(argvlist, i); |
| 4278 | PyErr_SetString( |
| 4279 | PyExc_TypeError, |
| 4280 | "spawnv() arg 2 must contain only strings"); |
| 4281 | Py_DECREF(opath); |
| 4282 | return NULL; |
| 4283 | } |
| 4284 | } |
| 4285 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4286 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4287 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4288 | Py_BEGIN_ALLOW_THREADS |
| 4289 | spawnval = spawnv(mode, path, argvlist); |
| 4290 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4291 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4292 | if (mode == _OLD_P_OVERLAY) |
| 4293 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4295 | Py_BEGIN_ALLOW_THREADS |
| 4296 | spawnval = _spawnv(mode, path, argvlist); |
| 4297 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4298 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4300 | free_string_array(argvlist, argc); |
| 4301 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4302 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4303 | if (spawnval == -1) |
| 4304 | return posix_error(); |
| 4305 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4306 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4307 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4308 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4309 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4310 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4314 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4315 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4316 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4317 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4318 | mode: mode of process creation\n\ |
| 4319 | path: path of executable file\n\ |
| 4320 | args: tuple or list of arguments\n\ |
| 4321 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4322 | |
| 4323 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4324 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4325 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4326 | PyObject *opath; |
| 4327 | char *path; |
| 4328 | PyObject *argv, *env; |
| 4329 | char **argvlist; |
| 4330 | char **envlist; |
| 4331 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4332 | int mode; |
| 4333 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4334 | Py_intptr_t spawnval; |
| 4335 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4336 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4338 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4339 | argv is a list or tuple of strings and env is a dictionary |
| 4340 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4342 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4343 | PyUnicode_FSConverter, |
| 4344 | &opath, &argv, &env)) |
| 4345 | return NULL; |
| 4346 | path = PyBytes_AsString(opath); |
| 4347 | if (PyList_Check(argv)) { |
| 4348 | argc = PyList_Size(argv); |
| 4349 | getitem = PyList_GetItem; |
| 4350 | } |
| 4351 | else if (PyTuple_Check(argv)) { |
| 4352 | argc = PyTuple_Size(argv); |
| 4353 | getitem = PyTuple_GetItem; |
| 4354 | } |
| 4355 | else { |
| 4356 | PyErr_SetString(PyExc_TypeError, |
| 4357 | "spawnve() arg 2 must be a tuple or list"); |
| 4358 | goto fail_0; |
| 4359 | } |
| 4360 | if (!PyMapping_Check(env)) { |
| 4361 | PyErr_SetString(PyExc_TypeError, |
| 4362 | "spawnve() arg 3 must be a mapping object"); |
| 4363 | goto fail_0; |
| 4364 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4366 | argvlist = PyMem_NEW(char *, argc+1); |
| 4367 | if (argvlist == NULL) { |
| 4368 | PyErr_NoMemory(); |
| 4369 | goto fail_0; |
| 4370 | } |
| 4371 | for (i = 0; i < argc; i++) { |
| 4372 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4373 | &argvlist[i])) |
| 4374 | { |
| 4375 | lastarg = i; |
| 4376 | goto fail_1; |
| 4377 | } |
| 4378 | } |
| 4379 | lastarg = argc; |
| 4380 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4382 | envlist = parse_envlist(env, &envc); |
| 4383 | if (envlist == NULL) |
| 4384 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4385 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4386 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4387 | Py_BEGIN_ALLOW_THREADS |
| 4388 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4389 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4390 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4391 | if (mode == _OLD_P_OVERLAY) |
| 4392 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4393 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4394 | Py_BEGIN_ALLOW_THREADS |
| 4395 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4396 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4397 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4398 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4399 | if (spawnval == -1) |
| 4400 | (void) posix_error(); |
| 4401 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4402 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4403 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4404 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4405 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4406 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4408 | while (--envc >= 0) |
| 4409 | PyMem_DEL(envlist[envc]); |
| 4410 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4411 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4412 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4413 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4414 | Py_DECREF(opath); |
| 4415 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4416 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4417 | |
| 4418 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4419 | #if defined(PYOS_OS2) |
| 4420 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4421 | "spawnvp(mode, file, args)\n\n\ |
| 4422 | Execute the program 'file' in a new process, using the environment\n\ |
| 4423 | search path to find the file.\n\ |
| 4424 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4425 | mode: mode of process creation\n\ |
| 4426 | file: executable file name\n\ |
| 4427 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4428 | |
| 4429 | static PyObject * |
| 4430 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4431 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4432 | PyObject *opath; |
| 4433 | char *path; |
| 4434 | PyObject *argv; |
| 4435 | char **argvlist; |
| 4436 | int mode, i, argc; |
| 4437 | Py_intptr_t spawnval; |
| 4438 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4440 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4441 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4443 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4444 | PyUnicode_FSConverter, |
| 4445 | &opath, &argv)) |
| 4446 | return NULL; |
| 4447 | path = PyBytes_AsString(opath); |
| 4448 | if (PyList_Check(argv)) { |
| 4449 | argc = PyList_Size(argv); |
| 4450 | getitem = PyList_GetItem; |
| 4451 | } |
| 4452 | else if (PyTuple_Check(argv)) { |
| 4453 | argc = PyTuple_Size(argv); |
| 4454 | getitem = PyTuple_GetItem; |
| 4455 | } |
| 4456 | else { |
| 4457 | PyErr_SetString(PyExc_TypeError, |
| 4458 | "spawnvp() arg 2 must be a tuple or list"); |
| 4459 | Py_DECREF(opath); |
| 4460 | return NULL; |
| 4461 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4462 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4463 | argvlist = PyMem_NEW(char *, argc+1); |
| 4464 | if (argvlist == NULL) { |
| 4465 | Py_DECREF(opath); |
| 4466 | return PyErr_NoMemory(); |
| 4467 | } |
| 4468 | for (i = 0; i < argc; i++) { |
| 4469 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4470 | &argvlist[i])) { |
| 4471 | free_string_array(argvlist, i); |
| 4472 | PyErr_SetString( |
| 4473 | PyExc_TypeError, |
| 4474 | "spawnvp() arg 2 must contain only strings"); |
| 4475 | Py_DECREF(opath); |
| 4476 | return NULL; |
| 4477 | } |
| 4478 | } |
| 4479 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4480 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4481 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4482 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4483 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4484 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4485 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4486 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4487 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4488 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4489 | free_string_array(argvlist, argc); |
| 4490 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4492 | if (spawnval == -1) |
| 4493 | return posix_error(); |
| 4494 | else |
| 4495 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | |
| 4499 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4500 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4501 | Execute the program 'file' in a new process, using the environment\n\ |
| 4502 | search path to find the file.\n\ |
| 4503 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4504 | mode: mode of process creation\n\ |
| 4505 | file: executable file name\n\ |
| 4506 | args: tuple or list of arguments\n\ |
| 4507 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4508 | |
| 4509 | static PyObject * |
| 4510 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4511 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4512 | PyObject *opath |
| 4513 | char *path; |
| 4514 | PyObject *argv, *env; |
| 4515 | char **argvlist; |
| 4516 | char **envlist; |
| 4517 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4518 | int mode; |
| 4519 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4520 | Py_intptr_t spawnval; |
| 4521 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4522 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4523 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4524 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4525 | argv is a list or tuple of strings and env is a dictionary |
| 4526 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4527 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4528 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4529 | PyUnicode_FSConverter, |
| 4530 | &opath, &argv, &env)) |
| 4531 | return NULL; |
| 4532 | path = PyBytes_AsString(opath); |
| 4533 | if (PyList_Check(argv)) { |
| 4534 | argc = PyList_Size(argv); |
| 4535 | getitem = PyList_GetItem; |
| 4536 | } |
| 4537 | else if (PyTuple_Check(argv)) { |
| 4538 | argc = PyTuple_Size(argv); |
| 4539 | getitem = PyTuple_GetItem; |
| 4540 | } |
| 4541 | else { |
| 4542 | PyErr_SetString(PyExc_TypeError, |
| 4543 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4544 | goto fail_0; |
| 4545 | } |
| 4546 | if (!PyMapping_Check(env)) { |
| 4547 | PyErr_SetString(PyExc_TypeError, |
| 4548 | "spawnvpe() arg 3 must be a mapping object"); |
| 4549 | goto fail_0; |
| 4550 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4551 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4552 | argvlist = PyMem_NEW(char *, argc+1); |
| 4553 | if (argvlist == NULL) { |
| 4554 | PyErr_NoMemory(); |
| 4555 | goto fail_0; |
| 4556 | } |
| 4557 | for (i = 0; i < argc; i++) { |
| 4558 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4559 | &argvlist[i])) |
| 4560 | { |
| 4561 | lastarg = i; |
| 4562 | goto fail_1; |
| 4563 | } |
| 4564 | } |
| 4565 | lastarg = argc; |
| 4566 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4567 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4568 | envlist = parse_envlist(env, &envc); |
| 4569 | if (envlist == NULL) |
| 4570 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4572 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4573 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4574 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4575 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4576 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4577 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4578 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4579 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4580 | if (spawnval == -1) |
| 4581 | (void) posix_error(); |
| 4582 | else |
| 4583 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4584 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4585 | while (--envc >= 0) |
| 4586 | PyMem_DEL(envlist[envc]); |
| 4587 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4588 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4589 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4590 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4591 | Py_DECREF(opath); |
| 4592 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4593 | } |
| 4594 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4595 | #endif /* HAVE_SPAWNV */ |
| 4596 | |
| 4597 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4598 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4599 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4600 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4601 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4602 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4603 | 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] | 4604 | |
| 4605 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4606 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4607 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4608 | pid_t pid; |
| 4609 | int result = 0; |
| 4610 | _PyImport_AcquireLock(); |
| 4611 | pid = fork1(); |
| 4612 | if (pid == 0) { |
| 4613 | /* child: this clobbers and resets the import lock. */ |
| 4614 | PyOS_AfterFork(); |
| 4615 | } else { |
| 4616 | /* parent: release the import lock. */ |
| 4617 | result = _PyImport_ReleaseLock(); |
| 4618 | } |
| 4619 | if (pid == -1) |
| 4620 | return posix_error(); |
| 4621 | if (result < 0) { |
| 4622 | /* Don't clobber the OSError if the fork failed. */ |
| 4623 | PyErr_SetString(PyExc_RuntimeError, |
| 4624 | "not holding the import lock"); |
| 4625 | return NULL; |
| 4626 | } |
| 4627 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4628 | } |
| 4629 | #endif |
| 4630 | |
| 4631 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4632 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4634 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4635 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4636 | 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] | 4637 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4638 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4639 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4640 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4641 | pid_t pid; |
| 4642 | int result = 0; |
| 4643 | _PyImport_AcquireLock(); |
| 4644 | pid = fork(); |
| 4645 | if (pid == 0) { |
| 4646 | /* child: this clobbers and resets the import lock. */ |
| 4647 | PyOS_AfterFork(); |
| 4648 | } else { |
| 4649 | /* parent: release the import lock. */ |
| 4650 | result = _PyImport_ReleaseLock(); |
| 4651 | } |
| 4652 | if (pid == -1) |
| 4653 | return posix_error(); |
| 4654 | if (result < 0) { |
| 4655 | /* Don't clobber the OSError if the fork failed. */ |
| 4656 | PyErr_SetString(PyExc_RuntimeError, |
| 4657 | "not holding the import lock"); |
| 4658 | return NULL; |
| 4659 | } |
| 4660 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4661 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4662 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4663 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4664 | #ifdef HAVE_SCHED_H |
| 4665 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4666 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4667 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4668 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4669 | "sched_get_priority_max(policy)\n\n\ |
| 4670 | Get the maximum scheduling priority for *policy*."); |
| 4671 | |
| 4672 | static PyObject * |
| 4673 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4674 | { |
| 4675 | int policy, max; |
| 4676 | |
| 4677 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4678 | return NULL; |
| 4679 | max = sched_get_priority_max(policy); |
| 4680 | if (max < 0) |
| 4681 | return posix_error(); |
| 4682 | return PyLong_FromLong(max); |
| 4683 | } |
| 4684 | |
| 4685 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4686 | "sched_get_priority_min(policy)\n\n\ |
| 4687 | Get the minimum scheduling priority for *policy*."); |
| 4688 | |
| 4689 | static PyObject * |
| 4690 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4691 | { |
| 4692 | int policy, min; |
| 4693 | |
| 4694 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4695 | return NULL; |
| 4696 | min = sched_get_priority_min(policy); |
| 4697 | if (min < 0) |
| 4698 | return posix_error(); |
| 4699 | return PyLong_FromLong(min); |
| 4700 | } |
| 4701 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4702 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4703 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4704 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4705 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4706 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4707 | "sched_getscheduler(pid)\n\n\ |
| 4708 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4709 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4710 | |
| 4711 | static PyObject * |
| 4712 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4713 | { |
| 4714 | pid_t pid; |
| 4715 | int policy; |
| 4716 | |
| 4717 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4718 | return NULL; |
| 4719 | policy = sched_getscheduler(pid); |
| 4720 | if (policy < 0) |
| 4721 | return posix_error(); |
| 4722 | return PyLong_FromLong(policy); |
| 4723 | } |
| 4724 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4725 | #endif |
| 4726 | |
| 4727 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4728 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4729 | static PyObject * |
| 4730 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4731 | { |
| 4732 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4733 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4734 | |
| 4735 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4736 | return NULL; |
| 4737 | res = PyStructSequence_New(type); |
| 4738 | if (!res) |
| 4739 | return NULL; |
| 4740 | Py_INCREF(priority); |
| 4741 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4742 | return res; |
| 4743 | } |
| 4744 | |
| 4745 | PyDoc_STRVAR(sched_param__doc__, |
| 4746 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4747 | Current has only one field: sched_priority"); |
| 4748 | |
| 4749 | static PyStructSequence_Field sched_param_fields[] = { |
| 4750 | {"sched_priority", "the scheduling priority"}, |
| 4751 | {0} |
| 4752 | }; |
| 4753 | |
| 4754 | static PyStructSequence_Desc sched_param_desc = { |
| 4755 | "sched_param", /* name */ |
| 4756 | sched_param__doc__, /* doc */ |
| 4757 | sched_param_fields, |
| 4758 | 1 |
| 4759 | }; |
| 4760 | |
| 4761 | static int |
| 4762 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4763 | { |
| 4764 | long priority; |
| 4765 | |
| 4766 | if (Py_TYPE(param) != &SchedParamType) { |
| 4767 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4768 | return 0; |
| 4769 | } |
| 4770 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4771 | if (priority == -1 && PyErr_Occurred()) |
| 4772 | return 0; |
| 4773 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4774 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4775 | return 0; |
| 4776 | } |
| 4777 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4778 | return 1; |
| 4779 | } |
| 4780 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4781 | #endif |
| 4782 | |
| 4783 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4784 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4785 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4786 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4787 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4788 | If *pid* is 0, the calling process is changed.\n\ |
| 4789 | *param* is an instance of sched_param."); |
| 4790 | |
| 4791 | static PyObject * |
| 4792 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4793 | { |
| 4794 | pid_t pid; |
| 4795 | int policy; |
| 4796 | struct sched_param param; |
| 4797 | |
| 4798 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4799 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4800 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4801 | |
| 4802 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4803 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4804 | ** scheduling policy under Solaris/Illumos, and others. |
| 4805 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4806 | */ |
| 4807 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4808 | return posix_error(); |
| 4809 | Py_RETURN_NONE; |
| 4810 | } |
| 4811 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4812 | #endif |
| 4813 | |
| 4814 | #ifdef HAVE_SCHED_SETPARAM |
| 4815 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4816 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4817 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4818 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4819 | sched_param class. A PID of 0 means the calling process."); |
| 4820 | |
| 4821 | static PyObject * |
| 4822 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4823 | { |
| 4824 | pid_t pid; |
| 4825 | struct sched_param param; |
| 4826 | PyObject *res, *priority; |
| 4827 | |
| 4828 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4829 | return NULL; |
| 4830 | if (sched_getparam(pid, ¶m)) |
| 4831 | return posix_error(); |
| 4832 | res = PyStructSequence_New(&SchedParamType); |
| 4833 | if (!res) |
| 4834 | return NULL; |
| 4835 | priority = PyLong_FromLong(param.sched_priority); |
| 4836 | if (!priority) { |
| 4837 | Py_DECREF(res); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4841 | return res; |
| 4842 | } |
| 4843 | |
| 4844 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4845 | "sched_setparam(pid, param)\n\n\ |
| 4846 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4847 | A PID of 0 means the calling process."); |
| 4848 | |
| 4849 | static PyObject * |
| 4850 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4851 | { |
| 4852 | pid_t pid; |
| 4853 | struct sched_param param; |
| 4854 | |
| 4855 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4856 | &pid, &convert_sched_param, ¶m)) |
| 4857 | return NULL; |
| 4858 | if (sched_setparam(pid, ¶m)) |
| 4859 | return posix_error(); |
| 4860 | Py_RETURN_NONE; |
| 4861 | } |
| 4862 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4863 | #endif |
| 4864 | |
| 4865 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4866 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4867 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4868 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4869 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4870 | |
| 4871 | static PyObject * |
| 4872 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4873 | { |
| 4874 | pid_t pid; |
| 4875 | struct timespec interval; |
| 4876 | |
| 4877 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4878 | return NULL; |
| 4879 | if (sched_rr_get_interval(pid, &interval)) |
| 4880 | return posix_error(); |
| 4881 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4882 | } |
| 4883 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4884 | #endif |
| 4885 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4886 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4887 | "sched_yield()\n\n\ |
| 4888 | Voluntarily relinquish the CPU."); |
| 4889 | |
| 4890 | static PyObject * |
| 4891 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4892 | { |
| 4893 | if (sched_yield()) |
| 4894 | return posix_error(); |
| 4895 | Py_RETURN_NONE; |
| 4896 | } |
| 4897 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4898 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4899 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4900 | typedef struct { |
| 4901 | PyObject_HEAD; |
| 4902 | Py_ssize_t size; |
| 4903 | int ncpus; |
| 4904 | cpu_set_t *set; |
| 4905 | } Py_cpu_set; |
| 4906 | |
| 4907 | static PyTypeObject cpu_set_type; |
| 4908 | |
| 4909 | static void |
| 4910 | cpu_set_dealloc(Py_cpu_set *set) |
| 4911 | { |
| 4912 | assert(set->set); |
| 4913 | CPU_FREE(set->set); |
| 4914 | Py_TYPE(set)->tp_free(set); |
| 4915 | } |
| 4916 | |
| 4917 | static Py_cpu_set * |
| 4918 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4919 | { |
| 4920 | Py_cpu_set *set; |
| 4921 | |
| 4922 | if (size < 0) { |
| 4923 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4924 | return NULL; |
| 4925 | } |
| 4926 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4927 | if (!set) |
| 4928 | return NULL; |
| 4929 | set->ncpus = size; |
| 4930 | set->size = CPU_ALLOC_SIZE(size); |
| 4931 | set->set = CPU_ALLOC(size); |
| 4932 | if (!set->set) { |
| 4933 | type->tp_free(set); |
| 4934 | PyErr_NoMemory(); |
| 4935 | return NULL; |
| 4936 | } |
| 4937 | CPU_ZERO_S(set->size, set->set); |
| 4938 | return set; |
| 4939 | } |
| 4940 | |
| 4941 | static PyObject * |
| 4942 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4943 | { |
| 4944 | int size; |
| 4945 | |
| 4946 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4947 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4948 | return NULL; |
| 4949 | return (PyObject *)make_new_cpu_set(type, size); |
| 4950 | } |
| 4951 | |
| 4952 | static PyObject * |
| 4953 | cpu_set_repr(Py_cpu_set *set) |
| 4954 | { |
| 4955 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4956 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4957 | |
| 4958 | static Py_ssize_t |
| 4959 | cpu_set_len(Py_cpu_set *set) |
| 4960 | { |
| 4961 | return set->ncpus; |
| 4962 | } |
| 4963 | |
| 4964 | static int |
| 4965 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4966 | { |
| 4967 | int cpu; |
| 4968 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4969 | return -1; |
| 4970 | if (cpu < 0) { |
| 4971 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4972 | return -1; |
| 4973 | } |
| 4974 | if (cpu >= set->ncpus) { |
| 4975 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4976 | return -1; |
| 4977 | } |
| 4978 | return cpu; |
| 4979 | } |
| 4980 | |
| 4981 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4982 | "cpu_set.set(i)\n\n\ |
| 4983 | Add CPU *i* to the set."); |
| 4984 | |
| 4985 | static PyObject * |
| 4986 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 4987 | { |
| 4988 | int cpu = _get_cpu(set, "i|set", args); |
| 4989 | if (cpu == -1) |
| 4990 | return NULL; |
| 4991 | CPU_SET_S(cpu, set->size, set->set); |
| 4992 | Py_RETURN_NONE; |
| 4993 | } |
| 4994 | |
| 4995 | PyDoc_STRVAR(cpu_set_count_doc, |
| 4996 | "cpu_set.count() -> int\n\n\ |
| 4997 | Return the number of CPUs active in the set."); |
| 4998 | |
| 4999 | static PyObject * |
| 5000 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5001 | { |
| 5002 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5003 | } |
| 5004 | |
| 5005 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5006 | "cpu_set.clear(i)\n\n\ |
| 5007 | Remove CPU *i* from the set."); |
| 5008 | |
| 5009 | static PyObject * |
| 5010 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5011 | { |
| 5012 | int cpu = _get_cpu(set, "i|clear", args); |
| 5013 | if (cpu == -1) |
| 5014 | return NULL; |
| 5015 | CPU_CLR_S(cpu, set->size, set->set); |
| 5016 | Py_RETURN_NONE; |
| 5017 | } |
| 5018 | |
| 5019 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5020 | "cpu_set.isset(i) -> bool\n\n\ |
| 5021 | Test if CPU *i* is in the set."); |
| 5022 | |
| 5023 | static PyObject * |
| 5024 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5025 | { |
| 5026 | int cpu = _get_cpu(set, "i|isset", args); |
| 5027 | if (cpu == -1) |
| 5028 | return NULL; |
| 5029 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5030 | Py_RETURN_TRUE; |
| 5031 | Py_RETURN_FALSE; |
| 5032 | } |
| 5033 | |
| 5034 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5035 | "cpu_set.zero()\n\n\ |
| 5036 | Clear the cpu_set."); |
| 5037 | |
| 5038 | static PyObject * |
| 5039 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5040 | { |
| 5041 | CPU_ZERO_S(set->size, set->set); |
| 5042 | Py_RETURN_NONE; |
| 5043 | } |
| 5044 | |
| 5045 | static PyObject * |
| 5046 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5047 | { |
| 5048 | int eq; |
| 5049 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5050 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5051 | Py_RETURN_NOTIMPLEMENTED; |
| 5052 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5053 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5054 | if ((op == Py_EQ) ? eq : !eq) |
| 5055 | Py_RETURN_TRUE; |
| 5056 | else |
| 5057 | Py_RETURN_FALSE; |
| 5058 | } |
| 5059 | |
| 5060 | #define CPU_SET_BINOP(name, op) \ |
| 5061 | static PyObject * \ |
| 5062 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5063 | if (res) { \ |
| 5064 | Py_INCREF(res); \ |
| 5065 | } \ |
| 5066 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5067 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5068 | if (!res) \ |
| 5069 | return NULL; \ |
| 5070 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5071 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5072 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5073 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5074 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5075 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5076 | op(res->size, res->set, left->set, right->set); \ |
| 5077 | return (PyObject *)res; \ |
| 5078 | } \ |
| 5079 | static PyObject * \ |
| 5080 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5081 | return do_cpu_set_##name(left, right, NULL); \ |
| 5082 | } \ |
| 5083 | static PyObject * \ |
| 5084 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5085 | return do_cpu_set_##name(left, right, left); \ |
| 5086 | } \ |
| 5087 | |
| 5088 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5089 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5090 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5091 | #undef CPU_SET_BINOP |
| 5092 | |
| 5093 | PyDoc_STRVAR(cpu_set_doc, |
| 5094 | "cpu_set(size)\n\n\ |
| 5095 | Create an empty mask of CPUs."); |
| 5096 | |
| 5097 | static PyNumberMethods cpu_set_as_number = { |
| 5098 | 0, /*nb_add*/ |
| 5099 | 0, /*nb_subtract*/ |
| 5100 | 0, /*nb_multiply*/ |
| 5101 | 0, /*nb_remainder*/ |
| 5102 | 0, /*nb_divmod*/ |
| 5103 | 0, /*nb_power*/ |
| 5104 | 0, /*nb_negative*/ |
| 5105 | 0, /*nb_positive*/ |
| 5106 | 0, /*nb_absolute*/ |
| 5107 | 0, /*nb_bool*/ |
| 5108 | 0, /*nb_invert*/ |
| 5109 | 0, /*nb_lshift*/ |
| 5110 | 0, /*nb_rshift*/ |
| 5111 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5112 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5113 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5114 | 0, /*nb_int*/ |
| 5115 | 0, /*nb_reserved*/ |
| 5116 | 0, /*nb_float*/ |
| 5117 | 0, /*nb_inplace_add*/ |
| 5118 | 0, /*nb_inplace_subtract*/ |
| 5119 | 0, /*nb_inplace_multiply*/ |
| 5120 | 0, /*nb_inplace_remainder*/ |
| 5121 | 0, /*nb_inplace_power*/ |
| 5122 | 0, /*nb_inplace_lshift*/ |
| 5123 | 0, /*nb_inplace_rshift*/ |
| 5124 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5125 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5126 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5127 | }; |
| 5128 | |
| 5129 | static PySequenceMethods cpu_set_as_sequence = { |
| 5130 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5131 | }; |
| 5132 | |
| 5133 | static PyMethodDef cpu_set_methods[] = { |
| 5134 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5135 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5136 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5137 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5138 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5139 | {NULL, NULL} /* sentinel */ |
| 5140 | }; |
| 5141 | |
| 5142 | static PyTypeObject cpu_set_type = { |
| 5143 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5144 | "posix.cpu_set", /* tp_name */ |
| 5145 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5146 | 0, /* tp_itemsize */ |
| 5147 | /* methods */ |
| 5148 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5149 | 0, /* tp_print */ |
| 5150 | 0, /* tp_getattr */ |
| 5151 | 0, /* tp_setattr */ |
| 5152 | 0, /* tp_reserved */ |
| 5153 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5154 | &cpu_set_as_number, /* tp_as_number */ |
| 5155 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5156 | 0, /* tp_as_mapping */ |
| 5157 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5158 | 0, /* tp_call */ |
| 5159 | 0, /* tp_str */ |
| 5160 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5161 | 0, /* tp_setattro */ |
| 5162 | 0, /* tp_as_buffer */ |
| 5163 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5164 | cpu_set_doc, /* tp_doc */ |
| 5165 | 0, /* tp_traverse */ |
| 5166 | 0, /* tp_clear */ |
| 5167 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5168 | 0, /* tp_weaklistoffset */ |
| 5169 | 0, /* tp_iter */ |
| 5170 | 0, /* tp_iternext */ |
| 5171 | cpu_set_methods, /* tp_methods */ |
| 5172 | 0, /* tp_members */ |
| 5173 | 0, /* tp_getset */ |
| 5174 | 0, /* tp_base */ |
| 5175 | 0, /* tp_dict */ |
| 5176 | 0, /* tp_descr_get */ |
| 5177 | 0, /* tp_descr_set */ |
| 5178 | 0, /* tp_dictoffset */ |
| 5179 | 0, /* tp_init */ |
| 5180 | PyType_GenericAlloc, /* tp_alloc */ |
| 5181 | cpu_set_new, /* tp_new */ |
| 5182 | PyObject_Del, /* tp_free */ |
| 5183 | }; |
| 5184 | |
| 5185 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5186 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5187 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5188 | |
| 5189 | static PyObject * |
| 5190 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5191 | { |
| 5192 | pid_t pid; |
| 5193 | Py_cpu_set *cpu_set; |
| 5194 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5195 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5196 | &pid, &cpu_set_type, &cpu_set)) |
| 5197 | return NULL; |
| 5198 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5199 | return posix_error(); |
| 5200 | Py_RETURN_NONE; |
| 5201 | } |
| 5202 | |
| 5203 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5204 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5205 | Return the affinity of the process with PID *pid*.\n\ |
| 5206 | The returned cpu_set will be of size *ncpus*."); |
| 5207 | |
| 5208 | static PyObject * |
| 5209 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5210 | { |
| 5211 | pid_t pid; |
| 5212 | int ncpus; |
| 5213 | Py_cpu_set *res; |
| 5214 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5215 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5216 | &pid, &ncpus)) |
| 5217 | return NULL; |
| 5218 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5219 | if (!res) |
| 5220 | return NULL; |
| 5221 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5222 | Py_DECREF(res); |
| 5223 | return posix_error(); |
| 5224 | } |
| 5225 | return (PyObject *)res; |
| 5226 | } |
| 5227 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5228 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5229 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5230 | #endif /* HAVE_SCHED_H */ |
| 5231 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5232 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5233 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5234 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5235 | #define DEV_PTY_FILE "/dev/ptc" |
| 5236 | #define HAVE_DEV_PTMX |
| 5237 | #else |
| 5238 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5239 | #endif |
| 5240 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5241 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5242 | #ifdef HAVE_PTY_H |
| 5243 | #include <pty.h> |
| 5244 | #else |
| 5245 | #ifdef HAVE_LIBUTIL_H |
| 5246 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5247 | #else |
| 5248 | #ifdef HAVE_UTIL_H |
| 5249 | #include <util.h> |
| 5250 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5251 | #endif /* HAVE_LIBUTIL_H */ |
| 5252 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5253 | #ifdef HAVE_STROPTS_H |
| 5254 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5255 | #endif |
| 5256 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5257 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5258 | #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] | 5259 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5260 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5261 | 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] | 5262 | |
| 5263 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5264 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5265 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5266 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5267 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5268 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5269 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5270 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5271 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5272 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5273 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5274 | #endif |
| 5275 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5276 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5277 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5278 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5279 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5280 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5281 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5282 | if (slave_name == NULL) |
| 5283 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5284 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5285 | slave_fd = open(slave_name, O_RDWR); |
| 5286 | if (slave_fd < 0) |
| 5287 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5288 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5289 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5290 | if (master_fd < 0) |
| 5291 | return posix_error(); |
| 5292 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5293 | /* change permission of slave */ |
| 5294 | if (grantpt(master_fd) < 0) { |
| 5295 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5296 | return posix_error(); |
| 5297 | } |
| 5298 | /* unlock slave */ |
| 5299 | if (unlockpt(master_fd) < 0) { |
| 5300 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5301 | return posix_error(); |
| 5302 | } |
| 5303 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5304 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5305 | if (slave_name == NULL) |
| 5306 | return posix_error(); |
| 5307 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5308 | if (slave_fd < 0) |
| 5309 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5310 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5311 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5312 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5313 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5314 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5315 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5316 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5317 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5319 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5320 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5321 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5322 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5323 | |
| 5324 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5326 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5327 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5328 | 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] | 5329 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5330 | |
| 5331 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5332 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5333 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5334 | int master_fd = -1, result = 0; |
| 5335 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5336 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5337 | _PyImport_AcquireLock(); |
| 5338 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5339 | if (pid == 0) { |
| 5340 | /* child: this clobbers and resets the import lock. */ |
| 5341 | PyOS_AfterFork(); |
| 5342 | } else { |
| 5343 | /* parent: release the import lock. */ |
| 5344 | result = _PyImport_ReleaseLock(); |
| 5345 | } |
| 5346 | if (pid == -1) |
| 5347 | return posix_error(); |
| 5348 | if (result < 0) { |
| 5349 | /* Don't clobber the OSError if the fork failed. */ |
| 5350 | PyErr_SetString(PyExc_RuntimeError, |
| 5351 | "not holding the import lock"); |
| 5352 | return NULL; |
| 5353 | } |
| 5354 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5355 | } |
| 5356 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5357 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5358 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5359 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5360 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5361 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5362 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5363 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5364 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5365 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5366 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5367 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5368 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5369 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5370 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5371 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5372 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5373 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5374 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5375 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5377 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5378 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5379 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5380 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5381 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5382 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5383 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5384 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5385 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5386 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5387 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5388 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5389 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5390 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5391 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5392 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5393 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5394 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5395 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5396 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5397 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5398 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5399 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5400 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5401 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5402 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5403 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5405 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5408 | #ifdef HAVE_GETGROUPLIST |
| 5409 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5410 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5411 | Returns a list of groups to which a user belongs.\n\n\ |
| 5412 | user: username to lookup\n\ |
| 5413 | group: base group id of the user"); |
| 5414 | |
| 5415 | static PyObject * |
| 5416 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5417 | { |
| 5418 | #ifdef NGROUPS_MAX |
| 5419 | #define MAX_GROUPS NGROUPS_MAX |
| 5420 | #else |
| 5421 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5422 | #define MAX_GROUPS 64 |
| 5423 | #endif |
| 5424 | |
| 5425 | const char *user; |
| 5426 | int i, ngroups; |
| 5427 | PyObject *list; |
| 5428 | #ifdef __APPLE__ |
| 5429 | int *groups, basegid; |
| 5430 | #else |
| 5431 | gid_t *groups, basegid; |
| 5432 | #endif |
| 5433 | ngroups = MAX_GROUPS; |
| 5434 | |
| 5435 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5436 | return NULL; |
| 5437 | |
| 5438 | #ifdef __APPLE__ |
| 5439 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5440 | #else |
| 5441 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5442 | #endif |
| 5443 | if (groups == NULL) |
| 5444 | return PyErr_NoMemory(); |
| 5445 | |
| 5446 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5447 | PyMem_Del(groups); |
| 5448 | return posix_error(); |
| 5449 | } |
| 5450 | |
| 5451 | list = PyList_New(ngroups); |
| 5452 | if (list == NULL) { |
| 5453 | PyMem_Del(groups); |
| 5454 | return NULL; |
| 5455 | } |
| 5456 | |
| 5457 | for (i = 0; i < ngroups; i++) { |
| 5458 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5459 | if (o == NULL) { |
| 5460 | Py_DECREF(list); |
| 5461 | PyMem_Del(groups); |
| 5462 | return NULL; |
| 5463 | } |
| 5464 | PyList_SET_ITEM(list, i, o); |
| 5465 | } |
| 5466 | |
| 5467 | PyMem_Del(groups); |
| 5468 | |
| 5469 | return list; |
| 5470 | } |
| 5471 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5472 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5473 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5474 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5475 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5476 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5477 | |
| 5478 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5479 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5480 | { |
| 5481 | PyObject *result = NULL; |
| 5482 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5483 | #ifdef NGROUPS_MAX |
| 5484 | #define MAX_GROUPS NGROUPS_MAX |
| 5485 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5486 | /* defined to be 16 on Solaris7, so this should be a small number */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5487 | #define MAX_GROUPS 64 |
| 5488 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5489 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5490 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5491 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5492 | * This is a helper variable to store the intermediate result when |
| 5493 | * that happens. |
| 5494 | * |
| 5495 | * To keep the code readable the OSX behaviour is unconditional, |
| 5496 | * according to the POSIX spec this should be safe on all unix-y |
| 5497 | * systems. |
| 5498 | */ |
| 5499 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5500 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5502 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5503 | if (n < 0) { |
| 5504 | if (errno == EINVAL) { |
| 5505 | n = getgroups(0, NULL); |
| 5506 | if (n == -1) { |
| 5507 | return posix_error(); |
| 5508 | } |
| 5509 | if (n == 0) { |
| 5510 | /* Avoid malloc(0) */ |
| 5511 | alt_grouplist = grouplist; |
| 5512 | } else { |
| 5513 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5514 | if (alt_grouplist == NULL) { |
| 5515 | errno = EINVAL; |
| 5516 | return posix_error(); |
| 5517 | } |
| 5518 | n = getgroups(n, alt_grouplist); |
| 5519 | if (n == -1) { |
| 5520 | PyMem_Free(alt_grouplist); |
| 5521 | return posix_error(); |
| 5522 | } |
| 5523 | } |
| 5524 | } else { |
| 5525 | return posix_error(); |
| 5526 | } |
| 5527 | } |
| 5528 | result = PyList_New(n); |
| 5529 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5530 | int i; |
| 5531 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5532 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5533 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5534 | Py_DECREF(result); |
| 5535 | result = NULL; |
| 5536 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5537 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5538 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5539 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | if (alt_grouplist != grouplist) { |
| 5543 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5544 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5545 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5546 | return result; |
| 5547 | } |
| 5548 | #endif |
| 5549 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5550 | #ifdef HAVE_INITGROUPS |
| 5551 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5552 | "initgroups(username, gid) -> None\n\n\ |
| 5553 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5554 | the groups of which the specified username is a member, plus the specified\n\ |
| 5555 | group id."); |
| 5556 | |
| 5557 | static PyObject * |
| 5558 | posix_initgroups(PyObject *self, PyObject *args) |
| 5559 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5560 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5561 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5562 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5563 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5564 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5565 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5566 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5567 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5568 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5569 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5570 | res = initgroups(username, (gid_t) gid); |
| 5571 | Py_DECREF(oname); |
| 5572 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5573 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5574 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5575 | Py_INCREF(Py_None); |
| 5576 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5577 | } |
| 5578 | #endif |
| 5579 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5580 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5581 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5582 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5583 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5584 | |
| 5585 | static PyObject * |
| 5586 | posix_getpgid(PyObject *self, PyObject *args) |
| 5587 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5588 | pid_t pid, pgid; |
| 5589 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5590 | return NULL; |
| 5591 | pgid = getpgid(pid); |
| 5592 | if (pgid < 0) |
| 5593 | return posix_error(); |
| 5594 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5595 | } |
| 5596 | #endif /* HAVE_GETPGID */ |
| 5597 | |
| 5598 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5599 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5600 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5601 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5602 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5603 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5604 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5605 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5606 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5607 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5608 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5609 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5610 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5611 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5612 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5613 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5615 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5616 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5617 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5618 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5619 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5620 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5621 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5622 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5623 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5624 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5625 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5626 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5627 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5628 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5629 | return posix_error(); |
| 5630 | Py_INCREF(Py_None); |
| 5631 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5634 | #endif /* HAVE_SETPGRP */ |
| 5635 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5636 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5637 | |
| 5638 | #ifdef MS_WINDOWS |
| 5639 | #include <tlhelp32.h> |
| 5640 | |
| 5641 | static PyObject* |
| 5642 | win32_getppid() |
| 5643 | { |
| 5644 | HANDLE snapshot; |
| 5645 | pid_t mypid; |
| 5646 | PyObject* result = NULL; |
| 5647 | BOOL have_record; |
| 5648 | PROCESSENTRY32 pe; |
| 5649 | |
| 5650 | mypid = getpid(); /* This function never fails */ |
| 5651 | |
| 5652 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5653 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5654 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5655 | |
| 5656 | pe.dwSize = sizeof(pe); |
| 5657 | have_record = Process32First(snapshot, &pe); |
| 5658 | while (have_record) { |
| 5659 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5660 | /* We could cache the ulong value in a static variable. */ |
| 5661 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5662 | break; |
| 5663 | } |
| 5664 | |
| 5665 | have_record = Process32Next(snapshot, &pe); |
| 5666 | } |
| 5667 | |
| 5668 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5669 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5670 | * error anyway, so let's raise it. */ |
| 5671 | if (!result) |
| 5672 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5673 | |
| 5674 | CloseHandle(snapshot); |
| 5675 | |
| 5676 | return result; |
| 5677 | } |
| 5678 | #endif /*MS_WINDOWS*/ |
| 5679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5680 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5681 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5682 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5683 | Windows machines will still return its id; others systems will return the id\n\ |
| 5684 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5685 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5686 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5687 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5688 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5689 | #ifdef MS_WINDOWS |
| 5690 | return win32_getppid(); |
| 5691 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5692 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5693 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5694 | } |
| 5695 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5696 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5697 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5698 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5699 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5700 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5701 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5702 | |
| 5703 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5704 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5706 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5707 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5708 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5709 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5710 | |
| 5711 | if (GetUserNameW(user_name, &num_chars)) { |
| 5712 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5713 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5714 | } |
| 5715 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5716 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5717 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5718 | char *name; |
| 5719 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5721 | errno = 0; |
| 5722 | name = getlogin(); |
| 5723 | if (name == NULL) { |
| 5724 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5725 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5726 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5727 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5728 | } |
| 5729 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5730 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5731 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5732 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5733 | return result; |
| 5734 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5735 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5736 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5737 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5738 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5739 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5740 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5741 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5742 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5743 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5744 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5745 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5746 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5747 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5748 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5749 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5750 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5751 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5752 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5753 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5754 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5755 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5756 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5757 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5758 | pid_t pid; |
| 5759 | int sig; |
| 5760 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5761 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5762 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5763 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5764 | APIRET rc; |
| 5765 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5766 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5767 | |
| 5768 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5769 | APIRET rc; |
| 5770 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5771 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5772 | |
| 5773 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5774 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5775 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5776 | if (kill(pid, sig) == -1) |
| 5777 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5779 | Py_INCREF(Py_None); |
| 5780 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5781 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5782 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5783 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5784 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5785 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5786 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5787 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5788 | |
| 5789 | static PyObject * |
| 5790 | posix_killpg(PyObject *self, PyObject *args) |
| 5791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5792 | int sig; |
| 5793 | pid_t pgid; |
| 5794 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5795 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5796 | take the same type. Moreover, pid_t is always at least as wide as |
| 5797 | int (else compilation of this module fails), which is safe. */ |
| 5798 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5799 | return NULL; |
| 5800 | if (killpg(pgid, sig) == -1) |
| 5801 | return posix_error(); |
| 5802 | Py_INCREF(Py_None); |
| 5803 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5804 | } |
| 5805 | #endif |
| 5806 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5807 | #ifdef MS_WINDOWS |
| 5808 | PyDoc_STRVAR(win32_kill__doc__, |
| 5809 | "kill(pid, sig)\n\n\ |
| 5810 | Kill a process with a signal."); |
| 5811 | |
| 5812 | static PyObject * |
| 5813 | win32_kill(PyObject *self, PyObject *args) |
| 5814 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5815 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5816 | DWORD pid, sig, err; |
| 5817 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5818 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5819 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5820 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5821 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5822 | /* Console processes which share a common console can be sent CTRL+C or |
| 5823 | CTRL+BREAK events, provided they handle said events. */ |
| 5824 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5825 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5826 | err = GetLastError(); |
| 5827 | PyErr_SetFromWindowsErr(err); |
| 5828 | } |
| 5829 | else |
| 5830 | Py_RETURN_NONE; |
| 5831 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5833 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5834 | attempt to open and terminate the process. */ |
| 5835 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5836 | if (handle == NULL) { |
| 5837 | err = GetLastError(); |
| 5838 | return PyErr_SetFromWindowsErr(err); |
| 5839 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5841 | if (TerminateProcess(handle, sig) == 0) { |
| 5842 | err = GetLastError(); |
| 5843 | result = PyErr_SetFromWindowsErr(err); |
| 5844 | } else { |
| 5845 | Py_INCREF(Py_None); |
| 5846 | result = Py_None; |
| 5847 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5849 | CloseHandle(handle); |
| 5850 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5851 | } |
| 5852 | #endif /* MS_WINDOWS */ |
| 5853 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5854 | #ifdef HAVE_PLOCK |
| 5855 | |
| 5856 | #ifdef HAVE_SYS_LOCK_H |
| 5857 | #include <sys/lock.h> |
| 5858 | #endif |
| 5859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5860 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5861 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5862 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5865 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5867 | int op; |
| 5868 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5869 | return NULL; |
| 5870 | if (plock(op) == -1) |
| 5871 | return posix_error(); |
| 5872 | Py_INCREF(Py_None); |
| 5873 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5874 | } |
| 5875 | #endif |
| 5876 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5877 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5878 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5879 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5880 | Set the current process's user id."); |
| 5881 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5882 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5883 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5884 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | long uid_arg; |
| 5886 | uid_t uid; |
| 5887 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5888 | return NULL; |
| 5889 | uid = uid_arg; |
| 5890 | if (uid != uid_arg) { |
| 5891 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5892 | return NULL; |
| 5893 | } |
| 5894 | if (setuid(uid) < 0) |
| 5895 | return posix_error(); |
| 5896 | Py_INCREF(Py_None); |
| 5897 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5898 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5899 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5900 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5901 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5902 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5903 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5904 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5905 | Set the current process's effective user id."); |
| 5906 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5907 | static PyObject * |
| 5908 | posix_seteuid (PyObject *self, PyObject *args) |
| 5909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5910 | long euid_arg; |
| 5911 | uid_t euid; |
| 5912 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5913 | return NULL; |
| 5914 | euid = euid_arg; |
| 5915 | if (euid != euid_arg) { |
| 5916 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5917 | return NULL; |
| 5918 | } |
| 5919 | if (seteuid(euid) < 0) { |
| 5920 | return posix_error(); |
| 5921 | } else { |
| 5922 | Py_INCREF(Py_None); |
| 5923 | return Py_None; |
| 5924 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5925 | } |
| 5926 | #endif /* HAVE_SETEUID */ |
| 5927 | |
| 5928 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5929 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5930 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5931 | Set the current process's effective group id."); |
| 5932 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5933 | static PyObject * |
| 5934 | posix_setegid (PyObject *self, PyObject *args) |
| 5935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | long egid_arg; |
| 5937 | gid_t egid; |
| 5938 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5939 | return NULL; |
| 5940 | egid = egid_arg; |
| 5941 | if (egid != egid_arg) { |
| 5942 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | if (setegid(egid) < 0) { |
| 5946 | return posix_error(); |
| 5947 | } else { |
| 5948 | Py_INCREF(Py_None); |
| 5949 | return Py_None; |
| 5950 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5951 | } |
| 5952 | #endif /* HAVE_SETEGID */ |
| 5953 | |
| 5954 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5955 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5956 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5957 | Set the current process's real and effective user ids."); |
| 5958 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5959 | static PyObject * |
| 5960 | posix_setreuid (PyObject *self, PyObject *args) |
| 5961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5962 | long ruid_arg, euid_arg; |
| 5963 | uid_t ruid, euid; |
| 5964 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5965 | return NULL; |
| 5966 | if (ruid_arg == -1) |
| 5967 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5968 | else |
| 5969 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5970 | if (euid_arg == -1) |
| 5971 | euid = (uid_t)-1; |
| 5972 | else |
| 5973 | euid = euid_arg; |
| 5974 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5975 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5976 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | if (setreuid(ruid, euid) < 0) { |
| 5980 | return posix_error(); |
| 5981 | } else { |
| 5982 | Py_INCREF(Py_None); |
| 5983 | return Py_None; |
| 5984 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5985 | } |
| 5986 | #endif /* HAVE_SETREUID */ |
| 5987 | |
| 5988 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5989 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5990 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5991 | Set the current process's real and effective group ids."); |
| 5992 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5993 | static PyObject * |
| 5994 | posix_setregid (PyObject *self, PyObject *args) |
| 5995 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5996 | long rgid_arg, egid_arg; |
| 5997 | gid_t rgid, egid; |
| 5998 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5999 | return NULL; |
| 6000 | if (rgid_arg == -1) |
| 6001 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6002 | else |
| 6003 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6004 | if (egid_arg == -1) |
| 6005 | egid = (gid_t)-1; |
| 6006 | else |
| 6007 | egid = egid_arg; |
| 6008 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6009 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6010 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6011 | return NULL; |
| 6012 | } |
| 6013 | if (setregid(rgid, egid) < 0) { |
| 6014 | return posix_error(); |
| 6015 | } else { |
| 6016 | Py_INCREF(Py_None); |
| 6017 | return Py_None; |
| 6018 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6019 | } |
| 6020 | #endif /* HAVE_SETREGID */ |
| 6021 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6022 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6023 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6024 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6025 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6026 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6027 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6028 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6029 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6030 | long gid_arg; |
| 6031 | gid_t gid; |
| 6032 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6033 | return NULL; |
| 6034 | gid = gid_arg; |
| 6035 | if (gid != gid_arg) { |
| 6036 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6037 | return NULL; |
| 6038 | } |
| 6039 | if (setgid(gid) < 0) |
| 6040 | return posix_error(); |
| 6041 | Py_INCREF(Py_None); |
| 6042 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6043 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6044 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6045 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6046 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6047 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6048 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6049 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6050 | |
| 6051 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6052 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6053 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6054 | int i, len; |
| 6055 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6056 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6057 | if (!PySequence_Check(groups)) { |
| 6058 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6059 | return NULL; |
| 6060 | } |
| 6061 | len = PySequence_Size(groups); |
| 6062 | if (len > MAX_GROUPS) { |
| 6063 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6064 | return NULL; |
| 6065 | } |
| 6066 | for(i = 0; i < len; i++) { |
| 6067 | PyObject *elem; |
| 6068 | elem = PySequence_GetItem(groups, i); |
| 6069 | if (!elem) |
| 6070 | return NULL; |
| 6071 | if (!PyLong_Check(elem)) { |
| 6072 | PyErr_SetString(PyExc_TypeError, |
| 6073 | "groups must be integers"); |
| 6074 | Py_DECREF(elem); |
| 6075 | return NULL; |
| 6076 | } else { |
| 6077 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6078 | if (PyErr_Occurred()) { |
| 6079 | PyErr_SetString(PyExc_TypeError, |
| 6080 | "group id too big"); |
| 6081 | Py_DECREF(elem); |
| 6082 | return NULL; |
| 6083 | } |
| 6084 | grouplist[i] = x; |
| 6085 | /* read back the value to see if it fitted in gid_t */ |
| 6086 | if (grouplist[i] != x) { |
| 6087 | PyErr_SetString(PyExc_TypeError, |
| 6088 | "group id too big"); |
| 6089 | Py_DECREF(elem); |
| 6090 | return NULL; |
| 6091 | } |
| 6092 | } |
| 6093 | Py_DECREF(elem); |
| 6094 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6095 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6096 | if (setgroups(len, grouplist) < 0) |
| 6097 | return posix_error(); |
| 6098 | Py_INCREF(Py_None); |
| 6099 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6100 | } |
| 6101 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6102 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6103 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6104 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6105 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6106 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6107 | PyObject *result; |
| 6108 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6109 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6111 | if (pid == -1) |
| 6112 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6114 | if (struct_rusage == NULL) { |
| 6115 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6116 | if (m == NULL) |
| 6117 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6118 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6119 | Py_DECREF(m); |
| 6120 | if (struct_rusage == NULL) |
| 6121 | return NULL; |
| 6122 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6123 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6124 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6125 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6126 | if (!result) |
| 6127 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6128 | |
| 6129 | #ifndef doubletime |
| 6130 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6131 | #endif |
| 6132 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6133 | PyStructSequence_SET_ITEM(result, 0, |
| 6134 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6135 | PyStructSequence_SET_ITEM(result, 1, |
| 6136 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6137 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6138 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6139 | SET_INT(result, 2, ru->ru_maxrss); |
| 6140 | SET_INT(result, 3, ru->ru_ixrss); |
| 6141 | SET_INT(result, 4, ru->ru_idrss); |
| 6142 | SET_INT(result, 5, ru->ru_isrss); |
| 6143 | SET_INT(result, 6, ru->ru_minflt); |
| 6144 | SET_INT(result, 7, ru->ru_majflt); |
| 6145 | SET_INT(result, 8, ru->ru_nswap); |
| 6146 | SET_INT(result, 9, ru->ru_inblock); |
| 6147 | SET_INT(result, 10, ru->ru_oublock); |
| 6148 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6149 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6150 | SET_INT(result, 13, ru->ru_nsignals); |
| 6151 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6152 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6153 | #undef SET_INT |
| 6154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | if (PyErr_Occurred()) { |
| 6156 | Py_DECREF(result); |
| 6157 | return NULL; |
| 6158 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6159 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6160 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6161 | } |
| 6162 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6163 | |
| 6164 | #ifdef HAVE_WAIT3 |
| 6165 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6166 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6167 | Wait for completion of a child process."); |
| 6168 | |
| 6169 | static PyObject * |
| 6170 | posix_wait3(PyObject *self, PyObject *args) |
| 6171 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6172 | pid_t pid; |
| 6173 | int options; |
| 6174 | struct rusage ru; |
| 6175 | WAIT_TYPE status; |
| 6176 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6177 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6178 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6179 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6180 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6181 | Py_BEGIN_ALLOW_THREADS |
| 6182 | pid = wait3(&status, options, &ru); |
| 6183 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6184 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6185 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6186 | } |
| 6187 | #endif /* HAVE_WAIT3 */ |
| 6188 | |
| 6189 | #ifdef HAVE_WAIT4 |
| 6190 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6191 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6192 | Wait for completion of a given child process."); |
| 6193 | |
| 6194 | static PyObject * |
| 6195 | posix_wait4(PyObject *self, PyObject *args) |
| 6196 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6197 | pid_t pid; |
| 6198 | int options; |
| 6199 | struct rusage ru; |
| 6200 | WAIT_TYPE status; |
| 6201 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6202 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6203 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6204 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6205 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6206 | Py_BEGIN_ALLOW_THREADS |
| 6207 | pid = wait4(pid, &status, options, &ru); |
| 6208 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6210 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6211 | } |
| 6212 | #endif /* HAVE_WAIT4 */ |
| 6213 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6214 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6215 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6216 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6217 | Wait for the completion of one or more child processes.\n\n\ |
| 6218 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6219 | id specifies the pid to wait on.\n\ |
| 6220 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6221 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6222 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6223 | no children in a waitable state."); |
| 6224 | |
| 6225 | static PyObject * |
| 6226 | posix_waitid(PyObject *self, PyObject *args) |
| 6227 | { |
| 6228 | PyObject *result; |
| 6229 | idtype_t idtype; |
| 6230 | id_t id; |
| 6231 | int options, res; |
| 6232 | siginfo_t si; |
| 6233 | si.si_pid = 0; |
| 6234 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6235 | return NULL; |
| 6236 | Py_BEGIN_ALLOW_THREADS |
| 6237 | res = waitid(idtype, id, &si, options); |
| 6238 | Py_END_ALLOW_THREADS |
| 6239 | if (res == -1) |
| 6240 | return posix_error(); |
| 6241 | |
| 6242 | if (si.si_pid == 0) |
| 6243 | Py_RETURN_NONE; |
| 6244 | |
| 6245 | result = PyStructSequence_New(&WaitidResultType); |
| 6246 | if (!result) |
| 6247 | return NULL; |
| 6248 | |
| 6249 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6250 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6251 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6252 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6253 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6254 | if (PyErr_Occurred()) { |
| 6255 | Py_DECREF(result); |
| 6256 | return NULL; |
| 6257 | } |
| 6258 | |
| 6259 | return result; |
| 6260 | } |
| 6261 | #endif |
| 6262 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6263 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6264 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6265 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6266 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6267 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6268 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6269 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6270 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | pid_t pid; |
| 6272 | int options; |
| 6273 | WAIT_TYPE status; |
| 6274 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6276 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6277 | return NULL; |
| 6278 | Py_BEGIN_ALLOW_THREADS |
| 6279 | pid = waitpid(pid, &status, options); |
| 6280 | Py_END_ALLOW_THREADS |
| 6281 | if (pid == -1) |
| 6282 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6283 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6284 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6287 | #elif defined(HAVE_CWAIT) |
| 6288 | |
| 6289 | /* 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] | 6290 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6291 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6292 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6293 | |
| 6294 | static PyObject * |
| 6295 | posix_waitpid(PyObject *self, PyObject *args) |
| 6296 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6297 | Py_intptr_t pid; |
| 6298 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6301 | return NULL; |
| 6302 | Py_BEGIN_ALLOW_THREADS |
| 6303 | pid = _cwait(&status, pid, options); |
| 6304 | Py_END_ALLOW_THREADS |
| 6305 | if (pid == -1) |
| 6306 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6307 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6308 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6309 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6310 | } |
| 6311 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6312 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6313 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6314 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6315 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6316 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6317 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6318 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6319 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6320 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6321 | pid_t pid; |
| 6322 | WAIT_TYPE status; |
| 6323 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6324 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6325 | Py_BEGIN_ALLOW_THREADS |
| 6326 | pid = wait(&status); |
| 6327 | Py_END_ALLOW_THREADS |
| 6328 | if (pid == -1) |
| 6329 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6331 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6332 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6333 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6334 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6336 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6337 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6338 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6339 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6340 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6341 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6342 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6343 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6344 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6345 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6346 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6347 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6348 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6349 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6350 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6351 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6352 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6355 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6356 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6357 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6358 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6359 | 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] | 6360 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6361 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6362 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6364 | PyObject* v; |
| 6365 | char buf[MAXPATHLEN]; |
| 6366 | PyObject *opath; |
| 6367 | char *path; |
| 6368 | int n; |
| 6369 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6370 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6371 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6372 | PyUnicode_FSConverter, &opath)) |
| 6373 | return NULL; |
| 6374 | path = PyBytes_AsString(opath); |
| 6375 | v = PySequence_GetItem(args, 0); |
| 6376 | if (v == NULL) { |
| 6377 | Py_DECREF(opath); |
| 6378 | return NULL; |
| 6379 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6381 | if (PyUnicode_Check(v)) { |
| 6382 | arg_is_unicode = 1; |
| 6383 | } |
| 6384 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6386 | Py_BEGIN_ALLOW_THREADS |
| 6387 | n = readlink(path, buf, (int) sizeof buf); |
| 6388 | Py_END_ALLOW_THREADS |
| 6389 | if (n < 0) |
| 6390 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6391 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6392 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6393 | if (arg_is_unicode) |
| 6394 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6395 | else |
| 6396 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6397 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6398 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6399 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6400 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6401 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6402 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6403 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6404 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6405 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6406 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6407 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6409 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6410 | } |
| 6411 | #endif /* HAVE_SYMLINK */ |
| 6412 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6413 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6414 | |
| 6415 | PyDoc_STRVAR(win_readlink__doc__, |
| 6416 | "readlink(path) -> path\n\n\ |
| 6417 | Return a string representing the path to which the symbolic link points."); |
| 6418 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6419 | /* Windows readlink implementation */ |
| 6420 | static PyObject * |
| 6421 | win_readlink(PyObject *self, PyObject *args) |
| 6422 | { |
| 6423 | wchar_t *path; |
| 6424 | DWORD n_bytes_returned; |
| 6425 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6426 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6427 | HANDLE reparse_point_handle; |
| 6428 | |
| 6429 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6430 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6431 | wchar_t *print_name; |
| 6432 | |
| 6433 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6434 | "U:readlink", |
| 6435 | &po)) |
| 6436 | return NULL; |
| 6437 | path = PyUnicode_AsUnicode(po); |
| 6438 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6439 | return NULL; |
| 6440 | |
| 6441 | /* First get a handle to the reparse point */ |
| 6442 | Py_BEGIN_ALLOW_THREADS |
| 6443 | reparse_point_handle = CreateFileW( |
| 6444 | path, |
| 6445 | 0, |
| 6446 | 0, |
| 6447 | 0, |
| 6448 | OPEN_EXISTING, |
| 6449 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6450 | 0); |
| 6451 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6452 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6453 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6454 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6455 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6456 | Py_BEGIN_ALLOW_THREADS |
| 6457 | /* New call DeviceIoControl to read the reparse point */ |
| 6458 | io_result = DeviceIoControl( |
| 6459 | reparse_point_handle, |
| 6460 | FSCTL_GET_REPARSE_POINT, |
| 6461 | 0, 0, /* in buffer */ |
| 6462 | target_buffer, sizeof(target_buffer), |
| 6463 | &n_bytes_returned, |
| 6464 | 0 /* we're not using OVERLAPPED_IO */ |
| 6465 | ); |
| 6466 | CloseHandle(reparse_point_handle); |
| 6467 | Py_END_ALLOW_THREADS |
| 6468 | |
| 6469 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6470 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6471 | |
| 6472 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6473 | { |
| 6474 | PyErr_SetString(PyExc_ValueError, |
| 6475 | "not a symbolic link"); |
| 6476 | return NULL; |
| 6477 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6478 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6479 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6480 | |
| 6481 | result = PyUnicode_FromWideChar(print_name, |
| 6482 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6483 | return result; |
| 6484 | } |
| 6485 | |
| 6486 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6487 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6488 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6489 | |
| 6490 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6491 | static int has_CreateSymbolicLinkW = 0; |
| 6492 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6493 | static int |
| 6494 | check_CreateSymbolicLinkW() |
| 6495 | { |
| 6496 | HINSTANCE hKernel32; |
| 6497 | /* only recheck */ |
| 6498 | if (has_CreateSymbolicLinkW) |
| 6499 | return has_CreateSymbolicLinkW; |
| 6500 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6501 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6502 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6503 | if (Py_CreateSymbolicLinkW) |
| 6504 | has_CreateSymbolicLinkW = 1; |
| 6505 | return has_CreateSymbolicLinkW; |
| 6506 | } |
| 6507 | |
| 6508 | PyDoc_STRVAR(win_symlink__doc__, |
| 6509 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6510 | Create a symbolic link pointing to src named dst.\n\ |
| 6511 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6512 | a directory.\n\ |
| 6513 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6514 | NotImplementedError otherwise."); |
| 6515 | |
| 6516 | static PyObject * |
| 6517 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6518 | { |
| 6519 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 6520 | PyObject *src, *dest; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6521 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6522 | int target_is_directory = 0; |
| 6523 | DWORD res; |
| 6524 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6525 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6526 | if (!check_CreateSymbolicLinkW()) |
| 6527 | { |
| 6528 | /* raise NotImplementedError */ |
| 6529 | return PyErr_Format(PyExc_NotImplementedError, |
| 6530 | "CreateSymbolicLinkW not found"); |
| 6531 | } |
| 6532 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 6533 | kwlist, &src, &dest, &target_is_directory)) |
| 6534 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6535 | |
| 6536 | if (win32_can_symlink == 0) |
| 6537 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6538 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6539 | if (!convert_to_unicode(&src)) |
| 6540 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6541 | if (!convert_to_unicode(&dest)) { |
| 6542 | Py_DECREF(src); |
| 6543 | return NULL; |
| 6544 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6545 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6546 | wsrc = PyUnicode_AsUnicode(src); |
| 6547 | if (wsrc == NULL) |
| 6548 | goto error; |
| 6549 | wdest = PyUnicode_AsUnicode(dest); |
| 6550 | if (wsrc == NULL) |
| 6551 | goto error; |
| 6552 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6553 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6554 | if( |
| 6555 | GetFileAttributesExW( |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6556 | wsrc, GetFileExInfoStandard, &src_info |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6557 | )) |
| 6558 | { |
| 6559 | target_is_directory = target_is_directory || |
| 6560 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6561 | } |
| 6562 | |
| 6563 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6564 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6565 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6566 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6567 | Py_DECREF(src); |
| 6568 | Py_DECREF(dest); |
| 6569 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6570 | return win32_error_object("symlink", src); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6571 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6572 | Py_INCREF(Py_None); |
| 6573 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6574 | |
| 6575 | error: |
| 6576 | Py_DECREF(src); |
| 6577 | Py_DECREF(dest); |
| 6578 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6579 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6580 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6581 | |
| 6582 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6583 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6584 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6585 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6586 | { |
| 6587 | ULONG value = 0; |
| 6588 | |
| 6589 | Py_BEGIN_ALLOW_THREADS |
| 6590 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6591 | Py_END_ALLOW_THREADS |
| 6592 | |
| 6593 | return value; |
| 6594 | } |
| 6595 | |
| 6596 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6597 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6598 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6599 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | return Py_BuildValue("ddddd", |
| 6601 | (double)0 /* t.tms_utime / HZ */, |
| 6602 | (double)0 /* t.tms_stime / HZ */, |
| 6603 | (double)0 /* t.tms_cutime / HZ */, |
| 6604 | (double)0 /* t.tms_cstime / HZ */, |
| 6605 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6606 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6607 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6608 | #define NEED_TICKS_PER_SECOND |
| 6609 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6610 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6611 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6612 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | struct tms t; |
| 6614 | clock_t c; |
| 6615 | errno = 0; |
| 6616 | c = times(&t); |
| 6617 | if (c == (clock_t) -1) |
| 6618 | return posix_error(); |
| 6619 | return Py_BuildValue("ddddd", |
| 6620 | (double)t.tms_utime / ticks_per_second, |
| 6621 | (double)t.tms_stime / ticks_per_second, |
| 6622 | (double)t.tms_cutime / ticks_per_second, |
| 6623 | (double)t.tms_cstime / ticks_per_second, |
| 6624 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6625 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6626 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6627 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6628 | |
| 6629 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6630 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6632 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6633 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6634 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6635 | FILETIME create, exit, kernel, user; |
| 6636 | HANDLE hProc; |
| 6637 | hProc = GetCurrentProcess(); |
| 6638 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6639 | /* The fields of a FILETIME structure are the hi and lo part |
| 6640 | of a 64-bit value expressed in 100 nanosecond units. |
| 6641 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6642 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6643 | */ |
| 6644 | return Py_BuildValue( |
| 6645 | "ddddd", |
| 6646 | (double)(user.dwHighDateTime*429.4967296 + |
| 6647 | user.dwLowDateTime*1e-7), |
| 6648 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6649 | kernel.dwLowDateTime*1e-7), |
| 6650 | (double)0, |
| 6651 | (double)0, |
| 6652 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6653 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6654 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6655 | |
| 6656 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6657 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6658 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6659 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6660 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6661 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6662 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6663 | #ifdef HAVE_GETSID |
| 6664 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6665 | "getsid(pid) -> sid\n\n\ |
| 6666 | Call the system call getsid()."); |
| 6667 | |
| 6668 | static PyObject * |
| 6669 | posix_getsid(PyObject *self, PyObject *args) |
| 6670 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6671 | pid_t pid; |
| 6672 | int sid; |
| 6673 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6674 | return NULL; |
| 6675 | sid = getsid(pid); |
| 6676 | if (sid < 0) |
| 6677 | return posix_error(); |
| 6678 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6679 | } |
| 6680 | #endif /* HAVE_GETSID */ |
| 6681 | |
| 6682 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6683 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6684 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6685 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6686 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6687 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6688 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6689 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | if (setsid() < 0) |
| 6692 | return posix_error(); |
| 6693 | Py_INCREF(Py_None); |
| 6694 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6695 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6696 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6697 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6698 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6699 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6700 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6701 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6702 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6703 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6704 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6706 | pid_t pid; |
| 6707 | int pgrp; |
| 6708 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6709 | return NULL; |
| 6710 | if (setpgid(pid, pgrp) < 0) |
| 6711 | return posix_error(); |
| 6712 | Py_INCREF(Py_None); |
| 6713 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6714 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6715 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6716 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6717 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6718 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6719 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6720 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6721 | 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] | 6722 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6723 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6724 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6725 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6726 | int fd; |
| 6727 | pid_t pgid; |
| 6728 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6729 | return NULL; |
| 6730 | pgid = tcgetpgrp(fd); |
| 6731 | if (pgid < 0) |
| 6732 | return posix_error(); |
| 6733 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6734 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6735 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6736 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6737 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6738 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6739 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6740 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6741 | 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] | 6742 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6743 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6744 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6745 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6746 | int fd; |
| 6747 | pid_t pgid; |
| 6748 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6749 | return NULL; |
| 6750 | if (tcsetpgrp(fd, pgid) < 0) |
| 6751 | return posix_error(); |
| 6752 | Py_INCREF(Py_None); |
| 6753 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6754 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6755 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6756 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6757 | /* Functions acting on file descriptors */ |
| 6758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6759 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6760 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6761 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6762 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6763 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6764 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6765 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6766 | PyObject *ofile; |
| 6767 | char *file; |
| 6768 | int flag; |
| 6769 | int mode = 0777; |
| 6770 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6771 | |
| 6772 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6773 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6774 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6775 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 6776 | if (wpath == NULL) |
| 6777 | return NULL; |
| 6778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6780 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6781 | Py_END_ALLOW_THREADS |
| 6782 | if (fd < 0) |
| 6783 | return posix_error(); |
| 6784 | return PyLong_FromLong((long)fd); |
| 6785 | } |
| 6786 | /* Drop the argument parsing error as narrow strings |
| 6787 | are also valid. */ |
| 6788 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6789 | #endif |
| 6790 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6791 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6792 | PyUnicode_FSConverter, &ofile, |
| 6793 | &flag, &mode)) |
| 6794 | return NULL; |
| 6795 | file = PyBytes_AsString(ofile); |
| 6796 | Py_BEGIN_ALLOW_THREADS |
| 6797 | fd = open(file, flag, mode); |
| 6798 | Py_END_ALLOW_THREADS |
| 6799 | if (fd < 0) |
| 6800 | return posix_error_with_allocated_filename(ofile); |
| 6801 | Py_DECREF(ofile); |
| 6802 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6805 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6806 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6807 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6808 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6809 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6810 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6811 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6812 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | int fd, res; |
| 6814 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6815 | return NULL; |
| 6816 | if (!_PyVerify_fd(fd)) |
| 6817 | return posix_error(); |
| 6818 | Py_BEGIN_ALLOW_THREADS |
| 6819 | res = close(fd); |
| 6820 | Py_END_ALLOW_THREADS |
| 6821 | if (res < 0) |
| 6822 | return posix_error(); |
| 6823 | Py_INCREF(Py_None); |
| 6824 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6828 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6829 | "closerange(fd_low, fd_high)\n\n\ |
| 6830 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6831 | |
| 6832 | static PyObject * |
| 6833 | posix_closerange(PyObject *self, PyObject *args) |
| 6834 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6835 | int fd_from, fd_to, i; |
| 6836 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6837 | return NULL; |
| 6838 | Py_BEGIN_ALLOW_THREADS |
| 6839 | for (i = fd_from; i < fd_to; i++) |
| 6840 | if (_PyVerify_fd(i)) |
| 6841 | close(i); |
| 6842 | Py_END_ALLOW_THREADS |
| 6843 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6847 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6848 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6849 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6850 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6851 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6852 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6853 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6854 | int fd; |
| 6855 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6856 | return NULL; |
| 6857 | if (!_PyVerify_fd(fd)) |
| 6858 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6859 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6860 | if (fd < 0) |
| 6861 | return posix_error(); |
| 6862 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6866 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6867 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6868 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6869 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6870 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6871 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6873 | int fd, fd2, res; |
| 6874 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6875 | return NULL; |
| 6876 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6877 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6878 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6879 | if (res < 0) |
| 6880 | return posix_error(); |
| 6881 | Py_INCREF(Py_None); |
| 6882 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6883 | } |
| 6884 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6885 | #ifdef HAVE_LOCKF |
| 6886 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6887 | "lockf(fd, cmd, len)\n\n\ |
| 6888 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6889 | fd is an open file descriptor.\n\ |
| 6890 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6891 | F_TEST.\n\ |
| 6892 | len specifies the section of the file to lock."); |
| 6893 | |
| 6894 | static PyObject * |
| 6895 | posix_lockf(PyObject *self, PyObject *args) |
| 6896 | { |
| 6897 | int fd, cmd, res; |
| 6898 | off_t len; |
| 6899 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6900 | &fd, &cmd, _parse_off_t, &len)) |
| 6901 | return NULL; |
| 6902 | |
| 6903 | Py_BEGIN_ALLOW_THREADS |
| 6904 | res = lockf(fd, cmd, len); |
| 6905 | Py_END_ALLOW_THREADS |
| 6906 | |
| 6907 | if (res < 0) |
| 6908 | return posix_error(); |
| 6909 | |
| 6910 | Py_RETURN_NONE; |
| 6911 | } |
| 6912 | #endif |
| 6913 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6915 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6916 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6917 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6918 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6920 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6922 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6923 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6924 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6925 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6926 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6927 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6928 | PyObject *posobj; |
| 6929 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6930 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6931 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6932 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6933 | switch (how) { |
| 6934 | case 0: how = SEEK_SET; break; |
| 6935 | case 1: how = SEEK_CUR; break; |
| 6936 | case 2: how = SEEK_END; break; |
| 6937 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6938 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6939 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6940 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6941 | pos = PyLong_AsLong(posobj); |
| 6942 | #else |
| 6943 | pos = PyLong_AsLongLong(posobj); |
| 6944 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6945 | if (PyErr_Occurred()) |
| 6946 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6948 | if (!_PyVerify_fd(fd)) |
| 6949 | return posix_error(); |
| 6950 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6951 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6952 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6953 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6954 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6955 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6956 | Py_END_ALLOW_THREADS |
| 6957 | if (res < 0) |
| 6958 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6959 | |
| 6960 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6961 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6962 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6963 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6964 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6968 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6969 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6970 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6971 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6972 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6973 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6974 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6975 | int fd, size; |
| 6976 | Py_ssize_t n; |
| 6977 | PyObject *buffer; |
| 6978 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6979 | return NULL; |
| 6980 | if (size < 0) { |
| 6981 | errno = EINVAL; |
| 6982 | return posix_error(); |
| 6983 | } |
| 6984 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6985 | if (buffer == NULL) |
| 6986 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6987 | if (!_PyVerify_fd(fd)) { |
| 6988 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6990 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6991 | Py_BEGIN_ALLOW_THREADS |
| 6992 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6993 | Py_END_ALLOW_THREADS |
| 6994 | if (n < 0) { |
| 6995 | Py_DECREF(buffer); |
| 6996 | return posix_error(); |
| 6997 | } |
| 6998 | if (n != size) |
| 6999 | _PyBytes_Resize(&buffer, n); |
| 7000 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7003 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7004 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7005 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7006 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7007 | { |
| 7008 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7009 | Py_ssize_t blen, total = 0; |
| 7010 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7011 | *iov = PyMem_New(struct iovec, cnt); |
| 7012 | if (*iov == NULL) { |
| 7013 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7014 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7015 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7016 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7017 | *buf = PyMem_New(Py_buffer, cnt); |
| 7018 | if (*buf == NULL) { |
| 7019 | PyMem_Del(*iov); |
| 7020 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7021 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
| 7024 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7025 | PyObject *item = PySequence_GetItem(seq, i); |
| 7026 | if (item == NULL) |
| 7027 | goto fail; |
| 7028 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7029 | Py_DECREF(item); |
| 7030 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7031 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7032 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7033 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7034 | blen = (*buf)[i].len; |
| 7035 | (*iov)[i].iov_len = blen; |
| 7036 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7037 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7038 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7039 | |
| 7040 | fail: |
| 7041 | PyMem_Del(*iov); |
| 7042 | for (j = 0; j < i; j++) { |
| 7043 | PyBuffer_Release(&(*buf)[j]); |
| 7044 | } |
| 7045 | PyMem_Del(*buf); |
| 7046 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | static void |
| 7050 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7051 | { |
| 7052 | int i; |
| 7053 | PyMem_Del(iov); |
| 7054 | for (i = 0; i < cnt; i++) { |
| 7055 | PyBuffer_Release(&buf[i]); |
| 7056 | } |
| 7057 | PyMem_Del(buf); |
| 7058 | } |
| 7059 | #endif |
| 7060 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7061 | #ifdef HAVE_READV |
| 7062 | PyDoc_STRVAR(posix_readv__doc__, |
| 7063 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7064 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7065 | is an arbitrary sequence of writable buffers.\n\ |
| 7066 | Returns the total number of bytes read."); |
| 7067 | |
| 7068 | static PyObject * |
| 7069 | posix_readv(PyObject *self, PyObject *args) |
| 7070 | { |
| 7071 | int fd, cnt; |
| 7072 | Py_ssize_t n; |
| 7073 | PyObject *seq; |
| 7074 | struct iovec *iov; |
| 7075 | Py_buffer *buf; |
| 7076 | |
| 7077 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7078 | return NULL; |
| 7079 | if (!PySequence_Check(seq)) { |
| 7080 | PyErr_SetString(PyExc_TypeError, |
| 7081 | "readv() arg 2 must be a sequence"); |
| 7082 | return NULL; |
| 7083 | } |
| 7084 | cnt = PySequence_Size(seq); |
| 7085 | |
| 7086 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7087 | return NULL; |
| 7088 | |
| 7089 | Py_BEGIN_ALLOW_THREADS |
| 7090 | n = readv(fd, iov, cnt); |
| 7091 | Py_END_ALLOW_THREADS |
| 7092 | |
| 7093 | iov_cleanup(iov, buf, cnt); |
| 7094 | return PyLong_FromSsize_t(n); |
| 7095 | } |
| 7096 | #endif |
| 7097 | |
| 7098 | #ifdef HAVE_PREAD |
| 7099 | PyDoc_STRVAR(posix_pread__doc__, |
| 7100 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7101 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7102 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7103 | |
| 7104 | static PyObject * |
| 7105 | posix_pread(PyObject *self, PyObject *args) |
| 7106 | { |
| 7107 | int fd, size; |
| 7108 | off_t offset; |
| 7109 | Py_ssize_t n; |
| 7110 | PyObject *buffer; |
| 7111 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7112 | return NULL; |
| 7113 | |
| 7114 | if (size < 0) { |
| 7115 | errno = EINVAL; |
| 7116 | return posix_error(); |
| 7117 | } |
| 7118 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7119 | if (buffer == NULL) |
| 7120 | return NULL; |
| 7121 | if (!_PyVerify_fd(fd)) { |
| 7122 | Py_DECREF(buffer); |
| 7123 | return posix_error(); |
| 7124 | } |
| 7125 | Py_BEGIN_ALLOW_THREADS |
| 7126 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7127 | Py_END_ALLOW_THREADS |
| 7128 | if (n < 0) { |
| 7129 | Py_DECREF(buffer); |
| 7130 | return posix_error(); |
| 7131 | } |
| 7132 | if (n != size) |
| 7133 | _PyBytes_Resize(&buffer, n); |
| 7134 | return buffer; |
| 7135 | } |
| 7136 | #endif |
| 7137 | |
| 7138 | PyDoc_STRVAR(posix_write__doc__, |
| 7139 | "write(fd, string) -> byteswritten\n\n\ |
| 7140 | Write a string to a file descriptor."); |
| 7141 | |
| 7142 | static PyObject * |
| 7143 | posix_write(PyObject *self, PyObject *args) |
| 7144 | { |
| 7145 | Py_buffer pbuf; |
| 7146 | int fd; |
| 7147 | Py_ssize_t size, len; |
| 7148 | |
| 7149 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7150 | return NULL; |
| 7151 | if (!_PyVerify_fd(fd)) { |
| 7152 | PyBuffer_Release(&pbuf); |
| 7153 | return posix_error(); |
| 7154 | } |
| 7155 | len = pbuf.len; |
| 7156 | Py_BEGIN_ALLOW_THREADS |
| 7157 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7158 | if (len > INT_MAX) |
| 7159 | len = INT_MAX; |
| 7160 | size = write(fd, pbuf.buf, (int)len); |
| 7161 | #else |
| 7162 | size = write(fd, pbuf.buf, len); |
| 7163 | #endif |
| 7164 | Py_END_ALLOW_THREADS |
| 7165 | PyBuffer_Release(&pbuf); |
| 7166 | if (size < 0) |
| 7167 | return posix_error(); |
| 7168 | return PyLong_FromSsize_t(size); |
| 7169 | } |
| 7170 | |
| 7171 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7172 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7173 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7174 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7175 | -> byteswritten\n\ |
| 7176 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7177 | |
| 7178 | static PyObject * |
| 7179 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7180 | { |
| 7181 | int in, out; |
| 7182 | Py_ssize_t ret; |
| 7183 | off_t offset; |
| 7184 | |
| 7185 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7186 | #ifndef __APPLE__ |
| 7187 | Py_ssize_t len; |
| 7188 | #endif |
| 7189 | PyObject *headers = NULL, *trailers = NULL; |
| 7190 | Py_buffer *hbuf, *tbuf; |
| 7191 | off_t sbytes; |
| 7192 | struct sf_hdtr sf; |
| 7193 | int flags = 0; |
| 7194 | sf.headers = NULL; |
| 7195 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7196 | static char *keywords[] = {"out", "in", |
| 7197 | "offset", "count", |
| 7198 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7199 | |
| 7200 | #ifdef __APPLE__ |
| 7201 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7202 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7203 | #else |
| 7204 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7205 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7206 | #endif |
| 7207 | &headers, &trailers, &flags)) |
| 7208 | return NULL; |
| 7209 | if (headers != NULL) { |
| 7210 | if (!PySequence_Check(headers)) { |
| 7211 | PyErr_SetString(PyExc_TypeError, |
| 7212 | "sendfile() headers must be a sequence or None"); |
| 7213 | return NULL; |
| 7214 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7215 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7216 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7217 | if (sf.hdr_cnt > 0 && |
| 7218 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7219 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7220 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7221 | #ifdef __APPLE__ |
| 7222 | sbytes += i; |
| 7223 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7224 | } |
| 7225 | } |
| 7226 | if (trailers != NULL) { |
| 7227 | if (!PySequence_Check(trailers)) { |
| 7228 | PyErr_SetString(PyExc_TypeError, |
| 7229 | "sendfile() trailers must be a sequence or None"); |
| 7230 | return NULL; |
| 7231 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7232 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7233 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7234 | if (sf.trl_cnt > 0 && |
| 7235 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7236 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7237 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7238 | #ifdef __APPLE__ |
| 7239 | sbytes += i; |
| 7240 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7241 | } |
| 7242 | } |
| 7243 | |
| 7244 | Py_BEGIN_ALLOW_THREADS |
| 7245 | #ifdef __APPLE__ |
| 7246 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7247 | #else |
| 7248 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7249 | #endif |
| 7250 | Py_END_ALLOW_THREADS |
| 7251 | |
| 7252 | if (sf.headers != NULL) |
| 7253 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7254 | if (sf.trailers != NULL) |
| 7255 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7256 | |
| 7257 | if (ret < 0) { |
| 7258 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7259 | if (sbytes != 0) { |
| 7260 | // some data has been sent |
| 7261 | goto done; |
| 7262 | } |
| 7263 | else { |
| 7264 | // no data has been sent; upper application is supposed |
| 7265 | // to retry on EAGAIN or EBUSY |
| 7266 | return posix_error(); |
| 7267 | } |
| 7268 | } |
| 7269 | return posix_error(); |
| 7270 | } |
| 7271 | goto done; |
| 7272 | |
| 7273 | done: |
| 7274 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7275 | return Py_BuildValue("l", sbytes); |
| 7276 | #else |
| 7277 | return Py_BuildValue("L", sbytes); |
| 7278 | #endif |
| 7279 | |
| 7280 | #else |
| 7281 | Py_ssize_t count; |
| 7282 | PyObject *offobj; |
| 7283 | static char *keywords[] = {"out", "in", |
| 7284 | "offset", "count", NULL}; |
| 7285 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7286 | keywords, &out, &in, &offobj, &count)) |
| 7287 | return NULL; |
| 7288 | #ifdef linux |
| 7289 | if (offobj == Py_None) { |
| 7290 | Py_BEGIN_ALLOW_THREADS |
| 7291 | ret = sendfile(out, in, NULL, count); |
| 7292 | Py_END_ALLOW_THREADS |
| 7293 | if (ret < 0) |
| 7294 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7295 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7296 | } |
| 7297 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7298 | if (!_parse_off_t(offobj, &offset)) |
| 7299 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7300 | Py_BEGIN_ALLOW_THREADS |
| 7301 | ret = sendfile(out, in, &offset, count); |
| 7302 | Py_END_ALLOW_THREADS |
| 7303 | if (ret < 0) |
| 7304 | return posix_error(); |
| 7305 | return Py_BuildValue("n", ret); |
| 7306 | #endif |
| 7307 | } |
| 7308 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7310 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7311 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7312 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7314 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7315 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7317 | int fd; |
| 7318 | STRUCT_STAT st; |
| 7319 | int res; |
| 7320 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7321 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7322 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7323 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7324 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7325 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7326 | if (!_PyVerify_fd(fd)) |
| 7327 | return posix_error(); |
| 7328 | Py_BEGIN_ALLOW_THREADS |
| 7329 | res = FSTAT(fd, &st); |
| 7330 | Py_END_ALLOW_THREADS |
| 7331 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7332 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7333 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7334 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7335 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7336 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7337 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7338 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7339 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7342 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7343 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7344 | 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] | 7345 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7346 | |
| 7347 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7348 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7350 | int fd; |
| 7351 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7352 | return NULL; |
| 7353 | if (!_PyVerify_fd(fd)) |
| 7354 | return PyBool_FromLong(0); |
| 7355 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7356 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7357 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7358 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7359 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7360 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7361 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7362 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7363 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7364 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7365 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7366 | #if defined(PYOS_OS2) |
| 7367 | HFILE read, write; |
| 7368 | APIRET rc; |
| 7369 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7370 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7371 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7372 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7373 | |
| 7374 | return Py_BuildValue("(ii)", read, write); |
| 7375 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7376 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7377 | int fds[2]; |
| 7378 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7379 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7380 | if (res != 0) |
| 7381 | return posix_error(); |
| 7382 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7383 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7384 | HANDLE read, write; |
| 7385 | int read_fd, write_fd; |
| 7386 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7387 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7388 | if (!ok) |
| 7389 | return win32_error("CreatePipe", NULL); |
| 7390 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7391 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7392 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7393 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7394 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7395 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7396 | #endif /* HAVE_PIPE */ |
| 7397 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7398 | #ifdef HAVE_PIPE2 |
| 7399 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7400 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7401 | Create a pipe with flags set atomically.\n\ |
| 7402 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7403 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7404 | "); |
| 7405 | |
| 7406 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7407 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7408 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7409 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7410 | int fds[2]; |
| 7411 | int res; |
| 7412 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7413 | flags = PyLong_AsLong(arg); |
| 7414 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7415 | return NULL; |
| 7416 | |
| 7417 | res = pipe2(fds, flags); |
| 7418 | if (res != 0) |
| 7419 | return posix_error(); |
| 7420 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7421 | } |
| 7422 | #endif /* HAVE_PIPE2 */ |
| 7423 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7424 | #ifdef HAVE_WRITEV |
| 7425 | PyDoc_STRVAR(posix_writev__doc__, |
| 7426 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7427 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7428 | arbitrary sequence of buffers.\n\ |
| 7429 | Returns the total bytes written."); |
| 7430 | |
| 7431 | static PyObject * |
| 7432 | posix_writev(PyObject *self, PyObject *args) |
| 7433 | { |
| 7434 | int fd, cnt; |
| 7435 | Py_ssize_t res; |
| 7436 | PyObject *seq; |
| 7437 | struct iovec *iov; |
| 7438 | Py_buffer *buf; |
| 7439 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7440 | return NULL; |
| 7441 | if (!PySequence_Check(seq)) { |
| 7442 | PyErr_SetString(PyExc_TypeError, |
| 7443 | "writev() arg 2 must be a sequence"); |
| 7444 | return NULL; |
| 7445 | } |
| 7446 | cnt = PySequence_Size(seq); |
| 7447 | |
| 7448 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7449 | return NULL; |
| 7450 | } |
| 7451 | |
| 7452 | Py_BEGIN_ALLOW_THREADS |
| 7453 | res = writev(fd, iov, cnt); |
| 7454 | Py_END_ALLOW_THREADS |
| 7455 | |
| 7456 | iov_cleanup(iov, buf, cnt); |
| 7457 | return PyLong_FromSsize_t(res); |
| 7458 | } |
| 7459 | #endif |
| 7460 | |
| 7461 | #ifdef HAVE_PWRITE |
| 7462 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7463 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7464 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7465 | offset unchanged."); |
| 7466 | |
| 7467 | static PyObject * |
| 7468 | posix_pwrite(PyObject *self, PyObject *args) |
| 7469 | { |
| 7470 | Py_buffer pbuf; |
| 7471 | int fd; |
| 7472 | off_t offset; |
| 7473 | Py_ssize_t size; |
| 7474 | |
| 7475 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7476 | return NULL; |
| 7477 | |
| 7478 | if (!_PyVerify_fd(fd)) { |
| 7479 | PyBuffer_Release(&pbuf); |
| 7480 | return posix_error(); |
| 7481 | } |
| 7482 | Py_BEGIN_ALLOW_THREADS |
| 7483 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7484 | Py_END_ALLOW_THREADS |
| 7485 | PyBuffer_Release(&pbuf); |
| 7486 | if (size < 0) |
| 7487 | return posix_error(); |
| 7488 | return PyLong_FromSsize_t(size); |
| 7489 | } |
| 7490 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7491 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7492 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7493 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7494 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7495 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7496 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7497 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7498 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7499 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7500 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7501 | char *filename; |
| 7502 | int mode = 0666; |
| 7503 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7504 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7505 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7506 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7507 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7508 | Py_BEGIN_ALLOW_THREADS |
| 7509 | res = mkfifo(filename, mode); |
| 7510 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7511 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7512 | if (res < 0) |
| 7513 | return posix_error(); |
| 7514 | Py_INCREF(Py_None); |
| 7515 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7516 | } |
| 7517 | #endif |
| 7518 | |
| 7519 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7520 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7521 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7522 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7523 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7524 | named filename. mode specifies both the permissions to use and the\n\ |
| 7525 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7526 | 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] | 7527 | device defines the newly created device special file (probably using\n\ |
| 7528 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7529 | |
| 7530 | |
| 7531 | static PyObject * |
| 7532 | posix_mknod(PyObject *self, PyObject *args) |
| 7533 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7534 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7535 | char *filename; |
| 7536 | int mode = 0600; |
| 7537 | int device = 0; |
| 7538 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7539 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7540 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7542 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7543 | Py_BEGIN_ALLOW_THREADS |
| 7544 | res = mknod(filename, mode, device); |
| 7545 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7546 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7547 | if (res < 0) |
| 7548 | return posix_error(); |
| 7549 | Py_INCREF(Py_None); |
| 7550 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7551 | } |
| 7552 | #endif |
| 7553 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7554 | #ifdef HAVE_DEVICE_MACROS |
| 7555 | PyDoc_STRVAR(posix_major__doc__, |
| 7556 | "major(device) -> major number\n\ |
| 7557 | Extracts a device major number from a raw device number."); |
| 7558 | |
| 7559 | static PyObject * |
| 7560 | posix_major(PyObject *self, PyObject *args) |
| 7561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7562 | int device; |
| 7563 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7564 | return NULL; |
| 7565 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
| 7568 | PyDoc_STRVAR(posix_minor__doc__, |
| 7569 | "minor(device) -> minor number\n\ |
| 7570 | Extracts a device minor number from a raw device number."); |
| 7571 | |
| 7572 | static PyObject * |
| 7573 | posix_minor(PyObject *self, PyObject *args) |
| 7574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7575 | int device; |
| 7576 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7577 | return NULL; |
| 7578 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7579 | } |
| 7580 | |
| 7581 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7582 | "makedev(major, minor) -> device number\n\ |
| 7583 | Composes a raw device number from the major and minor device numbers."); |
| 7584 | |
| 7585 | static PyObject * |
| 7586 | posix_makedev(PyObject *self, PyObject *args) |
| 7587 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7588 | int major, minor; |
| 7589 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7590 | return NULL; |
| 7591 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7592 | } |
| 7593 | #endif /* device macros */ |
| 7594 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7595 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7596 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7597 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7598 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7599 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7601 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7602 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | int fd; |
| 7605 | off_t length; |
| 7606 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7607 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7608 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7609 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7610 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7611 | Py_BEGIN_ALLOW_THREADS |
| 7612 | res = ftruncate(fd, length); |
| 7613 | Py_END_ALLOW_THREADS |
| 7614 | if (res < 0) |
| 7615 | return posix_error(); |
| 7616 | Py_INCREF(Py_None); |
| 7617 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7618 | } |
| 7619 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7620 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7621 | #ifdef HAVE_TRUNCATE |
| 7622 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7623 | "truncate(path, length)\n\n\ |
| 7624 | Truncate the file given by path to length bytes."); |
| 7625 | |
| 7626 | static PyObject * |
| 7627 | posix_truncate(PyObject *self, PyObject *args) |
| 7628 | { |
| 7629 | PyObject *opath; |
| 7630 | const char *path; |
| 7631 | off_t length; |
| 7632 | int res; |
| 7633 | |
| 7634 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7635 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7636 | return NULL; |
| 7637 | path = PyBytes_AsString(opath); |
| 7638 | |
| 7639 | Py_BEGIN_ALLOW_THREADS |
| 7640 | res = truncate(path, length); |
| 7641 | Py_END_ALLOW_THREADS |
| 7642 | Py_DECREF(opath); |
| 7643 | if (res < 0) |
| 7644 | return posix_error(); |
| 7645 | Py_RETURN_NONE; |
| 7646 | } |
| 7647 | #endif |
| 7648 | |
| 7649 | #ifdef HAVE_POSIX_FALLOCATE |
| 7650 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7651 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7652 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7653 | starting from offset and continuing for len bytes."); |
| 7654 | |
| 7655 | static PyObject * |
| 7656 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7657 | { |
| 7658 | off_t len, offset; |
| 7659 | int res, fd; |
| 7660 | |
| 7661 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7662 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7663 | return NULL; |
| 7664 | |
| 7665 | Py_BEGIN_ALLOW_THREADS |
| 7666 | res = posix_fallocate(fd, offset, len); |
| 7667 | Py_END_ALLOW_THREADS |
| 7668 | if (res != 0) { |
| 7669 | errno = res; |
| 7670 | return posix_error(); |
| 7671 | } |
| 7672 | Py_RETURN_NONE; |
| 7673 | } |
| 7674 | #endif |
| 7675 | |
| 7676 | #ifdef HAVE_POSIX_FADVISE |
| 7677 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7678 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7679 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7680 | the kernel to make optimizations.\n\ |
| 7681 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7682 | offset and continuing for len bytes.\n\ |
| 7683 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7684 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7685 | POSIX_FADV_DONTNEED."); |
| 7686 | |
| 7687 | static PyObject * |
| 7688 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7689 | { |
| 7690 | off_t len, offset; |
| 7691 | int res, fd, advice; |
| 7692 | |
| 7693 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7694 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7695 | return NULL; |
| 7696 | |
| 7697 | Py_BEGIN_ALLOW_THREADS |
| 7698 | res = posix_fadvise(fd, offset, len, advice); |
| 7699 | Py_END_ALLOW_THREADS |
| 7700 | if (res != 0) { |
| 7701 | errno = res; |
| 7702 | return posix_error(); |
| 7703 | } |
| 7704 | Py_RETURN_NONE; |
| 7705 | } |
| 7706 | #endif |
| 7707 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7708 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7709 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7710 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7711 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7712 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7713 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7714 | * get re-set with another call for the same key. */ |
| 7715 | static PyObject *posix_putenv_garbage; |
| 7716 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7717 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7718 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7719 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7720 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7721 | wchar_t *s1, *s2; |
| 7722 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7723 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7724 | PyObject *os1, *os2; |
| 7725 | char *s1, *s2; |
| 7726 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7727 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7728 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7730 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7731 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | if (!PyArg_ParseTuple(args, |
| 7733 | "uu:putenv", |
| 7734 | &s1, &s2)) |
| 7735 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7736 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7737 | if (!PyArg_ParseTuple(args, |
| 7738 | "O&O&:putenv", |
| 7739 | PyUnicode_FSConverter, &os1, |
| 7740 | PyUnicode_FSConverter, &os2)) |
| 7741 | return NULL; |
| 7742 | s1 = PyBytes_AsString(os1); |
| 7743 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7744 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7745 | |
| 7746 | #if defined(PYOS_OS2) |
| 7747 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7748 | APIRET rc; |
| 7749 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7750 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7751 | if (rc != NO_ERROR) { |
| 7752 | os2_error(rc); |
| 7753 | goto error; |
| 7754 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7755 | |
| 7756 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7757 | APIRET rc; |
| 7758 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7759 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7760 | if (rc != NO_ERROR) { |
| 7761 | os2_error(rc); |
| 7762 | goto error; |
| 7763 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7764 | } else { |
| 7765 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7766 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7767 | /* len includes space for a trailing \0; the size arg to |
| 7768 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7769 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | len = wcslen(s1) + wcslen(s2) + 2; |
| 7771 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7772 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7773 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7774 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7775 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7776 | if (newstr == NULL) { |
| 7777 | PyErr_NoMemory(); |
| 7778 | goto error; |
| 7779 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7780 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7782 | if (newenv == NULL) |
| 7783 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 7785 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7786 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7787 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7789 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7790 | newenv = PyBytes_AS_STRING(newstr); |
| 7791 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7792 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7793 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7794 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7795 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7796 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7798 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7799 | * this will cause previous value to be collected. This has to |
| 7800 | * happen after the real putenv() call because the old value |
| 7801 | * was still accessible until then. */ |
| 7802 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7803 | #ifdef MS_WINDOWS |
| 7804 | PyTuple_GET_ITEM(args, 0), |
| 7805 | #else |
| 7806 | os1, |
| 7807 | #endif |
| 7808 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7809 | /* really not much we can do; just leak */ |
| 7810 | PyErr_Clear(); |
| 7811 | } |
| 7812 | else { |
| 7813 | Py_DECREF(newstr); |
| 7814 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7815 | |
| 7816 | #if defined(PYOS_OS2) |
| 7817 | } |
| 7818 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7819 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7820 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7821 | Py_DECREF(os1); |
| 7822 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7823 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7824 | Py_RETURN_NONE; |
| 7825 | |
| 7826 | error: |
| 7827 | #ifndef MS_WINDOWS |
| 7828 | Py_DECREF(os1); |
| 7829 | Py_DECREF(os2); |
| 7830 | #endif |
| 7831 | Py_XDECREF(newstr); |
| 7832 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7833 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7834 | #endif /* putenv */ |
| 7835 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7836 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7837 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7838 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7839 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7840 | |
| 7841 | static PyObject * |
| 7842 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7843 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7844 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7845 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7847 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7848 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7849 | #else |
| 7850 | PyObject *os1; |
| 7851 | char *s1; |
| 7852 | |
| 7853 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7854 | PyUnicode_FSConverter, &os1)) |
| 7855 | return NULL; |
| 7856 | s1 = PyBytes_AsString(os1); |
| 7857 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7859 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7861 | /* Remove the key from posix_putenv_garbage; |
| 7862 | * this will cause it to be collected. This has to |
| 7863 | * happen after the real unsetenv() call because the |
| 7864 | * old value was still accessible until then. |
| 7865 | */ |
| 7866 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7867 | #ifdef MS_WINDOWS |
| 7868 | PyTuple_GET_ITEM(args, 0) |
| 7869 | #else |
| 7870 | os1 |
| 7871 | #endif |
| 7872 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7873 | /* really not much we can do; just leak */ |
| 7874 | PyErr_Clear(); |
| 7875 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7876 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7877 | #ifndef MS_WINDOWS |
| 7878 | Py_DECREF(os1); |
| 7879 | #endif |
| 7880 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7881 | } |
| 7882 | #endif /* unsetenv */ |
| 7883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7884 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7885 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7886 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7887 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7889 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7890 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7891 | int code; |
| 7892 | char *message; |
| 7893 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7894 | return NULL; |
| 7895 | message = strerror(code); |
| 7896 | if (message == NULL) { |
| 7897 | PyErr_SetString(PyExc_ValueError, |
| 7898 | "strerror() argument out of range"); |
| 7899 | return NULL; |
| 7900 | } |
| 7901 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7902 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7903 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7904 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7905 | #ifdef HAVE_SYS_WAIT_H |
| 7906 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7907 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7908 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7909 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7910 | 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] | 7911 | |
| 7912 | static PyObject * |
| 7913 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7914 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7915 | WAIT_TYPE status; |
| 7916 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7917 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7918 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7919 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7921 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7922 | } |
| 7923 | #endif /* WCOREDUMP */ |
| 7924 | |
| 7925 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7926 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7927 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7928 | 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] | 7929 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7930 | |
| 7931 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7932 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7933 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7934 | WAIT_TYPE status; |
| 7935 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7937 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7938 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7939 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7940 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7941 | } |
| 7942 | #endif /* WIFCONTINUED */ |
| 7943 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7944 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7945 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7946 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7947 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7948 | |
| 7949 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7950 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7951 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7952 | WAIT_TYPE status; |
| 7953 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7955 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7956 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7957 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7958 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7959 | } |
| 7960 | #endif /* WIFSTOPPED */ |
| 7961 | |
| 7962 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7963 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7964 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7965 | 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] | 7966 | |
| 7967 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7968 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7969 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7970 | WAIT_TYPE status; |
| 7971 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7973 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7974 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7975 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7976 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7977 | } |
| 7978 | #endif /* WIFSIGNALED */ |
| 7979 | |
| 7980 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7981 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7982 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7983 | 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] | 7984 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7985 | |
| 7986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7987 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7988 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7989 | WAIT_TYPE status; |
| 7990 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7992 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7993 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7994 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7995 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7996 | } |
| 7997 | #endif /* WIFEXITED */ |
| 7998 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7999 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8000 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8001 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8002 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8003 | |
| 8004 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8005 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8006 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | WAIT_TYPE status; |
| 8008 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8009 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8010 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8011 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8012 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8013 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8014 | } |
| 8015 | #endif /* WEXITSTATUS */ |
| 8016 | |
| 8017 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8018 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8019 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8020 | 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] | 8021 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8022 | |
| 8023 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8024 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8025 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8026 | WAIT_TYPE status; |
| 8027 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8028 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8029 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8030 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8033 | } |
| 8034 | #endif /* WTERMSIG */ |
| 8035 | |
| 8036 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8037 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8038 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8039 | Return the signal that stopped the process that provided\n\ |
| 8040 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8041 | |
| 8042 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8043 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8045 | WAIT_TYPE status; |
| 8046 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8047 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8048 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8049 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8051 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8052 | } |
| 8053 | #endif /* WSTOPSIG */ |
| 8054 | |
| 8055 | #endif /* HAVE_SYS_WAIT_H */ |
| 8056 | |
| 8057 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8058 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8059 | #ifdef _SCO_DS |
| 8060 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8061 | needed definitions in sys/statvfs.h */ |
| 8062 | #define _SVID3 |
| 8063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8064 | #include <sys/statvfs.h> |
| 8065 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8066 | static PyObject* |
| 8067 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8068 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8069 | if (v == NULL) |
| 8070 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8071 | |
| 8072 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8073 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8074 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8075 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8076 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8077 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8078 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8079 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8080 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8081 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8082 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8083 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8084 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8085 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8086 | PyStructSequence_SET_ITEM(v, 2, |
| 8087 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8088 | PyStructSequence_SET_ITEM(v, 3, |
| 8089 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8090 | PyStructSequence_SET_ITEM(v, 4, |
| 8091 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8092 | PyStructSequence_SET_ITEM(v, 5, |
| 8093 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8094 | PyStructSequence_SET_ITEM(v, 6, |
| 8095 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8096 | PyStructSequence_SET_ITEM(v, 7, |
| 8097 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8098 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8099 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8100 | #endif |
| 8101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8102 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8103 | } |
| 8104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8105 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8106 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8107 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8108 | |
| 8109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8110 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8111 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8112 | int fd, res; |
| 8113 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8114 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8115 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8116 | return NULL; |
| 8117 | Py_BEGIN_ALLOW_THREADS |
| 8118 | res = fstatvfs(fd, &st); |
| 8119 | Py_END_ALLOW_THREADS |
| 8120 | if (res != 0) |
| 8121 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8122 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8123 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8124 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8125 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8126 | |
| 8127 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8128 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8129 | #include <sys/statvfs.h> |
| 8130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8131 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8132 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8133 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8134 | |
| 8135 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8136 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8137 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8138 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8139 | int res; |
| 8140 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8141 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | return NULL; |
| 8143 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8144 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8145 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8146 | if (res != 0) { |
| 8147 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8148 | Py_DECREF(path); |
| 8149 | return NULL; |
| 8150 | } |
| 8151 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8153 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8154 | } |
| 8155 | #endif /* HAVE_STATVFS */ |
| 8156 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8157 | #ifdef MS_WINDOWS |
| 8158 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8159 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8160 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8161 | |
| 8162 | static PyObject * |
| 8163 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8164 | { |
| 8165 | BOOL retval; |
| 8166 | ULARGE_INTEGER _, total, free; |
| 8167 | LPCTSTR path; |
| 8168 | |
| 8169 | if (! PyArg_ParseTuple(args, "s", &path)) |
| 8170 | return NULL; |
| 8171 | |
| 8172 | Py_BEGIN_ALLOW_THREADS |
| 8173 | retval = GetDiskFreeSpaceEx(path, &_, &total, &free); |
| 8174 | Py_END_ALLOW_THREADS |
| 8175 | if (retval == 0) |
| 8176 | return PyErr_SetFromWindowsErr(0); |
| 8177 | |
| 8178 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8179 | } |
| 8180 | #endif |
| 8181 | |
| 8182 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8183 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8184 | * It maps strings representing configuration variable names to |
| 8185 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8186 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8187 | * rarely-used constants. There are three separate tables that use |
| 8188 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8189 | * |
| 8190 | * This code is always included, even if none of the interfaces that |
| 8191 | * need it are included. The #if hackery needed to avoid it would be |
| 8192 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8193 | */ |
| 8194 | struct constdef { |
| 8195 | char *name; |
| 8196 | long value; |
| 8197 | }; |
| 8198 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8199 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8200 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8201 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8202 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8203 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8204 | *valuep = PyLong_AS_LONG(arg); |
| 8205 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8206 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8207 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8208 | /* look up the value in the table using a binary search */ |
| 8209 | size_t lo = 0; |
| 8210 | size_t mid; |
| 8211 | size_t hi = tablesize; |
| 8212 | int cmp; |
| 8213 | const char *confname; |
| 8214 | if (!PyUnicode_Check(arg)) { |
| 8215 | PyErr_SetString(PyExc_TypeError, |
| 8216 | "configuration names must be strings or integers"); |
| 8217 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8218 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8219 | confname = _PyUnicode_AsString(arg); |
| 8220 | if (confname == NULL) |
| 8221 | return 0; |
| 8222 | while (lo < hi) { |
| 8223 | mid = (lo + hi) / 2; |
| 8224 | cmp = strcmp(confname, table[mid].name); |
| 8225 | if (cmp < 0) |
| 8226 | hi = mid; |
| 8227 | else if (cmp > 0) |
| 8228 | lo = mid + 1; |
| 8229 | else { |
| 8230 | *valuep = table[mid].value; |
| 8231 | return 1; |
| 8232 | } |
| 8233 | } |
| 8234 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8235 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8236 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8237 | } |
| 8238 | |
| 8239 | |
| 8240 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8241 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8242 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8243 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8244 | #endif |
| 8245 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8246 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8247 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8248 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8249 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8250 | #endif |
| 8251 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8252 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8253 | #endif |
| 8254 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8255 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8256 | #endif |
| 8257 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8258 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8259 | #endif |
| 8260 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8261 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8262 | #endif |
| 8263 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8264 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8265 | #endif |
| 8266 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8267 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8268 | #endif |
| 8269 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8270 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8271 | #endif |
| 8272 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8273 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8274 | #endif |
| 8275 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8276 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8277 | #endif |
| 8278 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8279 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8280 | #endif |
| 8281 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8282 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8283 | #endif |
| 8284 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8285 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8286 | #endif |
| 8287 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8288 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8289 | #endif |
| 8290 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8291 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8292 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8293 | #ifdef _PC_ACL_ENABLED |
| 8294 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8295 | #endif |
| 8296 | #ifdef _PC_MIN_HOLE_SIZE |
| 8297 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8298 | #endif |
| 8299 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8300 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8301 | #endif |
| 8302 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8303 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8304 | #endif |
| 8305 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8306 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8307 | #endif |
| 8308 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8309 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8310 | #endif |
| 8311 | #ifdef _PC_REC_XFER_ALIGN |
| 8312 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8313 | #endif |
| 8314 | #ifdef _PC_SYMLINK_MAX |
| 8315 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8316 | #endif |
| 8317 | #ifdef _PC_XATTR_ENABLED |
| 8318 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8319 | #endif |
| 8320 | #ifdef _PC_XATTR_EXISTS |
| 8321 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8322 | #endif |
| 8323 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8324 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8325 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8326 | }; |
| 8327 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8328 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8329 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8330 | { |
| 8331 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8332 | sizeof(posix_constants_pathconf) |
| 8333 | / sizeof(struct constdef)); |
| 8334 | } |
| 8335 | #endif |
| 8336 | |
| 8337 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8338 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8339 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8340 | 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] | 8341 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8342 | |
| 8343 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8344 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | { |
| 8346 | PyObject *result = NULL; |
| 8347 | int name, fd; |
| 8348 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8349 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8350 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8351 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8352 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8353 | errno = 0; |
| 8354 | limit = fpathconf(fd, name); |
| 8355 | if (limit == -1 && errno != 0) |
| 8356 | posix_error(); |
| 8357 | else |
| 8358 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8359 | } |
| 8360 | return result; |
| 8361 | } |
| 8362 | #endif |
| 8363 | |
| 8364 | |
| 8365 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8366 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8367 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8368 | 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] | 8369 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8370 | |
| 8371 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8372 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8373 | { |
| 8374 | PyObject *result = NULL; |
| 8375 | int name; |
| 8376 | char *path; |
| 8377 | |
| 8378 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8379 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8380 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8382 | errno = 0; |
| 8383 | limit = pathconf(path, name); |
| 8384 | if (limit == -1 && errno != 0) { |
| 8385 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8386 | /* could be a path or name problem */ |
| 8387 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8388 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8389 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8390 | } |
| 8391 | else |
| 8392 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8393 | } |
| 8394 | return result; |
| 8395 | } |
| 8396 | #endif |
| 8397 | |
| 8398 | #ifdef HAVE_CONFSTR |
| 8399 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8400 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8401 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8402 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8403 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8404 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8405 | #endif |
| 8406 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8407 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8408 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8409 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8410 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8411 | #endif |
| 8412 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8413 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8414 | #endif |
| 8415 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8416 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8417 | #endif |
| 8418 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8419 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8420 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8421 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8422 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8423 | #endif |
| 8424 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8425 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8426 | #endif |
| 8427 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8428 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8429 | #endif |
| 8430 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8431 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8432 | #endif |
| 8433 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8434 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8435 | #endif |
| 8436 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8437 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8438 | #endif |
| 8439 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8440 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8441 | #endif |
| 8442 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8443 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8444 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8445 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8446 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8447 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8448 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8449 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8450 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8451 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8452 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8453 | #endif |
| 8454 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8455 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8456 | #endif |
| 8457 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8458 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8459 | #endif |
| 8460 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8461 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8462 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8463 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8464 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8465 | #endif |
| 8466 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8468 | #endif |
| 8469 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8470 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8471 | #endif |
| 8472 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8473 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8474 | #endif |
| 8475 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8476 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8477 | #endif |
| 8478 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8479 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8480 | #endif |
| 8481 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8482 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8483 | #endif |
| 8484 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8486 | #endif |
| 8487 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8488 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8489 | #endif |
| 8490 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8491 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8492 | #endif |
| 8493 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8494 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8495 | #endif |
| 8496 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8497 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8498 | #endif |
| 8499 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8500 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8501 | #endif |
| 8502 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8503 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8504 | #endif |
| 8505 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8506 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8507 | #endif |
| 8508 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8509 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8510 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8511 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8513 | #endif |
| 8514 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8515 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8516 | #endif |
| 8517 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8518 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8519 | #endif |
| 8520 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8521 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8522 | #endif |
| 8523 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8524 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8525 | #endif |
| 8526 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8527 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8528 | #endif |
| 8529 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8530 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8531 | #endif |
| 8532 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8533 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8534 | #endif |
| 8535 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8536 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8537 | #endif |
| 8538 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8539 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8540 | #endif |
| 8541 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8542 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8543 | #endif |
| 8544 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8545 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8546 | #endif |
| 8547 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8548 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8549 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8550 | }; |
| 8551 | |
| 8552 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8553 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8554 | { |
| 8555 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8556 | sizeof(posix_constants_confstr) |
| 8557 | / sizeof(struct constdef)); |
| 8558 | } |
| 8559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8560 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8561 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8562 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8563 | |
| 8564 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8565 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8566 | { |
| 8567 | PyObject *result = NULL; |
| 8568 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8569 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8570 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8571 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8572 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8573 | return NULL; |
| 8574 | |
| 8575 | errno = 0; |
| 8576 | len = confstr(name, buffer, sizeof(buffer)); |
| 8577 | if (len == 0) { |
| 8578 | if (errno) { |
| 8579 | posix_error(); |
| 8580 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8581 | } |
| 8582 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8583 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8584 | } |
| 8585 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8586 | |
| 8587 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8588 | char *buf = PyMem_Malloc(len); |
| 8589 | if (buf == NULL) |
| 8590 | return PyErr_NoMemory(); |
| 8591 | confstr(name, buf, len); |
| 8592 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8593 | PyMem_Free(buf); |
| 8594 | } |
| 8595 | else |
| 8596 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8597 | return result; |
| 8598 | } |
| 8599 | #endif |
| 8600 | |
| 8601 | |
| 8602 | #ifdef HAVE_SYSCONF |
| 8603 | static struct constdef posix_constants_sysconf[] = { |
| 8604 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8605 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8606 | #endif |
| 8607 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8608 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8609 | #endif |
| 8610 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8611 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8612 | #endif |
| 8613 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8614 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8615 | #endif |
| 8616 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8617 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8618 | #endif |
| 8619 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8620 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8621 | #endif |
| 8622 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8623 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8624 | #endif |
| 8625 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8626 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8627 | #endif |
| 8628 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8629 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8630 | #endif |
| 8631 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8632 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8633 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8634 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8635 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8636 | #endif |
| 8637 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8638 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8639 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8640 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8641 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8642 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8643 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8644 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8645 | #endif |
| 8646 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8647 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8648 | #endif |
| 8649 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8650 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8651 | #endif |
| 8652 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8653 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8654 | #endif |
| 8655 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8657 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8658 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8660 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8661 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8662 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8663 | #endif |
| 8664 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8665 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8666 | #endif |
| 8667 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8668 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8669 | #endif |
| 8670 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8671 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8672 | #endif |
| 8673 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8674 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8675 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8676 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8677 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8678 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8679 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8680 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8681 | #endif |
| 8682 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8683 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8684 | #endif |
| 8685 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8686 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8687 | #endif |
| 8688 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8689 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8690 | #endif |
| 8691 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8692 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8693 | #endif |
| 8694 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8695 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8696 | #endif |
| 8697 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8698 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | #endif |
| 8700 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8701 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8702 | #endif |
| 8703 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8704 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8705 | #endif |
| 8706 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8707 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8708 | #endif |
| 8709 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8710 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8711 | #endif |
| 8712 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8713 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8714 | #endif |
| 8715 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8716 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8717 | #endif |
| 8718 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8720 | #endif |
| 8721 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8722 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8723 | #endif |
| 8724 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8725 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | #endif |
| 8727 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8728 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8729 | #endif |
| 8730 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8731 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8732 | #endif |
| 8733 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8734 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8735 | #endif |
| 8736 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8738 | #endif |
| 8739 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8740 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8741 | #endif |
| 8742 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8743 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8744 | #endif |
| 8745 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8746 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8747 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8748 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8749 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8750 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8751 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8752 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8753 | #endif |
| 8754 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8755 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8756 | #endif |
| 8757 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8758 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8759 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8760 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8762 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8763 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8764 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8765 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8766 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8768 | #endif |
| 8769 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8770 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8771 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8772 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8773 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8774 | #endif |
| 8775 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8776 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8777 | #endif |
| 8778 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8779 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8780 | #endif |
| 8781 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8782 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8783 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8784 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8785 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8786 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8787 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8788 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8789 | #endif |
| 8790 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8791 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8792 | #endif |
| 8793 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8794 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8795 | #endif |
| 8796 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8797 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8798 | #endif |
| 8799 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8800 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8801 | #endif |
| 8802 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8803 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8804 | #endif |
| 8805 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8807 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8808 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8809 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8810 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8811 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8812 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8813 | #endif |
| 8814 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8815 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8816 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8817 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8818 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8819 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8820 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8821 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8822 | #endif |
| 8823 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8824 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8825 | #endif |
| 8826 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8827 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8828 | #endif |
| 8829 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8830 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8831 | #endif |
| 8832 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8833 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8834 | #endif |
| 8835 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8836 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8837 | #endif |
| 8838 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8839 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8840 | #endif |
| 8841 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8843 | #endif |
| 8844 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8845 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8846 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8847 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8848 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8849 | #endif |
| 8850 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8851 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8852 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8853 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8854 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8855 | #endif |
| 8856 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8857 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8858 | #endif |
| 8859 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8860 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8861 | #endif |
| 8862 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8863 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8864 | #endif |
| 8865 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8866 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8867 | #endif |
| 8868 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8869 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8870 | #endif |
| 8871 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8872 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8873 | #endif |
| 8874 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8875 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8876 | #endif |
| 8877 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8878 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8879 | #endif |
| 8880 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8882 | #endif |
| 8883 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8884 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8885 | #endif |
| 8886 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8887 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8888 | #endif |
| 8889 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8890 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8891 | #endif |
| 8892 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8893 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8894 | #endif |
| 8895 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8896 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8897 | #endif |
| 8898 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8900 | #endif |
| 8901 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8902 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8903 | #endif |
| 8904 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8905 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8906 | #endif |
| 8907 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8908 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8909 | #endif |
| 8910 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8911 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8912 | #endif |
| 8913 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8914 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8915 | #endif |
| 8916 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8917 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8918 | #endif |
| 8919 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8920 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8921 | #endif |
| 8922 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8923 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8924 | #endif |
| 8925 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8926 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8927 | #endif |
| 8928 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8929 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8930 | #endif |
| 8931 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8932 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8933 | #endif |
| 8934 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8936 | #endif |
| 8937 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8938 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8939 | #endif |
| 8940 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8941 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8942 | #endif |
| 8943 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8944 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8945 | #endif |
| 8946 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8947 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8948 | #endif |
| 8949 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8950 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8951 | #endif |
| 8952 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8953 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8954 | #endif |
| 8955 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8956 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8957 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8958 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8959 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8960 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8961 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8962 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8963 | #endif |
| 8964 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8965 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8966 | #endif |
| 8967 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8968 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8969 | #endif |
| 8970 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8971 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8972 | #endif |
| 8973 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8974 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8975 | #endif |
| 8976 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8977 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8978 | #endif |
| 8979 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8980 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8981 | #endif |
| 8982 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8983 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8984 | #endif |
| 8985 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8986 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8987 | #endif |
| 8988 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8989 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8990 | #endif |
| 8991 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8992 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8993 | #endif |
| 8994 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8995 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8996 | #endif |
| 8997 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8998 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8999 | #endif |
| 9000 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9001 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9002 | #endif |
| 9003 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9004 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9005 | #endif |
| 9006 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9007 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9008 | #endif |
| 9009 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9010 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9011 | #endif |
| 9012 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9013 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9014 | #endif |
| 9015 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9016 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9017 | #endif |
| 9018 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9019 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9020 | #endif |
| 9021 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9022 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9023 | #endif |
| 9024 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9025 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9026 | #endif |
| 9027 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9028 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9029 | #endif |
| 9030 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9031 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9032 | #endif |
| 9033 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9034 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9035 | #endif |
| 9036 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9037 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9038 | #endif |
| 9039 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9040 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9041 | #endif |
| 9042 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9043 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9044 | #endif |
| 9045 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9046 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9047 | #endif |
| 9048 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9049 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9050 | #endif |
| 9051 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9052 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9053 | #endif |
| 9054 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9055 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9056 | #endif |
| 9057 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9058 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9059 | #endif |
| 9060 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9061 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9062 | #endif |
| 9063 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9064 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9065 | #endif |
| 9066 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9067 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9068 | #endif |
| 9069 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9070 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9071 | #endif |
| 9072 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9073 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9074 | #endif |
| 9075 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9076 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9077 | #endif |
| 9078 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9079 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9080 | #endif |
| 9081 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9082 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9083 | #endif |
| 9084 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9085 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9086 | #endif |
| 9087 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9088 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9089 | #endif |
| 9090 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9091 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9092 | #endif |
| 9093 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9094 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9095 | #endif |
| 9096 | }; |
| 9097 | |
| 9098 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9099 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9100 | { |
| 9101 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9102 | sizeof(posix_constants_sysconf) |
| 9103 | / sizeof(struct constdef)); |
| 9104 | } |
| 9105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9106 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9107 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9108 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9109 | |
| 9110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9111 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9112 | { |
| 9113 | PyObject *result = NULL; |
| 9114 | int name; |
| 9115 | |
| 9116 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9117 | int value; |
| 9118 | |
| 9119 | errno = 0; |
| 9120 | value = sysconf(name); |
| 9121 | if (value == -1 && errno != 0) |
| 9122 | posix_error(); |
| 9123 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9124 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9125 | } |
| 9126 | return result; |
| 9127 | } |
| 9128 | #endif |
| 9129 | |
| 9130 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9131 | /* This code is used to ensure that the tables of configuration value names |
| 9132 | * are in sorted order as required by conv_confname(), and also to build the |
| 9133 | * the exported dictionaries that are used to publish information about the |
| 9134 | * names available on the host platform. |
| 9135 | * |
| 9136 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9137 | * when used, even for platforms we're not able to test on. It also makes |
| 9138 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9139 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9140 | |
| 9141 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9142 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9143 | { |
| 9144 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9145 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9146 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9147 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9148 | |
| 9149 | return strcmp(c1->name, c2->name); |
| 9150 | } |
| 9151 | |
| 9152 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9153 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9154 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9155 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9156 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9157 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9158 | |
| 9159 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9160 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9161 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9162 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9163 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9164 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9165 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9166 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9167 | Py_XDECREF(o); |
| 9168 | Py_DECREF(d); |
| 9169 | return -1; |
| 9170 | } |
| 9171 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9172 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9173 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9174 | } |
| 9175 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9176 | /* Return -1 on failure, 0 on success. */ |
| 9177 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9178 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9179 | { |
| 9180 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9181 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9182 | sizeof(posix_constants_pathconf) |
| 9183 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9184 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9185 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9186 | #endif |
| 9187 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9188 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9189 | sizeof(posix_constants_confstr) |
| 9190 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9191 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9192 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9193 | #endif |
| 9194 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9195 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9196 | sizeof(posix_constants_sysconf) |
| 9197 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9198 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9199 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9200 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9201 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9202 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9203 | |
| 9204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9205 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9206 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9207 | 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] | 9208 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9209 | |
| 9210 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9211 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9212 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9213 | abort(); |
| 9214 | /*NOTREACHED*/ |
| 9215 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9216 | return NULL; |
| 9217 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9218 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9219 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9220 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9221 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9222 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9223 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9224 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9225 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9226 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9227 | application (if any) its extension is associated.\n\ |
| 9228 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9229 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9230 | \n\ |
| 9231 | startfile returns as soon as the associated application is launched.\n\ |
| 9232 | There is no option to wait for the application to close, and no way\n\ |
| 9233 | to retrieve the application's exit status.\n\ |
| 9234 | \n\ |
| 9235 | The filepath is relative to the current directory. If you want to use\n\ |
| 9236 | 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] | 9237 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9238 | |
| 9239 | static PyObject * |
| 9240 | win32_startfile(PyObject *self, PyObject *args) |
| 9241 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | PyObject *ofilepath; |
| 9243 | char *filepath; |
| 9244 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9245 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9246 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9247 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9248 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9249 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9250 | &unipath, &operation)) { |
| 9251 | PyErr_Clear(); |
| 9252 | goto normal; |
| 9253 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9254 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9255 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9256 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9258 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9259 | PyErr_Clear(); |
| 9260 | operation = NULL; |
| 9261 | goto normal; |
| 9262 | } |
| 9263 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9264 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9265 | wpath = PyUnicode_AsUnicode(unipath); |
| 9266 | if (wpath == NULL) |
| 9267 | goto normal; |
| 9268 | if (uoperation) { |
| 9269 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9270 | if (woperation == NULL) |
| 9271 | goto normal; |
| 9272 | } |
| 9273 | else |
| 9274 | woperation = NULL; |
| 9275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9276 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9277 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9278 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9279 | Py_END_ALLOW_THREADS |
| 9280 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9281 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9282 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9283 | win32_error_object("startfile", unipath); |
| 9284 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9285 | } |
| 9286 | Py_INCREF(Py_None); |
| 9287 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9288 | |
| 9289 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9290 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9291 | PyUnicode_FSConverter, &ofilepath, |
| 9292 | &operation)) |
| 9293 | return NULL; |
| 9294 | filepath = PyBytes_AsString(ofilepath); |
| 9295 | Py_BEGIN_ALLOW_THREADS |
| 9296 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9297 | NULL, NULL, SW_SHOWNORMAL); |
| 9298 | Py_END_ALLOW_THREADS |
| 9299 | if (rc <= (HINSTANCE)32) { |
| 9300 | PyObject *errval = win32_error("startfile", filepath); |
| 9301 | Py_DECREF(ofilepath); |
| 9302 | return errval; |
| 9303 | } |
| 9304 | Py_DECREF(ofilepath); |
| 9305 | Py_INCREF(Py_None); |
| 9306 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9307 | } |
| 9308 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9309 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9310 | #ifdef HAVE_GETLOADAVG |
| 9311 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9312 | "getloadavg() -> (float, float, float)\n\n\ |
| 9313 | Return the number of processes in the system run queue averaged over\n\ |
| 9314 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9315 | was unobtainable"); |
| 9316 | |
| 9317 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9318 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9319 | { |
| 9320 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9321 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9322 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9323 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9324 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9325 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9326 | } |
| 9327 | #endif |
| 9328 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9329 | #ifdef MS_WINDOWS |
| 9330 | |
| 9331 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9332 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9333 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9334 | |
| 9335 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9336 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9337 | DWORD dwFlags ); |
| 9338 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9339 | BYTE *pbBuffer ); |
| 9340 | |
| 9341 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9342 | /* This handle is never explicitly released. Instead, the operating |
| 9343 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9344 | static HCRYPTPROV hCryptProv = 0; |
| 9345 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9346 | static PyObject* |
| 9347 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9348 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9349 | int howMany; |
| 9350 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9351 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | /* Read arguments */ |
| 9353 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9354 | return NULL; |
| 9355 | if (howMany < 0) |
| 9356 | return PyErr_Format(PyExc_ValueError, |
| 9357 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9359 | if (hCryptProv == 0) { |
| 9360 | HINSTANCE hAdvAPI32 = NULL; |
| 9361 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9363 | /* Obtain handle to the DLL containing CryptoAPI |
| 9364 | This should not fail */ |
| 9365 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 9366 | if(hAdvAPI32 == NULL) |
| 9367 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | /* Obtain pointers to the CryptoAPI functions |
| 9370 | This will fail on some early versions of Win95 */ |
| 9371 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9372 | hAdvAPI32, |
| 9373 | "CryptAcquireContextA"); |
| 9374 | if (pCryptAcquireContext == NULL) |
| 9375 | return PyErr_Format(PyExc_NotImplementedError, |
| 9376 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9377 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9378 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9379 | hAdvAPI32, "CryptGenRandom"); |
| 9380 | if (pCryptGenRandom == NULL) |
| 9381 | return PyErr_Format(PyExc_NotImplementedError, |
| 9382 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9384 | /* Acquire context */ |
| 9385 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9386 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9387 | return win32_error("CryptAcquireContext", NULL); |
| 9388 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9389 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9390 | /* Allocate bytes */ |
| 9391 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9392 | if (result != NULL) { |
| 9393 | /* Get random data */ |
| 9394 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9395 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9396 | PyBytes_AS_STRING(result))) { |
| 9397 | Py_DECREF(result); |
| 9398 | return win32_error("CryptGenRandom", NULL); |
| 9399 | } |
| 9400 | } |
| 9401 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9402 | } |
| 9403 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9404 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9405 | PyDoc_STRVAR(device_encoding__doc__, |
| 9406 | "device_encoding(fd) -> str\n\n\ |
| 9407 | Return a string describing the encoding of the device\n\ |
| 9408 | if the output is a terminal; else return None."); |
| 9409 | |
| 9410 | static PyObject * |
| 9411 | device_encoding(PyObject *self, PyObject *args) |
| 9412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9414 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9415 | UINT cp; |
| 9416 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9417 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9418 | return NULL; |
| 9419 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9420 | Py_INCREF(Py_None); |
| 9421 | return Py_None; |
| 9422 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9423 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9424 | if (fd == 0) |
| 9425 | cp = GetConsoleCP(); |
| 9426 | else if (fd == 1 || fd == 2) |
| 9427 | cp = GetConsoleOutputCP(); |
| 9428 | else |
| 9429 | cp = 0; |
| 9430 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9431 | has no console */ |
| 9432 | if (cp != 0) |
| 9433 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9434 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9435 | { |
| 9436 | char *codeset = nl_langinfo(CODESET); |
| 9437 | if (codeset != NULL && codeset[0] != 0) |
| 9438 | return PyUnicode_FromString(codeset); |
| 9439 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9440 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9441 | Py_INCREF(Py_None); |
| 9442 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9443 | } |
| 9444 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9445 | #ifdef __VMS |
| 9446 | /* Use openssl random routine */ |
| 9447 | #include <openssl/rand.h> |
| 9448 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9449 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9450 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9451 | |
| 9452 | static PyObject* |
| 9453 | vms_urandom(PyObject *self, PyObject *args) |
| 9454 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | int howMany; |
| 9456 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9457 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | /* Read arguments */ |
| 9459 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9460 | return NULL; |
| 9461 | if (howMany < 0) |
| 9462 | return PyErr_Format(PyExc_ValueError, |
| 9463 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9464 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9465 | /* Allocate bytes */ |
| 9466 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9467 | if (result != NULL) { |
| 9468 | /* Get random data */ |
| 9469 | if (RAND_pseudo_bytes((unsigned char*) |
| 9470 | PyBytes_AS_STRING(result), |
| 9471 | howMany) < 0) { |
| 9472 | Py_DECREF(result); |
| 9473 | return PyErr_Format(PyExc_ValueError, |
| 9474 | "RAND_pseudo_bytes"); |
| 9475 | } |
| 9476 | } |
| 9477 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9478 | } |
| 9479 | #endif |
| 9480 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9481 | #ifdef HAVE_SETRESUID |
| 9482 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9483 | "setresuid(ruid, euid, suid)\n\n\ |
| 9484 | Set the current process's real, effective, and saved user ids."); |
| 9485 | |
| 9486 | static PyObject* |
| 9487 | posix_setresuid (PyObject *self, PyObject *args) |
| 9488 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9489 | /* We assume uid_t is no larger than a long. */ |
| 9490 | long ruid, euid, suid; |
| 9491 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9492 | return NULL; |
| 9493 | if (setresuid(ruid, euid, suid) < 0) |
| 9494 | return posix_error(); |
| 9495 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9496 | } |
| 9497 | #endif |
| 9498 | |
| 9499 | #ifdef HAVE_SETRESGID |
| 9500 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9501 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9502 | Set the current process's real, effective, and saved group ids."); |
| 9503 | |
| 9504 | static PyObject* |
| 9505 | posix_setresgid (PyObject *self, PyObject *args) |
| 9506 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9507 | /* We assume uid_t is no larger than a long. */ |
| 9508 | long rgid, egid, sgid; |
| 9509 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9510 | return NULL; |
| 9511 | if (setresgid(rgid, egid, sgid) < 0) |
| 9512 | return posix_error(); |
| 9513 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9514 | } |
| 9515 | #endif |
| 9516 | |
| 9517 | #ifdef HAVE_GETRESUID |
| 9518 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9519 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9520 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9521 | |
| 9522 | static PyObject* |
| 9523 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9524 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9525 | uid_t ruid, euid, suid; |
| 9526 | long l_ruid, l_euid, l_suid; |
| 9527 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9528 | return posix_error(); |
| 9529 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9530 | l_ruid = ruid; |
| 9531 | l_euid = euid; |
| 9532 | l_suid = suid; |
| 9533 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9534 | } |
| 9535 | #endif |
| 9536 | |
| 9537 | #ifdef HAVE_GETRESGID |
| 9538 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9539 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9540 | Get tuple of the current process's real, effective, and saved group ids."); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9541 | |
| 9542 | static PyObject* |
| 9543 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9544 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9545 | uid_t rgid, egid, sgid; |
| 9546 | long l_rgid, l_egid, l_sgid; |
| 9547 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9548 | return posix_error(); |
| 9549 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9550 | l_rgid = rgid; |
| 9551 | l_egid = egid; |
| 9552 | l_sgid = sgid; |
| 9553 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9554 | } |
| 9555 | #endif |
| 9556 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9557 | /* Posix *at family of functions: |
| 9558 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9559 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9560 | unlinkat, utimensat, mkfifoat */ |
| 9561 | |
| 9562 | #ifdef HAVE_FACCESSAT |
| 9563 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9564 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9565 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9566 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9567 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9568 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9569 | is interpreted relative to the current working directory."); |
| 9570 | |
| 9571 | static PyObject * |
| 9572 | posix_faccessat(PyObject *self, PyObject *args) |
| 9573 | { |
| 9574 | PyObject *opath; |
| 9575 | char *path; |
| 9576 | int mode; |
| 9577 | int res; |
| 9578 | int dirfd, flags = 0; |
| 9579 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9580 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9581 | return NULL; |
| 9582 | path = PyBytes_AsString(opath); |
| 9583 | Py_BEGIN_ALLOW_THREADS |
| 9584 | res = faccessat(dirfd, path, mode, flags); |
| 9585 | Py_END_ALLOW_THREADS |
| 9586 | Py_DECREF(opath); |
| 9587 | return PyBool_FromLong(res == 0); |
| 9588 | } |
| 9589 | #endif |
| 9590 | |
| 9591 | #ifdef HAVE_FCHMODAT |
| 9592 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9593 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9594 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9595 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9596 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9597 | is interpreted relative to the current working directory."); |
| 9598 | |
| 9599 | static PyObject * |
| 9600 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9601 | { |
| 9602 | int dirfd, mode, res; |
| 9603 | int flags = 0; |
| 9604 | PyObject *opath; |
| 9605 | char *path; |
| 9606 | |
| 9607 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9608 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9609 | return NULL; |
| 9610 | |
| 9611 | path = PyBytes_AsString(opath); |
| 9612 | |
| 9613 | Py_BEGIN_ALLOW_THREADS |
| 9614 | res = fchmodat(dirfd, path, mode, flags); |
| 9615 | Py_END_ALLOW_THREADS |
| 9616 | Py_DECREF(opath); |
| 9617 | if (res < 0) |
| 9618 | return posix_error(); |
| 9619 | Py_RETURN_NONE; |
| 9620 | } |
| 9621 | #endif /* HAVE_FCHMODAT */ |
| 9622 | |
| 9623 | #ifdef HAVE_FCHOWNAT |
| 9624 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9625 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9626 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9627 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9628 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9629 | is interpreted relative to the current working directory."); |
| 9630 | |
| 9631 | static PyObject * |
| 9632 | posix_fchownat(PyObject *self, PyObject *args) |
| 9633 | { |
| 9634 | PyObject *opath; |
| 9635 | int dirfd, res; |
| 9636 | long uid, gid; |
| 9637 | int flags = 0; |
| 9638 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9639 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9640 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9641 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9642 | return NULL; |
| 9643 | |
| 9644 | path = PyBytes_AsString(opath); |
| 9645 | |
| 9646 | Py_BEGIN_ALLOW_THREADS |
| 9647 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9648 | Py_END_ALLOW_THREADS |
| 9649 | Py_DECREF(opath); |
| 9650 | if (res < 0) |
| 9651 | return posix_error(); |
| 9652 | Py_RETURN_NONE; |
| 9653 | } |
| 9654 | #endif /* HAVE_FCHOWNAT */ |
| 9655 | |
| 9656 | #ifdef HAVE_FSTATAT |
| 9657 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9658 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9659 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9660 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9661 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9662 | is interpreted relative to the current working directory."); |
| 9663 | |
| 9664 | static PyObject * |
| 9665 | posix_fstatat(PyObject *self, PyObject *args) |
| 9666 | { |
| 9667 | PyObject *opath; |
| 9668 | char *path; |
| 9669 | STRUCT_STAT st; |
| 9670 | int dirfd, res, flags = 0; |
| 9671 | |
| 9672 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9673 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9674 | return NULL; |
| 9675 | path = PyBytes_AsString(opath); |
| 9676 | |
| 9677 | Py_BEGIN_ALLOW_THREADS |
| 9678 | res = fstatat(dirfd, path, &st, flags); |
| 9679 | Py_END_ALLOW_THREADS |
| 9680 | Py_DECREF(opath); |
| 9681 | if (res != 0) |
| 9682 | return posix_error(); |
| 9683 | |
| 9684 | return _pystat_fromstructstat(&st); |
| 9685 | } |
| 9686 | #endif |
| 9687 | |
| 9688 | #ifdef HAVE_FUTIMESAT |
| 9689 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 9690 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 9691 | futimesat(dirfd, path, None)\n\n\ |
| 9692 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9693 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9694 | is interpreted relative to the current working directory."); |
| 9695 | |
| 9696 | static PyObject * |
| 9697 | posix_futimesat(PyObject *self, PyObject *args) |
| 9698 | { |
| 9699 | PyObject *opath; |
| 9700 | char *path; |
| 9701 | int res, dirfd; |
| 9702 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9703 | time_t atime, mtime; |
| 9704 | long ausec, musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9705 | |
| 9706 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 9707 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9708 | return NULL; |
| 9709 | path = PyBytes_AsString(opath); |
| 9710 | if (arg == Py_None) { |
| 9711 | /* optional time values not given */ |
| 9712 | Py_BEGIN_ALLOW_THREADS |
| 9713 | res = futimesat(dirfd, path, NULL); |
| 9714 | Py_END_ALLOW_THREADS |
| 9715 | } |
| 9716 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9717 | PyErr_SetString(PyExc_TypeError, |
| 9718 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9719 | Py_DECREF(opath); |
| 9720 | return NULL; |
| 9721 | } |
| 9722 | else { |
| 9723 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9724 | &atime, &ausec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9725 | Py_DECREF(opath); |
| 9726 | return NULL; |
| 9727 | } |
| 9728 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9729 | &mtime, &musec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9730 | Py_DECREF(opath); |
| 9731 | return NULL; |
| 9732 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9733 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9734 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9735 | { |
| 9736 | #ifdef HAVE_UTIMENSAT |
| 9737 | struct timespec buf[2]; |
| 9738 | buf[0].tv_sec = atime; |
| 9739 | buf[0].tv_nsec = ausec; |
| 9740 | buf[1].tv_sec = mtime; |
| 9741 | buf[1].tv_nsec = musec; |
| 9742 | res = utimensat(dirfd, path, buf, 0); |
| 9743 | #else |
| 9744 | struct timeval buf[2]; |
| 9745 | buf[0].tv_sec = atime; |
| 9746 | buf[0].tv_usec = ausec; |
| 9747 | buf[1].tv_sec = mtime; |
| 9748 | buf[1].tv_usec = musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9749 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9750 | #endif |
| 9751 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9752 | Py_END_ALLOW_THREADS |
| 9753 | } |
| 9754 | Py_DECREF(opath); |
| 9755 | if (res < 0) { |
| 9756 | return posix_error(); |
| 9757 | } |
| 9758 | Py_RETURN_NONE; |
| 9759 | } |
| 9760 | #endif |
| 9761 | |
| 9762 | #ifdef HAVE_LINKAT |
| 9763 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9764 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9765 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9766 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9767 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9768 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9769 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9770 | also applies for dstpath."); |
| 9771 | |
| 9772 | static PyObject * |
| 9773 | posix_linkat(PyObject *self, PyObject *args) |
| 9774 | { |
| 9775 | PyObject *osrc, *odst; |
| 9776 | char *src, *dst; |
| 9777 | int res, srcfd, dstfd; |
| 9778 | int flags = 0; |
| 9779 | |
| 9780 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9781 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9782 | return NULL; |
| 9783 | src = PyBytes_AsString(osrc); |
| 9784 | dst = PyBytes_AsString(odst); |
| 9785 | Py_BEGIN_ALLOW_THREADS |
| 9786 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9787 | Py_END_ALLOW_THREADS |
| 9788 | Py_DECREF(osrc); |
| 9789 | Py_DECREF(odst); |
| 9790 | if (res < 0) |
| 9791 | return posix_error(); |
| 9792 | Py_RETURN_NONE; |
| 9793 | } |
| 9794 | #endif /* HAVE_LINKAT */ |
| 9795 | |
| 9796 | #ifdef HAVE_MKDIRAT |
| 9797 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9798 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9799 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9800 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9801 | is interpreted relative to the current working directory."); |
| 9802 | |
| 9803 | static PyObject * |
| 9804 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9805 | { |
| 9806 | int res, dirfd; |
| 9807 | PyObject *opath; |
| 9808 | char *path; |
| 9809 | int mode = 0777; |
| 9810 | |
| 9811 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9812 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9813 | return NULL; |
| 9814 | path = PyBytes_AsString(opath); |
| 9815 | Py_BEGIN_ALLOW_THREADS |
| 9816 | res = mkdirat(dirfd, path, mode); |
| 9817 | Py_END_ALLOW_THREADS |
| 9818 | Py_DECREF(opath); |
| 9819 | if (res < 0) |
| 9820 | return posix_error(); |
| 9821 | Py_RETURN_NONE; |
| 9822 | } |
| 9823 | #endif |
| 9824 | |
| 9825 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9826 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9827 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9828 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9829 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9830 | is interpreted relative to the current working directory."); |
| 9831 | |
| 9832 | static PyObject * |
| 9833 | posix_mknodat(PyObject *self, PyObject *args) |
| 9834 | { |
| 9835 | PyObject *opath; |
| 9836 | char *filename; |
| 9837 | int mode = 0600; |
| 9838 | int device = 0; |
| 9839 | int res, dirfd; |
| 9840 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9841 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9842 | return NULL; |
| 9843 | filename = PyBytes_AS_STRING(opath); |
| 9844 | Py_BEGIN_ALLOW_THREADS |
| 9845 | res = mknodat(dirfd, filename, mode, device); |
| 9846 | Py_END_ALLOW_THREADS |
| 9847 | Py_DECREF(opath); |
| 9848 | if (res < 0) |
| 9849 | return posix_error(); |
| 9850 | Py_RETURN_NONE; |
| 9851 | } |
| 9852 | #endif |
| 9853 | |
| 9854 | #ifdef HAVE_OPENAT |
| 9855 | PyDoc_STRVAR(posix_openat__doc__, |
| 9856 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9857 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9858 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9859 | is interpreted relative to the current working directory."); |
| 9860 | |
| 9861 | static PyObject * |
| 9862 | posix_openat(PyObject *self, PyObject *args) |
| 9863 | { |
| 9864 | PyObject *ofile; |
| 9865 | char *file; |
| 9866 | int flag, dirfd, fd; |
| 9867 | int mode = 0777; |
| 9868 | |
| 9869 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9870 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9871 | &flag, &mode)) |
| 9872 | return NULL; |
| 9873 | file = PyBytes_AsString(ofile); |
| 9874 | Py_BEGIN_ALLOW_THREADS |
| 9875 | fd = openat(dirfd, file, flag, mode); |
| 9876 | Py_END_ALLOW_THREADS |
| 9877 | Py_DECREF(ofile); |
| 9878 | if (fd < 0) |
| 9879 | return posix_error(); |
| 9880 | return PyLong_FromLong((long)fd); |
| 9881 | } |
| 9882 | #endif |
| 9883 | |
| 9884 | #ifdef HAVE_READLINKAT |
| 9885 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9886 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9887 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9888 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9889 | is interpreted relative to the current working directory."); |
| 9890 | |
| 9891 | static PyObject * |
| 9892 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9893 | { |
| 9894 | PyObject *v, *opath; |
| 9895 | char buf[MAXPATHLEN]; |
| 9896 | char *path; |
| 9897 | int n, dirfd; |
| 9898 | int arg_is_unicode = 0; |
| 9899 | |
| 9900 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9901 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9902 | return NULL; |
| 9903 | path = PyBytes_AsString(opath); |
| 9904 | v = PySequence_GetItem(args, 1); |
| 9905 | if (v == NULL) { |
| 9906 | Py_DECREF(opath); |
| 9907 | return NULL; |
| 9908 | } |
| 9909 | |
| 9910 | if (PyUnicode_Check(v)) { |
| 9911 | arg_is_unicode = 1; |
| 9912 | } |
| 9913 | Py_DECREF(v); |
| 9914 | |
| 9915 | Py_BEGIN_ALLOW_THREADS |
| 9916 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9917 | Py_END_ALLOW_THREADS |
| 9918 | Py_DECREF(opath); |
| 9919 | if (n < 0) |
| 9920 | return posix_error(); |
| 9921 | |
| 9922 | if (arg_is_unicode) |
| 9923 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9924 | else |
| 9925 | return PyBytes_FromStringAndSize(buf, n); |
| 9926 | } |
| 9927 | #endif /* HAVE_READLINKAT */ |
| 9928 | |
| 9929 | #ifdef HAVE_RENAMEAT |
| 9930 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9931 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9932 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9933 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9934 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9935 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9936 | also applies for newpath."); |
| 9937 | |
| 9938 | static PyObject * |
| 9939 | posix_renameat(PyObject *self, PyObject *args) |
| 9940 | { |
| 9941 | int res; |
| 9942 | PyObject *opathold, *opathnew; |
| 9943 | char *opath, *npath; |
| 9944 | int oldfd, newfd; |
| 9945 | |
| 9946 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9947 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9948 | return NULL; |
| 9949 | opath = PyBytes_AsString(opathold); |
| 9950 | npath = PyBytes_AsString(opathnew); |
| 9951 | Py_BEGIN_ALLOW_THREADS |
| 9952 | res = renameat(oldfd, opath, newfd, npath); |
| 9953 | Py_END_ALLOW_THREADS |
| 9954 | Py_DECREF(opathold); |
| 9955 | Py_DECREF(opathnew); |
| 9956 | if (res < 0) |
| 9957 | return posix_error(); |
| 9958 | Py_RETURN_NONE; |
| 9959 | } |
| 9960 | #endif |
| 9961 | |
| 9962 | #if HAVE_SYMLINKAT |
| 9963 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9964 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9965 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9966 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9967 | is interpreted relative to the current working directory."); |
| 9968 | |
| 9969 | static PyObject * |
| 9970 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9971 | { |
| 9972 | int res, dstfd; |
| 9973 | PyObject *osrc, *odst; |
| 9974 | char *src, *dst; |
| 9975 | |
| 9976 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9977 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9978 | return NULL; |
| 9979 | src = PyBytes_AsString(osrc); |
| 9980 | dst = PyBytes_AsString(odst); |
| 9981 | Py_BEGIN_ALLOW_THREADS |
| 9982 | res = symlinkat(src, dstfd, dst); |
| 9983 | Py_END_ALLOW_THREADS |
| 9984 | Py_DECREF(osrc); |
| 9985 | Py_DECREF(odst); |
| 9986 | if (res < 0) |
| 9987 | return posix_error(); |
| 9988 | Py_RETURN_NONE; |
| 9989 | } |
| 9990 | #endif /* HAVE_SYMLINKAT */ |
| 9991 | |
| 9992 | #ifdef HAVE_UNLINKAT |
| 9993 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9994 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9995 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9996 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9997 | specified, unlinkat() behaves like rmdir().\n\ |
| 9998 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9999 | is interpreted relative to the current working directory."); |
| 10000 | |
| 10001 | static PyObject * |
| 10002 | posix_unlinkat(PyObject *self, PyObject *args) |
| 10003 | { |
| 10004 | int dirfd, res, flags = 0; |
| 10005 | PyObject *opath; |
| 10006 | char *path; |
| 10007 | |
| 10008 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 10009 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 10010 | return NULL; |
| 10011 | path = PyBytes_AsString(opath); |
| 10012 | Py_BEGIN_ALLOW_THREADS |
| 10013 | res = unlinkat(dirfd, path, flags); |
| 10014 | Py_END_ALLOW_THREADS |
| 10015 | Py_DECREF(opath); |
| 10016 | if (res < 0) |
| 10017 | return posix_error(); |
| 10018 | Py_RETURN_NONE; |
| 10019 | } |
| 10020 | #endif |
| 10021 | |
| 10022 | #ifdef HAVE_UTIMENSAT |
| 10023 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 10024 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 10025 | (mtime_sec, mtime_nsec), flags)\n\ |
| 10026 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 10027 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 10028 | relative, it is taken as relative to dirfd.\n\ |
| 10029 | The second form sets atime and mtime to the current time.\n\ |
| 10030 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 10031 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10032 | is interpreted relative to the current working directory.\n\ |
| 10033 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 10034 | current time.\n\ |
| 10035 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 10036 | |
| 10037 | static PyObject * |
| 10038 | posix_utimensat(PyObject *self, PyObject *args) |
| 10039 | { |
| 10040 | PyObject *opath; |
| 10041 | char *path; |
| 10042 | int res, dirfd, flags = 0; |
| 10043 | PyObject *atime, *mtime; |
| 10044 | |
| 10045 | struct timespec buf[2]; |
| 10046 | |
| 10047 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 10048 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10049 | return NULL; |
| 10050 | path = PyBytes_AsString(opath); |
| 10051 | if (atime == Py_None && mtime == Py_None) { |
| 10052 | /* optional time values not given */ |
| 10053 | Py_BEGIN_ALLOW_THREADS |
| 10054 | res = utimensat(dirfd, path, NULL, flags); |
| 10055 | Py_END_ALLOW_THREADS |
| 10056 | } |
| 10057 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10058 | PyErr_SetString(PyExc_TypeError, |
| 10059 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10060 | Py_DECREF(opath); |
| 10061 | return NULL; |
| 10062 | } |
| 10063 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10064 | PyErr_SetString(PyExc_TypeError, |
| 10065 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10066 | Py_DECREF(opath); |
| 10067 | return NULL; |
| 10068 | } |
| 10069 | else { |
| 10070 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10071 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10072 | Py_DECREF(opath); |
| 10073 | return NULL; |
| 10074 | } |
| 10075 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10076 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10077 | Py_DECREF(opath); |
| 10078 | return NULL; |
| 10079 | } |
| 10080 | Py_BEGIN_ALLOW_THREADS |
| 10081 | res = utimensat(dirfd, path, buf, flags); |
| 10082 | Py_END_ALLOW_THREADS |
| 10083 | } |
| 10084 | Py_DECREF(opath); |
| 10085 | if (res < 0) { |
| 10086 | return posix_error(); |
| 10087 | } |
| 10088 | Py_RETURN_NONE; |
| 10089 | } |
| 10090 | #endif |
| 10091 | |
| 10092 | #ifdef HAVE_MKFIFOAT |
| 10093 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10094 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10095 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10096 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10097 | is interpreted relative to the current working directory."); |
| 10098 | |
| 10099 | static PyObject * |
| 10100 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10101 | { |
| 10102 | PyObject *opath; |
| 10103 | char *filename; |
| 10104 | int mode = 0666; |
| 10105 | int res, dirfd; |
| 10106 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10107 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10108 | return NULL; |
| 10109 | filename = PyBytes_AS_STRING(opath); |
| 10110 | Py_BEGIN_ALLOW_THREADS |
| 10111 | res = mkfifoat(dirfd, filename, mode); |
| 10112 | Py_END_ALLOW_THREADS |
| 10113 | Py_DECREF(opath); |
| 10114 | if (res < 0) |
| 10115 | return posix_error(); |
| 10116 | Py_RETURN_NONE; |
| 10117 | } |
| 10118 | #endif |
| 10119 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10120 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10121 | |
| 10122 | static int |
| 10123 | try_getxattr(const char *path, const char *name, |
| 10124 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10125 | Py_ssize_t buf_size, PyObject **res) |
| 10126 | { |
| 10127 | PyObject *value; |
| 10128 | Py_ssize_t len; |
| 10129 | |
| 10130 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10131 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10132 | if (!value) |
| 10133 | return 0; |
| 10134 | Py_BEGIN_ALLOW_THREADS; |
| 10135 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10136 | Py_END_ALLOW_THREADS; |
| 10137 | if (len < 0) { |
| 10138 | Py_DECREF(value); |
| 10139 | if (errno == ERANGE) { |
| 10140 | value = NULL; |
| 10141 | } |
| 10142 | else { |
| 10143 | posix_error(); |
| 10144 | return 0; |
| 10145 | } |
| 10146 | } |
| 10147 | else if (len != buf_size) { |
| 10148 | /* Can only shrink. */ |
| 10149 | _PyBytes_Resize(&value, len); |
| 10150 | } |
| 10151 | *res = value; |
| 10152 | return 1; |
| 10153 | } |
| 10154 | |
| 10155 | static PyObject * |
| 10156 | getxattr_common(const char *path, PyObject *name_obj, |
| 10157 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10158 | { |
| 10159 | PyObject *value; |
| 10160 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10161 | |
| 10162 | /* Try a small value first. */ |
| 10163 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10164 | return NULL; |
| 10165 | if (value) |
| 10166 | return value; |
| 10167 | /* Now the maximum possible one. */ |
| 10168 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10169 | return NULL; |
| 10170 | assert(value); |
| 10171 | return value; |
| 10172 | } |
| 10173 | |
| 10174 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10175 | "getxattr(path, attr) -> value\n\n\ |
| 10176 | Return the value of extended attribute *name* on *path*."); |
| 10177 | |
| 10178 | static PyObject * |
| 10179 | posix_getxattr(PyObject *self, PyObject *args) |
| 10180 | { |
| 10181 | PyObject *path, *res, *name; |
| 10182 | |
| 10183 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10184 | PyUnicode_FSConverter, &name)) |
| 10185 | return NULL; |
| 10186 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10187 | Py_DECREF(path); |
| 10188 | Py_DECREF(name); |
| 10189 | return res; |
| 10190 | } |
| 10191 | |
| 10192 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10193 | "lgetxattr(path, attr) -> value\n\n\ |
| 10194 | Like getxattr but don't follow symlinks."); |
| 10195 | |
| 10196 | static PyObject * |
| 10197 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10198 | { |
| 10199 | PyObject *path, *res, *name; |
| 10200 | |
| 10201 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10202 | PyUnicode_FSConverter, &name)) |
| 10203 | return NULL; |
| 10204 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10205 | Py_DECREF(path); |
| 10206 | Py_DECREF(name); |
| 10207 | return res; |
| 10208 | } |
| 10209 | |
| 10210 | static ssize_t |
| 10211 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10212 | { |
| 10213 | /* Hack to share code. */ |
| 10214 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10215 | } |
| 10216 | |
| 10217 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10218 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10219 | Like getxattr but operate on a fd instead of a path."); |
| 10220 | |
| 10221 | static PyObject * |
| 10222 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10223 | { |
| 10224 | PyObject *res, *name; |
| 10225 | int fd; |
| 10226 | |
| 10227 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10228 | return NULL; |
| 10229 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10230 | Py_DECREF(name); |
| 10231 | return res; |
| 10232 | } |
| 10233 | |
| 10234 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10235 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10236 | Set extended attribute *attr* on *path* to *value*."); |
| 10237 | |
| 10238 | static PyObject * |
| 10239 | posix_setxattr(PyObject *self, PyObject *args) |
| 10240 | { |
| 10241 | PyObject *path, *name; |
| 10242 | Py_buffer data; |
| 10243 | int flags = 0, err; |
| 10244 | |
| 10245 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10246 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10247 | return NULL; |
| 10248 | Py_BEGIN_ALLOW_THREADS; |
| 10249 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10250 | data.buf, data.len, flags); |
| 10251 | Py_END_ALLOW_THREADS; |
| 10252 | Py_DECREF(path); |
| 10253 | Py_DECREF(name); |
| 10254 | PyBuffer_Release(&data); |
| 10255 | if (err) |
| 10256 | return posix_error(); |
| 10257 | Py_RETURN_NONE; |
| 10258 | } |
| 10259 | |
| 10260 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10261 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10262 | Like setxattr but don't follow symlinks."); |
| 10263 | |
| 10264 | static PyObject * |
| 10265 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10266 | { |
| 10267 | PyObject *path, *name; |
| 10268 | Py_buffer data; |
| 10269 | int flags = 0, err; |
| 10270 | |
| 10271 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10272 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10273 | return NULL; |
| 10274 | Py_BEGIN_ALLOW_THREADS; |
| 10275 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10276 | data.buf, data.len, flags); |
| 10277 | Py_END_ALLOW_THREADS; |
| 10278 | Py_DECREF(path); |
| 10279 | Py_DECREF(name); |
| 10280 | PyBuffer_Release(&data); |
| 10281 | if (err) |
| 10282 | return posix_error(); |
| 10283 | Py_RETURN_NONE; |
| 10284 | } |
| 10285 | |
| 10286 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10287 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10288 | Like setxattr but operates on *fd* instead of a path."); |
| 10289 | |
| 10290 | static PyObject * |
| 10291 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10292 | { |
| 10293 | Py_buffer data; |
| 10294 | const char *name; |
| 10295 | int fd, flags = 0, err; |
| 10296 | |
| 10297 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10298 | &name, &data, &flags)) |
| 10299 | return NULL; |
| 10300 | Py_BEGIN_ALLOW_THREADS; |
| 10301 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10302 | Py_END_ALLOW_THREADS; |
| 10303 | Py_DECREF(name); |
| 10304 | PyBuffer_Release(&data); |
| 10305 | if (err) |
| 10306 | return posix_error(); |
| 10307 | Py_RETURN_NONE; |
| 10308 | } |
| 10309 | |
| 10310 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10311 | "removexattr(path, attr)\n\n\ |
| 10312 | Remove extended attribute *attr* on *path*."); |
| 10313 | |
| 10314 | static PyObject * |
| 10315 | posix_removexattr(PyObject *self, PyObject *args) |
| 10316 | { |
| 10317 | PyObject *path, *name; |
| 10318 | int err; |
| 10319 | |
| 10320 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10321 | PyUnicode_FSConverter, &name)) |
| 10322 | return NULL; |
| 10323 | Py_BEGIN_ALLOW_THREADS; |
| 10324 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10325 | Py_END_ALLOW_THREADS; |
| 10326 | Py_DECREF(path); |
| 10327 | Py_DECREF(name); |
| 10328 | if (err) |
| 10329 | return posix_error(); |
| 10330 | Py_RETURN_NONE; |
| 10331 | } |
| 10332 | |
| 10333 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10334 | "lremovexattr(path, attr)\n\n\ |
| 10335 | Like removexattr but don't follow symlinks."); |
| 10336 | |
| 10337 | static PyObject * |
| 10338 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10339 | { |
| 10340 | PyObject *path, *name; |
| 10341 | int err; |
| 10342 | |
| 10343 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10344 | PyUnicode_FSConverter, &name)) |
| 10345 | return NULL; |
| 10346 | Py_BEGIN_ALLOW_THREADS; |
| 10347 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10348 | Py_END_ALLOW_THREADS; |
| 10349 | Py_DECREF(path); |
| 10350 | Py_DECREF(name); |
| 10351 | if (err) |
| 10352 | return posix_error(); |
| 10353 | Py_RETURN_NONE; |
| 10354 | } |
| 10355 | |
| 10356 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10357 | "fremovexattr(fd, attr)\n\n\ |
| 10358 | Like removexattr but operates on a file descriptor."); |
| 10359 | |
| 10360 | static PyObject * |
| 10361 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10362 | { |
| 10363 | PyObject *name; |
| 10364 | int fd, err; |
| 10365 | |
| 10366 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10367 | PyUnicode_FSConverter, &name)) |
| 10368 | return NULL; |
| 10369 | Py_BEGIN_ALLOW_THREADS; |
| 10370 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10371 | Py_END_ALLOW_THREADS; |
| 10372 | Py_DECREF(name); |
| 10373 | if (err) |
| 10374 | return posix_error(); |
| 10375 | Py_RETURN_NONE; |
| 10376 | } |
| 10377 | |
| 10378 | static Py_ssize_t |
| 10379 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10380 | Py_ssize_t buf_size, char **buf) |
| 10381 | { |
| 10382 | Py_ssize_t len; |
| 10383 | |
| 10384 | *buf = PyMem_MALLOC(buf_size); |
| 10385 | if (!*buf) { |
| 10386 | PyErr_NoMemory(); |
| 10387 | return -1; |
| 10388 | } |
| 10389 | Py_BEGIN_ALLOW_THREADS; |
| 10390 | len = list(path, *buf, buf_size); |
| 10391 | Py_END_ALLOW_THREADS; |
| 10392 | if (len < 0) { |
| 10393 | PyMem_FREE(*buf); |
| 10394 | if (errno != ERANGE) |
| 10395 | posix_error(); |
| 10396 | return -1; |
| 10397 | } |
| 10398 | return len; |
| 10399 | } |
| 10400 | |
| 10401 | static PyObject * |
| 10402 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10403 | { |
| 10404 | PyObject *res, *attr; |
| 10405 | Py_ssize_t len, err, start, i; |
| 10406 | char *buf; |
| 10407 | |
| 10408 | len = try_listxattr(path, list, 256, &buf); |
| 10409 | if (len < 0) { |
| 10410 | if (PyErr_Occurred()) |
| 10411 | return NULL; |
| 10412 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10413 | if (len < 0) |
| 10414 | return NULL; |
| 10415 | } |
| 10416 | res = PyList_New(0); |
| 10417 | if (!res) { |
| 10418 | PyMem_FREE(buf); |
| 10419 | return NULL; |
| 10420 | } |
| 10421 | for (start = i = 0; i < len; i++) { |
| 10422 | if (!buf[i]) { |
| 10423 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10424 | if (!attr) { |
| 10425 | Py_DECREF(res); |
| 10426 | PyMem_FREE(buf); |
| 10427 | return NULL; |
| 10428 | } |
| 10429 | err = PyList_Append(res, attr); |
| 10430 | Py_DECREF(attr); |
| 10431 | if (err) { |
| 10432 | Py_DECREF(res); |
| 10433 | PyMem_FREE(buf); |
| 10434 | return NULL; |
| 10435 | } |
| 10436 | start = i + 1; |
| 10437 | } |
| 10438 | } |
| 10439 | PyMem_FREE(buf); |
| 10440 | return res; |
| 10441 | } |
| 10442 | |
| 10443 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10444 | "listxattr(path)\n\n\ |
| 10445 | Return a list of extended attributes on *path*."); |
| 10446 | |
| 10447 | static PyObject * |
| 10448 | posix_listxattr(PyObject *self, PyObject *args) |
| 10449 | { |
| 10450 | PyObject *path, *res; |
| 10451 | |
| 10452 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10453 | return NULL; |
| 10454 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10455 | Py_DECREF(path); |
| 10456 | return res; |
| 10457 | } |
| 10458 | |
| 10459 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10460 | "llistxattr(path)\n\n\ |
| 10461 | Like listxattr but don't follow symlinks.."); |
| 10462 | |
| 10463 | static PyObject * |
| 10464 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10465 | { |
| 10466 | PyObject *path, *res; |
| 10467 | |
| 10468 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10469 | return NULL; |
| 10470 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10471 | Py_DECREF(path); |
| 10472 | return res; |
| 10473 | } |
| 10474 | |
| 10475 | static ssize_t |
| 10476 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10477 | { |
| 10478 | /* Hack to share code. */ |
| 10479 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10480 | } |
| 10481 | |
| 10482 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10483 | "flistxattr(path)\n\n\ |
| 10484 | Like flistxattr but operates on a file descriptor."); |
| 10485 | |
| 10486 | static PyObject * |
| 10487 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10488 | { |
| 10489 | long fd; |
| 10490 | |
| 10491 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10492 | return NULL; |
| 10493 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10494 | } |
| 10495 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10496 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10497 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10498 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10499 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10500 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10501 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10502 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10504 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10505 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10506 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10508 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10509 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10510 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10511 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10513 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10514 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10515 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10516 | #endif /* HAVE_LCHMOD */ |
| 10517 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10518 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10519 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10520 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10522 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10523 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10525 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10526 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10527 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10528 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10529 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10530 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10531 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10532 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10533 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10534 | METH_NOARGS, posix_getcwd__doc__}, |
| 10535 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10536 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10537 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10538 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10540 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10542 | #ifdef HAVE_FDOPENDIR |
| 10543 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10544 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10545 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10546 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10547 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10549 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10550 | #ifdef HAVE_GETPRIORITY |
| 10551 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10552 | #endif /* HAVE_GETPRIORITY */ |
| 10553 | #ifdef HAVE_SETPRIORITY |
| 10554 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10555 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10556 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10557 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10558 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10559 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10560 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10561 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10562 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10563 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10564 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10565 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10566 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10568 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10569 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10570 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10571 | win_symlink__doc__}, |
| 10572 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10573 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10574 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10575 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10577 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10579 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10581 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10582 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10583 | #ifdef HAVE_FUTIMES |
| 10584 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10585 | #endif |
| 10586 | #ifdef HAVE_LUTIMES |
| 10587 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10588 | #endif |
| 10589 | #ifdef HAVE_FUTIMENS |
| 10590 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10591 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10592 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10593 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10594 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10596 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10597 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10598 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10599 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10600 | #ifdef HAVE_FEXECVE |
| 10601 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10602 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10603 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10605 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10606 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10608 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10609 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10610 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10611 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10612 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10613 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10614 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10615 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10616 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10617 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10618 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10619 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10620 | {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__}, |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10621 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10622 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10623 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10624 | #endif |
| 10625 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10626 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10627 | #endif |
| 10628 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10629 | {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10630 | #endif |
| 10631 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10632 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10633 | #endif |
| 10634 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10635 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10636 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10637 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10638 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10639 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10640 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10641 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10642 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10643 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10645 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10646 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10647 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10648 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10649 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10651 | #endif /* HAVE_GETEGID */ |
| 10652 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10653 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10654 | #endif /* HAVE_GETEUID */ |
| 10655 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10656 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10657 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10658 | #ifdef HAVE_GETGROUPLIST |
| 10659 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10660 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10661 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10663 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10664 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10665 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10667 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10668 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10670 | #endif /* HAVE_GETPPID */ |
| 10671 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10673 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10674 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10676 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10677 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10679 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10680 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10682 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10683 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10685 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10686 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10688 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10689 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10690 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10691 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10692 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10693 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10694 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10695 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10696 | #endif /* HAVE_SETEUID */ |
| 10697 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10698 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10699 | #endif /* HAVE_SETEGID */ |
| 10700 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10701 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10702 | #endif /* HAVE_SETREUID */ |
| 10703 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10704 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10705 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10706 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10707 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10708 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10709 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10711 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10712 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10714 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10715 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10716 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10717 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10718 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10719 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10720 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10721 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10723 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10724 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10726 | #endif /* HAVE_WAIT3 */ |
| 10727 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10729 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10730 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10731 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10732 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10733 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10735 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10736 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10737 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10738 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10739 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10741 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10742 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10744 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10745 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10747 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10748 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10750 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10752 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10753 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10754 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10755 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10756 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10757 | #ifdef HAVE_LOCKF |
| 10758 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10759 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10760 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10761 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10762 | #ifdef HAVE_READV |
| 10763 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10764 | #endif |
| 10765 | #ifdef HAVE_PREAD |
| 10766 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10767 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10768 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10769 | #ifdef HAVE_WRITEV |
| 10770 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10771 | #endif |
| 10772 | #ifdef HAVE_PWRITE |
| 10773 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10774 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10775 | #ifdef HAVE_SENDFILE |
| 10776 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10777 | posix_sendfile__doc__}, |
| 10778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10779 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10780 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10781 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10783 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10784 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10785 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10786 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10787 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10789 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10790 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10792 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10793 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10795 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10796 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10797 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10798 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10800 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10801 | #ifdef HAVE_TRUNCATE |
| 10802 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10803 | #endif |
| 10804 | #ifdef HAVE_POSIX_FALLOCATE |
| 10805 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10806 | #endif |
| 10807 | #ifdef HAVE_POSIX_FADVISE |
| 10808 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10809 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10810 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10811 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10812 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10813 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10815 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10816 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10817 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10819 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10820 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10822 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10823 | #ifdef HAVE_SYNC |
| 10824 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10825 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10826 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10828 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10829 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10830 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10831 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10832 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10833 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10834 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10835 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10836 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10837 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10838 | #endif /* WIFSTOPPED */ |
| 10839 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10840 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10841 | #endif /* WIFSIGNALED */ |
| 10842 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10843 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10844 | #endif /* WIFEXITED */ |
| 10845 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10846 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10847 | #endif /* WEXITSTATUS */ |
| 10848 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10849 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10850 | #endif /* WTERMSIG */ |
| 10851 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10852 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10853 | #endif /* WSTOPSIG */ |
| 10854 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10855 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10857 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10858 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10859 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10860 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10861 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10862 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10863 | #endif |
| 10864 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10866 | #endif |
| 10867 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10868 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10869 | #endif |
| 10870 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10872 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10874 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10875 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10876 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10877 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10878 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10879 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10880 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10881 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10883 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10884 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10886 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10887 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10889 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10890 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10892 | #endif |
| 10893 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10895 | #endif |
| 10896 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10901 | #endif |
| 10902 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10903 | /* posix *at family of functions */ |
| 10904 | #ifdef HAVE_FACCESSAT |
| 10905 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10906 | #endif |
| 10907 | #ifdef HAVE_FCHMODAT |
| 10908 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10909 | #endif /* HAVE_FCHMODAT */ |
| 10910 | #ifdef HAVE_FCHOWNAT |
| 10911 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10912 | #endif /* HAVE_FCHOWNAT */ |
| 10913 | #ifdef HAVE_FSTATAT |
| 10914 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10915 | #endif |
| 10916 | #ifdef HAVE_FUTIMESAT |
| 10917 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10918 | #endif |
| 10919 | #ifdef HAVE_LINKAT |
| 10920 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10921 | #endif /* HAVE_LINKAT */ |
| 10922 | #ifdef HAVE_MKDIRAT |
| 10923 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10924 | #endif |
| 10925 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10926 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10927 | #endif |
| 10928 | #ifdef HAVE_OPENAT |
| 10929 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10930 | #endif |
| 10931 | #ifdef HAVE_READLINKAT |
| 10932 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10933 | #endif /* HAVE_READLINKAT */ |
| 10934 | #ifdef HAVE_RENAMEAT |
| 10935 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10936 | #endif |
| 10937 | #if HAVE_SYMLINKAT |
| 10938 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10939 | #endif /* HAVE_SYMLINKAT */ |
| 10940 | #ifdef HAVE_UNLINKAT |
| 10941 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10942 | #endif |
| 10943 | #ifdef HAVE_UTIMENSAT |
| 10944 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 10945 | #endif |
| 10946 | #ifdef HAVE_MKFIFOAT |
| 10947 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10948 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10949 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10950 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10951 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10952 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10953 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10954 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10955 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10956 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10957 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10958 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10959 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10960 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10961 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10962 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10964 | }; |
| 10965 | |
| 10966 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10967 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10968 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10969 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10971 | } |
| 10972 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10973 | #if defined(PYOS_OS2) |
| 10974 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10975 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10976 | { |
| 10977 | APIRET rc; |
| 10978 | ULONG values[QSV_MAX+1]; |
| 10979 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10980 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10981 | |
| 10982 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10983 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10984 | Py_END_ALLOW_THREADS |
| 10985 | |
| 10986 | if (rc != NO_ERROR) { |
| 10987 | os2_error(rc); |
| 10988 | return -1; |
| 10989 | } |
| 10990 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10991 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10992 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10993 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10994 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10995 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10996 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10997 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10998 | |
| 10999 | switch (values[QSV_VERSION_MINOR]) { |
| 11000 | case 0: ver = "2.00"; break; |
| 11001 | case 10: ver = "2.10"; break; |
| 11002 | case 11: ver = "2.11"; break; |
| 11003 | case 30: ver = "3.00"; break; |
| 11004 | case 40: ver = "4.00"; break; |
| 11005 | case 50: ver = "5.00"; break; |
| 11006 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11007 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11008 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11009 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11010 | ver = &tmp[0]; |
| 11011 | } |
| 11012 | |
| 11013 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11014 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11015 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11016 | |
| 11017 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11018 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11019 | tmp[1] = ':'; |
| 11020 | tmp[2] = '\0'; |
| 11021 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11022 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11023 | } |
| 11024 | #endif |
| 11025 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11026 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11027 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11028 | enable_symlink() |
| 11029 | { |
| 11030 | HANDLE tok; |
| 11031 | TOKEN_PRIVILEGES tok_priv; |
| 11032 | LUID luid; |
| 11033 | int meth_idx = 0; |
| 11034 | |
| 11035 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11036 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11037 | |
| 11038 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11039 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11040 | |
| 11041 | tok_priv.PrivilegeCount = 1; |
| 11042 | tok_priv.Privileges[0].Luid = luid; |
| 11043 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11044 | |
| 11045 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11046 | sizeof(TOKEN_PRIVILEGES), |
| 11047 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11048 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11049 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11050 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11051 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11052 | } |
| 11053 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11054 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11055 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11056 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11057 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11058 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11060 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11061 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11064 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11066 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11067 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11069 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11070 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11072 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11073 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11075 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11076 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11078 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11079 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11081 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11082 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11084 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11085 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11087 | #endif |
| 11088 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11090 | #endif |
| 11091 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11099 | #endif |
| 11100 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11123 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11124 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11126 | #endif |
| 11127 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11129 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11130 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11132 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11133 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11135 | #endif |
| 11136 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11138 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11139 | #ifdef PRIO_PROCESS |
| 11140 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11141 | #endif |
| 11142 | #ifdef PRIO_PGRP |
| 11143 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11144 | #endif |
| 11145 | #ifdef PRIO_USER |
| 11146 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11147 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11148 | #ifdef O_CLOEXEC |
| 11149 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11150 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11151 | /* posix - constants for *at functions */ |
| 11152 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11153 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11154 | #endif |
| 11155 | #ifdef AT_EACCESS |
| 11156 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11157 | #endif |
| 11158 | #ifdef AT_FDCWD |
| 11159 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11160 | #endif |
| 11161 | #ifdef AT_REMOVEDIR |
| 11162 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11163 | #endif |
| 11164 | #ifdef AT_SYMLINK_FOLLOW |
| 11165 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11166 | #endif |
| 11167 | #ifdef UTIME_NOW |
| 11168 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11169 | #endif |
| 11170 | #ifdef UTIME_OMIT |
| 11171 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11172 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11173 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11174 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11175 | /* MS Windows */ |
| 11176 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | /* Don't inherit in child processes. */ |
| 11178 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11179 | #endif |
| 11180 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | /* Optimize for short life (keep in memory). */ |
| 11182 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11183 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11184 | #endif |
| 11185 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11186 | /* Automatically delete when last handle is closed. */ |
| 11187 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | /* Optimize for random access. */ |
| 11191 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | /* Optimize for sequential access. */ |
| 11195 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11196 | #endif |
| 11197 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11198 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11199 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11200 | /* Send a SIGIO signal whenever input or output |
| 11201 | becomes available on file descriptor */ |
| 11202 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11203 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11204 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | /* Direct disk access. */ |
| 11206 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11207 | #endif |
| 11208 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | /* Must be a directory. */ |
| 11210 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11211 | #endif |
| 11212 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11213 | /* Do not follow links. */ |
| 11214 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11215 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11216 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | /* Do not update the access time. */ |
| 11218 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11219 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11222 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11224 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11225 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11227 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11228 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11230 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11231 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11233 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11234 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11236 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11237 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11239 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11240 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11242 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11243 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11245 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11246 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11248 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11249 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11251 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11252 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11254 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11255 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11257 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11258 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11260 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11261 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11263 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11264 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11266 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11267 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11269 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11270 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11272 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11273 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11274 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11275 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11276 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11277 | #endif /* ST_RDONLY */ |
| 11278 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11279 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11280 | #endif /* ST_NOSUID */ |
| 11281 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11282 | /* FreeBSD sendfile() constants */ |
| 11283 | #ifdef SF_NODISKIO |
| 11284 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11285 | #endif |
| 11286 | #ifdef SF_MNOWAIT |
| 11287 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11288 | #endif |
| 11289 | #ifdef SF_SYNC |
| 11290 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11291 | #endif |
| 11292 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11293 | /* constants for posix_fadvise */ |
| 11294 | #ifdef POSIX_FADV_NORMAL |
| 11295 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11296 | #endif |
| 11297 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11298 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11299 | #endif |
| 11300 | #ifdef POSIX_FADV_RANDOM |
| 11301 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11302 | #endif |
| 11303 | #ifdef POSIX_FADV_NOREUSE |
| 11304 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11305 | #endif |
| 11306 | #ifdef POSIX_FADV_WILLNEED |
| 11307 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11308 | #endif |
| 11309 | #ifdef POSIX_FADV_DONTNEED |
| 11310 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11311 | #endif |
| 11312 | |
| 11313 | /* constants for waitid */ |
| 11314 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11315 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11316 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11317 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11318 | #endif |
| 11319 | #ifdef WEXITED |
| 11320 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11321 | #endif |
| 11322 | #ifdef WNOWAIT |
| 11323 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11324 | #endif |
| 11325 | #ifdef WSTOPPED |
| 11326 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11327 | #endif |
| 11328 | #ifdef CLD_EXITED |
| 11329 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11330 | #endif |
| 11331 | #ifdef CLD_DUMPED |
| 11332 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11333 | #endif |
| 11334 | #ifdef CLD_TRAPPED |
| 11335 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11336 | #endif |
| 11337 | #ifdef CLD_CONTINUED |
| 11338 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11339 | #endif |
| 11340 | |
| 11341 | /* constants for lockf */ |
| 11342 | #ifdef F_LOCK |
| 11343 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11344 | #endif |
| 11345 | #ifdef F_TLOCK |
| 11346 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11347 | #endif |
| 11348 | #ifdef F_ULOCK |
| 11349 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11350 | #endif |
| 11351 | #ifdef F_TEST |
| 11352 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11353 | #endif |
| 11354 | |
| 11355 | /* constants for futimens */ |
| 11356 | #ifdef UTIME_NOW |
| 11357 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11358 | #endif |
| 11359 | #ifdef UTIME_OMIT |
| 11360 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11361 | #endif |
| 11362 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11363 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11364 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11366 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11367 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11368 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11369 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11370 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11371 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11372 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11373 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11374 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11375 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11376 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11377 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11378 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11379 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11380 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11381 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11382 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11383 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11384 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11385 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11387 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11388 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11389 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11390 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11391 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11392 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11393 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11394 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11395 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11396 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11397 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11398 | #ifdef SCHED_SPORADIC |
| 11399 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11400 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11401 | #ifdef SCHED_BATCH |
| 11402 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11403 | #endif |
| 11404 | #ifdef SCHED_IDLE |
| 11405 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11406 | #endif |
| 11407 | #ifdef SCHED_RESET_ON_FORK |
| 11408 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11409 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11410 | #ifdef SCHED_SYS |
| 11411 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11412 | #endif |
| 11413 | #ifdef SCHED_IA |
| 11414 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11415 | #endif |
| 11416 | #ifdef SCHED_FSS |
| 11417 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11418 | #endif |
| 11419 | #ifdef SCHED_FX |
| 11420 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11421 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11422 | #endif |
| 11423 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11424 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11425 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11426 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11427 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11428 | #endif |
| 11429 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11430 | #ifdef RTLD_LAZY |
| 11431 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11432 | #endif |
| 11433 | #ifdef RTLD_NOW |
| 11434 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11435 | #endif |
| 11436 | #ifdef RTLD_GLOBAL |
| 11437 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11438 | #endif |
| 11439 | #ifdef RTLD_LOCAL |
| 11440 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11441 | #endif |
| 11442 | #ifdef RTLD_NODELETE |
| 11443 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11444 | #endif |
| 11445 | #ifdef RTLD_NOLOAD |
| 11446 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11447 | #endif |
| 11448 | #ifdef RTLD_DEEPBIND |
| 11449 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11450 | #endif |
| 11451 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11452 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11453 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11454 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11456 | } |
| 11457 | |
| 11458 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11459 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11460 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11461 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11462 | |
| 11463 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11464 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11465 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11466 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11467 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11468 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11469 | #define MODNAME "posix" |
| 11470 | #endif |
| 11471 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11472 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | PyModuleDef_HEAD_INIT, |
| 11474 | MODNAME, |
| 11475 | posix__doc__, |
| 11476 | -1, |
| 11477 | posix_methods, |
| 11478 | NULL, |
| 11479 | NULL, |
| 11480 | NULL, |
| 11481 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11482 | }; |
| 11483 | |
| 11484 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11485 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11486 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11487 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11489 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11490 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11491 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11492 | #endif |
| 11493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | m = PyModule_Create(&posixmodule); |
| 11495 | if (m == NULL) |
| 11496 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11497 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | /* Initialize environ dictionary */ |
| 11499 | v = convertenviron(); |
| 11500 | Py_XINCREF(v); |
| 11501 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11502 | return NULL; |
| 11503 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11504 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | if (all_ins(m)) |
| 11506 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | if (setup_confname_tables(m)) |
| 11509 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11510 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | Py_INCREF(PyExc_OSError); |
| 11512 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11513 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11514 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11515 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11516 | return NULL; |
| 11517 | Py_INCREF(&cpu_set_type); |
| 11518 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11519 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11520 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11521 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11522 | if (posix_putenv_garbage == NULL) |
| 11523 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11524 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11525 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11527 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11528 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11529 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11530 | #endif |
| 11531 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11533 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11534 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11535 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11536 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11537 | structseq_new = StatResultType.tp_new; |
| 11538 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11541 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11542 | #ifdef NEED_TICKS_PER_SECOND |
| 11543 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11544 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11545 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11546 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11547 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11549 | # endif |
| 11550 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11551 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11552 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11553 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11554 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11555 | SchedParamType.tp_new = sched_param_new; |
| 11556 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11558 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11559 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11560 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11561 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11562 | Py_INCREF((PyObject*) &StatResultType); |
| 11563 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11564 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11565 | PyModule_AddObject(m, "statvfs_result", |
| 11566 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11567 | |
| 11568 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11569 | Py_INCREF(&SchedParamType); |
| 11570 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11571 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11573 | |
| 11574 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11575 | /* |
| 11576 | * Step 2 of weak-linking support on Mac OS X. |
| 11577 | * |
| 11578 | * The code below removes functions that are not available on the |
| 11579 | * currently active platform. |
| 11580 | * |
| 11581 | * This block allow one to use a python binary that was build on |
| 11582 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11583 | * OSX 10.4. |
| 11584 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11585 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11586 | if (fstatvfs == NULL) { |
| 11587 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11588 | return NULL; |
| 11589 | } |
| 11590 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11591 | #endif /* HAVE_FSTATVFS */ |
| 11592 | |
| 11593 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11594 | if (statvfs == NULL) { |
| 11595 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11596 | return NULL; |
| 11597 | } |
| 11598 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11599 | #endif /* HAVE_STATVFS */ |
| 11600 | |
| 11601 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11602 | if (lchown == NULL) { |
| 11603 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11604 | return NULL; |
| 11605 | } |
| 11606 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11607 | #endif /* HAVE_LCHOWN */ |
| 11608 | |
| 11609 | |
| 11610 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11611 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11612 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11613 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11614 | |
| 11615 | #ifdef __cplusplus |
| 11616 | } |
| 11617 | #endif |