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 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 125 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 126 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 127 | #include <process.h> |
| 128 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 129 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_GETCWD 1 |
| 131 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 132 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #if defined(__OS2__) |
| 134 | #define HAVE_EXECV 1 |
| 135 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 136 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #include <process.h> |
| 138 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 139 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #define HAVE_EXECV 1 |
| 141 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #define HAVE_OPENDIR 1 |
| 143 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #define HAVE_WAIT 1 |
| 146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 148 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 149 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 150 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 152 | #define HAVE_EXECV 1 |
| 153 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 154 | #define HAVE_SYSTEM 1 |
| 155 | #define HAVE_CWAIT 1 |
| 156 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 157 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 158 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 159 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 160 | /* 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] | 161 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | /* Unix functions that the configure script doesn't check for */ |
| 163 | #define HAVE_EXECV 1 |
| 164 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 166 | #define HAVE_FORK1 1 |
| 167 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #define HAVE_GETCWD 1 |
| 169 | #define HAVE_GETEGID 1 |
| 170 | #define HAVE_GETEUID 1 |
| 171 | #define HAVE_GETGID 1 |
| 172 | #define HAVE_GETPPID 1 |
| 173 | #define HAVE_GETUID 1 |
| 174 | #define HAVE_KILL 1 |
| 175 | #define HAVE_OPENDIR 1 |
| 176 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 177 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 179 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 180 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #endif /* _MSC_VER */ |
| 182 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 183 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 184 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 186 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 187 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 188 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 189 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 190 | (default) */ |
| 191 | extern char *ctermid_r(char *); |
| 192 | #endif |
| 193 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 194 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 195 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 197 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 198 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 199 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 202 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 203 | #endif |
| 204 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 205 | extern int chdir(char *); |
| 206 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int chdir(const char *); |
| 209 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 210 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 211 | #ifdef __BORLANDC__ |
| 212 | extern int chmod(const char *, int); |
| 213 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 214 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 215 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 216 | /*#ifdef HAVE_FCHMOD |
| 217 | extern int fchmod(int, mode_t); |
| 218 | #endif*/ |
| 219 | /*#ifdef HAVE_LCHMOD |
| 220 | extern int lchmod(const char *, mode_t); |
| 221 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 222 | extern int chown(const char *, uid_t, gid_t); |
| 223 | extern char *getcwd(char *, int); |
| 224 | extern char *strerror(int); |
| 225 | extern int link(const char *, const char *); |
| 226 | extern int rename(const char *, const char *); |
| 227 | extern int stat(const char *, struct stat *); |
| 228 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 230 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 233 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 234 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #ifdef HAVE_UTIME_H |
| 240 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 241 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 243 | #ifdef HAVE_SYS_UTIME_H |
| 244 | #include <sys/utime.h> |
| 245 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 246 | #endif /* HAVE_SYS_UTIME_H */ |
| 247 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #ifdef HAVE_SYS_TIMES_H |
| 249 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef HAVE_SYS_PARAM_H |
| 253 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
| 256 | #ifdef HAVE_SYS_UTSNAME_H |
| 257 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 258 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 260 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 262 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 263 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 264 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #include <direct.h> |
| 266 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 267 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 270 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #endif |
| 274 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #endif |
| 277 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 279 | #endif |
| 280 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 282 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 292 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 293 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 296 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 299 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 300 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 301 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 302 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 303 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 304 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 305 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 306 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 307 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 308 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 309 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 310 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 312 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 313 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 314 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 317 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 318 | #define MAXPATHLEN PATH_MAX |
| 319 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 322 | #endif /* MAXPATHLEN */ |
| 323 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 324 | #ifdef UNION_WAIT |
| 325 | /* Emulate some macros on systems that have a union instead of macros */ |
| 326 | |
| 327 | #ifndef WIFEXITED |
| 328 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 329 | #endif |
| 330 | |
| 331 | #ifndef WEXITSTATUS |
| 332 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WTERMSIG |
| 336 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 337 | #endif |
| 338 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 339 | #define WAIT_TYPE union wait |
| 340 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 341 | |
| 342 | #else /* !UNION_WAIT */ |
| 343 | #define WAIT_TYPE int |
| 344 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 345 | #endif /* UNION_WAIT */ |
| 346 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 347 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 348 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 349 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 350 | #define USE_CTERMID_R |
| 351 | #endif |
| 352 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 353 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 354 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 355 | #undef FSTAT |
| 356 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 357 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 358 | # define STAT win32_stat |
| 359 | # define FSTAT win32_fstat |
| 360 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT stat |
| 363 | # define FSTAT fstat |
| 364 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 367 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 368 | #include <sys/mkdev.h> |
| 369 | #else |
| 370 | #if defined(MAJOR_IN_SYSMACROS) |
| 371 | #include <sys/sysmacros.h> |
| 372 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 373 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 374 | #include <sys/mkdev.h> |
| 375 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 376 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 377 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 378 | /* A helper used by a number of POSIX-only functions */ |
| 379 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 380 | static int |
| 381 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 382 | { |
| 383 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 384 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 385 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 386 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 387 | #endif |
| 388 | if (PyErr_Occurred()) |
| 389 | return 0; |
| 390 | return 1; |
| 391 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 392 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 393 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 394 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 395 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 396 | * valid and throw an assertion if it isn't. |
| 397 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 398 | * an assertion can be useful, but it does contradict the POSIX standard |
| 399 | * which for write(2) states: |
| 400 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 401 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 402 | * writing." |
| 403 | * Furthermore, python allows the user to enter any old integer |
| 404 | * as a fd and should merely raise a python exception on error. |
| 405 | * The Microsoft CRT doesn't provide an official way to check for the |
| 406 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 407 | * 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] | 408 | * internal structures involved. |
| 409 | * The structures below must be updated for each version of visual studio |
| 410 | * according to the file internal.h in the CRT source, until MS comes |
| 411 | * up with a less hacky way to do this. |
| 412 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 413 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 414 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 415 | /* The actual size of the structure is determined at runtime. |
| 416 | * Only the first items must be present. |
| 417 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 418 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 419 | intptr_t osfhnd; |
| 420 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 421 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 422 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 423 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 424 | #define IOINFO_L2E 5 |
| 425 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 426 | #define IOINFO_ARRAYS 64 |
| 427 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 428 | #define FOPEN 0x01 |
| 429 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 430 | |
| 431 | /* This function emulates what the windows CRT does to validate file handles */ |
| 432 | int |
| 433 | _PyVerify_fd(int fd) |
| 434 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 435 | const int i1 = fd >> IOINFO_L2E; |
| 436 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 438 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 440 | /* Determine the actual size of the ioinfo structure, |
| 441 | * as used by the CRT loaded in memory |
| 442 | */ |
| 443 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 444 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 445 | } |
| 446 | if (sizeof_ioinfo == 0) { |
| 447 | /* This should not happen... */ |
| 448 | goto fail; |
| 449 | } |
| 450 | |
| 451 | /* See that it isn't a special CLEAR fileno */ |
| 452 | if (fd != _NO_CONSOLE_FILENO) { |
| 453 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 454 | * we check pointer validity and other info |
| 455 | */ |
| 456 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 457 | /* finally, check that the file is open */ |
| 458 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 459 | if (info->osfile & FOPEN) { |
| 460 | return 1; |
| 461 | } |
| 462 | } |
| 463 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 464 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 465 | errno = EBADF; |
| 466 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 470 | static int |
| 471 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 473 | if (!_PyVerify_fd(fd1)) |
| 474 | return 0; |
| 475 | if (fd2 == _NO_CONSOLE_FILENO) |
| 476 | return 0; |
| 477 | if ((unsigned)fd2 < _NHANDLE_) |
| 478 | return 1; |
| 479 | else |
| 480 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 481 | } |
| 482 | #else |
| 483 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 484 | #define _PyVerify_fd_dup2(A, B) (1) |
| 485 | #endif |
| 486 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 487 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 488 | /* The following structure was copied from |
| 489 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 490 | include doesn't seem to be present in the Windows SDK (at least as included |
| 491 | with Visual Studio Express). */ |
| 492 | typedef struct _REPARSE_DATA_BUFFER { |
| 493 | ULONG ReparseTag; |
| 494 | USHORT ReparseDataLength; |
| 495 | USHORT Reserved; |
| 496 | union { |
| 497 | struct { |
| 498 | USHORT SubstituteNameOffset; |
| 499 | USHORT SubstituteNameLength; |
| 500 | USHORT PrintNameOffset; |
| 501 | USHORT PrintNameLength; |
| 502 | ULONG Flags; |
| 503 | WCHAR PathBuffer[1]; |
| 504 | } SymbolicLinkReparseBuffer; |
| 505 | |
| 506 | struct { |
| 507 | USHORT SubstituteNameOffset; |
| 508 | USHORT SubstituteNameLength; |
| 509 | USHORT PrintNameOffset; |
| 510 | USHORT PrintNameLength; |
| 511 | WCHAR PathBuffer[1]; |
| 512 | } MountPointReparseBuffer; |
| 513 | |
| 514 | struct { |
| 515 | UCHAR DataBuffer[1]; |
| 516 | } GenericReparseBuffer; |
| 517 | }; |
| 518 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 519 | |
| 520 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 521 | GenericReparseBuffer) |
| 522 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 523 | |
| 524 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 525 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 526 | { |
| 527 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 528 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 529 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 530 | |
| 531 | if (0 == DeviceIoControl( |
| 532 | reparse_point_handle, |
| 533 | FSCTL_GET_REPARSE_POINT, |
| 534 | NULL, 0, /* in buffer */ |
| 535 | target_buffer, sizeof(target_buffer), |
| 536 | &n_bytes_returned, |
| 537 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 538 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 539 | |
| 540 | if (reparse_tag) |
| 541 | *reparse_tag = rdb->ReparseTag; |
| 542 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 543 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 544 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 545 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 548 | #ifdef WITH_NEXT_FRAMEWORK |
| 549 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 550 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 551 | */ |
| 552 | #include <crt_externs.h> |
| 553 | static char **environ; |
| 554 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 556 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 558 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 559 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 561 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 562 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 563 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 564 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 565 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 566 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 567 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 568 | APIRET rc; |
| 569 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 570 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 572 | d = PyDict_New(); |
| 573 | if (d == NULL) |
| 574 | return NULL; |
| 575 | #ifdef WITH_NEXT_FRAMEWORK |
| 576 | if (environ == NULL) |
| 577 | environ = *_NSGetEnviron(); |
| 578 | #endif |
| 579 | #ifdef MS_WINDOWS |
| 580 | /* _wenviron must be initialized in this way if the program is started |
| 581 | through main() instead of wmain(). */ |
| 582 | _wgetenv(L""); |
| 583 | if (_wenviron == NULL) |
| 584 | return d; |
| 585 | /* This part ignores errors */ |
| 586 | for (e = _wenviron; *e != NULL; e++) { |
| 587 | PyObject *k; |
| 588 | PyObject *v; |
| 589 | wchar_t *p = wcschr(*e, L'='); |
| 590 | if (p == NULL) |
| 591 | continue; |
| 592 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 593 | if (k == NULL) { |
| 594 | PyErr_Clear(); |
| 595 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 596 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 597 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 598 | if (v == NULL) { |
| 599 | PyErr_Clear(); |
| 600 | Py_DECREF(k); |
| 601 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 602 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 603 | if (PyDict_GetItem(d, k) == NULL) { |
| 604 | if (PyDict_SetItem(d, k, v) != 0) |
| 605 | PyErr_Clear(); |
| 606 | } |
| 607 | Py_DECREF(k); |
| 608 | Py_DECREF(v); |
| 609 | } |
| 610 | #else |
| 611 | if (environ == NULL) |
| 612 | return d; |
| 613 | /* This part ignores errors */ |
| 614 | for (e = environ; *e != NULL; e++) { |
| 615 | PyObject *k; |
| 616 | PyObject *v; |
| 617 | char *p = strchr(*e, '='); |
| 618 | if (p == NULL) |
| 619 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 620 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 621 | if (k == NULL) { |
| 622 | PyErr_Clear(); |
| 623 | continue; |
| 624 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 625 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 626 | if (v == NULL) { |
| 627 | PyErr_Clear(); |
| 628 | Py_DECREF(k); |
| 629 | continue; |
| 630 | } |
| 631 | if (PyDict_GetItem(d, k) == NULL) { |
| 632 | if (PyDict_SetItem(d, k, v) != 0) |
| 633 | PyErr_Clear(); |
| 634 | } |
| 635 | Py_DECREF(k); |
| 636 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 637 | } |
| 638 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 639 | #if defined(PYOS_OS2) |
| 640 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 641 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 642 | PyObject *v = PyBytes_FromString(buffer); |
| 643 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 644 | Py_DECREF(v); |
| 645 | } |
| 646 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 647 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 648 | PyObject *v = PyBytes_FromString(buffer); |
| 649 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 650 | Py_DECREF(v); |
| 651 | } |
| 652 | #endif |
| 653 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 657 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 658 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 659 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 660 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 661 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 662 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 664 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 666 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 669 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 670 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 671 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 672 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 673 | PyObject *name_str, *rc; |
| 674 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 675 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 676 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 677 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 678 | name_str); |
| 679 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 680 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 683 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 684 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 685 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 687 | /* XXX We should pass the function name along in the future. |
| 688 | (winreg.c also wants to pass the function name.) |
| 689 | This would however require an additional param to the |
| 690 | Windows error object, which is non-trivial. |
| 691 | */ |
| 692 | errno = GetLastError(); |
| 693 | if (filename) |
| 694 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 695 | else |
| 696 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 697 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 698 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 699 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 700 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 702 | /* XXX - see win32_error for comments on 'function' */ |
| 703 | errno = GetLastError(); |
| 704 | if (filename) |
| 705 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 706 | else |
| 707 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 710 | static PyObject * |
| 711 | win32_error_object(char* function, PyObject* filename) |
| 712 | { |
| 713 | /* XXX - see win32_error for comments on 'function' */ |
| 714 | errno = GetLastError(); |
| 715 | if (filename) |
| 716 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 717 | PyExc_WindowsError, |
| 718 | errno, |
| 719 | filename); |
| 720 | else |
| 721 | return PyErr_SetFromWindowsErr(errno); |
| 722 | } |
| 723 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 724 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 725 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 726 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 727 | if (PyUnicode_CheckExact(*param)) |
| 728 | Py_INCREF(*param); |
| 729 | else if (PyUnicode_Check(*param)) |
| 730 | /* For a Unicode subtype that's not a Unicode object, |
| 731 | return a true Unicode object with the same data. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame^] | 732 | *param = PyUnicode_Copy(*param); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 733 | else |
| 734 | *param = PyUnicode_FromEncodedObject(*param, |
| 735 | Py_FileSystemDefaultEncoding, |
| 736 | "strict"); |
| 737 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 740 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 742 | #if defined(PYOS_OS2) |
| 743 | /********************************************************************** |
| 744 | * Helper Function to Trim and Format OS/2 Messages |
| 745 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 746 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 747 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 748 | { |
| 749 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 750 | |
| 751 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 752 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 753 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 754 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 755 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 756 | } |
| 757 | |
| 758 | /* Add Optional Reason Text */ |
| 759 | if (reason) { |
| 760 | strcat(msgbuf, " : "); |
| 761 | strcat(msgbuf, reason); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /********************************************************************** |
| 766 | * Decode an OS/2 Operating System Error Code |
| 767 | * |
| 768 | * A convenience function to lookup an OS/2 error code and return a |
| 769 | * text message we can use to raise a Python exception. |
| 770 | * |
| 771 | * Notes: |
| 772 | * The messages for errors returned from the OS/2 kernel reside in |
| 773 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 774 | * |
| 775 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 776 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 777 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 778 | { |
| 779 | APIRET rc; |
| 780 | ULONG msglen; |
| 781 | |
| 782 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 783 | Py_BEGIN_ALLOW_THREADS |
| 784 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 785 | errorcode, "oso001.msg", &msglen); |
| 786 | Py_END_ALLOW_THREADS |
| 787 | |
| 788 | if (rc == NO_ERROR) |
| 789 | os2_formatmsg(msgbuf, msglen, reason); |
| 790 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 791 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 792 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 793 | |
| 794 | return msgbuf; |
| 795 | } |
| 796 | |
| 797 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 798 | errors are not in a global variable e.g. 'errno' nor are |
| 799 | they congruent with posix error numbers. */ |
| 800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 801 | static PyObject * |
| 802 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 803 | { |
| 804 | char text[1024]; |
| 805 | PyObject *v; |
| 806 | |
| 807 | os2_strerror(text, sizeof(text), code, ""); |
| 808 | |
| 809 | v = Py_BuildValue("(is)", code, text); |
| 810 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 811 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 812 | Py_DECREF(v); |
| 813 | } |
| 814 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 815 | } |
| 816 | |
| 817 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 818 | |
| 819 | /* POSIX generic methods */ |
| 820 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 821 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 822 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 823 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 824 | int fd; |
| 825 | int res; |
| 826 | fd = PyObject_AsFileDescriptor(fdobj); |
| 827 | if (fd < 0) |
| 828 | return NULL; |
| 829 | if (!_PyVerify_fd(fd)) |
| 830 | return posix_error(); |
| 831 | Py_BEGIN_ALLOW_THREADS |
| 832 | res = (*func)(fd); |
| 833 | Py_END_ALLOW_THREADS |
| 834 | if (res < 0) |
| 835 | return posix_error(); |
| 836 | Py_INCREF(Py_None); |
| 837 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 838 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 839 | |
| 840 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 841 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 843 | PyObject *opath1 = NULL; |
| 844 | char *path1; |
| 845 | int res; |
| 846 | if (!PyArg_ParseTuple(args, format, |
| 847 | PyUnicode_FSConverter, &opath1)) |
| 848 | return NULL; |
| 849 | path1 = PyBytes_AsString(opath1); |
| 850 | Py_BEGIN_ALLOW_THREADS |
| 851 | res = (*func)(path1); |
| 852 | Py_END_ALLOW_THREADS |
| 853 | if (res < 0) |
| 854 | return posix_error_with_allocated_filename(opath1); |
| 855 | Py_DECREF(opath1); |
| 856 | Py_INCREF(Py_None); |
| 857 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 860 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 861 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 862 | char *format, |
| 863 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 864 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 865 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 866 | char *path1, *path2; |
| 867 | int res; |
| 868 | if (!PyArg_ParseTuple(args, format, |
| 869 | PyUnicode_FSConverter, &opath1, |
| 870 | PyUnicode_FSConverter, &opath2)) { |
| 871 | return NULL; |
| 872 | } |
| 873 | path1 = PyBytes_AsString(opath1); |
| 874 | path2 = PyBytes_AsString(opath2); |
| 875 | Py_BEGIN_ALLOW_THREADS |
| 876 | res = (*func)(path1, path2); |
| 877 | Py_END_ALLOW_THREADS |
| 878 | Py_DECREF(opath1); |
| 879 | Py_DECREF(opath2); |
| 880 | if (res != 0) |
| 881 | /* XXX how to report both path1 and path2??? */ |
| 882 | return posix_error(); |
| 883 | Py_INCREF(Py_None); |
| 884 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 887 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 888 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 889 | win32_1str(PyObject* args, char* func, |
| 890 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 891 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 892 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 893 | PyObject *uni; |
| 894 | char *ansi; |
| 895 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 896 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 897 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 898 | { |
| 899 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 900 | if (wstr == NULL) |
| 901 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 902 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 903 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 904 | Py_END_ALLOW_THREADS |
| 905 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 906 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 907 | Py_INCREF(Py_None); |
| 908 | return Py_None; |
| 909 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 910 | PyErr_Clear(); |
| 911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 912 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 913 | return NULL; |
| 914 | Py_BEGIN_ALLOW_THREADS |
| 915 | result = funcA(ansi); |
| 916 | Py_END_ALLOW_THREADS |
| 917 | if (!result) |
| 918 | return win32_error(func, ansi); |
| 919 | Py_INCREF(Py_None); |
| 920 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 921 | |
| 922 | } |
| 923 | |
| 924 | /* This is a reimplementation of the C library's chdir function, |
| 925 | but one that produces Win32 errors instead of DOS error codes. |
| 926 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 927 | it also needs to set "magic" environment variables indicating |
| 928 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 929 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 930 | win32_chdir(LPCSTR path) |
| 931 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 932 | char new_path[MAX_PATH+1]; |
| 933 | int result; |
| 934 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 936 | if(!SetCurrentDirectoryA(path)) |
| 937 | return FALSE; |
| 938 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 939 | if (!result) |
| 940 | return FALSE; |
| 941 | /* In the ANSI API, there should not be any paths longer |
| 942 | than MAX_PATH. */ |
| 943 | assert(result <= MAX_PATH+1); |
| 944 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 945 | strncmp(new_path, "//", 2) == 0) |
| 946 | /* UNC path, nothing to do. */ |
| 947 | return TRUE; |
| 948 | env[1] = new_path[0]; |
| 949 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* The Unicode version differs from the ANSI version |
| 953 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 954 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 955 | win32_wchdir(LPCWSTR path) |
| 956 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 957 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 958 | int result; |
| 959 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 961 | if(!SetCurrentDirectoryW(path)) |
| 962 | return FALSE; |
| 963 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 964 | if (!result) |
| 965 | return FALSE; |
| 966 | if (result > MAX_PATH+1) { |
| 967 | new_path = malloc(result * sizeof(wchar_t)); |
| 968 | if (!new_path) { |
| 969 | SetLastError(ERROR_OUTOFMEMORY); |
| 970 | return FALSE; |
| 971 | } |
| 972 | result = GetCurrentDirectoryW(result, new_path); |
| 973 | if (!result) { |
| 974 | free(new_path); |
| 975 | return FALSE; |
| 976 | } |
| 977 | } |
| 978 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 979 | wcsncmp(new_path, L"//", 2) == 0) |
| 980 | /* UNC path, nothing to do. */ |
| 981 | return TRUE; |
| 982 | env[1] = new_path[0]; |
| 983 | result = SetEnvironmentVariableW(env, new_path); |
| 984 | if (new_path != _new_path) |
| 985 | free(new_path); |
| 986 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 987 | } |
| 988 | #endif |
| 989 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 990 | #ifdef MS_WINDOWS |
| 991 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 992 | - time stamps are restricted to second resolution |
| 993 | - file modification times suffer from forth-and-back conversions between |
| 994 | UTC and local time |
| 995 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 996 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 997 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 998 | |
| 999 | struct win32_stat{ |
| 1000 | int st_dev; |
| 1001 | __int64 st_ino; |
| 1002 | unsigned short st_mode; |
| 1003 | int st_nlink; |
| 1004 | int st_uid; |
| 1005 | int st_gid; |
| 1006 | int st_rdev; |
| 1007 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1008 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1009 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1010 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1011 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1012 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1013 | int st_ctime_nsec; |
| 1014 | }; |
| 1015 | |
| 1016 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1017 | |
| 1018 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1019 | 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] | 1020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1021 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1022 | /* Cannot simply cast and dereference in_ptr, |
| 1023 | since it might not be aligned properly */ |
| 1024 | __int64 in; |
| 1025 | memcpy(&in, in_ptr, sizeof(in)); |
| 1026 | *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] | 1027 | *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] | 1028 | } |
| 1029 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1030 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1031 | 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] | 1032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1033 | /* XXX endianness */ |
| 1034 | __int64 out; |
| 1035 | out = time_in + secs_between_epochs; |
| 1036 | out = out * 10000000 + nsec_in / 100; |
| 1037 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1040 | /* Below, we *know* that ugo+r is 0444 */ |
| 1041 | #if _S_IREAD != 0400 |
| 1042 | #error Unsupported C library |
| 1043 | #endif |
| 1044 | static int |
| 1045 | attributes_to_mode(DWORD attr) |
| 1046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1047 | int m = 0; |
| 1048 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1049 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1050 | else |
| 1051 | m |= _S_IFREG; |
| 1052 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1053 | m |= 0444; |
| 1054 | else |
| 1055 | m |= 0666; |
| 1056 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1060 | 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] | 1061 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1062 | memset(result, 0, sizeof(*result)); |
| 1063 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1064 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1065 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1066 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1067 | 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] | 1068 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1069 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1070 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1071 | /* first clear the S_IFMT bits */ |
| 1072 | result->st_mode ^= (result->st_mode & 0170000); |
| 1073 | /* now set the bits that make this a symlink */ |
| 1074 | result->st_mode |= 0120000; |
| 1075 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1077 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1080 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1081 | 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] | 1082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1083 | HANDLE hFindFile; |
| 1084 | WIN32_FIND_DATAA FileData; |
| 1085 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1086 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1087 | return FALSE; |
| 1088 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1089 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1090 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1091 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1092 | info->ftCreationTime = FileData.ftCreationTime; |
| 1093 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1094 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1095 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1096 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1097 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1098 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1099 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1100 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1104 | 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] | 1105 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1106 | HANDLE hFindFile; |
| 1107 | WIN32_FIND_DATAW FileData; |
| 1108 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1109 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1110 | return FALSE; |
| 1111 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1113 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1114 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1115 | info->ftCreationTime = FileData.ftCreationTime; |
| 1116 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1117 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1118 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1119 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1120 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1121 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1122 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1123 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1126 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1127 | static int has_GetFinalPathNameByHandle = 0; |
| 1128 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1129 | DWORD); |
| 1130 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1131 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1132 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1133 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1134 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1135 | HINSTANCE hKernel32; |
| 1136 | /* only recheck */ |
| 1137 | if (!has_GetFinalPathNameByHandle) |
| 1138 | { |
| 1139 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1140 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1141 | "GetFinalPathNameByHandleA"); |
| 1142 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1143 | "GetFinalPathNameByHandleW"); |
| 1144 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1145 | Py_GetFinalPathNameByHandleW; |
| 1146 | } |
| 1147 | return has_GetFinalPathNameByHandle; |
| 1148 | } |
| 1149 | |
| 1150 | static BOOL |
| 1151 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1152 | { |
| 1153 | int buf_size, result_length; |
| 1154 | wchar_t *buf; |
| 1155 | |
| 1156 | /* We have a good handle to the target, use it to determine |
| 1157 | the target path name (then we'll call lstat on it). */ |
| 1158 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1159 | VOLUME_NAME_DOS); |
| 1160 | if(!buf_size) |
| 1161 | return FALSE; |
| 1162 | |
| 1163 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1164 | if (!buf) { |
| 1165 | SetLastError(ERROR_OUTOFMEMORY); |
| 1166 | return FALSE; |
| 1167 | } |
| 1168 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1169 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1170 | buf, buf_size, VOLUME_NAME_DOS); |
| 1171 | |
| 1172 | if(!result_length) { |
| 1173 | free(buf); |
| 1174 | return FALSE; |
| 1175 | } |
| 1176 | |
| 1177 | if(!CloseHandle(hdl)) { |
| 1178 | free(buf); |
| 1179 | return FALSE; |
| 1180 | } |
| 1181 | |
| 1182 | buf[result_length] = 0; |
| 1183 | |
| 1184 | *target_path = buf; |
| 1185 | return TRUE; |
| 1186 | } |
| 1187 | |
| 1188 | static int |
| 1189 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1190 | BOOL traverse); |
| 1191 | static int |
| 1192 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1193 | BOOL traverse) |
| 1194 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1195 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1196 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1197 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1198 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1199 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1200 | const char *dot; |
| 1201 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1202 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1203 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1204 | traverse reparse point. */ |
| 1205 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1208 | hFile = CreateFileA( |
| 1209 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1210 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1211 | 0, /* share mode */ |
| 1212 | NULL, /* security attributes */ |
| 1213 | OPEN_EXISTING, |
| 1214 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1215 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1216 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1217 | the symlink path agin and not the actual final path. */ |
| 1218 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1219 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1220 | NULL); |
| 1221 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1222 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1223 | /* Either the target doesn't exist, or we don't have access to |
| 1224 | get a handle to it. If the former, we need to return an error. |
| 1225 | If the latter, we can use attributes_from_dir. */ |
| 1226 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1227 | return -1; |
| 1228 | /* Could not get attributes on open file. Fall back to |
| 1229 | reading the directory. */ |
| 1230 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1231 | /* Very strange. This should not fail now */ |
| 1232 | return -1; |
| 1233 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1234 | if (traverse) { |
| 1235 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1236 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1237 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1238 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1239 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1242 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1243 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1244 | } |
| 1245 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1246 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1247 | return -1; |
| 1248 | |
| 1249 | /* Close the outer open file handle now that we're about to |
| 1250 | reopen it with different flags. */ |
| 1251 | if (!CloseHandle(hFile)) |
| 1252 | return -1; |
| 1253 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1254 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1255 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1256 | the file without the reparse handling flag set. */ |
| 1257 | hFile2 = CreateFileA( |
| 1258 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1259 | NULL, OPEN_EXISTING, |
| 1260 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1261 | NULL); |
| 1262 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1263 | return -1; |
| 1264 | |
| 1265 | if (!get_target_path(hFile2, &target_path)) |
| 1266 | return -1; |
| 1267 | |
| 1268 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1269 | free(target_path); |
| 1270 | return code; |
| 1271 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1272 | } else |
| 1273 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1274 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1275 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1278 | dot = strrchr(path, '.'); |
| 1279 | if (dot) { |
| 1280 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1281 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1282 | result->st_mode |= 0111; |
| 1283 | } |
| 1284 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1288 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1289 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1290 | { |
| 1291 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1292 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1293 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1294 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1295 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1296 | const wchar_t *dot; |
| 1297 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1298 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1299 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1300 | traverse reparse point. */ |
| 1301 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1304 | hFile = CreateFileW( |
| 1305 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1306 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1307 | 0, /* share mode */ |
| 1308 | NULL, /* security attributes */ |
| 1309 | OPEN_EXISTING, |
| 1310 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1311 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1312 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1313 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1314 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1315 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1316 | NULL); |
| 1317 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1318 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1319 | /* Either the target doesn't exist, or we don't have access to |
| 1320 | get a handle to it. If the former, we need to return an error. |
| 1321 | If the latter, we can use attributes_from_dir. */ |
| 1322 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1323 | return -1; |
| 1324 | /* Could not get attributes on open file. Fall back to |
| 1325 | reading the directory. */ |
| 1326 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1327 | /* Very strange. This should not fail now */ |
| 1328 | return -1; |
| 1329 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1330 | if (traverse) { |
| 1331 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1332 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1333 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1334 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1335 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1336 | } else { |
| 1337 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1338 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1339 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1340 | } |
| 1341 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1342 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1343 | return -1; |
| 1344 | |
| 1345 | /* Close the outer open file handle now that we're about to |
| 1346 | reopen it with different flags. */ |
| 1347 | if (!CloseHandle(hFile)) |
| 1348 | return -1; |
| 1349 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1350 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1351 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1352 | the file without the reparse handling flag set. */ |
| 1353 | hFile2 = CreateFileW( |
| 1354 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1355 | NULL, OPEN_EXISTING, |
| 1356 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1357 | NULL); |
| 1358 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1359 | return -1; |
| 1360 | |
| 1361 | if (!get_target_path(hFile2, &target_path)) |
| 1362 | return -1; |
| 1363 | |
| 1364 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1365 | free(target_path); |
| 1366 | return code; |
| 1367 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1368 | } else |
| 1369 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1370 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1371 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1374 | dot = wcsrchr(path, '.'); |
| 1375 | if (dot) { |
| 1376 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1377 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1378 | result->st_mode |= 0111; |
| 1379 | } |
| 1380 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1384 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1385 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1386 | /* Protocol violation: we explicitly clear errno, instead of |
| 1387 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1388 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1389 | errno = 0; |
| 1390 | return code; |
| 1391 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1392 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1393 | static int |
| 1394 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1395 | { |
| 1396 | /* Protocol violation: we explicitly clear errno, instead of |
| 1397 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1398 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1399 | errno = 0; |
| 1400 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1401 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1402 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1403 | |
| 1404 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1405 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1406 | default does not traverse symlinks and instead returns attributes for |
| 1407 | the symlink. |
| 1408 | |
| 1409 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1410 | win32_stat will first explicitly resolve the symlink target and then will |
| 1411 | call win32_lstat on that result. |
| 1412 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1413 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1414 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1415 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1416 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1417 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1418 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1421 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1422 | 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] | 1423 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1424 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | static int |
| 1428 | win32_stat(const char* path, struct win32_stat *result) |
| 1429 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1430 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1433 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1434 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1435 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1436 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static int |
| 1440 | win32_fstat(int file_number, struct win32_stat *result) |
| 1441 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1442 | BY_HANDLE_FILE_INFORMATION info; |
| 1443 | HANDLE h; |
| 1444 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1445 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1447 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1448 | /* Protocol violation: we explicitly clear errno, instead of |
| 1449 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1450 | errno = 0; |
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 | if (h == INVALID_HANDLE_VALUE) { |
| 1453 | /* This is really a C library error (invalid file handle). |
| 1454 | We set the Win32 error to the closes one matching. */ |
| 1455 | SetLastError(ERROR_INVALID_HANDLE); |
| 1456 | return -1; |
| 1457 | } |
| 1458 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1460 | type = GetFileType(h); |
| 1461 | if (type == FILE_TYPE_UNKNOWN) { |
| 1462 | DWORD error = GetLastError(); |
| 1463 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1464 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1465 | } |
| 1466 | /* else: valid but unknown file */ |
| 1467 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1468 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1469 | if (type != FILE_TYPE_DISK) { |
| 1470 | if (type == FILE_TYPE_CHAR) |
| 1471 | result->st_mode = _S_IFCHR; |
| 1472 | else if (type == FILE_TYPE_PIPE) |
| 1473 | result->st_mode = _S_IFIFO; |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | if (!GetFileInformationByHandle(h, &info)) { |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1481 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1482 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1483 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1484 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | #endif /* MS_WINDOWS */ |
| 1488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1489 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1490 | "stat_result: Result from stat or lstat.\n\n\ |
| 1491 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1492 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1493 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1494 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1495 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1496 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1497 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1498 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1499 | |
| 1500 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | {"st_mode", "protection bits"}, |
| 1502 | {"st_ino", "inode"}, |
| 1503 | {"st_dev", "device"}, |
| 1504 | {"st_nlink", "number of hard links"}, |
| 1505 | {"st_uid", "user ID of owner"}, |
| 1506 | {"st_gid", "group ID of owner"}, |
| 1507 | {"st_size", "total size, in bytes"}, |
| 1508 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1509 | {NULL, "integer time of last access"}, |
| 1510 | {NULL, "integer time of last modification"}, |
| 1511 | {NULL, "integer time of last change"}, |
| 1512 | {"st_atime", "time of last access"}, |
| 1513 | {"st_mtime", "time of last modification"}, |
| 1514 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1515 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1516 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1517 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1518 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1519 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1520 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1521 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1522 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1523 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1524 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1525 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1526 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1527 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1528 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1529 | #endif |
| 1530 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1531 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1532 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1533 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1534 | }; |
| 1535 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1536 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1537 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1538 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1539 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1540 | #endif |
| 1541 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1542 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1543 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1544 | #else |
| 1545 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1546 | #endif |
| 1547 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1548 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1549 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1550 | #else |
| 1551 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1552 | #endif |
| 1553 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1554 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1555 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1556 | #else |
| 1557 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1558 | #endif |
| 1559 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1560 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1561 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1562 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1563 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1564 | #endif |
| 1565 | |
| 1566 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1567 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1568 | #else |
| 1569 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1570 | #endif |
| 1571 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1572 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1573 | "stat_result", /* name */ |
| 1574 | stat_result__doc__, /* doc */ |
| 1575 | stat_result_fields, |
| 1576 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1577 | }; |
| 1578 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1579 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1580 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1581 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1582 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1583 | 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] | 1584 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1585 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1586 | |
| 1587 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1588 | {"f_bsize", }, |
| 1589 | {"f_frsize", }, |
| 1590 | {"f_blocks", }, |
| 1591 | {"f_bfree", }, |
| 1592 | {"f_bavail", }, |
| 1593 | {"f_files", }, |
| 1594 | {"f_ffree", }, |
| 1595 | {"f_favail", }, |
| 1596 | {"f_flag", }, |
| 1597 | {"f_namemax",}, |
| 1598 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1599 | }; |
| 1600 | |
| 1601 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1602 | "statvfs_result", /* name */ |
| 1603 | statvfs_result__doc__, /* doc */ |
| 1604 | statvfs_result_fields, |
| 1605 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1606 | }; |
| 1607 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1608 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1609 | PyDoc_STRVAR(waitid_result__doc__, |
| 1610 | "waitid_result: Result from waitid.\n\n\ |
| 1611 | This object may be accessed either as a tuple of\n\ |
| 1612 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1613 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1614 | \n\ |
| 1615 | See os.waitid for more information."); |
| 1616 | |
| 1617 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1618 | {"si_pid", }, |
| 1619 | {"si_uid", }, |
| 1620 | {"si_signo", }, |
| 1621 | {"si_status", }, |
| 1622 | {"si_code", }, |
| 1623 | {0} |
| 1624 | }; |
| 1625 | |
| 1626 | static PyStructSequence_Desc waitid_result_desc = { |
| 1627 | "waitid_result", /* name */ |
| 1628 | waitid_result__doc__, /* doc */ |
| 1629 | waitid_result_fields, |
| 1630 | 5 |
| 1631 | }; |
| 1632 | static PyTypeObject WaitidResultType; |
| 1633 | #endif |
| 1634 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1635 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1636 | static PyTypeObject StatResultType; |
| 1637 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1638 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1639 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1640 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1641 | static newfunc structseq_new; |
| 1642 | |
| 1643 | static PyObject * |
| 1644 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1645 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1646 | PyStructSequence *result; |
| 1647 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1648 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1649 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1650 | if (!result) |
| 1651 | return NULL; |
| 1652 | /* If we have been initialized from a tuple, |
| 1653 | st_?time might be set to None. Initialize it |
| 1654 | from the int slots. */ |
| 1655 | for (i = 7; i <= 9; i++) { |
| 1656 | if (result->ob_item[i+3] == Py_None) { |
| 1657 | Py_DECREF(Py_None); |
| 1658 | Py_INCREF(result->ob_item[i]); |
| 1659 | result->ob_item[i+3] = result->ob_item[i]; |
| 1660 | } |
| 1661 | } |
| 1662 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1668 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1669 | |
| 1670 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1671 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1672 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1673 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1674 | future calls return ints. \n\ |
| 1675 | If newval is omitted, return the current setting.\n"); |
| 1676 | |
| 1677 | static PyObject* |
| 1678 | stat_float_times(PyObject* self, PyObject *args) |
| 1679 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1680 | int newval = -1; |
| 1681 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1682 | return NULL; |
| 1683 | if (newval == -1) |
| 1684 | /* Return old value */ |
| 1685 | return PyBool_FromLong(_stat_float_times); |
| 1686 | _stat_float_times = newval; |
| 1687 | Py_INCREF(Py_None); |
| 1688 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1689 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1690 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1691 | static void |
| 1692 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1693 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1694 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1695 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1696 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1697 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1698 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1699 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1700 | if (!ival) |
| 1701 | return; |
| 1702 | if (_stat_float_times) { |
| 1703 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1704 | } else { |
| 1705 | fval = ival; |
| 1706 | Py_INCREF(fval); |
| 1707 | } |
| 1708 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1709 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1712 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1713 | (used by posix_stat() and posix_fstat()) */ |
| 1714 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1715 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1716 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1717 | unsigned long ansec, mnsec, cnsec; |
| 1718 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1719 | if (v == NULL) |
| 1720 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1721 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1723 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1724 | PyStructSequence_SET_ITEM(v, 1, |
| 1725 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1726 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1727 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1728 | #endif |
| 1729 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1730 | PyStructSequence_SET_ITEM(v, 2, |
| 1731 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1732 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1733 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1734 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1735 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1736 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1737 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1738 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1739 | PyStructSequence_SET_ITEM(v, 6, |
| 1740 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1741 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1742 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1743 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1744 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1745 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | ansec = st->st_atim.tv_nsec; |
| 1747 | mnsec = st->st_mtim.tv_nsec; |
| 1748 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1749 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1750 | ansec = st->st_atimespec.tv_nsec; |
| 1751 | mnsec = st->st_mtimespec.tv_nsec; |
| 1752 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1753 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1754 | ansec = st->st_atime_nsec; |
| 1755 | mnsec = st->st_mtime_nsec; |
| 1756 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1757 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1759 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1760 | fill_time(v, 7, st->st_atime, ansec); |
| 1761 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1762 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1763 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1764 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1765 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1766 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1767 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1768 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1770 | PyLong_FromLong((long)st->st_blocks)); |
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_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1773 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1774 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1775 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1776 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1778 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1779 | #endif |
| 1780 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1781 | { |
| 1782 | PyObject *val; |
| 1783 | unsigned long bsec,bnsec; |
| 1784 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1785 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1786 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1787 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1788 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1789 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1790 | if (_stat_float_times) { |
| 1791 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1792 | } else { |
| 1793 | val = PyLong_FromLong((long)bsec); |
| 1794 | } |
| 1795 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1796 | val); |
| 1797 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1798 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1799 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1800 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1801 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1802 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1803 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1804 | if (PyErr_Occurred()) { |
| 1805 | Py_DECREF(v); |
| 1806 | return NULL; |
| 1807 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1808 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1809 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1812 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1813 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1814 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1815 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1816 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1817 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1818 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1819 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1820 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1821 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1822 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1823 | STRUCT_STAT st; |
| 1824 | PyObject *opath; |
| 1825 | char *path; |
| 1826 | int res; |
| 1827 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1828 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1829 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1830 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1831 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1832 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 1833 | if (wpath == NULL) |
| 1834 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1835 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1836 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1837 | res = wstatfunc(wpath, &st); |
| 1838 | Py_END_ALLOW_THREADS |
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 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1841 | return win32_error_object("stat", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1842 | return _pystat_fromstructstat(&st); |
| 1843 | } |
| 1844 | /* Drop the argument parsing error as narrow strings |
| 1845 | are also valid. */ |
| 1846 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1847 | #endif |
| 1848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1849 | if (!PyArg_ParseTuple(args, format, |
| 1850 | PyUnicode_FSConverter, &opath)) |
| 1851 | return NULL; |
| 1852 | path = PyBytes_AsString(opath); |
| 1853 | Py_BEGIN_ALLOW_THREADS |
| 1854 | res = (*statfunc)(path, &st); |
| 1855 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1857 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1858 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1860 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1862 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1863 | } |
| 1864 | else |
| 1865 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1866 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | Py_DECREF(opath); |
| 1868 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1871 | /* POSIX methods */ |
| 1872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1873 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1874 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1875 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1876 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1877 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1878 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1879 | 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] | 1880 | |
| 1881 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1882 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1884 | PyObject *opath; |
| 1885 | char *path; |
| 1886 | int mode; |
| 1887 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1888 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1889 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1890 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1892 | wchar_t* wpath = PyUnicode_AsUnicode(po); |
| 1893 | if (wpath == NULL) |
| 1894 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1895 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1896 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | Py_END_ALLOW_THREADS |
| 1898 | goto finish; |
| 1899 | } |
| 1900 | /* Drop the argument parsing error as narrow strings |
| 1901 | are also valid. */ |
| 1902 | PyErr_Clear(); |
| 1903 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1904 | PyUnicode_FSConverter, &opath, &mode)) |
| 1905 | return NULL; |
| 1906 | path = PyBytes_AsString(opath); |
| 1907 | Py_BEGIN_ALLOW_THREADS |
| 1908 | attr = GetFileAttributesA(path); |
| 1909 | Py_END_ALLOW_THREADS |
| 1910 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1911 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1912 | if (attr == 0xFFFFFFFF) |
| 1913 | /* File does not exist, or cannot read attributes */ |
| 1914 | return PyBool_FromLong(0); |
| 1915 | /* Access is possible if either write access wasn't requested, or |
| 1916 | the file isn't read-only, or if it's a directory, as there are |
| 1917 | no read-only directories on Windows. */ |
| 1918 | return PyBool_FromLong(!(mode & 2) |
| 1919 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1920 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1921 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1922 | int res; |
| 1923 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1924 | PyUnicode_FSConverter, &opath, &mode)) |
| 1925 | return NULL; |
| 1926 | path = PyBytes_AsString(opath); |
| 1927 | Py_BEGIN_ALLOW_THREADS |
| 1928 | res = access(path, mode); |
| 1929 | Py_END_ALLOW_THREADS |
| 1930 | Py_DECREF(opath); |
| 1931 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1932 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1935 | #ifndef F_OK |
| 1936 | #define F_OK 0 |
| 1937 | #endif |
| 1938 | #ifndef R_OK |
| 1939 | #define R_OK 4 |
| 1940 | #endif |
| 1941 | #ifndef W_OK |
| 1942 | #define W_OK 2 |
| 1943 | #endif |
| 1944 | #ifndef X_OK |
| 1945 | #define X_OK 1 |
| 1946 | #endif |
| 1947 | |
| 1948 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1949 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1950 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1951 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1952 | |
| 1953 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1954 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1955 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1956 | int id; |
| 1957 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1958 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1960 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1961 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1962 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1963 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1964 | if (id == 0) { |
| 1965 | ret = ttyname(); |
| 1966 | } |
| 1967 | else { |
| 1968 | ret = NULL; |
| 1969 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1970 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1972 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1973 | if (ret == NULL) |
| 1974 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1975 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1976 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1977 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1978 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1980 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1981 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1982 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1983 | |
| 1984 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1985 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1986 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1987 | char *ret; |
| 1988 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1989 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1990 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1992 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1993 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1994 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1995 | if (ret == NULL) |
| 1996 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1997 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1998 | } |
| 1999 | #endif |
| 2000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2002 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2003 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2004 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2005 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2006 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2007 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2008 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2009 | 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] | 2010 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2011 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2012 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2013 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2014 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2016 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2019 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2020 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2021 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2022 | 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] | 2023 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2024 | |
| 2025 | static PyObject * |
| 2026 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2027 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2028 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2029 | } |
| 2030 | #endif /* HAVE_FCHDIR */ |
| 2031 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2033 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2034 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2035 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2036 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2037 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2038 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2039 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2040 | PyObject *opath = NULL; |
| 2041 | char *path = NULL; |
| 2042 | int i; |
| 2043 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2044 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2046 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2047 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2048 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2049 | if (wpath == NULL) |
| 2050 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2051 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2052 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2053 | if (attr != 0xFFFFFFFF) { |
| 2054 | if (i & _S_IWRITE) |
| 2055 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2056 | else |
| 2057 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2058 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2059 | } |
| 2060 | else |
| 2061 | res = 0; |
| 2062 | Py_END_ALLOW_THREADS |
| 2063 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2064 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2065 | Py_INCREF(Py_None); |
| 2066 | return Py_None; |
| 2067 | } |
| 2068 | /* Drop the argument parsing error as narrow strings |
| 2069 | are also valid. */ |
| 2070 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2071 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2072 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2073 | &opath, &i)) |
| 2074 | return NULL; |
| 2075 | path = PyBytes_AsString(opath); |
| 2076 | Py_BEGIN_ALLOW_THREADS |
| 2077 | attr = GetFileAttributesA(path); |
| 2078 | if (attr != 0xFFFFFFFF) { |
| 2079 | if (i & _S_IWRITE) |
| 2080 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2081 | else |
| 2082 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2083 | res = SetFileAttributesA(path, attr); |
| 2084 | } |
| 2085 | else |
| 2086 | res = 0; |
| 2087 | Py_END_ALLOW_THREADS |
| 2088 | if (!res) { |
| 2089 | win32_error("chmod", path); |
| 2090 | Py_DECREF(opath); |
| 2091 | return NULL; |
| 2092 | } |
| 2093 | Py_DECREF(opath); |
| 2094 | Py_INCREF(Py_None); |
| 2095 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2096 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2097 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2098 | &opath, &i)) |
| 2099 | return NULL; |
| 2100 | path = PyBytes_AsString(opath); |
| 2101 | Py_BEGIN_ALLOW_THREADS |
| 2102 | res = chmod(path, i); |
| 2103 | Py_END_ALLOW_THREADS |
| 2104 | if (res < 0) |
| 2105 | return posix_error_with_allocated_filename(opath); |
| 2106 | Py_DECREF(opath); |
| 2107 | Py_INCREF(Py_None); |
| 2108 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2109 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2112 | #ifdef HAVE_FCHMOD |
| 2113 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2114 | "fchmod(fd, mode)\n\n\ |
| 2115 | Change the access permissions of the file given by file\n\ |
| 2116 | descriptor fd."); |
| 2117 | |
| 2118 | static PyObject * |
| 2119 | posix_fchmod(PyObject *self, PyObject *args) |
| 2120 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2121 | int fd, mode, res; |
| 2122 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2123 | return NULL; |
| 2124 | Py_BEGIN_ALLOW_THREADS |
| 2125 | res = fchmod(fd, mode); |
| 2126 | Py_END_ALLOW_THREADS |
| 2127 | if (res < 0) |
| 2128 | return posix_error(); |
| 2129 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2130 | } |
| 2131 | #endif /* HAVE_FCHMOD */ |
| 2132 | |
| 2133 | #ifdef HAVE_LCHMOD |
| 2134 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2135 | "lchmod(path, mode)\n\n\ |
| 2136 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2137 | affects the link itself rather than the target."); |
| 2138 | |
| 2139 | static PyObject * |
| 2140 | posix_lchmod(PyObject *self, PyObject *args) |
| 2141 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2142 | PyObject *opath; |
| 2143 | char *path; |
| 2144 | int i; |
| 2145 | int res; |
| 2146 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2147 | &opath, &i)) |
| 2148 | return NULL; |
| 2149 | path = PyBytes_AsString(opath); |
| 2150 | Py_BEGIN_ALLOW_THREADS |
| 2151 | res = lchmod(path, i); |
| 2152 | Py_END_ALLOW_THREADS |
| 2153 | if (res < 0) |
| 2154 | return posix_error_with_allocated_filename(opath); |
| 2155 | Py_DECREF(opath); |
| 2156 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2157 | } |
| 2158 | #endif /* HAVE_LCHMOD */ |
| 2159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2160 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2161 | #ifdef HAVE_CHFLAGS |
| 2162 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2163 | "chflags(path, flags)\n\n\ |
| 2164 | Set file flags."); |
| 2165 | |
| 2166 | static PyObject * |
| 2167 | posix_chflags(PyObject *self, PyObject *args) |
| 2168 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2169 | PyObject *opath; |
| 2170 | char *path; |
| 2171 | unsigned long flags; |
| 2172 | int res; |
| 2173 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2174 | PyUnicode_FSConverter, &opath, &flags)) |
| 2175 | return NULL; |
| 2176 | path = PyBytes_AsString(opath); |
| 2177 | Py_BEGIN_ALLOW_THREADS |
| 2178 | res = chflags(path, flags); |
| 2179 | Py_END_ALLOW_THREADS |
| 2180 | if (res < 0) |
| 2181 | return posix_error_with_allocated_filename(opath); |
| 2182 | Py_DECREF(opath); |
| 2183 | Py_INCREF(Py_None); |
| 2184 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2185 | } |
| 2186 | #endif /* HAVE_CHFLAGS */ |
| 2187 | |
| 2188 | #ifdef HAVE_LCHFLAGS |
| 2189 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2190 | "lchflags(path, flags)\n\n\ |
| 2191 | Set file flags.\n\ |
| 2192 | This function will not follow symbolic links."); |
| 2193 | |
| 2194 | static PyObject * |
| 2195 | posix_lchflags(PyObject *self, PyObject *args) |
| 2196 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2197 | PyObject *opath; |
| 2198 | char *path; |
| 2199 | unsigned long flags; |
| 2200 | int res; |
| 2201 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2202 | PyUnicode_FSConverter, &opath, &flags)) |
| 2203 | return NULL; |
| 2204 | path = PyBytes_AsString(opath); |
| 2205 | Py_BEGIN_ALLOW_THREADS |
| 2206 | res = lchflags(path, flags); |
| 2207 | Py_END_ALLOW_THREADS |
| 2208 | if (res < 0) |
| 2209 | return posix_error_with_allocated_filename(opath); |
| 2210 | Py_DECREF(opath); |
| 2211 | Py_INCREF(Py_None); |
| 2212 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2213 | } |
| 2214 | #endif /* HAVE_LCHFLAGS */ |
| 2215 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2216 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2217 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2218 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2219 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2220 | |
| 2221 | static PyObject * |
| 2222 | posix_chroot(PyObject *self, PyObject *args) |
| 2223 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2224 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2225 | } |
| 2226 | #endif |
| 2227 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2228 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2229 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2230 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2231 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2232 | |
| 2233 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2234 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2235 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2236 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2237 | } |
| 2238 | #endif /* HAVE_FSYNC */ |
| 2239 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2240 | #ifdef HAVE_SYNC |
| 2241 | PyDoc_STRVAR(posix_sync__doc__, |
| 2242 | "sync()\n\n\ |
| 2243 | Force write of everything to disk."); |
| 2244 | |
| 2245 | static PyObject * |
| 2246 | posix_sync(PyObject *self, PyObject *noargs) |
| 2247 | { |
| 2248 | Py_BEGIN_ALLOW_THREADS |
| 2249 | sync(); |
| 2250 | Py_END_ALLOW_THREADS |
| 2251 | Py_RETURN_NONE; |
| 2252 | } |
| 2253 | #endif |
| 2254 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2255 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2256 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2257 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2258 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2259 | #endif |
| 2260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2261 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2262 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2263 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2264 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2265 | |
| 2266 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2267 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2268 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2269 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2270 | } |
| 2271 | #endif /* HAVE_FDATASYNC */ |
| 2272 | |
| 2273 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2274 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2275 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2276 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2277 | 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] | 2278 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2279 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2280 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2281 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2282 | PyObject *opath; |
| 2283 | char *path; |
| 2284 | long uid, gid; |
| 2285 | int res; |
| 2286 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2287 | PyUnicode_FSConverter, &opath, |
| 2288 | &uid, &gid)) |
| 2289 | return NULL; |
| 2290 | path = PyBytes_AsString(opath); |
| 2291 | Py_BEGIN_ALLOW_THREADS |
| 2292 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2293 | Py_END_ALLOW_THREADS |
| 2294 | if (res < 0) |
| 2295 | return posix_error_with_allocated_filename(opath); |
| 2296 | Py_DECREF(opath); |
| 2297 | Py_INCREF(Py_None); |
| 2298 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2299 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2300 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2301 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2302 | #ifdef HAVE_FCHOWN |
| 2303 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2304 | "fchown(fd, uid, gid)\n\n\ |
| 2305 | Change the owner and group id of the file given by file descriptor\n\ |
| 2306 | fd to the numeric uid and gid."); |
| 2307 | |
| 2308 | static PyObject * |
| 2309 | posix_fchown(PyObject *self, PyObject *args) |
| 2310 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2311 | int fd; |
| 2312 | long uid, gid; |
| 2313 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2314 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2315 | return NULL; |
| 2316 | Py_BEGIN_ALLOW_THREADS |
| 2317 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2318 | Py_END_ALLOW_THREADS |
| 2319 | if (res < 0) |
| 2320 | return posix_error(); |
| 2321 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2322 | } |
| 2323 | #endif /* HAVE_FCHOWN */ |
| 2324 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2325 | #ifdef HAVE_LCHOWN |
| 2326 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2327 | "lchown(path, uid, gid)\n\n\ |
| 2328 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2329 | This function will not follow symbolic links."); |
| 2330 | |
| 2331 | static PyObject * |
| 2332 | posix_lchown(PyObject *self, PyObject *args) |
| 2333 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2334 | PyObject *opath; |
| 2335 | char *path; |
| 2336 | long uid, gid; |
| 2337 | int res; |
| 2338 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2339 | PyUnicode_FSConverter, &opath, |
| 2340 | &uid, &gid)) |
| 2341 | return NULL; |
| 2342 | path = PyBytes_AsString(opath); |
| 2343 | Py_BEGIN_ALLOW_THREADS |
| 2344 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2345 | Py_END_ALLOW_THREADS |
| 2346 | if (res < 0) |
| 2347 | return posix_error_with_allocated_filename(opath); |
| 2348 | Py_DECREF(opath); |
| 2349 | Py_INCREF(Py_None); |
| 2350 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2351 | } |
| 2352 | #endif /* HAVE_LCHOWN */ |
| 2353 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2354 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2355 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2356 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2357 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2358 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2359 | char buf[1026]; |
| 2360 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2361 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2362 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2363 | if (!use_bytes) { |
| 2364 | wchar_t wbuf[1026]; |
| 2365 | wchar_t *wbuf2 = wbuf; |
| 2366 | PyObject *resobj; |
| 2367 | DWORD len; |
| 2368 | Py_BEGIN_ALLOW_THREADS |
| 2369 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2370 | /* If the buffer is large enough, len does not include the |
| 2371 | terminating \0. If the buffer is too small, len includes |
| 2372 | the space needed for the terminator. */ |
| 2373 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2374 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2375 | if (wbuf2) |
| 2376 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2377 | } |
| 2378 | Py_END_ALLOW_THREADS |
| 2379 | if (!wbuf2) { |
| 2380 | PyErr_NoMemory(); |
| 2381 | return NULL; |
| 2382 | } |
| 2383 | if (!len) { |
| 2384 | if (wbuf2 != wbuf) free(wbuf2); |
| 2385 | return win32_error("getcwdu", NULL); |
| 2386 | } |
| 2387 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2388 | if (wbuf2 != wbuf) free(wbuf2); |
| 2389 | return resobj; |
| 2390 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2391 | #endif |
| 2392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2393 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2394 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2395 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2396 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2397 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2398 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2399 | Py_END_ALLOW_THREADS |
| 2400 | if (res == NULL) |
| 2401 | return posix_error(); |
| 2402 | if (use_bytes) |
| 2403 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2404 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2406 | |
| 2407 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2408 | "getcwd() -> path\n\n\ |
| 2409 | Return a unicode string representing the current working directory."); |
| 2410 | |
| 2411 | static PyObject * |
| 2412 | posix_getcwd_unicode(PyObject *self) |
| 2413 | { |
| 2414 | return posix_getcwd(0); |
| 2415 | } |
| 2416 | |
| 2417 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2418 | "getcwdb() -> path\n\n\ |
| 2419 | Return a bytes string representing the current working directory."); |
| 2420 | |
| 2421 | static PyObject * |
| 2422 | posix_getcwd_bytes(PyObject *self) |
| 2423 | { |
| 2424 | return posix_getcwd(1); |
| 2425 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2426 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2427 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2428 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2429 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2431 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2432 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2434 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2435 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2436 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2437 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2438 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2439 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2440 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2441 | #ifdef MS_WINDOWS |
| 2442 | PyDoc_STRVAR(win32_link__doc__, |
| 2443 | "link(src, dst)\n\n\ |
| 2444 | Create a hard link to a file."); |
| 2445 | |
| 2446 | static PyObject * |
| 2447 | win32_link(PyObject *self, PyObject *args) |
| 2448 | { |
| 2449 | PyObject *osrc, *odst; |
| 2450 | char *src, *dst; |
| 2451 | BOOL rslt; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2452 | PyObject *usrc, *udst; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2453 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2454 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) |
| 2455 | { |
| 2456 | wchar_t *wsrc, *wdst; |
| 2457 | wsrc = PyUnicode_AsUnicode(usrc); |
| 2458 | if (wsrc == NULL) |
| 2459 | return NULL; |
| 2460 | wdst = PyUnicode_AsUnicode(udst); |
| 2461 | if (wdst == NULL) |
| 2462 | return NULL; |
| 2463 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2464 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2465 | rslt = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2466 | Py_END_ALLOW_THREADS |
| 2467 | |
| 2468 | if (rslt == 0) |
| 2469 | return win32_error("link", NULL); |
| 2470 | |
| 2471 | Py_RETURN_NONE; |
| 2472 | } |
| 2473 | |
| 2474 | /* Narrow strings also valid. */ |
| 2475 | PyErr_Clear(); |
| 2476 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2477 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2478 | PyUnicode_FSConverter, &odst)) |
| 2479 | return NULL; |
| 2480 | |
| 2481 | src = PyBytes_AsString(osrc); |
| 2482 | dst = PyBytes_AsString(odst); |
| 2483 | |
| 2484 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2485 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2486 | Py_END_ALLOW_THREADS |
| 2487 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2488 | Py_DECREF(osrc); |
| 2489 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2490 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2491 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2492 | |
| 2493 | Py_RETURN_NONE; |
| 2494 | } |
| 2495 | #endif /* MS_WINDOWS */ |
| 2496 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2498 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2499 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2500 | Return a list containing the names of the entries in the directory.\n\ |
| 2501 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2502 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2503 | \n\ |
| 2504 | 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] | 2505 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2506 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2507 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2508 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2509 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2510 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2511 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2512 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2513 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2514 | PyObject *d, *v; |
| 2515 | HANDLE hFindFile; |
| 2516 | BOOL result; |
| 2517 | WIN32_FIND_DATA FileData; |
| 2518 | PyObject *opath; |
| 2519 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2520 | char *bufptr = namebuf; |
| 2521 | 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] | 2522 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2523 | PyObject *po = NULL; |
| 2524 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2525 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2526 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2527 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2528 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2529 | po_wchars = L"."; |
| 2530 | len = 1; |
| 2531 | } else { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2532 | po_wchars = PyUnicode_AsUnicode(po); |
| 2533 | if (po_wchars == NULL) |
| 2534 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2535 | len = PyUnicode_GET_SIZE(po); |
| 2536 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2537 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2538 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2539 | if (!wnamebuf) { |
| 2540 | PyErr_NoMemory(); |
| 2541 | return NULL; |
| 2542 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2543 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2544 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2545 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2546 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2547 | wnamebuf[len++] = L'\\'; |
| 2548 | wcscpy(wnamebuf + len, L"*.*"); |
| 2549 | } |
| 2550 | if ((d = PyList_New(0)) == NULL) { |
| 2551 | free(wnamebuf); |
| 2552 | return NULL; |
| 2553 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2554 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2555 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2556 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2557 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2558 | int error = GetLastError(); |
| 2559 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2560 | free(wnamebuf); |
| 2561 | return d; |
| 2562 | } |
| 2563 | Py_DECREF(d); |
| 2564 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2565 | free(wnamebuf); |
| 2566 | return NULL; |
| 2567 | } |
| 2568 | do { |
| 2569 | /* Skip over . and .. */ |
| 2570 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2571 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2572 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2573 | if (v == NULL) { |
| 2574 | Py_DECREF(d); |
| 2575 | d = NULL; |
| 2576 | break; |
| 2577 | } |
| 2578 | if (PyList_Append(d, v) != 0) { |
| 2579 | Py_DECREF(v); |
| 2580 | Py_DECREF(d); |
| 2581 | d = NULL; |
| 2582 | break; |
| 2583 | } |
| 2584 | Py_DECREF(v); |
| 2585 | } |
| 2586 | Py_BEGIN_ALLOW_THREADS |
| 2587 | result = FindNextFileW(hFindFile, &wFileData); |
| 2588 | Py_END_ALLOW_THREADS |
| 2589 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2590 | it got to the end of the directory. */ |
| 2591 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2592 | Py_DECREF(d); |
| 2593 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2594 | FindClose(hFindFile); |
| 2595 | free(wnamebuf); |
| 2596 | return NULL; |
| 2597 | } |
| 2598 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2599 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2600 | if (FindClose(hFindFile) == FALSE) { |
| 2601 | Py_DECREF(d); |
| 2602 | win32_error_unicode("FindClose", wnamebuf); |
| 2603 | free(wnamebuf); |
| 2604 | return NULL; |
| 2605 | } |
| 2606 | free(wnamebuf); |
| 2607 | return d; |
| 2608 | } |
| 2609 | /* Drop the argument parsing error as narrow strings |
| 2610 | are also valid. */ |
| 2611 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2612 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2613 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2614 | PyUnicode_FSConverter, &opath)) |
| 2615 | return NULL; |
| 2616 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2617 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2618 | Py_DECREF(opath); |
| 2619 | return NULL; |
| 2620 | } |
| 2621 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2622 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2623 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2624 | if (len > 0) { |
| 2625 | char ch = namebuf[len-1]; |
| 2626 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2627 | namebuf[len++] = '/'; |
| 2628 | strcpy(namebuf + len, "*.*"); |
| 2629 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2630 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2631 | if ((d = PyList_New(0)) == NULL) |
| 2632 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2634 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2635 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2636 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2637 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2638 | int error = GetLastError(); |
| 2639 | if (error == ERROR_FILE_NOT_FOUND) |
| 2640 | return d; |
| 2641 | Py_DECREF(d); |
| 2642 | return win32_error("FindFirstFile", namebuf); |
| 2643 | } |
| 2644 | do { |
| 2645 | /* Skip over . and .. */ |
| 2646 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2647 | strcmp(FileData.cFileName, "..") != 0) { |
| 2648 | v = PyBytes_FromString(FileData.cFileName); |
| 2649 | if (v == NULL) { |
| 2650 | Py_DECREF(d); |
| 2651 | d = NULL; |
| 2652 | break; |
| 2653 | } |
| 2654 | if (PyList_Append(d, v) != 0) { |
| 2655 | Py_DECREF(v); |
| 2656 | Py_DECREF(d); |
| 2657 | d = NULL; |
| 2658 | break; |
| 2659 | } |
| 2660 | Py_DECREF(v); |
| 2661 | } |
| 2662 | Py_BEGIN_ALLOW_THREADS |
| 2663 | result = FindNextFile(hFindFile, &FileData); |
| 2664 | Py_END_ALLOW_THREADS |
| 2665 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2666 | it got to the end of the directory. */ |
| 2667 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2668 | Py_DECREF(d); |
| 2669 | win32_error("FindNextFile", namebuf); |
| 2670 | FindClose(hFindFile); |
| 2671 | return NULL; |
| 2672 | } |
| 2673 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2674 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2675 | if (FindClose(hFindFile) == FALSE) { |
| 2676 | Py_DECREF(d); |
| 2677 | return win32_error("FindClose", namebuf); |
| 2678 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2679 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2680 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2681 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2682 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2683 | |
| 2684 | #ifndef MAX_PATH |
| 2685 | #define MAX_PATH CCHMAXPATH |
| 2686 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2687 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2688 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2689 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2690 | PyObject *d, *v; |
| 2691 | char namebuf[MAX_PATH+5]; |
| 2692 | HDIR hdir = 1; |
| 2693 | ULONG srchcnt = 1; |
| 2694 | FILEFINDBUF3 ep; |
| 2695 | APIRET rc; |
| 2696 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2697 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2698 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2699 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2700 | name = PyBytes_AsString(oname); |
| 2701 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2702 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2703 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2704 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2705 | return NULL; |
| 2706 | } |
| 2707 | strcpy(namebuf, name); |
| 2708 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2709 | if (*pt == ALTSEP) |
| 2710 | *pt = SEP; |
| 2711 | if (namebuf[len-1] != SEP) |
| 2712 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2713 | strcpy(namebuf + len, "*.*"); |
| 2714 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2715 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2716 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2717 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2718 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2719 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2720 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2721 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2722 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2723 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2724 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2725 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2726 | |
| 2727 | if (rc != NO_ERROR) { |
| 2728 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2729 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2732 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2733 | do { |
| 2734 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2735 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2736 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2737 | |
| 2738 | strcpy(namebuf, ep.achName); |
| 2739 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2740 | /* Leave Case of Name Alone -- In Native Form */ |
| 2741 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2742 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2743 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2744 | if (v == NULL) { |
| 2745 | Py_DECREF(d); |
| 2746 | d = NULL; |
| 2747 | break; |
| 2748 | } |
| 2749 | if (PyList_Append(d, v) != 0) { |
| 2750 | Py_DECREF(v); |
| 2751 | Py_DECREF(d); |
| 2752 | d = NULL; |
| 2753 | break; |
| 2754 | } |
| 2755 | Py_DECREF(v); |
| 2756 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2757 | } |
| 2758 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2759 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2760 | return d; |
| 2761 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2762 | PyObject *oname; |
| 2763 | char *name; |
| 2764 | PyObject *d, *v; |
| 2765 | DIR *dirp; |
| 2766 | struct dirent *ep; |
| 2767 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2770 | /* v is never read, so it does not need to be initialized yet. */ |
| 2771 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2772 | arg_is_unicode = 0; |
| 2773 | PyErr_Clear(); |
| 2774 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2775 | oname = NULL; |
| 2776 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2777 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2778 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2779 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2780 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2781 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2782 | Py_BEGIN_ALLOW_THREADS |
| 2783 | dirp = opendir(name); |
| 2784 | Py_END_ALLOW_THREADS |
| 2785 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2786 | return posix_error_with_allocated_filename(oname); |
| 2787 | } |
| 2788 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2789 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2790 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2791 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2792 | Py_DECREF(oname); |
| 2793 | return NULL; |
| 2794 | } |
| 2795 | for (;;) { |
| 2796 | errno = 0; |
| 2797 | Py_BEGIN_ALLOW_THREADS |
| 2798 | ep = readdir(dirp); |
| 2799 | Py_END_ALLOW_THREADS |
| 2800 | if (ep == NULL) { |
| 2801 | if (errno == 0) { |
| 2802 | break; |
| 2803 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2804 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2806 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2807 | Py_DECREF(d); |
| 2808 | return posix_error_with_allocated_filename(oname); |
| 2809 | } |
| 2810 | } |
| 2811 | if (ep->d_name[0] == '.' && |
| 2812 | (NAMLEN(ep) == 1 || |
| 2813 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2814 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2815 | if (arg_is_unicode) |
| 2816 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2817 | else |
| 2818 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2819 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2820 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | break; |
| 2822 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2823 | if (PyList_Append(d, v) != 0) { |
| 2824 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2825 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2826 | break; |
| 2827 | } |
| 2828 | Py_DECREF(v); |
| 2829 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2830 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2831 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2832 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2833 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2834 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2835 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2836 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2837 | #endif /* which OS */ |
| 2838 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2840 | #ifdef HAVE_FDOPENDIR |
| 2841 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2842 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2843 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2844 | After succesful execution of this function, fd will be closed."); |
| 2845 | |
| 2846 | static PyObject * |
| 2847 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2848 | { |
| 2849 | PyObject *d, *v; |
| 2850 | DIR *dirp; |
| 2851 | struct dirent *ep; |
| 2852 | int fd; |
| 2853 | |
| 2854 | errno = 0; |
| 2855 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2856 | return NULL; |
| 2857 | Py_BEGIN_ALLOW_THREADS |
| 2858 | dirp = fdopendir(fd); |
| 2859 | Py_END_ALLOW_THREADS |
| 2860 | if (dirp == NULL) { |
| 2861 | close(fd); |
| 2862 | return posix_error(); |
| 2863 | } |
| 2864 | if ((d = PyList_New(0)) == NULL) { |
| 2865 | Py_BEGIN_ALLOW_THREADS |
| 2866 | closedir(dirp); |
| 2867 | Py_END_ALLOW_THREADS |
| 2868 | return NULL; |
| 2869 | } |
| 2870 | for (;;) { |
| 2871 | errno = 0; |
| 2872 | Py_BEGIN_ALLOW_THREADS |
| 2873 | ep = readdir(dirp); |
| 2874 | Py_END_ALLOW_THREADS |
| 2875 | if (ep == NULL) { |
| 2876 | if (errno == 0) { |
| 2877 | break; |
| 2878 | } else { |
| 2879 | Py_BEGIN_ALLOW_THREADS |
| 2880 | closedir(dirp); |
| 2881 | Py_END_ALLOW_THREADS |
| 2882 | Py_DECREF(d); |
| 2883 | return posix_error(); |
| 2884 | } |
| 2885 | } |
| 2886 | if (ep->d_name[0] == '.' && |
| 2887 | (NAMLEN(ep) == 1 || |
| 2888 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2889 | continue; |
| 2890 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2891 | if (v == NULL) { |
| 2892 | Py_CLEAR(d); |
| 2893 | break; |
| 2894 | } |
| 2895 | if (PyList_Append(d, v) != 0) { |
| 2896 | Py_DECREF(v); |
| 2897 | Py_CLEAR(d); |
| 2898 | break; |
| 2899 | } |
| 2900 | Py_DECREF(v); |
| 2901 | } |
| 2902 | Py_BEGIN_ALLOW_THREADS |
| 2903 | closedir(dirp); |
| 2904 | Py_END_ALLOW_THREADS |
| 2905 | |
| 2906 | return d; |
| 2907 | } |
| 2908 | #endif |
| 2909 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2910 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2911 | /* A helper function for abspath on win32 */ |
| 2912 | static PyObject * |
| 2913 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2914 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | PyObject *opath; |
| 2916 | char *path; |
| 2917 | char outbuf[MAX_PATH*2]; |
| 2918 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2919 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2920 | PyObject *po; |
| 2921 | |
| 2922 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 2923 | { |
| 2924 | wchar_t *wpath; |
| 2925 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2926 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2927 | DWORD result; |
| 2928 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2929 | |
| 2930 | wpath = PyUnicode_AsUnicode(po); |
| 2931 | if (wpath == NULL) |
| 2932 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2933 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2934 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2935 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2936 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2937 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2938 | if (!woutbufp) |
| 2939 | return PyErr_NoMemory(); |
| 2940 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2941 | } |
| 2942 | if (result) |
| 2943 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2944 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2945 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2946 | if (woutbufp != woutbuf) |
| 2947 | free(woutbufp); |
| 2948 | return v; |
| 2949 | } |
| 2950 | /* Drop the argument parsing error as narrow strings |
| 2951 | are also valid. */ |
| 2952 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2953 | #endif |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2954 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2955 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2956 | PyUnicode_FSConverter, &opath)) |
| 2957 | return NULL; |
| 2958 | path = PyBytes_AsString(opath); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2959 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2960 | outbuf, &temp)) { |
| 2961 | win32_error("GetFullPathName", path); |
| 2962 | Py_DECREF(opath); |
| 2963 | return NULL; |
| 2964 | } |
| 2965 | Py_DECREF(opath); |
| 2966 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2967 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2968 | Py_FileSystemDefaultEncoding, NULL); |
| 2969 | } |
| 2970 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2971 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2972 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2973 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2974 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2975 | /* A helper function for samepath on windows */ |
| 2976 | static PyObject * |
| 2977 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2978 | { |
| 2979 | HANDLE hFile; |
| 2980 | int buf_size; |
| 2981 | wchar_t *target_path; |
| 2982 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2983 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2984 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2985 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2986 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2987 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2988 | path = PyUnicode_AsUnicode(po); |
| 2989 | if (path == NULL) |
| 2990 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2991 | |
| 2992 | if(!check_GetFinalPathNameByHandle()) { |
| 2993 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2994 | NotImplementedError. */ |
| 2995 | return PyErr_Format(PyExc_NotImplementedError, |
| 2996 | "GetFinalPathNameByHandle not available on this platform"); |
| 2997 | } |
| 2998 | |
| 2999 | hFile = CreateFileW( |
| 3000 | path, |
| 3001 | 0, /* desired access */ |
| 3002 | 0, /* share mode */ |
| 3003 | NULL, /* security attributes */ |
| 3004 | OPEN_EXISTING, |
| 3005 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3006 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3007 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3008 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3009 | if(hFile == INVALID_HANDLE_VALUE) |
| 3010 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3011 | |
| 3012 | /* We have a good handle to the target, use it to determine the |
| 3013 | target path name. */ |
| 3014 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3015 | |
| 3016 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3017 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3018 | |
| 3019 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3020 | if(!target_path) |
| 3021 | return PyErr_NoMemory(); |
| 3022 | |
| 3023 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3024 | buf_size, VOLUME_NAME_DOS); |
| 3025 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3026 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3027 | |
| 3028 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3029 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3030 | |
| 3031 | target_path[result_length] = 0; |
| 3032 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 3033 | free(target_path); |
| 3034 | return result; |
| 3035 | |
| 3036 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3037 | |
| 3038 | static PyObject * |
| 3039 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3040 | { |
| 3041 | HANDLE hFile; |
| 3042 | BY_HANDLE_FILE_INFORMATION info; |
| 3043 | int fd; |
| 3044 | |
| 3045 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3046 | return NULL; |
| 3047 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3048 | if (!_PyVerify_fd(fd)) |
| 3049 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3050 | |
| 3051 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3052 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3053 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3054 | |
| 3055 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3056 | return win32_error("_getfileinformation", NULL); |
| 3057 | |
| 3058 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3059 | info.nFileIndexHigh, |
| 3060 | info.nFileIndexLow); |
| 3061 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3062 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3063 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3064 | "Return true if the pathname refers to an existing directory."); |
| 3065 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3066 | static PyObject * |
| 3067 | posix__isdir(PyObject *self, PyObject *args) |
| 3068 | { |
| 3069 | PyObject *opath; |
| 3070 | char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3071 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3072 | DWORD attributes; |
| 3073 | |
| 3074 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3075 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3076 | if (wpath == NULL) |
| 3077 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3078 | |
| 3079 | attributes = GetFileAttributesW(wpath); |
| 3080 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3081 | Py_RETURN_FALSE; |
| 3082 | goto check; |
| 3083 | } |
| 3084 | /* Drop the argument parsing error as narrow strings |
| 3085 | are also valid. */ |
| 3086 | PyErr_Clear(); |
| 3087 | |
| 3088 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 3089 | PyUnicode_FSConverter, &opath)) |
| 3090 | return NULL; |
| 3091 | |
| 3092 | path = PyBytes_AsString(opath); |
| 3093 | attributes = GetFileAttributesA(path); |
| 3094 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3095 | Py_RETURN_FALSE; |
| 3096 | |
| 3097 | check: |
| 3098 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3099 | Py_RETURN_TRUE; |
| 3100 | else |
| 3101 | Py_RETURN_FALSE; |
| 3102 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3103 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3105 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3106 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3107 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3110 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3111 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3112 | int res; |
| 3113 | PyObject *opath; |
| 3114 | char *path; |
| 3115 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3116 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3117 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3118 | PyObject *po; |
| 3119 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3120 | { |
| 3121 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3122 | if (wpath == NULL) |
| 3123 | return NULL; |
| 3124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3125 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3126 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3127 | Py_END_ALLOW_THREADS |
| 3128 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3129 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3130 | Py_INCREF(Py_None); |
| 3131 | return Py_None; |
| 3132 | } |
| 3133 | /* Drop the argument parsing error as narrow strings |
| 3134 | are also valid. */ |
| 3135 | PyErr_Clear(); |
| 3136 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3137 | PyUnicode_FSConverter, &opath, &mode)) |
| 3138 | return NULL; |
| 3139 | path = PyBytes_AsString(opath); |
| 3140 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3141 | res = CreateDirectoryA(path, NULL); |
| 3142 | Py_END_ALLOW_THREADS |
| 3143 | if (!res) { |
| 3144 | win32_error("mkdir", path); |
| 3145 | Py_DECREF(opath); |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | Py_DECREF(opath); |
| 3149 | Py_INCREF(Py_None); |
| 3150 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3151 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3154 | PyUnicode_FSConverter, &opath, &mode)) |
| 3155 | return NULL; |
| 3156 | path = PyBytes_AsString(opath); |
| 3157 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3158 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3159 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3160 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3161 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3162 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3163 | Py_END_ALLOW_THREADS |
| 3164 | if (res < 0) |
| 3165 | return posix_error_with_allocated_filename(opath); |
| 3166 | Py_DECREF(opath); |
| 3167 | Py_INCREF(Py_None); |
| 3168 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3169 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3172 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3173 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3174 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3175 | #include <sys/resource.h> |
| 3176 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3177 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3178 | |
| 3179 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3180 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3181 | "nice(inc) -> new_priority\n\n\ |
| 3182 | 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] | 3183 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3184 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3185 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3186 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3187 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3188 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3189 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3190 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3192 | /* There are two flavours of 'nice': one that returns the new |
| 3193 | priority (as required by almost all standards out there) and the |
| 3194 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3195 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3196 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3197 | If we are of the nice family that returns the new priority, we |
| 3198 | need to clear errno before the call, and check if errno is filled |
| 3199 | before calling posix_error() on a returnvalue of -1, because the |
| 3200 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3202 | errno = 0; |
| 3203 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3204 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3205 | if (value == 0) |
| 3206 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3207 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3208 | if (value == -1 && errno != 0) |
| 3209 | /* either nice() or getpriority() returned an error */ |
| 3210 | return posix_error(); |
| 3211 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3212 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3213 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3214 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3215 | |
| 3216 | #ifdef HAVE_GETPRIORITY |
| 3217 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3218 | "getpriority(which, who) -> current_priority\n\n\ |
| 3219 | Get program scheduling priority."); |
| 3220 | |
| 3221 | static PyObject * |
| 3222 | posix_getpriority(PyObject *self, PyObject *args) |
| 3223 | { |
| 3224 | int which, who, retval; |
| 3225 | |
| 3226 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3227 | return NULL; |
| 3228 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3229 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3230 | if (errno != 0) |
| 3231 | return posix_error(); |
| 3232 | return PyLong_FromLong((long)retval); |
| 3233 | } |
| 3234 | #endif /* HAVE_GETPRIORITY */ |
| 3235 | |
| 3236 | |
| 3237 | #ifdef HAVE_SETPRIORITY |
| 3238 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3239 | "setpriority(which, who, prio) -> None\n\n\ |
| 3240 | Set program scheduling priority."); |
| 3241 | |
| 3242 | static PyObject * |
| 3243 | posix_setpriority(PyObject *self, PyObject *args) |
| 3244 | { |
| 3245 | int which, who, prio, retval; |
| 3246 | |
| 3247 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3248 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3249 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3250 | if (retval == -1) |
| 3251 | return posix_error(); |
| 3252 | Py_RETURN_NONE; |
| 3253 | } |
| 3254 | #endif /* HAVE_SETPRIORITY */ |
| 3255 | |
| 3256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3257 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3258 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3259 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3260 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3261 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3262 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3263 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3264 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3265 | PyObject *o1, *o2; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3266 | wchar_t *w1, *w2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3267 | char *p1, *p2; |
| 3268 | BOOL result; |
| 3269 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3270 | goto error; |
| 3271 | if (!convert_to_unicode(&o1)) |
| 3272 | goto error; |
| 3273 | if (!convert_to_unicode(&o2)) { |
| 3274 | Py_DECREF(o1); |
| 3275 | goto error; |
| 3276 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3277 | w1 = PyUnicode_AsUnicode(o1); |
| 3278 | if (w1 == NULL) |
| 3279 | goto error; |
| 3280 | w2 = PyUnicode_AsUnicode(o2); |
| 3281 | if (w2 == NULL) |
| 3282 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3283 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3284 | result = MoveFileW(w1, w2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3285 | Py_END_ALLOW_THREADS |
| 3286 | Py_DECREF(o1); |
| 3287 | Py_DECREF(o2); |
| 3288 | if (!result) |
| 3289 | return win32_error("rename", NULL); |
| 3290 | Py_INCREF(Py_None); |
| 3291 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3292 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3293 | PyErr_Clear(); |
| 3294 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3295 | return NULL; |
| 3296 | Py_BEGIN_ALLOW_THREADS |
| 3297 | result = MoveFileA(p1, p2); |
| 3298 | Py_END_ALLOW_THREADS |
| 3299 | if (!result) |
| 3300 | return win32_error("rename", NULL); |
| 3301 | Py_INCREF(Py_None); |
| 3302 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3303 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3304 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3305 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3308 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3309 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3310 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3311 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3312 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3313 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3314 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3315 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3316 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3317 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3318 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3319 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3320 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3323 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3324 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3325 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3326 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3328 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3329 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3330 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3331 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3332 | 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] | 3333 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3334 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3335 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3338 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3339 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3340 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3341 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3342 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3343 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3344 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3345 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3346 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3347 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3348 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3349 | wchar_t *command; |
| 3350 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3351 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3352 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3353 | Py_BEGIN_ALLOW_THREADS |
| 3354 | sts = _wsystem(command); |
| 3355 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3356 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3357 | PyObject *command_obj; |
| 3358 | char *command; |
| 3359 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3360 | PyUnicode_FSConverter, &command_obj)) |
| 3361 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3363 | command = PyBytes_AsString(command_obj); |
| 3364 | Py_BEGIN_ALLOW_THREADS |
| 3365 | sts = system(command); |
| 3366 | Py_END_ALLOW_THREADS |
| 3367 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3368 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3369 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3370 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3371 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3372 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3373 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3374 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3375 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3376 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3377 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3378 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3379 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3381 | int i; |
| 3382 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3383 | return NULL; |
| 3384 | i = (int)umask(i); |
| 3385 | if (i < 0) |
| 3386 | return posix_error(); |
| 3387 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3390 | #ifdef MS_WINDOWS |
| 3391 | |
| 3392 | /* override the default DeleteFileW behavior so that directory |
| 3393 | symlinks can be removed with this function, the same as with |
| 3394 | Unix symlinks */ |
| 3395 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3396 | { |
| 3397 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3398 | WIN32_FIND_DATAW find_data; |
| 3399 | HANDLE find_data_handle; |
| 3400 | int is_directory = 0; |
| 3401 | int is_link = 0; |
| 3402 | |
| 3403 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3404 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3405 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3406 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3407 | it is a symlink */ |
| 3408 | if(is_directory && |
| 3409 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3410 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3411 | |
| 3412 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3413 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3414 | FindClose(find_data_handle); |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | if (is_directory && is_link) |
| 3420 | return RemoveDirectoryW(lpFileName); |
| 3421 | |
| 3422 | return DeleteFileW(lpFileName); |
| 3423 | } |
| 3424 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3426 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3427 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3428 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3430 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3431 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3432 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3433 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3434 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3435 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3436 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3437 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3438 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3439 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3440 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3441 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3442 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3445 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3446 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3447 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3448 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3449 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3450 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3451 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3452 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3453 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3454 | struct utsname u; |
| 3455 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3456 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3457 | Py_BEGIN_ALLOW_THREADS |
| 3458 | res = uname(&u); |
| 3459 | Py_END_ALLOW_THREADS |
| 3460 | if (res < 0) |
| 3461 | return posix_error(); |
| 3462 | return Py_BuildValue("(sssss)", |
| 3463 | u.sysname, |
| 3464 | u.nodename, |
| 3465 | u.release, |
| 3466 | u.version, |
| 3467 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3468 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3469 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3470 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3471 | |
| 3472 | /* |
| 3473 | * Classic POSIX utime functions supported microseconds (1m/sec). |
| 3474 | * Newer POSIX functions support nanoseconds (1 billion per sec). |
| 3475 | * posixmodule now uses the new functions where possible. |
| 3476 | * This improves accuracy in many situations, for example shutil.copy2(). |
| 3477 | * |
| 3478 | * The implementation isn't currently sophisticated enough to handle |
| 3479 | * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false. |
| 3480 | * Specifically, posix_futimes() would break. |
| 3481 | * |
| 3482 | * Supporting such a platform wouldn't be impossible; you'd need two |
| 3483 | * extract_time() functions, or make its precision a parameter. |
| 3484 | * Since such a platform seems unlikely we haven't bothered. |
| 3485 | */ |
| 3486 | #if defined(HAVE_UTIMENSAT) |
| 3487 | #define EXTRACT_TIME_PRECISION (1e9) |
| 3488 | #if !defined(HAVE_FUTIMENS) |
| 3489 | #error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment. |
| 3490 | #endif |
| 3491 | #else |
| 3492 | #define EXTRACT_TIME_PRECISION (1e6) |
| 3493 | #endif |
| 3494 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3495 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3496 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3497 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3498 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | if (PyFloat_Check(t)) { |
| 3500 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3501 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3502 | if (!intobj) |
| 3503 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3504 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3505 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3506 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3507 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3508 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3509 | Py_DECREF(intobj); |
| 3510 | if (intval == -1 && PyErr_Occurred()) |
| 3511 | return -1; |
| 3512 | *sec = intval; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3513 | |
| 3514 | *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3515 | if (*usec < 0) |
| 3516 | /* If rounding gave us a negative number, |
| 3517 | truncate. */ |
| 3518 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3519 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3520 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3521 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3522 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3523 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3524 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3525 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3526 | if (intval == -1 && PyErr_Occurred()) |
| 3527 | return -1; |
| 3528 | *sec = intval; |
| 3529 | *usec = 0; |
| 3530 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3531 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3533 | PyDoc_STRVAR(posix_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3534 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3535 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3536 | 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] | 3537 | 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] | 3538 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3540 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3541 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3542 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3543 | PyObject *arg; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3544 | PyObject *obwpath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3545 | wchar_t *wpath = NULL; |
| 3546 | PyObject *oapath; |
| 3547 | char *apath; |
| 3548 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3549 | time_t atimesec, mtimesec; |
| 3550 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | FILETIME atime, mtime; |
| 3552 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3553 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3555 | wpath = PyUnicode_AsUnicode(obwpath); |
| 3556 | if (wpath == NULL) |
| 3557 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3558 | Py_BEGIN_ALLOW_THREADS |
| 3559 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3560 | NULL, OPEN_EXISTING, |
| 3561 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3562 | Py_END_ALLOW_THREADS |
| 3563 | if (hFile == INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3564 | return win32_error_object("utime", obwpath); |
| 3565 | } |
| 3566 | else { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3567 | /* Drop the argument parsing error as narrow strings |
| 3568 | are also valid. */ |
| 3569 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3570 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3571 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3572 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3573 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3574 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3575 | apath = PyBytes_AsString(oapath); |
| 3576 | Py_BEGIN_ALLOW_THREADS |
| 3577 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3578 | NULL, OPEN_EXISTING, |
| 3579 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3580 | Py_END_ALLOW_THREADS |
| 3581 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3582 | win32_error("utime", apath); |
| 3583 | Py_DECREF(oapath); |
| 3584 | return NULL; |
| 3585 | } |
| 3586 | Py_DECREF(oapath); |
| 3587 | } |
| 3588 | |
| 3589 | if (arg == Py_None) { |
| 3590 | SYSTEMTIME now; |
| 3591 | GetSystemTime(&now); |
| 3592 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3593 | !SystemTimeToFileTime(&now, &atime)) { |
| 3594 | win32_error("utime", NULL); |
| 3595 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3596 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3597 | } |
| 3598 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3599 | PyErr_SetString(PyExc_TypeError, |
| 3600 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3601 | goto done; |
| 3602 | } |
| 3603 | else { |
| 3604 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3605 | &atimesec, &ausec) == -1) |
| 3606 | goto done; |
| 3607 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3608 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3609 | &mtimesec, &musec) == -1) |
| 3610 | goto done; |
| 3611 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3612 | } |
| 3613 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3614 | /* Avoid putting the file name into the error here, |
| 3615 | as that may confuse the user into believing that |
| 3616 | something is wrong with the file, when it also |
| 3617 | could be the time stamp that gives a problem. */ |
| 3618 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3619 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3620 | } |
| 3621 | Py_INCREF(Py_None); |
| 3622 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3623 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3624 | CloseHandle(hFile); |
| 3625 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3626 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3627 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | PyObject *opath; |
| 3629 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3630 | time_t atime, mtime; |
| 3631 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3632 | int res; |
| 3633 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3634 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3635 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3636 | PyUnicode_FSConverter, &opath, &arg)) |
| 3637 | return NULL; |
| 3638 | path = PyBytes_AsString(opath); |
| 3639 | if (arg == Py_None) { |
| 3640 | /* optional time values not given */ |
| 3641 | Py_BEGIN_ALLOW_THREADS |
| 3642 | res = utime(path, NULL); |
| 3643 | Py_END_ALLOW_THREADS |
| 3644 | } |
| 3645 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3646 | PyErr_SetString(PyExc_TypeError, |
| 3647 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3648 | Py_DECREF(opath); |
| 3649 | return NULL; |
| 3650 | } |
| 3651 | else { |
| 3652 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3653 | &atime, &ausec) == -1) { |
| 3654 | Py_DECREF(opath); |
| 3655 | return NULL; |
| 3656 | } |
| 3657 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3658 | &mtime, &musec) == -1) { |
| 3659 | Py_DECREF(opath); |
| 3660 | return NULL; |
| 3661 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3662 | |
| 3663 | Py_BEGIN_ALLOW_THREADS |
| 3664 | { |
| 3665 | #ifdef HAVE_UTIMENSAT |
| 3666 | struct timespec buf[2]; |
| 3667 | buf[0].tv_sec = atime; |
| 3668 | buf[0].tv_nsec = ausec; |
| 3669 | buf[1].tv_sec = mtime; |
| 3670 | buf[1].tv_nsec = musec; |
| 3671 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3672 | #elif defined(HAVE_UTIMES) |
| 3673 | struct timeval buf[2]; |
| 3674 | buf[0].tv_sec = atime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3675 | buf[0].tv_usec = ausec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3676 | buf[1].tv_sec = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3677 | buf[1].tv_usec = musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3678 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3679 | #elif defined(HAVE_UTIME_H) |
| 3680 | /* XXX should define struct utimbuf instead, above */ |
| 3681 | struct utimbuf buf; |
| 3682 | buf.actime = atime; |
| 3683 | buf.modtime = mtime; |
| 3684 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3685 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3686 | time_t buf[2]; |
| 3687 | buf[0] = atime; |
| 3688 | buf[1] = mtime; |
| 3689 | res = utime(path, buf); |
| 3690 | #endif |
| 3691 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3692 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3693 | } |
| 3694 | if (res < 0) { |
| 3695 | return posix_error_with_allocated_filename(opath); |
| 3696 | } |
| 3697 | Py_DECREF(opath); |
| 3698 | Py_INCREF(Py_None); |
| 3699 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3700 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3701 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3704 | #ifdef HAVE_FUTIMES |
| 3705 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3706 | "futimes(fd, (atime, mtime))\n\ |
| 3707 | futimes(fd, None)\n\n\ |
| 3708 | Set the access and modified time of the file specified by the file\n\ |
| 3709 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3710 | access and modified times to the current time."); |
| 3711 | |
| 3712 | static PyObject * |
| 3713 | posix_futimes(PyObject *self, PyObject *args) |
| 3714 | { |
| 3715 | int res, fd; |
| 3716 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3717 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3718 | long ausec, musec; |
| 3719 | |
| 3720 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3721 | return NULL; |
| 3722 | |
| 3723 | if (arg == Py_None) { |
| 3724 | /* optional time values not given */ |
| 3725 | Py_BEGIN_ALLOW_THREADS |
| 3726 | res = futimes(fd, NULL); |
| 3727 | Py_END_ALLOW_THREADS |
| 3728 | } |
| 3729 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3730 | PyErr_SetString(PyExc_TypeError, |
| 3731 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3732 | return NULL; |
| 3733 | } |
| 3734 | else { |
| 3735 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3736 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3737 | return NULL; |
| 3738 | } |
| 3739 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3740 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3741 | return NULL; |
| 3742 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3743 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3744 | { |
| 3745 | #ifdef HAVE_FUTIMENS |
| 3746 | struct timespec buf[2]; |
| 3747 | buf[0].tv_sec = atime; |
| 3748 | buf[0].tv_nsec = ausec; |
| 3749 | buf[1].tv_sec = mtime; |
| 3750 | buf[1].tv_nsec = musec; |
| 3751 | res = futimens(fd, buf); |
| 3752 | #else |
| 3753 | struct timeval buf[2]; |
| 3754 | buf[0].tv_sec = atime; |
| 3755 | buf[0].tv_usec = ausec; |
| 3756 | buf[1].tv_sec = mtime; |
| 3757 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3758 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3759 | #endif |
| 3760 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3761 | Py_END_ALLOW_THREADS |
| 3762 | } |
| 3763 | if (res < 0) |
| 3764 | return posix_error(); |
| 3765 | Py_RETURN_NONE; |
| 3766 | } |
| 3767 | #endif |
| 3768 | |
| 3769 | #ifdef HAVE_LUTIMES |
| 3770 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3771 | "lutimes(path, (atime, mtime))\n\ |
| 3772 | lutimes(path, None)\n\n\ |
| 3773 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3774 | |
| 3775 | static PyObject * |
| 3776 | posix_lutimes(PyObject *self, PyObject *args) |
| 3777 | { |
| 3778 | PyObject *opath, *arg; |
| 3779 | const char *path; |
| 3780 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3781 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3782 | long ausec, musec; |
| 3783 | |
| 3784 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3785 | PyUnicode_FSConverter, &opath, &arg)) |
| 3786 | return NULL; |
| 3787 | path = PyBytes_AsString(opath); |
| 3788 | if (arg == Py_None) { |
| 3789 | /* optional time values not given */ |
| 3790 | Py_BEGIN_ALLOW_THREADS |
| 3791 | res = lutimes(path, NULL); |
| 3792 | Py_END_ALLOW_THREADS |
| 3793 | } |
| 3794 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3795 | PyErr_SetString(PyExc_TypeError, |
| 3796 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3797 | Py_DECREF(opath); |
| 3798 | return NULL; |
| 3799 | } |
| 3800 | else { |
| 3801 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3802 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3803 | Py_DECREF(opath); |
| 3804 | return NULL; |
| 3805 | } |
| 3806 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3807 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3808 | Py_DECREF(opath); |
| 3809 | return NULL; |
| 3810 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3811 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3812 | { |
| 3813 | #ifdef HAVE_UTIMENSAT |
| 3814 | struct timespec buf[2]; |
| 3815 | buf[0].tv_sec = atime; |
| 3816 | buf[0].tv_nsec = ausec; |
| 3817 | buf[1].tv_sec = mtime; |
| 3818 | buf[1].tv_nsec = musec; |
| 3819 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3820 | #else |
| 3821 | struct timeval buf[2]; |
| 3822 | buf[0].tv_sec = atime; |
| 3823 | buf[0].tv_usec = ausec; |
| 3824 | buf[1].tv_sec = mtime; |
| 3825 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3826 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3827 | #endif |
| 3828 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3829 | Py_END_ALLOW_THREADS |
| 3830 | } |
| 3831 | Py_DECREF(opath); |
| 3832 | if (res < 0) |
| 3833 | return posix_error(); |
| 3834 | Py_RETURN_NONE; |
| 3835 | } |
| 3836 | #endif |
| 3837 | |
| 3838 | #ifdef HAVE_FUTIMENS |
| 3839 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3840 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3841 | futimens(fd, None, None)\n\n\ |
| 3842 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3843 | nanosecond precision.\n\ |
| 3844 | The second form sets atime and mtime to the current time.\n\ |
| 3845 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3846 | current time.\n\ |
| 3847 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3848 | |
| 3849 | static PyObject * |
| 3850 | posix_futimens(PyObject *self, PyObject *args) |
| 3851 | { |
| 3852 | int res, fd; |
| 3853 | PyObject *atime, *mtime; |
| 3854 | struct timespec buf[2]; |
| 3855 | |
| 3856 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3857 | &fd, &atime, &mtime)) |
| 3858 | return NULL; |
| 3859 | if (atime == Py_None && mtime == Py_None) { |
| 3860 | /* optional time values not given */ |
| 3861 | Py_BEGIN_ALLOW_THREADS |
| 3862 | res = futimens(fd, NULL); |
| 3863 | Py_END_ALLOW_THREADS |
| 3864 | } |
| 3865 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3866 | PyErr_SetString(PyExc_TypeError, |
| 3867 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3868 | return NULL; |
| 3869 | } |
| 3870 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3871 | PyErr_SetString(PyExc_TypeError, |
| 3872 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3873 | return NULL; |
| 3874 | } |
| 3875 | else { |
| 3876 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3877 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3878 | return NULL; |
| 3879 | } |
| 3880 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3881 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3882 | return NULL; |
| 3883 | } |
| 3884 | Py_BEGIN_ALLOW_THREADS |
| 3885 | res = futimens(fd, buf); |
| 3886 | Py_END_ALLOW_THREADS |
| 3887 | } |
| 3888 | if (res < 0) |
| 3889 | return posix_error(); |
| 3890 | Py_RETURN_NONE; |
| 3891 | } |
| 3892 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3893 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3894 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3896 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3897 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3898 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3899 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3900 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3901 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3902 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3903 | int sts; |
| 3904 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3905 | return NULL; |
| 3906 | _exit(sts); |
| 3907 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3910 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3911 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3912 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3913 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3914 | Py_ssize_t i; |
| 3915 | for (i = 0; i < count; i++) |
| 3916 | PyMem_Free(array[i]); |
| 3917 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3918 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3919 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3920 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3921 | int fsconvert_strdup(PyObject *o, char**out) |
| 3922 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3923 | PyObject *bytes; |
| 3924 | Py_ssize_t size; |
| 3925 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3926 | return 0; |
| 3927 | size = PyBytes_GET_SIZE(bytes); |
| 3928 | *out = PyMem_Malloc(size+1); |
| 3929 | if (!*out) |
| 3930 | return 0; |
| 3931 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3932 | Py_DECREF(bytes); |
| 3933 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3934 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3935 | #endif |
| 3936 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3937 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3938 | static char** |
| 3939 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3940 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3941 | char **envlist; |
| 3942 | Py_ssize_t i, pos, envc; |
| 3943 | PyObject *keys=NULL, *vals=NULL; |
| 3944 | PyObject *key, *val, *key2, *val2; |
| 3945 | char *p, *k, *v; |
| 3946 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3948 | i = PyMapping_Size(env); |
| 3949 | if (i < 0) |
| 3950 | return NULL; |
| 3951 | envlist = PyMem_NEW(char *, i + 1); |
| 3952 | if (envlist == NULL) { |
| 3953 | PyErr_NoMemory(); |
| 3954 | return NULL; |
| 3955 | } |
| 3956 | envc = 0; |
| 3957 | keys = PyMapping_Keys(env); |
| 3958 | vals = PyMapping_Values(env); |
| 3959 | if (!keys || !vals) |
| 3960 | goto error; |
| 3961 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3962 | PyErr_Format(PyExc_TypeError, |
| 3963 | "env.keys() or env.values() is not a list"); |
| 3964 | goto error; |
| 3965 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3967 | for (pos = 0; pos < i; pos++) { |
| 3968 | key = PyList_GetItem(keys, pos); |
| 3969 | val = PyList_GetItem(vals, pos); |
| 3970 | if (!key || !val) |
| 3971 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3972 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3973 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3974 | goto error; |
| 3975 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3976 | Py_DECREF(key2); |
| 3977 | goto error; |
| 3978 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3979 | |
| 3980 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3981 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3982 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3983 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3984 | k = PyBytes_AsString(key2); |
| 3985 | v = PyBytes_AsString(val2); |
| 3986 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3987 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3988 | p = PyMem_NEW(char, len); |
| 3989 | if (p == NULL) { |
| 3990 | PyErr_NoMemory(); |
| 3991 | Py_DECREF(key2); |
| 3992 | Py_DECREF(val2); |
| 3993 | goto error; |
| 3994 | } |
| 3995 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3996 | envlist[envc++] = p; |
| 3997 | Py_DECREF(key2); |
| 3998 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3999 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4000 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4001 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4002 | } |
| 4003 | Py_DECREF(vals); |
| 4004 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4006 | envlist[envc] = 0; |
| 4007 | *envc_ptr = envc; |
| 4008 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4009 | |
| 4010 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4011 | Py_XDECREF(keys); |
| 4012 | Py_XDECREF(vals); |
| 4013 | while (--envc >= 0) |
| 4014 | PyMem_DEL(envlist[envc]); |
| 4015 | PyMem_DEL(envlist); |
| 4016 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4017 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4018 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4019 | static char** |
| 4020 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4021 | { |
| 4022 | int i; |
| 4023 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4024 | if (argvlist == NULL) { |
| 4025 | PyErr_NoMemory(); |
| 4026 | return NULL; |
| 4027 | } |
| 4028 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4029 | PyObject* item = PySequence_ITEM(argv, i); |
| 4030 | if (item == NULL) |
| 4031 | goto fail; |
| 4032 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4033 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4034 | goto fail; |
| 4035 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4036 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4037 | } |
| 4038 | argvlist[*argc] = NULL; |
| 4039 | return argvlist; |
| 4040 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4041 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4042 | free_string_array(argvlist, *argc); |
| 4043 | return NULL; |
| 4044 | } |
| 4045 | #endif |
| 4046 | |
| 4047 | #ifdef HAVE_EXECV |
| 4048 | PyDoc_STRVAR(posix_execv__doc__, |
| 4049 | "execv(path, args)\n\n\ |
| 4050 | Execute an executable path with arguments, replacing current process.\n\ |
| 4051 | \n\ |
| 4052 | path: path of executable file\n\ |
| 4053 | args: tuple or list of strings"); |
| 4054 | |
| 4055 | static PyObject * |
| 4056 | posix_execv(PyObject *self, PyObject *args) |
| 4057 | { |
| 4058 | PyObject *opath; |
| 4059 | char *path; |
| 4060 | PyObject *argv; |
| 4061 | char **argvlist; |
| 4062 | Py_ssize_t argc; |
| 4063 | |
| 4064 | /* execv has two arguments: (path, argv), where |
| 4065 | argv is a list or tuple of strings. */ |
| 4066 | |
| 4067 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4068 | PyUnicode_FSConverter, |
| 4069 | &opath, &argv)) |
| 4070 | return NULL; |
| 4071 | path = PyBytes_AsString(opath); |
| 4072 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4073 | PyErr_SetString(PyExc_TypeError, |
| 4074 | "execv() arg 2 must be a tuple or list"); |
| 4075 | Py_DECREF(opath); |
| 4076 | return NULL; |
| 4077 | } |
| 4078 | argc = PySequence_Size(argv); |
| 4079 | if (argc < 1) { |
| 4080 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4081 | Py_DECREF(opath); |
| 4082 | return NULL; |
| 4083 | } |
| 4084 | |
| 4085 | argvlist = parse_arglist(argv, &argc); |
| 4086 | if (argvlist == NULL) { |
| 4087 | Py_DECREF(opath); |
| 4088 | return NULL; |
| 4089 | } |
| 4090 | |
| 4091 | execv(path, argvlist); |
| 4092 | |
| 4093 | /* If we get here it's definitely an error */ |
| 4094 | |
| 4095 | free_string_array(argvlist, argc); |
| 4096 | Py_DECREF(opath); |
| 4097 | return posix_error(); |
| 4098 | } |
| 4099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4100 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4101 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4102 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4103 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4104 | path: path of executable file\n\ |
| 4105 | args: tuple or list of arguments\n\ |
| 4106 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4107 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4109 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4110 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4111 | PyObject *opath; |
| 4112 | char *path; |
| 4113 | PyObject *argv, *env; |
| 4114 | char **argvlist; |
| 4115 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4116 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4117 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4118 | /* execve has three arguments: (path, argv, env), where |
| 4119 | argv is a list or tuple of strings and env is a dictionary |
| 4120 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4122 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4123 | PyUnicode_FSConverter, |
| 4124 | &opath, &argv, &env)) |
| 4125 | return NULL; |
| 4126 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4127 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4128 | PyErr_SetString(PyExc_TypeError, |
| 4129 | "execve() arg 2 must be a tuple or list"); |
| 4130 | goto fail_0; |
| 4131 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4132 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4133 | if (!PyMapping_Check(env)) { |
| 4134 | PyErr_SetString(PyExc_TypeError, |
| 4135 | "execve() arg 3 must be a mapping object"); |
| 4136 | goto fail_0; |
| 4137 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4138 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4139 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4140 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4141 | goto fail_0; |
| 4142 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4143 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4144 | envlist = parse_envlist(env, &envc); |
| 4145 | if (envlist == NULL) |
| 4146 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4147 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4148 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4149 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4150 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4152 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4153 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4154 | while (--envc >= 0) |
| 4155 | PyMem_DEL(envlist[envc]); |
| 4156 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4157 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4158 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4159 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4160 | Py_DECREF(opath); |
| 4161 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4162 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4163 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4164 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4165 | #ifdef HAVE_FEXECVE |
| 4166 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4167 | "fexecve(fd, args, env)\n\n\ |
| 4168 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4169 | environment, replacing the current process.\n\ |
| 4170 | \n\ |
| 4171 | fd: file descriptor of executable\n\ |
| 4172 | args: tuple or list of arguments\n\ |
| 4173 | env: dictionary of strings mapping to strings"); |
| 4174 | |
| 4175 | static PyObject * |
| 4176 | posix_fexecve(PyObject *self, PyObject *args) |
| 4177 | { |
| 4178 | int fd; |
| 4179 | PyObject *argv, *env; |
| 4180 | char **argvlist; |
| 4181 | char **envlist; |
| 4182 | Py_ssize_t argc, envc; |
| 4183 | |
| 4184 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4185 | &fd, &argv, &env)) |
| 4186 | return NULL; |
| 4187 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4188 | PyErr_SetString(PyExc_TypeError, |
| 4189 | "fexecve() arg 2 must be a tuple or list"); |
| 4190 | return NULL; |
| 4191 | } |
| 4192 | argc = PySequence_Size(argv); |
| 4193 | if (!PyMapping_Check(env)) { |
| 4194 | PyErr_SetString(PyExc_TypeError, |
| 4195 | "fexecve() arg 3 must be a mapping object"); |
| 4196 | return NULL; |
| 4197 | } |
| 4198 | |
| 4199 | argvlist = parse_arglist(argv, &argc); |
| 4200 | if (argvlist == NULL) |
| 4201 | return NULL; |
| 4202 | |
| 4203 | envlist = parse_envlist(env, &envc); |
| 4204 | if (envlist == NULL) |
| 4205 | goto fail; |
| 4206 | |
| 4207 | fexecve(fd, argvlist, envlist); |
| 4208 | |
| 4209 | /* If we get here it's definitely an error */ |
| 4210 | |
| 4211 | (void) posix_error(); |
| 4212 | |
| 4213 | while (--envc >= 0) |
| 4214 | PyMem_DEL(envlist[envc]); |
| 4215 | PyMem_DEL(envlist); |
| 4216 | fail: |
| 4217 | free_string_array(argvlist, argc); |
| 4218 | return NULL; |
| 4219 | } |
| 4220 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4221 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4222 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4223 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4224 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4225 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4226 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4227 | mode: mode of process creation\n\ |
| 4228 | path: path of executable file\n\ |
| 4229 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4230 | |
| 4231 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4232 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4234 | PyObject *opath; |
| 4235 | char *path; |
| 4236 | PyObject *argv; |
| 4237 | char **argvlist; |
| 4238 | int mode, i; |
| 4239 | Py_ssize_t argc; |
| 4240 | Py_intptr_t spawnval; |
| 4241 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4243 | /* spawnv has three arguments: (mode, path, argv), where |
| 4244 | argv is a list or tuple of strings. */ |
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 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4247 | PyUnicode_FSConverter, |
| 4248 | &opath, &argv)) |
| 4249 | return NULL; |
| 4250 | path = PyBytes_AsString(opath); |
| 4251 | if (PyList_Check(argv)) { |
| 4252 | argc = PyList_Size(argv); |
| 4253 | getitem = PyList_GetItem; |
| 4254 | } |
| 4255 | else if (PyTuple_Check(argv)) { |
| 4256 | argc = PyTuple_Size(argv); |
| 4257 | getitem = PyTuple_GetItem; |
| 4258 | } |
| 4259 | else { |
| 4260 | PyErr_SetString(PyExc_TypeError, |
| 4261 | "spawnv() arg 2 must be a tuple or list"); |
| 4262 | Py_DECREF(opath); |
| 4263 | return NULL; |
| 4264 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4265 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4266 | argvlist = PyMem_NEW(char *, argc+1); |
| 4267 | if (argvlist == NULL) { |
| 4268 | Py_DECREF(opath); |
| 4269 | return PyErr_NoMemory(); |
| 4270 | } |
| 4271 | for (i = 0; i < argc; i++) { |
| 4272 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4273 | &argvlist[i])) { |
| 4274 | free_string_array(argvlist, i); |
| 4275 | PyErr_SetString( |
| 4276 | PyExc_TypeError, |
| 4277 | "spawnv() arg 2 must contain only strings"); |
| 4278 | Py_DECREF(opath); |
| 4279 | return NULL; |
| 4280 | } |
| 4281 | } |
| 4282 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4283 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4284 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4285 | Py_BEGIN_ALLOW_THREADS |
| 4286 | spawnval = spawnv(mode, path, argvlist); |
| 4287 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4288 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4289 | if (mode == _OLD_P_OVERLAY) |
| 4290 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4292 | Py_BEGIN_ALLOW_THREADS |
| 4293 | spawnval = _spawnv(mode, path, argvlist); |
| 4294 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4295 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4296 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4297 | free_string_array(argvlist, argc); |
| 4298 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4300 | if (spawnval == -1) |
| 4301 | return posix_error(); |
| 4302 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4303 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4304 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4305 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4306 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4307 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4311 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4312 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4313 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4314 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4315 | mode: mode of process creation\n\ |
| 4316 | path: path of executable file\n\ |
| 4317 | args: tuple or list of arguments\n\ |
| 4318 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4319 | |
| 4320 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4321 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4322 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4323 | PyObject *opath; |
| 4324 | char *path; |
| 4325 | PyObject *argv, *env; |
| 4326 | char **argvlist; |
| 4327 | char **envlist; |
| 4328 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4329 | int mode; |
| 4330 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4331 | Py_intptr_t spawnval; |
| 4332 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4333 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4335 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4336 | argv is a list or tuple of strings and env is a dictionary |
| 4337 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4338 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4339 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4340 | PyUnicode_FSConverter, |
| 4341 | &opath, &argv, &env)) |
| 4342 | return NULL; |
| 4343 | path = PyBytes_AsString(opath); |
| 4344 | if (PyList_Check(argv)) { |
| 4345 | argc = PyList_Size(argv); |
| 4346 | getitem = PyList_GetItem; |
| 4347 | } |
| 4348 | else if (PyTuple_Check(argv)) { |
| 4349 | argc = PyTuple_Size(argv); |
| 4350 | getitem = PyTuple_GetItem; |
| 4351 | } |
| 4352 | else { |
| 4353 | PyErr_SetString(PyExc_TypeError, |
| 4354 | "spawnve() arg 2 must be a tuple or list"); |
| 4355 | goto fail_0; |
| 4356 | } |
| 4357 | if (!PyMapping_Check(env)) { |
| 4358 | PyErr_SetString(PyExc_TypeError, |
| 4359 | "spawnve() arg 3 must be a mapping object"); |
| 4360 | goto fail_0; |
| 4361 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4363 | argvlist = PyMem_NEW(char *, argc+1); |
| 4364 | if (argvlist == NULL) { |
| 4365 | PyErr_NoMemory(); |
| 4366 | goto fail_0; |
| 4367 | } |
| 4368 | for (i = 0; i < argc; i++) { |
| 4369 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4370 | &argvlist[i])) |
| 4371 | { |
| 4372 | lastarg = i; |
| 4373 | goto fail_1; |
| 4374 | } |
| 4375 | } |
| 4376 | lastarg = argc; |
| 4377 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4379 | envlist = parse_envlist(env, &envc); |
| 4380 | if (envlist == NULL) |
| 4381 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4382 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4383 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4384 | Py_BEGIN_ALLOW_THREADS |
| 4385 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4386 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4387 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4388 | if (mode == _OLD_P_OVERLAY) |
| 4389 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4390 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4391 | Py_BEGIN_ALLOW_THREADS |
| 4392 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4393 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4394 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4395 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4396 | if (spawnval == -1) |
| 4397 | (void) posix_error(); |
| 4398 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4399 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4400 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4401 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4402 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4403 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4405 | while (--envc >= 0) |
| 4406 | PyMem_DEL(envlist[envc]); |
| 4407 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4408 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4409 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4410 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4411 | Py_DECREF(opath); |
| 4412 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4413 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4414 | |
| 4415 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4416 | #if defined(PYOS_OS2) |
| 4417 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4418 | "spawnvp(mode, file, args)\n\n\ |
| 4419 | Execute the program 'file' in a new process, using the environment\n\ |
| 4420 | search path to find the file.\n\ |
| 4421 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4422 | mode: mode of process creation\n\ |
| 4423 | file: executable file name\n\ |
| 4424 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4425 | |
| 4426 | static PyObject * |
| 4427 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4428 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4429 | PyObject *opath; |
| 4430 | char *path; |
| 4431 | PyObject *argv; |
| 4432 | char **argvlist; |
| 4433 | int mode, i, argc; |
| 4434 | Py_intptr_t spawnval; |
| 4435 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4437 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4438 | argv is a list or tuple of strings. */ |
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 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4441 | PyUnicode_FSConverter, |
| 4442 | &opath, &argv)) |
| 4443 | return NULL; |
| 4444 | path = PyBytes_AsString(opath); |
| 4445 | if (PyList_Check(argv)) { |
| 4446 | argc = PyList_Size(argv); |
| 4447 | getitem = PyList_GetItem; |
| 4448 | } |
| 4449 | else if (PyTuple_Check(argv)) { |
| 4450 | argc = PyTuple_Size(argv); |
| 4451 | getitem = PyTuple_GetItem; |
| 4452 | } |
| 4453 | else { |
| 4454 | PyErr_SetString(PyExc_TypeError, |
| 4455 | "spawnvp() arg 2 must be a tuple or list"); |
| 4456 | Py_DECREF(opath); |
| 4457 | return NULL; |
| 4458 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4459 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4460 | argvlist = PyMem_NEW(char *, argc+1); |
| 4461 | if (argvlist == NULL) { |
| 4462 | Py_DECREF(opath); |
| 4463 | return PyErr_NoMemory(); |
| 4464 | } |
| 4465 | for (i = 0; i < argc; i++) { |
| 4466 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4467 | &argvlist[i])) { |
| 4468 | free_string_array(argvlist, i); |
| 4469 | PyErr_SetString( |
| 4470 | PyExc_TypeError, |
| 4471 | "spawnvp() arg 2 must contain only strings"); |
| 4472 | Py_DECREF(opath); |
| 4473 | return NULL; |
| 4474 | } |
| 4475 | } |
| 4476 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4478 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4479 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4480 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4481 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4482 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4483 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4484 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4486 | free_string_array(argvlist, argc); |
| 4487 | Py_DECREF(opath); |
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 | if (spawnval == -1) |
| 4490 | return posix_error(); |
| 4491 | else |
| 4492 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | |
| 4496 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4497 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4498 | Execute the program 'file' in a new process, using the environment\n\ |
| 4499 | search path to find the file.\n\ |
| 4500 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4501 | mode: mode of process creation\n\ |
| 4502 | file: executable file name\n\ |
| 4503 | args: tuple or list of arguments\n\ |
| 4504 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4505 | |
| 4506 | static PyObject * |
| 4507 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4508 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4509 | PyObject *opath |
| 4510 | char *path; |
| 4511 | PyObject *argv, *env; |
| 4512 | char **argvlist; |
| 4513 | char **envlist; |
| 4514 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4515 | int mode; |
| 4516 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4517 | Py_intptr_t spawnval; |
| 4518 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4519 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4520 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4521 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4522 | argv is a list or tuple of strings and env is a dictionary |
| 4523 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4525 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4526 | PyUnicode_FSConverter, |
| 4527 | &opath, &argv, &env)) |
| 4528 | return NULL; |
| 4529 | path = PyBytes_AsString(opath); |
| 4530 | if (PyList_Check(argv)) { |
| 4531 | argc = PyList_Size(argv); |
| 4532 | getitem = PyList_GetItem; |
| 4533 | } |
| 4534 | else if (PyTuple_Check(argv)) { |
| 4535 | argc = PyTuple_Size(argv); |
| 4536 | getitem = PyTuple_GetItem; |
| 4537 | } |
| 4538 | else { |
| 4539 | PyErr_SetString(PyExc_TypeError, |
| 4540 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4541 | goto fail_0; |
| 4542 | } |
| 4543 | if (!PyMapping_Check(env)) { |
| 4544 | PyErr_SetString(PyExc_TypeError, |
| 4545 | "spawnvpe() arg 3 must be a mapping object"); |
| 4546 | goto fail_0; |
| 4547 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4549 | argvlist = PyMem_NEW(char *, argc+1); |
| 4550 | if (argvlist == NULL) { |
| 4551 | PyErr_NoMemory(); |
| 4552 | goto fail_0; |
| 4553 | } |
| 4554 | for (i = 0; i < argc; i++) { |
| 4555 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4556 | &argvlist[i])) |
| 4557 | { |
| 4558 | lastarg = i; |
| 4559 | goto fail_1; |
| 4560 | } |
| 4561 | } |
| 4562 | lastarg = argc; |
| 4563 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4565 | envlist = parse_envlist(env, &envc); |
| 4566 | if (envlist == NULL) |
| 4567 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4568 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4569 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4570 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4571 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4572 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4573 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4574 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4575 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4576 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4577 | if (spawnval == -1) |
| 4578 | (void) posix_error(); |
| 4579 | else |
| 4580 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4582 | while (--envc >= 0) |
| 4583 | PyMem_DEL(envlist[envc]); |
| 4584 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4585 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4586 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4587 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4588 | Py_DECREF(opath); |
| 4589 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4590 | } |
| 4591 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4592 | #endif /* HAVE_SPAWNV */ |
| 4593 | |
| 4594 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4595 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4596 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4597 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4598 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4599 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4600 | 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] | 4601 | |
| 4602 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4603 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4604 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4605 | pid_t pid; |
| 4606 | int result = 0; |
| 4607 | _PyImport_AcquireLock(); |
| 4608 | pid = fork1(); |
| 4609 | if (pid == 0) { |
| 4610 | /* child: this clobbers and resets the import lock. */ |
| 4611 | PyOS_AfterFork(); |
| 4612 | } else { |
| 4613 | /* parent: release the import lock. */ |
| 4614 | result = _PyImport_ReleaseLock(); |
| 4615 | } |
| 4616 | if (pid == -1) |
| 4617 | return posix_error(); |
| 4618 | if (result < 0) { |
| 4619 | /* Don't clobber the OSError if the fork failed. */ |
| 4620 | PyErr_SetString(PyExc_RuntimeError, |
| 4621 | "not holding the import lock"); |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4625 | } |
| 4626 | #endif |
| 4627 | |
| 4628 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4629 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4630 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4631 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4632 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4633 | 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] | 4634 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4635 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4636 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4637 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4638 | pid_t pid; |
| 4639 | int result = 0; |
| 4640 | _PyImport_AcquireLock(); |
| 4641 | pid = fork(); |
| 4642 | if (pid == 0) { |
| 4643 | /* child: this clobbers and resets the import lock. */ |
| 4644 | PyOS_AfterFork(); |
| 4645 | } else { |
| 4646 | /* parent: release the import lock. */ |
| 4647 | result = _PyImport_ReleaseLock(); |
| 4648 | } |
| 4649 | if (pid == -1) |
| 4650 | return posix_error(); |
| 4651 | if (result < 0) { |
| 4652 | /* Don't clobber the OSError if the fork failed. */ |
| 4653 | PyErr_SetString(PyExc_RuntimeError, |
| 4654 | "not holding the import lock"); |
| 4655 | return NULL; |
| 4656 | } |
| 4657 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4658 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4659 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4660 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4661 | #ifdef HAVE_SCHED_H |
| 4662 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4663 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4664 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4665 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4666 | "sched_get_priority_max(policy)\n\n\ |
| 4667 | Get the maximum scheduling priority for *policy*."); |
| 4668 | |
| 4669 | static PyObject * |
| 4670 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4671 | { |
| 4672 | int policy, max; |
| 4673 | |
| 4674 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4675 | return NULL; |
| 4676 | max = sched_get_priority_max(policy); |
| 4677 | if (max < 0) |
| 4678 | return posix_error(); |
| 4679 | return PyLong_FromLong(max); |
| 4680 | } |
| 4681 | |
| 4682 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4683 | "sched_get_priority_min(policy)\n\n\ |
| 4684 | Get the minimum scheduling priority for *policy*."); |
| 4685 | |
| 4686 | static PyObject * |
| 4687 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4688 | { |
| 4689 | int policy, min; |
| 4690 | |
| 4691 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4692 | return NULL; |
| 4693 | min = sched_get_priority_min(policy); |
| 4694 | if (min < 0) |
| 4695 | return posix_error(); |
| 4696 | return PyLong_FromLong(min); |
| 4697 | } |
| 4698 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4699 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4700 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4701 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4702 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4703 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4704 | "sched_getscheduler(pid)\n\n\ |
| 4705 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4706 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4707 | |
| 4708 | static PyObject * |
| 4709 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4710 | { |
| 4711 | pid_t pid; |
| 4712 | int policy; |
| 4713 | |
| 4714 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4715 | return NULL; |
| 4716 | policy = sched_getscheduler(pid); |
| 4717 | if (policy < 0) |
| 4718 | return posix_error(); |
| 4719 | return PyLong_FromLong(policy); |
| 4720 | } |
| 4721 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4722 | #endif |
| 4723 | |
| 4724 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4725 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4726 | static PyObject * |
| 4727 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4728 | { |
| 4729 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4730 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4731 | |
| 4732 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4733 | return NULL; |
| 4734 | res = PyStructSequence_New(type); |
| 4735 | if (!res) |
| 4736 | return NULL; |
| 4737 | Py_INCREF(priority); |
| 4738 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4739 | return res; |
| 4740 | } |
| 4741 | |
| 4742 | PyDoc_STRVAR(sched_param__doc__, |
| 4743 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4744 | Current has only one field: sched_priority"); |
| 4745 | |
| 4746 | static PyStructSequence_Field sched_param_fields[] = { |
| 4747 | {"sched_priority", "the scheduling priority"}, |
| 4748 | {0} |
| 4749 | }; |
| 4750 | |
| 4751 | static PyStructSequence_Desc sched_param_desc = { |
| 4752 | "sched_param", /* name */ |
| 4753 | sched_param__doc__, /* doc */ |
| 4754 | sched_param_fields, |
| 4755 | 1 |
| 4756 | }; |
| 4757 | |
| 4758 | static int |
| 4759 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4760 | { |
| 4761 | long priority; |
| 4762 | |
| 4763 | if (Py_TYPE(param) != &SchedParamType) { |
| 4764 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4765 | return 0; |
| 4766 | } |
| 4767 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4768 | if (priority == -1 && PyErr_Occurred()) |
| 4769 | return 0; |
| 4770 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4771 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4772 | return 0; |
| 4773 | } |
| 4774 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4775 | return 1; |
| 4776 | } |
| 4777 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4778 | #endif |
| 4779 | |
| 4780 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4781 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4782 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4783 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4784 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4785 | If *pid* is 0, the calling process is changed.\n\ |
| 4786 | *param* is an instance of sched_param."); |
| 4787 | |
| 4788 | static PyObject * |
| 4789 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4790 | { |
| 4791 | pid_t pid; |
| 4792 | int policy; |
| 4793 | struct sched_param param; |
| 4794 | |
| 4795 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4796 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4797 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4798 | |
| 4799 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4800 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4801 | ** scheduling policy under Solaris/Illumos, and others. |
| 4802 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4803 | */ |
| 4804 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4805 | return posix_error(); |
| 4806 | Py_RETURN_NONE; |
| 4807 | } |
| 4808 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4809 | #endif |
| 4810 | |
| 4811 | #ifdef HAVE_SCHED_SETPARAM |
| 4812 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4813 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4814 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4815 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4816 | sched_param class. A PID of 0 means the calling process."); |
| 4817 | |
| 4818 | static PyObject * |
| 4819 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4820 | { |
| 4821 | pid_t pid; |
| 4822 | struct sched_param param; |
| 4823 | PyObject *res, *priority; |
| 4824 | |
| 4825 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4826 | return NULL; |
| 4827 | if (sched_getparam(pid, ¶m)) |
| 4828 | return posix_error(); |
| 4829 | res = PyStructSequence_New(&SchedParamType); |
| 4830 | if (!res) |
| 4831 | return NULL; |
| 4832 | priority = PyLong_FromLong(param.sched_priority); |
| 4833 | if (!priority) { |
| 4834 | Py_DECREF(res); |
| 4835 | return NULL; |
| 4836 | } |
| 4837 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4838 | return res; |
| 4839 | } |
| 4840 | |
| 4841 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4842 | "sched_setparam(pid, param)\n\n\ |
| 4843 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4844 | A PID of 0 means the calling process."); |
| 4845 | |
| 4846 | static PyObject * |
| 4847 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4848 | { |
| 4849 | pid_t pid; |
| 4850 | struct sched_param param; |
| 4851 | |
| 4852 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4853 | &pid, &convert_sched_param, ¶m)) |
| 4854 | return NULL; |
| 4855 | if (sched_setparam(pid, ¶m)) |
| 4856 | return posix_error(); |
| 4857 | Py_RETURN_NONE; |
| 4858 | } |
| 4859 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4860 | #endif |
| 4861 | |
| 4862 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4863 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4864 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4865 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4866 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4867 | |
| 4868 | static PyObject * |
| 4869 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4870 | { |
| 4871 | pid_t pid; |
| 4872 | struct timespec interval; |
| 4873 | |
| 4874 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4875 | return NULL; |
| 4876 | if (sched_rr_get_interval(pid, &interval)) |
| 4877 | return posix_error(); |
| 4878 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4879 | } |
| 4880 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4881 | #endif |
| 4882 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4883 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4884 | "sched_yield()\n\n\ |
| 4885 | Voluntarily relinquish the CPU."); |
| 4886 | |
| 4887 | static PyObject * |
| 4888 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4889 | { |
| 4890 | if (sched_yield()) |
| 4891 | return posix_error(); |
| 4892 | Py_RETURN_NONE; |
| 4893 | } |
| 4894 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4895 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4896 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4897 | typedef struct { |
| 4898 | PyObject_HEAD; |
| 4899 | Py_ssize_t size; |
| 4900 | int ncpus; |
| 4901 | cpu_set_t *set; |
| 4902 | } Py_cpu_set; |
| 4903 | |
| 4904 | static PyTypeObject cpu_set_type; |
| 4905 | |
| 4906 | static void |
| 4907 | cpu_set_dealloc(Py_cpu_set *set) |
| 4908 | { |
| 4909 | assert(set->set); |
| 4910 | CPU_FREE(set->set); |
| 4911 | Py_TYPE(set)->tp_free(set); |
| 4912 | } |
| 4913 | |
| 4914 | static Py_cpu_set * |
| 4915 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4916 | { |
| 4917 | Py_cpu_set *set; |
| 4918 | |
| 4919 | if (size < 0) { |
| 4920 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4921 | return NULL; |
| 4922 | } |
| 4923 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4924 | if (!set) |
| 4925 | return NULL; |
| 4926 | set->ncpus = size; |
| 4927 | set->size = CPU_ALLOC_SIZE(size); |
| 4928 | set->set = CPU_ALLOC(size); |
| 4929 | if (!set->set) { |
| 4930 | type->tp_free(set); |
| 4931 | PyErr_NoMemory(); |
| 4932 | return NULL; |
| 4933 | } |
| 4934 | CPU_ZERO_S(set->size, set->set); |
| 4935 | return set; |
| 4936 | } |
| 4937 | |
| 4938 | static PyObject * |
| 4939 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4940 | { |
| 4941 | int size; |
| 4942 | |
| 4943 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4944 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4945 | return NULL; |
| 4946 | return (PyObject *)make_new_cpu_set(type, size); |
| 4947 | } |
| 4948 | |
| 4949 | static PyObject * |
| 4950 | cpu_set_repr(Py_cpu_set *set) |
| 4951 | { |
| 4952 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4953 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4954 | |
| 4955 | static Py_ssize_t |
| 4956 | cpu_set_len(Py_cpu_set *set) |
| 4957 | { |
| 4958 | return set->ncpus; |
| 4959 | } |
| 4960 | |
| 4961 | static int |
| 4962 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4963 | { |
| 4964 | int cpu; |
| 4965 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4966 | return -1; |
| 4967 | if (cpu < 0) { |
| 4968 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4969 | return -1; |
| 4970 | } |
| 4971 | if (cpu >= set->ncpus) { |
| 4972 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4973 | return -1; |
| 4974 | } |
| 4975 | return cpu; |
| 4976 | } |
| 4977 | |
| 4978 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4979 | "cpu_set.set(i)\n\n\ |
| 4980 | Add CPU *i* to the set."); |
| 4981 | |
| 4982 | static PyObject * |
| 4983 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 4984 | { |
| 4985 | int cpu = _get_cpu(set, "i|set", args); |
| 4986 | if (cpu == -1) |
| 4987 | return NULL; |
| 4988 | CPU_SET_S(cpu, set->size, set->set); |
| 4989 | Py_RETURN_NONE; |
| 4990 | } |
| 4991 | |
| 4992 | PyDoc_STRVAR(cpu_set_count_doc, |
| 4993 | "cpu_set.count() -> int\n\n\ |
| 4994 | Return the number of CPUs active in the set."); |
| 4995 | |
| 4996 | static PyObject * |
| 4997 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 4998 | { |
| 4999 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5000 | } |
| 5001 | |
| 5002 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5003 | "cpu_set.clear(i)\n\n\ |
| 5004 | Remove CPU *i* from the set."); |
| 5005 | |
| 5006 | static PyObject * |
| 5007 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5008 | { |
| 5009 | int cpu = _get_cpu(set, "i|clear", args); |
| 5010 | if (cpu == -1) |
| 5011 | return NULL; |
| 5012 | CPU_CLR_S(cpu, set->size, set->set); |
| 5013 | Py_RETURN_NONE; |
| 5014 | } |
| 5015 | |
| 5016 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5017 | "cpu_set.isset(i) -> bool\n\n\ |
| 5018 | Test if CPU *i* is in the set."); |
| 5019 | |
| 5020 | static PyObject * |
| 5021 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5022 | { |
| 5023 | int cpu = _get_cpu(set, "i|isset", args); |
| 5024 | if (cpu == -1) |
| 5025 | return NULL; |
| 5026 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5027 | Py_RETURN_TRUE; |
| 5028 | Py_RETURN_FALSE; |
| 5029 | } |
| 5030 | |
| 5031 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5032 | "cpu_set.zero()\n\n\ |
| 5033 | Clear the cpu_set."); |
| 5034 | |
| 5035 | static PyObject * |
| 5036 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5037 | { |
| 5038 | CPU_ZERO_S(set->size, set->set); |
| 5039 | Py_RETURN_NONE; |
| 5040 | } |
| 5041 | |
| 5042 | static PyObject * |
| 5043 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5044 | { |
| 5045 | int eq; |
| 5046 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5047 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5048 | Py_RETURN_NOTIMPLEMENTED; |
| 5049 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5050 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5051 | if ((op == Py_EQ) ? eq : !eq) |
| 5052 | Py_RETURN_TRUE; |
| 5053 | else |
| 5054 | Py_RETURN_FALSE; |
| 5055 | } |
| 5056 | |
| 5057 | #define CPU_SET_BINOP(name, op) \ |
| 5058 | static PyObject * \ |
| 5059 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5060 | if (res) { \ |
| 5061 | Py_INCREF(res); \ |
| 5062 | } \ |
| 5063 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5064 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5065 | if (!res) \ |
| 5066 | return NULL; \ |
| 5067 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5068 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5069 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5070 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5071 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5072 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5073 | op(res->size, res->set, left->set, right->set); \ |
| 5074 | return (PyObject *)res; \ |
| 5075 | } \ |
| 5076 | static PyObject * \ |
| 5077 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5078 | return do_cpu_set_##name(left, right, NULL); \ |
| 5079 | } \ |
| 5080 | static PyObject * \ |
| 5081 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5082 | return do_cpu_set_##name(left, right, left); \ |
| 5083 | } \ |
| 5084 | |
| 5085 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5086 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5087 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5088 | #undef CPU_SET_BINOP |
| 5089 | |
| 5090 | PyDoc_STRVAR(cpu_set_doc, |
| 5091 | "cpu_set(size)\n\n\ |
| 5092 | Create an empty mask of CPUs."); |
| 5093 | |
| 5094 | static PyNumberMethods cpu_set_as_number = { |
| 5095 | 0, /*nb_add*/ |
| 5096 | 0, /*nb_subtract*/ |
| 5097 | 0, /*nb_multiply*/ |
| 5098 | 0, /*nb_remainder*/ |
| 5099 | 0, /*nb_divmod*/ |
| 5100 | 0, /*nb_power*/ |
| 5101 | 0, /*nb_negative*/ |
| 5102 | 0, /*nb_positive*/ |
| 5103 | 0, /*nb_absolute*/ |
| 5104 | 0, /*nb_bool*/ |
| 5105 | 0, /*nb_invert*/ |
| 5106 | 0, /*nb_lshift*/ |
| 5107 | 0, /*nb_rshift*/ |
| 5108 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5109 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5110 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5111 | 0, /*nb_int*/ |
| 5112 | 0, /*nb_reserved*/ |
| 5113 | 0, /*nb_float*/ |
| 5114 | 0, /*nb_inplace_add*/ |
| 5115 | 0, /*nb_inplace_subtract*/ |
| 5116 | 0, /*nb_inplace_multiply*/ |
| 5117 | 0, /*nb_inplace_remainder*/ |
| 5118 | 0, /*nb_inplace_power*/ |
| 5119 | 0, /*nb_inplace_lshift*/ |
| 5120 | 0, /*nb_inplace_rshift*/ |
| 5121 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5122 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5123 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5124 | }; |
| 5125 | |
| 5126 | static PySequenceMethods cpu_set_as_sequence = { |
| 5127 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5128 | }; |
| 5129 | |
| 5130 | static PyMethodDef cpu_set_methods[] = { |
| 5131 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5132 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5133 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5134 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5135 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5136 | {NULL, NULL} /* sentinel */ |
| 5137 | }; |
| 5138 | |
| 5139 | static PyTypeObject cpu_set_type = { |
| 5140 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5141 | "posix.cpu_set", /* tp_name */ |
| 5142 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5143 | 0, /* tp_itemsize */ |
| 5144 | /* methods */ |
| 5145 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5146 | 0, /* tp_print */ |
| 5147 | 0, /* tp_getattr */ |
| 5148 | 0, /* tp_setattr */ |
| 5149 | 0, /* tp_reserved */ |
| 5150 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5151 | &cpu_set_as_number, /* tp_as_number */ |
| 5152 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5153 | 0, /* tp_as_mapping */ |
| 5154 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5155 | 0, /* tp_call */ |
| 5156 | 0, /* tp_str */ |
| 5157 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5158 | 0, /* tp_setattro */ |
| 5159 | 0, /* tp_as_buffer */ |
| 5160 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5161 | cpu_set_doc, /* tp_doc */ |
| 5162 | 0, /* tp_traverse */ |
| 5163 | 0, /* tp_clear */ |
| 5164 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5165 | 0, /* tp_weaklistoffset */ |
| 5166 | 0, /* tp_iter */ |
| 5167 | 0, /* tp_iternext */ |
| 5168 | cpu_set_methods, /* tp_methods */ |
| 5169 | 0, /* tp_members */ |
| 5170 | 0, /* tp_getset */ |
| 5171 | 0, /* tp_base */ |
| 5172 | 0, /* tp_dict */ |
| 5173 | 0, /* tp_descr_get */ |
| 5174 | 0, /* tp_descr_set */ |
| 5175 | 0, /* tp_dictoffset */ |
| 5176 | 0, /* tp_init */ |
| 5177 | PyType_GenericAlloc, /* tp_alloc */ |
| 5178 | cpu_set_new, /* tp_new */ |
| 5179 | PyObject_Del, /* tp_free */ |
| 5180 | }; |
| 5181 | |
| 5182 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5183 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5184 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5185 | |
| 5186 | static PyObject * |
| 5187 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5188 | { |
| 5189 | pid_t pid; |
| 5190 | Py_cpu_set *cpu_set; |
| 5191 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5192 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5193 | &pid, &cpu_set_type, &cpu_set)) |
| 5194 | return NULL; |
| 5195 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5196 | return posix_error(); |
| 5197 | Py_RETURN_NONE; |
| 5198 | } |
| 5199 | |
| 5200 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5201 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5202 | Return the affinity of the process with PID *pid*.\n\ |
| 5203 | The returned cpu_set will be of size *ncpus*."); |
| 5204 | |
| 5205 | static PyObject * |
| 5206 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5207 | { |
| 5208 | pid_t pid; |
| 5209 | int ncpus; |
| 5210 | Py_cpu_set *res; |
| 5211 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5212 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5213 | &pid, &ncpus)) |
| 5214 | return NULL; |
| 5215 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5216 | if (!res) |
| 5217 | return NULL; |
| 5218 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5219 | Py_DECREF(res); |
| 5220 | return posix_error(); |
| 5221 | } |
| 5222 | return (PyObject *)res; |
| 5223 | } |
| 5224 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5225 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5226 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5227 | #endif /* HAVE_SCHED_H */ |
| 5228 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5229 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5230 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5231 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5232 | #define DEV_PTY_FILE "/dev/ptc" |
| 5233 | #define HAVE_DEV_PTMX |
| 5234 | #else |
| 5235 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5236 | #endif |
| 5237 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5238 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5239 | #ifdef HAVE_PTY_H |
| 5240 | #include <pty.h> |
| 5241 | #else |
| 5242 | #ifdef HAVE_LIBUTIL_H |
| 5243 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5244 | #else |
| 5245 | #ifdef HAVE_UTIL_H |
| 5246 | #include <util.h> |
| 5247 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5248 | #endif /* HAVE_LIBUTIL_H */ |
| 5249 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5250 | #ifdef HAVE_STROPTS_H |
| 5251 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5252 | #endif |
| 5253 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5254 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5255 | #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] | 5256 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5257 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5258 | 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] | 5259 | |
| 5260 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5261 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5262 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5263 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5264 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5265 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5266 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5267 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5268 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5269 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5271 | #endif |
| 5272 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5273 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5274 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5275 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5276 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5277 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5278 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5279 | if (slave_name == NULL) |
| 5280 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5281 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5282 | slave_fd = open(slave_name, O_RDWR); |
| 5283 | if (slave_fd < 0) |
| 5284 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5285 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5286 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5287 | if (master_fd < 0) |
| 5288 | return posix_error(); |
| 5289 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5290 | /* change permission of slave */ |
| 5291 | if (grantpt(master_fd) < 0) { |
| 5292 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5293 | return posix_error(); |
| 5294 | } |
| 5295 | /* unlock slave */ |
| 5296 | if (unlockpt(master_fd) < 0) { |
| 5297 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5298 | return posix_error(); |
| 5299 | } |
| 5300 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5301 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5302 | if (slave_name == NULL) |
| 5303 | return posix_error(); |
| 5304 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5305 | if (slave_fd < 0) |
| 5306 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5307 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5308 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5309 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5310 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5311 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5312 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5313 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5314 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5316 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5317 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5318 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5319 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5320 | |
| 5321 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5322 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5323 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5324 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5325 | 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] | 5326 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5327 | |
| 5328 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5329 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5330 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5331 | int master_fd = -1, result = 0; |
| 5332 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5333 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5334 | _PyImport_AcquireLock(); |
| 5335 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5336 | if (pid == 0) { |
| 5337 | /* child: this clobbers and resets the import lock. */ |
| 5338 | PyOS_AfterFork(); |
| 5339 | } else { |
| 5340 | /* parent: release the import lock. */ |
| 5341 | result = _PyImport_ReleaseLock(); |
| 5342 | } |
| 5343 | if (pid == -1) |
| 5344 | return posix_error(); |
| 5345 | if (result < 0) { |
| 5346 | /* Don't clobber the OSError if the fork failed. */ |
| 5347 | PyErr_SetString(PyExc_RuntimeError, |
| 5348 | "not holding the import lock"); |
| 5349 | return NULL; |
| 5350 | } |
| 5351 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5352 | } |
| 5353 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5354 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5355 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5356 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5357 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5358 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5359 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5360 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5361 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5362 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5364 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5365 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5366 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5367 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5368 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5369 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5370 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5371 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5372 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5373 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5374 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5375 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5376 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5377 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5378 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5379 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5380 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5381 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5382 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5383 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5384 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5385 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5386 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5387 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5388 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5389 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5390 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5391 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5392 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5393 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5395 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5396 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5397 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5398 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5399 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5400 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5401 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5402 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5405 | #ifdef HAVE_GETGROUPLIST |
| 5406 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5407 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5408 | Returns a list of groups to which a user belongs.\n\n\ |
| 5409 | user: username to lookup\n\ |
| 5410 | group: base group id of the user"); |
| 5411 | |
| 5412 | static PyObject * |
| 5413 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5414 | { |
| 5415 | #ifdef NGROUPS_MAX |
| 5416 | #define MAX_GROUPS NGROUPS_MAX |
| 5417 | #else |
| 5418 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5419 | #define MAX_GROUPS 64 |
| 5420 | #endif |
| 5421 | |
| 5422 | const char *user; |
| 5423 | int i, ngroups; |
| 5424 | PyObject *list; |
| 5425 | #ifdef __APPLE__ |
| 5426 | int *groups, basegid; |
| 5427 | #else |
| 5428 | gid_t *groups, basegid; |
| 5429 | #endif |
| 5430 | ngroups = MAX_GROUPS; |
| 5431 | |
| 5432 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5433 | return NULL; |
| 5434 | |
| 5435 | #ifdef __APPLE__ |
| 5436 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5437 | #else |
| 5438 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5439 | #endif |
| 5440 | if (groups == NULL) |
| 5441 | return PyErr_NoMemory(); |
| 5442 | |
| 5443 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5444 | PyMem_Del(groups); |
| 5445 | return posix_error(); |
| 5446 | } |
| 5447 | |
| 5448 | list = PyList_New(ngroups); |
| 5449 | if (list == NULL) { |
| 5450 | PyMem_Del(groups); |
| 5451 | return NULL; |
| 5452 | } |
| 5453 | |
| 5454 | for (i = 0; i < ngroups; i++) { |
| 5455 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5456 | if (o == NULL) { |
| 5457 | Py_DECREF(list); |
| 5458 | PyMem_Del(groups); |
| 5459 | return NULL; |
| 5460 | } |
| 5461 | PyList_SET_ITEM(list, i, o); |
| 5462 | } |
| 5463 | |
| 5464 | PyMem_Del(groups); |
| 5465 | |
| 5466 | return list; |
| 5467 | } |
| 5468 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5469 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5470 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5471 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5472 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5473 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5474 | |
| 5475 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5476 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5477 | { |
| 5478 | PyObject *result = NULL; |
| 5479 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5480 | #ifdef NGROUPS_MAX |
| 5481 | #define MAX_GROUPS NGROUPS_MAX |
| 5482 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5483 | /* 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] | 5484 | #define MAX_GROUPS 64 |
| 5485 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5486 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5487 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5488 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5489 | * This is a helper variable to store the intermediate result when |
| 5490 | * that happens. |
| 5491 | * |
| 5492 | * To keep the code readable the OSX behaviour is unconditional, |
| 5493 | * according to the POSIX spec this should be safe on all unix-y |
| 5494 | * systems. |
| 5495 | */ |
| 5496 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5497 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5498 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5499 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5500 | if (n < 0) { |
| 5501 | if (errno == EINVAL) { |
| 5502 | n = getgroups(0, NULL); |
| 5503 | if (n == -1) { |
| 5504 | return posix_error(); |
| 5505 | } |
| 5506 | if (n == 0) { |
| 5507 | /* Avoid malloc(0) */ |
| 5508 | alt_grouplist = grouplist; |
| 5509 | } else { |
| 5510 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5511 | if (alt_grouplist == NULL) { |
| 5512 | errno = EINVAL; |
| 5513 | return posix_error(); |
| 5514 | } |
| 5515 | n = getgroups(n, alt_grouplist); |
| 5516 | if (n == -1) { |
| 5517 | PyMem_Free(alt_grouplist); |
| 5518 | return posix_error(); |
| 5519 | } |
| 5520 | } |
| 5521 | } else { |
| 5522 | return posix_error(); |
| 5523 | } |
| 5524 | } |
| 5525 | result = PyList_New(n); |
| 5526 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5527 | int i; |
| 5528 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5529 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5530 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5531 | Py_DECREF(result); |
| 5532 | result = NULL; |
| 5533 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5534 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5535 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5536 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | if (alt_grouplist != grouplist) { |
| 5540 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5541 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5542 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5543 | return result; |
| 5544 | } |
| 5545 | #endif |
| 5546 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5547 | #ifdef HAVE_INITGROUPS |
| 5548 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5549 | "initgroups(username, gid) -> None\n\n\ |
| 5550 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5551 | the groups of which the specified username is a member, plus the specified\n\ |
| 5552 | group id."); |
| 5553 | |
| 5554 | static PyObject * |
| 5555 | posix_initgroups(PyObject *self, PyObject *args) |
| 5556 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5557 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5558 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5559 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5560 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5561 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5562 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5563 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5564 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5565 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5566 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5567 | res = initgroups(username, (gid_t) gid); |
| 5568 | Py_DECREF(oname); |
| 5569 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5570 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5572 | Py_INCREF(Py_None); |
| 5573 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5574 | } |
| 5575 | #endif |
| 5576 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5577 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5578 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5579 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5580 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5581 | |
| 5582 | static PyObject * |
| 5583 | posix_getpgid(PyObject *self, PyObject *args) |
| 5584 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5585 | pid_t pid, pgid; |
| 5586 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5587 | return NULL; |
| 5588 | pgid = getpgid(pid); |
| 5589 | if (pgid < 0) |
| 5590 | return posix_error(); |
| 5591 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5592 | } |
| 5593 | #endif /* HAVE_GETPGID */ |
| 5594 | |
| 5595 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5596 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5597 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5598 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5599 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5600 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5601 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5602 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5603 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5604 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5605 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5606 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5607 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5608 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5609 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5610 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5611 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5612 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5613 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5614 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5615 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5616 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5617 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5618 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5619 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5620 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5621 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5622 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5623 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5624 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5625 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5626 | return posix_error(); |
| 5627 | Py_INCREF(Py_None); |
| 5628 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5631 | #endif /* HAVE_SETPGRP */ |
| 5632 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5633 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5634 | |
| 5635 | #ifdef MS_WINDOWS |
| 5636 | #include <tlhelp32.h> |
| 5637 | |
| 5638 | static PyObject* |
| 5639 | win32_getppid() |
| 5640 | { |
| 5641 | HANDLE snapshot; |
| 5642 | pid_t mypid; |
| 5643 | PyObject* result = NULL; |
| 5644 | BOOL have_record; |
| 5645 | PROCESSENTRY32 pe; |
| 5646 | |
| 5647 | mypid = getpid(); /* This function never fails */ |
| 5648 | |
| 5649 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5650 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5651 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5652 | |
| 5653 | pe.dwSize = sizeof(pe); |
| 5654 | have_record = Process32First(snapshot, &pe); |
| 5655 | while (have_record) { |
| 5656 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5657 | /* We could cache the ulong value in a static variable. */ |
| 5658 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5659 | break; |
| 5660 | } |
| 5661 | |
| 5662 | have_record = Process32Next(snapshot, &pe); |
| 5663 | } |
| 5664 | |
| 5665 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5666 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5667 | * error anyway, so let's raise it. */ |
| 5668 | if (!result) |
| 5669 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5670 | |
| 5671 | CloseHandle(snapshot); |
| 5672 | |
| 5673 | return result; |
| 5674 | } |
| 5675 | #endif /*MS_WINDOWS*/ |
| 5676 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5677 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5678 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5679 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5680 | Windows machines will still return its id; others systems will return the id\n\ |
| 5681 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5683 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5684 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5685 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5686 | #ifdef MS_WINDOWS |
| 5687 | return win32_getppid(); |
| 5688 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5689 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5690 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5691 | } |
| 5692 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5693 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5694 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5695 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5696 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5697 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5698 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5699 | |
| 5700 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5701 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5702 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5703 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5704 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5705 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5706 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5707 | |
| 5708 | if (GetUserNameW(user_name, &num_chars)) { |
| 5709 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5710 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5711 | } |
| 5712 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5713 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5714 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5715 | char *name; |
| 5716 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5717 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5718 | errno = 0; |
| 5719 | name = getlogin(); |
| 5720 | if (name == NULL) { |
| 5721 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5722 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5723 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5724 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5725 | } |
| 5726 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5727 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5728 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5729 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5730 | return result; |
| 5731 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5732 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5733 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5734 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5735 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5736 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5737 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5738 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5739 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5740 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5741 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5742 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5743 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5744 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5745 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5746 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5747 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5748 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5749 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5750 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5751 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5752 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5753 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5754 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5755 | pid_t pid; |
| 5756 | int sig; |
| 5757 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5758 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5759 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5760 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5761 | APIRET rc; |
| 5762 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5763 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5764 | |
| 5765 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5766 | APIRET rc; |
| 5767 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5768 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5769 | |
| 5770 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5771 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5772 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5773 | if (kill(pid, sig) == -1) |
| 5774 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5775 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5776 | Py_INCREF(Py_None); |
| 5777 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5778 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5779 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5780 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5781 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5782 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5783 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5784 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5785 | |
| 5786 | static PyObject * |
| 5787 | posix_killpg(PyObject *self, PyObject *args) |
| 5788 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5789 | int sig; |
| 5790 | pid_t pgid; |
| 5791 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5792 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5793 | take the same type. Moreover, pid_t is always at least as wide as |
| 5794 | int (else compilation of this module fails), which is safe. */ |
| 5795 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5796 | return NULL; |
| 5797 | if (killpg(pgid, sig) == -1) |
| 5798 | return posix_error(); |
| 5799 | Py_INCREF(Py_None); |
| 5800 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5801 | } |
| 5802 | #endif |
| 5803 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5804 | #ifdef MS_WINDOWS |
| 5805 | PyDoc_STRVAR(win32_kill__doc__, |
| 5806 | "kill(pid, sig)\n\n\ |
| 5807 | Kill a process with a signal."); |
| 5808 | |
| 5809 | static PyObject * |
| 5810 | win32_kill(PyObject *self, PyObject *args) |
| 5811 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5812 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5813 | DWORD pid, sig, err; |
| 5814 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5815 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5816 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5817 | return NULL; |
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 | /* Console processes which share a common console can be sent CTRL+C or |
| 5820 | CTRL+BREAK events, provided they handle said events. */ |
| 5821 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5822 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5823 | err = GetLastError(); |
| 5824 | PyErr_SetFromWindowsErr(err); |
| 5825 | } |
| 5826 | else |
| 5827 | Py_RETURN_NONE; |
| 5828 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5829 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5830 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5831 | attempt to open and terminate the process. */ |
| 5832 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5833 | if (handle == NULL) { |
| 5834 | err = GetLastError(); |
| 5835 | return PyErr_SetFromWindowsErr(err); |
| 5836 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5838 | if (TerminateProcess(handle, sig) == 0) { |
| 5839 | err = GetLastError(); |
| 5840 | result = PyErr_SetFromWindowsErr(err); |
| 5841 | } else { |
| 5842 | Py_INCREF(Py_None); |
| 5843 | result = Py_None; |
| 5844 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5845 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5846 | CloseHandle(handle); |
| 5847 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5848 | } |
| 5849 | #endif /* MS_WINDOWS */ |
| 5850 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5851 | #ifdef HAVE_PLOCK |
| 5852 | |
| 5853 | #ifdef HAVE_SYS_LOCK_H |
| 5854 | #include <sys/lock.h> |
| 5855 | #endif |
| 5856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5857 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5858 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5859 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5860 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5861 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5862 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5863 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | int op; |
| 5865 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5866 | return NULL; |
| 5867 | if (plock(op) == -1) |
| 5868 | return posix_error(); |
| 5869 | Py_INCREF(Py_None); |
| 5870 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5871 | } |
| 5872 | #endif |
| 5873 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5874 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5875 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5876 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5877 | Set the current process's user id."); |
| 5878 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5879 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5880 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5881 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5882 | long uid_arg; |
| 5883 | uid_t uid; |
| 5884 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5885 | return NULL; |
| 5886 | uid = uid_arg; |
| 5887 | if (uid != uid_arg) { |
| 5888 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5889 | return NULL; |
| 5890 | } |
| 5891 | if (setuid(uid) < 0) |
| 5892 | return posix_error(); |
| 5893 | Py_INCREF(Py_None); |
| 5894 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5895 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5896 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5897 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5898 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5899 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5900 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5901 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5902 | Set the current process's effective user id."); |
| 5903 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5904 | static PyObject * |
| 5905 | posix_seteuid (PyObject *self, PyObject *args) |
| 5906 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5907 | long euid_arg; |
| 5908 | uid_t euid; |
| 5909 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5910 | return NULL; |
| 5911 | euid = euid_arg; |
| 5912 | if (euid != euid_arg) { |
| 5913 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5914 | return NULL; |
| 5915 | } |
| 5916 | if (seteuid(euid) < 0) { |
| 5917 | return posix_error(); |
| 5918 | } else { |
| 5919 | Py_INCREF(Py_None); |
| 5920 | return Py_None; |
| 5921 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5922 | } |
| 5923 | #endif /* HAVE_SETEUID */ |
| 5924 | |
| 5925 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5926 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5927 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5928 | Set the current process's effective group id."); |
| 5929 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5930 | static PyObject * |
| 5931 | posix_setegid (PyObject *self, PyObject *args) |
| 5932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5933 | long egid_arg; |
| 5934 | gid_t egid; |
| 5935 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5936 | return NULL; |
| 5937 | egid = egid_arg; |
| 5938 | if (egid != egid_arg) { |
| 5939 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5940 | return NULL; |
| 5941 | } |
| 5942 | if (setegid(egid) < 0) { |
| 5943 | return posix_error(); |
| 5944 | } else { |
| 5945 | Py_INCREF(Py_None); |
| 5946 | return Py_None; |
| 5947 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5948 | } |
| 5949 | #endif /* HAVE_SETEGID */ |
| 5950 | |
| 5951 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5952 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5953 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5954 | Set the current process's real and effective user ids."); |
| 5955 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5956 | static PyObject * |
| 5957 | posix_setreuid (PyObject *self, PyObject *args) |
| 5958 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5959 | long ruid_arg, euid_arg; |
| 5960 | uid_t ruid, euid; |
| 5961 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5962 | return NULL; |
| 5963 | if (ruid_arg == -1) |
| 5964 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5965 | else |
| 5966 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5967 | if (euid_arg == -1) |
| 5968 | euid = (uid_t)-1; |
| 5969 | else |
| 5970 | euid = euid_arg; |
| 5971 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5972 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5973 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5974 | return NULL; |
| 5975 | } |
| 5976 | if (setreuid(ruid, euid) < 0) { |
| 5977 | return posix_error(); |
| 5978 | } else { |
| 5979 | Py_INCREF(Py_None); |
| 5980 | return Py_None; |
| 5981 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5982 | } |
| 5983 | #endif /* HAVE_SETREUID */ |
| 5984 | |
| 5985 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5986 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5987 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5988 | Set the current process's real and effective group ids."); |
| 5989 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5990 | static PyObject * |
| 5991 | posix_setregid (PyObject *self, PyObject *args) |
| 5992 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | long rgid_arg, egid_arg; |
| 5994 | gid_t rgid, egid; |
| 5995 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5996 | return NULL; |
| 5997 | if (rgid_arg == -1) |
| 5998 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5999 | else |
| 6000 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6001 | if (egid_arg == -1) |
| 6002 | egid = (gid_t)-1; |
| 6003 | else |
| 6004 | egid = egid_arg; |
| 6005 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6006 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6007 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6008 | return NULL; |
| 6009 | } |
| 6010 | if (setregid(rgid, egid) < 0) { |
| 6011 | return posix_error(); |
| 6012 | } else { |
| 6013 | Py_INCREF(Py_None); |
| 6014 | return Py_None; |
| 6015 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6016 | } |
| 6017 | #endif /* HAVE_SETREGID */ |
| 6018 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6019 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6020 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6021 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6022 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6023 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6024 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6025 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6026 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6027 | long gid_arg; |
| 6028 | gid_t gid; |
| 6029 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6030 | return NULL; |
| 6031 | gid = gid_arg; |
| 6032 | if (gid != gid_arg) { |
| 6033 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6034 | return NULL; |
| 6035 | } |
| 6036 | if (setgid(gid) < 0) |
| 6037 | return posix_error(); |
| 6038 | Py_INCREF(Py_None); |
| 6039 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6040 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6041 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6042 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6043 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6044 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6045 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6046 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6047 | |
| 6048 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6049 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6051 | int i, len; |
| 6052 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6053 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6054 | if (!PySequence_Check(groups)) { |
| 6055 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6056 | return NULL; |
| 6057 | } |
| 6058 | len = PySequence_Size(groups); |
| 6059 | if (len > MAX_GROUPS) { |
| 6060 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6061 | return NULL; |
| 6062 | } |
| 6063 | for(i = 0; i < len; i++) { |
| 6064 | PyObject *elem; |
| 6065 | elem = PySequence_GetItem(groups, i); |
| 6066 | if (!elem) |
| 6067 | return NULL; |
| 6068 | if (!PyLong_Check(elem)) { |
| 6069 | PyErr_SetString(PyExc_TypeError, |
| 6070 | "groups must be integers"); |
| 6071 | Py_DECREF(elem); |
| 6072 | return NULL; |
| 6073 | } else { |
| 6074 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6075 | if (PyErr_Occurred()) { |
| 6076 | PyErr_SetString(PyExc_TypeError, |
| 6077 | "group id too big"); |
| 6078 | Py_DECREF(elem); |
| 6079 | return NULL; |
| 6080 | } |
| 6081 | grouplist[i] = x; |
| 6082 | /* read back the value to see if it fitted in gid_t */ |
| 6083 | if (grouplist[i] != x) { |
| 6084 | PyErr_SetString(PyExc_TypeError, |
| 6085 | "group id too big"); |
| 6086 | Py_DECREF(elem); |
| 6087 | return NULL; |
| 6088 | } |
| 6089 | } |
| 6090 | Py_DECREF(elem); |
| 6091 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6092 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6093 | if (setgroups(len, grouplist) < 0) |
| 6094 | return posix_error(); |
| 6095 | Py_INCREF(Py_None); |
| 6096 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6097 | } |
| 6098 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6099 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6100 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6101 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6102 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6104 | PyObject *result; |
| 6105 | static PyObject *struct_rusage; |
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 | if (pid == -1) |
| 6108 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6109 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6110 | if (struct_rusage == NULL) { |
| 6111 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6112 | if (m == NULL) |
| 6113 | return NULL; |
| 6114 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 6115 | Py_DECREF(m); |
| 6116 | if (struct_rusage == NULL) |
| 6117 | return NULL; |
| 6118 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6119 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6120 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6121 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6122 | if (!result) |
| 6123 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6124 | |
| 6125 | #ifndef doubletime |
| 6126 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6127 | #endif |
| 6128 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6129 | PyStructSequence_SET_ITEM(result, 0, |
| 6130 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6131 | PyStructSequence_SET_ITEM(result, 1, |
| 6132 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6133 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6134 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6135 | SET_INT(result, 2, ru->ru_maxrss); |
| 6136 | SET_INT(result, 3, ru->ru_ixrss); |
| 6137 | SET_INT(result, 4, ru->ru_idrss); |
| 6138 | SET_INT(result, 5, ru->ru_isrss); |
| 6139 | SET_INT(result, 6, ru->ru_minflt); |
| 6140 | SET_INT(result, 7, ru->ru_majflt); |
| 6141 | SET_INT(result, 8, ru->ru_nswap); |
| 6142 | SET_INT(result, 9, ru->ru_inblock); |
| 6143 | SET_INT(result, 10, ru->ru_oublock); |
| 6144 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6145 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6146 | SET_INT(result, 13, ru->ru_nsignals); |
| 6147 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6148 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6149 | #undef SET_INT |
| 6150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6151 | if (PyErr_Occurred()) { |
| 6152 | Py_DECREF(result); |
| 6153 | return NULL; |
| 6154 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6155 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6156 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6157 | } |
| 6158 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6159 | |
| 6160 | #ifdef HAVE_WAIT3 |
| 6161 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6162 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6163 | Wait for completion of a child process."); |
| 6164 | |
| 6165 | static PyObject * |
| 6166 | posix_wait3(PyObject *self, PyObject *args) |
| 6167 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6168 | pid_t pid; |
| 6169 | int options; |
| 6170 | struct rusage ru; |
| 6171 | WAIT_TYPE status; |
| 6172 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6173 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6174 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6175 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6176 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6177 | Py_BEGIN_ALLOW_THREADS |
| 6178 | pid = wait3(&status, options, &ru); |
| 6179 | Py_END_ALLOW_THREADS |
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 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6182 | } |
| 6183 | #endif /* HAVE_WAIT3 */ |
| 6184 | |
| 6185 | #ifdef HAVE_WAIT4 |
| 6186 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6187 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6188 | Wait for completion of a given child process."); |
| 6189 | |
| 6190 | static PyObject * |
| 6191 | posix_wait4(PyObject *self, PyObject *args) |
| 6192 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6193 | pid_t pid; |
| 6194 | int options; |
| 6195 | struct rusage ru; |
| 6196 | WAIT_TYPE status; |
| 6197 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6198 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6199 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6200 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6201 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6202 | Py_BEGIN_ALLOW_THREADS |
| 6203 | pid = wait4(pid, &status, options, &ru); |
| 6204 | Py_END_ALLOW_THREADS |
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 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6207 | } |
| 6208 | #endif /* HAVE_WAIT4 */ |
| 6209 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6210 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6211 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6212 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6213 | Wait for the completion of one or more child processes.\n\n\ |
| 6214 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6215 | id specifies the pid to wait on.\n\ |
| 6216 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6217 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6218 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6219 | no children in a waitable state."); |
| 6220 | |
| 6221 | static PyObject * |
| 6222 | posix_waitid(PyObject *self, PyObject *args) |
| 6223 | { |
| 6224 | PyObject *result; |
| 6225 | idtype_t idtype; |
| 6226 | id_t id; |
| 6227 | int options, res; |
| 6228 | siginfo_t si; |
| 6229 | si.si_pid = 0; |
| 6230 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6231 | return NULL; |
| 6232 | Py_BEGIN_ALLOW_THREADS |
| 6233 | res = waitid(idtype, id, &si, options); |
| 6234 | Py_END_ALLOW_THREADS |
| 6235 | if (res == -1) |
| 6236 | return posix_error(); |
| 6237 | |
| 6238 | if (si.si_pid == 0) |
| 6239 | Py_RETURN_NONE; |
| 6240 | |
| 6241 | result = PyStructSequence_New(&WaitidResultType); |
| 6242 | if (!result) |
| 6243 | return NULL; |
| 6244 | |
| 6245 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6246 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6247 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6248 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6249 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6250 | if (PyErr_Occurred()) { |
| 6251 | Py_DECREF(result); |
| 6252 | return NULL; |
| 6253 | } |
| 6254 | |
| 6255 | return result; |
| 6256 | } |
| 6257 | #endif |
| 6258 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6259 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6260 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6261 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6262 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6263 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6264 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6265 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6266 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6267 | pid_t pid; |
| 6268 | int options; |
| 6269 | WAIT_TYPE status; |
| 6270 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6271 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6272 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6273 | return NULL; |
| 6274 | Py_BEGIN_ALLOW_THREADS |
| 6275 | pid = waitpid(pid, &status, options); |
| 6276 | Py_END_ALLOW_THREADS |
| 6277 | if (pid == -1) |
| 6278 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6279 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6280 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6283 | #elif defined(HAVE_CWAIT) |
| 6284 | |
| 6285 | /* 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] | 6286 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6287 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6288 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6289 | |
| 6290 | static PyObject * |
| 6291 | posix_waitpid(PyObject *self, PyObject *args) |
| 6292 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | Py_intptr_t pid; |
| 6294 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6295 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6296 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6297 | return NULL; |
| 6298 | Py_BEGIN_ALLOW_THREADS |
| 6299 | pid = _cwait(&status, pid, options); |
| 6300 | Py_END_ALLOW_THREADS |
| 6301 | if (pid == -1) |
| 6302 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6304 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6305 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6306 | } |
| 6307 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6308 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6309 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6310 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6311 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6312 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6314 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6315 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6317 | pid_t pid; |
| 6318 | WAIT_TYPE status; |
| 6319 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6321 | Py_BEGIN_ALLOW_THREADS |
| 6322 | pid = wait(&status); |
| 6323 | Py_END_ALLOW_THREADS |
| 6324 | if (pid == -1) |
| 6325 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6327 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6328 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6329 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6330 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6332 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6333 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6334 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6335 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6336 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6337 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6338 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6339 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6340 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6341 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6342 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6343 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6344 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6345 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6346 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6347 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6348 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6351 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6352 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6353 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6354 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6355 | 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] | 6356 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6357 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6358 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6359 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6360 | PyObject* v; |
| 6361 | char buf[MAXPATHLEN]; |
| 6362 | PyObject *opath; |
| 6363 | char *path; |
| 6364 | int n; |
| 6365 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6367 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6368 | PyUnicode_FSConverter, &opath)) |
| 6369 | return NULL; |
| 6370 | path = PyBytes_AsString(opath); |
| 6371 | v = PySequence_GetItem(args, 0); |
| 6372 | if (v == NULL) { |
| 6373 | Py_DECREF(opath); |
| 6374 | return NULL; |
| 6375 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6377 | if (PyUnicode_Check(v)) { |
| 6378 | arg_is_unicode = 1; |
| 6379 | } |
| 6380 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6382 | Py_BEGIN_ALLOW_THREADS |
| 6383 | n = readlink(path, buf, (int) sizeof buf); |
| 6384 | Py_END_ALLOW_THREADS |
| 6385 | if (n < 0) |
| 6386 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6387 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6388 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6389 | if (arg_is_unicode) |
| 6390 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6391 | else |
| 6392 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6393 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6394 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6395 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6396 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6397 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6398 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6399 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6400 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6401 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6402 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6403 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6404 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6405 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6406 | } |
| 6407 | #endif /* HAVE_SYMLINK */ |
| 6408 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6409 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6410 | |
| 6411 | PyDoc_STRVAR(win_readlink__doc__, |
| 6412 | "readlink(path) -> path\n\n\ |
| 6413 | Return a string representing the path to which the symbolic link points."); |
| 6414 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6415 | /* Windows readlink implementation */ |
| 6416 | static PyObject * |
| 6417 | win_readlink(PyObject *self, PyObject *args) |
| 6418 | { |
| 6419 | wchar_t *path; |
| 6420 | DWORD n_bytes_returned; |
| 6421 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6422 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6423 | HANDLE reparse_point_handle; |
| 6424 | |
| 6425 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6426 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6427 | wchar_t *print_name; |
| 6428 | |
| 6429 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6430 | "U:readlink", |
| 6431 | &po)) |
| 6432 | return NULL; |
| 6433 | path = PyUnicode_AsUnicode(po); |
| 6434 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6435 | return NULL; |
| 6436 | |
| 6437 | /* First get a handle to the reparse point */ |
| 6438 | Py_BEGIN_ALLOW_THREADS |
| 6439 | reparse_point_handle = CreateFileW( |
| 6440 | path, |
| 6441 | 0, |
| 6442 | 0, |
| 6443 | 0, |
| 6444 | OPEN_EXISTING, |
| 6445 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6446 | 0); |
| 6447 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6448 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6449 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6450 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6451 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6452 | Py_BEGIN_ALLOW_THREADS |
| 6453 | /* New call DeviceIoControl to read the reparse point */ |
| 6454 | io_result = DeviceIoControl( |
| 6455 | reparse_point_handle, |
| 6456 | FSCTL_GET_REPARSE_POINT, |
| 6457 | 0, 0, /* in buffer */ |
| 6458 | target_buffer, sizeof(target_buffer), |
| 6459 | &n_bytes_returned, |
| 6460 | 0 /* we're not using OVERLAPPED_IO */ |
| 6461 | ); |
| 6462 | CloseHandle(reparse_point_handle); |
| 6463 | Py_END_ALLOW_THREADS |
| 6464 | |
| 6465 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6466 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6467 | |
| 6468 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6469 | { |
| 6470 | PyErr_SetString(PyExc_ValueError, |
| 6471 | "not a symbolic link"); |
| 6472 | return NULL; |
| 6473 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6474 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6475 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6476 | |
| 6477 | result = PyUnicode_FromWideChar(print_name, |
| 6478 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6479 | return result; |
| 6480 | } |
| 6481 | |
| 6482 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6483 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6484 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6485 | |
| 6486 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6487 | static int has_CreateSymbolicLinkW = 0; |
| 6488 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6489 | static int |
| 6490 | check_CreateSymbolicLinkW() |
| 6491 | { |
| 6492 | HINSTANCE hKernel32; |
| 6493 | /* only recheck */ |
| 6494 | if (has_CreateSymbolicLinkW) |
| 6495 | return has_CreateSymbolicLinkW; |
| 6496 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6497 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6498 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6499 | if (Py_CreateSymbolicLinkW) |
| 6500 | has_CreateSymbolicLinkW = 1; |
| 6501 | return has_CreateSymbolicLinkW; |
| 6502 | } |
| 6503 | |
| 6504 | PyDoc_STRVAR(win_symlink__doc__, |
| 6505 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6506 | Create a symbolic link pointing to src named dst.\n\ |
| 6507 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6508 | a directory.\n\ |
| 6509 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6510 | NotImplementedError otherwise."); |
| 6511 | |
| 6512 | static PyObject * |
| 6513 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6514 | { |
| 6515 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 6516 | PyObject *src, *dest; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6517 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6518 | int target_is_directory = 0; |
| 6519 | DWORD res; |
| 6520 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6521 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6522 | if (!check_CreateSymbolicLinkW()) |
| 6523 | { |
| 6524 | /* raise NotImplementedError */ |
| 6525 | return PyErr_Format(PyExc_NotImplementedError, |
| 6526 | "CreateSymbolicLinkW not found"); |
| 6527 | } |
| 6528 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 6529 | kwlist, &src, &dest, &target_is_directory)) |
| 6530 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6531 | |
| 6532 | if (win32_can_symlink == 0) |
| 6533 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6534 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6535 | if (!convert_to_unicode(&src)) |
| 6536 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6537 | if (!convert_to_unicode(&dest)) { |
| 6538 | Py_DECREF(src); |
| 6539 | return NULL; |
| 6540 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6541 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6542 | wsrc = PyUnicode_AsUnicode(src); |
| 6543 | if (wsrc == NULL) |
| 6544 | goto error; |
| 6545 | wdest = PyUnicode_AsUnicode(dest); |
| 6546 | if (wsrc == NULL) |
| 6547 | goto error; |
| 6548 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6549 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6550 | if( |
| 6551 | GetFileAttributesExW( |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6552 | wsrc, GetFileExInfoStandard, &src_info |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6553 | )) |
| 6554 | { |
| 6555 | target_is_directory = target_is_directory || |
| 6556 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6557 | } |
| 6558 | |
| 6559 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6560 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6561 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6562 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6563 | Py_DECREF(src); |
| 6564 | Py_DECREF(dest); |
| 6565 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6566 | return win32_error_object("symlink", src); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6567 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6568 | Py_INCREF(Py_None); |
| 6569 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6570 | |
| 6571 | error: |
| 6572 | Py_DECREF(src); |
| 6573 | Py_DECREF(dest); |
| 6574 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6575 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6576 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6577 | |
| 6578 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6579 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6580 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6581 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6582 | { |
| 6583 | ULONG value = 0; |
| 6584 | |
| 6585 | Py_BEGIN_ALLOW_THREADS |
| 6586 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6587 | Py_END_ALLOW_THREADS |
| 6588 | |
| 6589 | return value; |
| 6590 | } |
| 6591 | |
| 6592 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6593 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6594 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6595 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | return Py_BuildValue("ddddd", |
| 6597 | (double)0 /* t.tms_utime / HZ */, |
| 6598 | (double)0 /* t.tms_stime / HZ */, |
| 6599 | (double)0 /* t.tms_cutime / HZ */, |
| 6600 | (double)0 /* t.tms_cstime / HZ */, |
| 6601 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6602 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6603 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6604 | #define NEED_TICKS_PER_SECOND |
| 6605 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6606 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6607 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6608 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6609 | struct tms t; |
| 6610 | clock_t c; |
| 6611 | errno = 0; |
| 6612 | c = times(&t); |
| 6613 | if (c == (clock_t) -1) |
| 6614 | return posix_error(); |
| 6615 | return Py_BuildValue("ddddd", |
| 6616 | (double)t.tms_utime / ticks_per_second, |
| 6617 | (double)t.tms_stime / ticks_per_second, |
| 6618 | (double)t.tms_cutime / ticks_per_second, |
| 6619 | (double)t.tms_cstime / ticks_per_second, |
| 6620 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6621 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6622 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6623 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6624 | |
| 6625 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6626 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6627 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6628 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6629 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6630 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | FILETIME create, exit, kernel, user; |
| 6632 | HANDLE hProc; |
| 6633 | hProc = GetCurrentProcess(); |
| 6634 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6635 | /* The fields of a FILETIME structure are the hi and lo part |
| 6636 | of a 64-bit value expressed in 100 nanosecond units. |
| 6637 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6638 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6639 | */ |
| 6640 | return Py_BuildValue( |
| 6641 | "ddddd", |
| 6642 | (double)(user.dwHighDateTime*429.4967296 + |
| 6643 | user.dwLowDateTime*1e-7), |
| 6644 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6645 | kernel.dwLowDateTime*1e-7), |
| 6646 | (double)0, |
| 6647 | (double)0, |
| 6648 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6649 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6650 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6651 | |
| 6652 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6653 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6654 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6655 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6656 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6657 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6658 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6659 | #ifdef HAVE_GETSID |
| 6660 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6661 | "getsid(pid) -> sid\n\n\ |
| 6662 | Call the system call getsid()."); |
| 6663 | |
| 6664 | static PyObject * |
| 6665 | posix_getsid(PyObject *self, PyObject *args) |
| 6666 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6667 | pid_t pid; |
| 6668 | int sid; |
| 6669 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6670 | return NULL; |
| 6671 | sid = getsid(pid); |
| 6672 | if (sid < 0) |
| 6673 | return posix_error(); |
| 6674 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6675 | } |
| 6676 | #endif /* HAVE_GETSID */ |
| 6677 | |
| 6678 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6679 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6680 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6681 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6682 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6683 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6684 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6685 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6687 | if (setsid() < 0) |
| 6688 | return posix_error(); |
| 6689 | Py_INCREF(Py_None); |
| 6690 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6691 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6692 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6693 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6694 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6695 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6696 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6697 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6698 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6699 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6700 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6702 | pid_t pid; |
| 6703 | int pgrp; |
| 6704 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6705 | return NULL; |
| 6706 | if (setpgid(pid, pgrp) < 0) |
| 6707 | return posix_error(); |
| 6708 | Py_INCREF(Py_None); |
| 6709 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6710 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6711 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6712 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6713 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6714 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6716 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6717 | 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] | 6718 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6719 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6720 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | int fd; |
| 6723 | pid_t pgid; |
| 6724 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6725 | return NULL; |
| 6726 | pgid = tcgetpgrp(fd); |
| 6727 | if (pgid < 0) |
| 6728 | return posix_error(); |
| 6729 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6730 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6731 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6733 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6734 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6735 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6736 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6737 | 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] | 6738 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6739 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6740 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6741 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6742 | int fd; |
| 6743 | pid_t pgid; |
| 6744 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6745 | return NULL; |
| 6746 | if (tcsetpgrp(fd, pgid) < 0) |
| 6747 | return posix_error(); |
| 6748 | Py_INCREF(Py_None); |
| 6749 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6750 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6751 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6752 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6753 | /* Functions acting on file descriptors */ |
| 6754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6755 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6756 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6757 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6758 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6759 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6760 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6761 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | PyObject *ofile; |
| 6763 | char *file; |
| 6764 | int flag; |
| 6765 | int mode = 0777; |
| 6766 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6767 | |
| 6768 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6769 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6770 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6771 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 6772 | if (wpath == NULL) |
| 6773 | return NULL; |
| 6774 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6775 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6776 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6777 | Py_END_ALLOW_THREADS |
| 6778 | if (fd < 0) |
| 6779 | return posix_error(); |
| 6780 | return PyLong_FromLong((long)fd); |
| 6781 | } |
| 6782 | /* Drop the argument parsing error as narrow strings |
| 6783 | are also valid. */ |
| 6784 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6785 | #endif |
| 6786 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6787 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | PyUnicode_FSConverter, &ofile, |
| 6789 | &flag, &mode)) |
| 6790 | return NULL; |
| 6791 | file = PyBytes_AsString(ofile); |
| 6792 | Py_BEGIN_ALLOW_THREADS |
| 6793 | fd = open(file, flag, mode); |
| 6794 | Py_END_ALLOW_THREADS |
| 6795 | if (fd < 0) |
| 6796 | return posix_error_with_allocated_filename(ofile); |
| 6797 | Py_DECREF(ofile); |
| 6798 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6801 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6802 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6803 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6805 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6806 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6807 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6808 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6809 | int fd, res; |
| 6810 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6811 | return NULL; |
| 6812 | if (!_PyVerify_fd(fd)) |
| 6813 | return posix_error(); |
| 6814 | Py_BEGIN_ALLOW_THREADS |
| 6815 | res = close(fd); |
| 6816 | Py_END_ALLOW_THREADS |
| 6817 | if (res < 0) |
| 6818 | return posix_error(); |
| 6819 | Py_INCREF(Py_None); |
| 6820 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6823 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6825 | "closerange(fd_low, fd_high)\n\n\ |
| 6826 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6827 | |
| 6828 | static PyObject * |
| 6829 | posix_closerange(PyObject *self, PyObject *args) |
| 6830 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6831 | int fd_from, fd_to, i; |
| 6832 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6833 | return NULL; |
| 6834 | Py_BEGIN_ALLOW_THREADS |
| 6835 | for (i = fd_from; i < fd_to; i++) |
| 6836 | if (_PyVerify_fd(i)) |
| 6837 | close(i); |
| 6838 | Py_END_ALLOW_THREADS |
| 6839 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6843 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6844 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6845 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6846 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6847 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6848 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6849 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6850 | int fd; |
| 6851 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6852 | return NULL; |
| 6853 | if (!_PyVerify_fd(fd)) |
| 6854 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6855 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6856 | if (fd < 0) |
| 6857 | return posix_error(); |
| 6858 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6859 | } |
| 6860 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6862 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6863 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6864 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6866 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6867 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6868 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6869 | int fd, fd2, res; |
| 6870 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6871 | return NULL; |
| 6872 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6873 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6874 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6875 | if (res < 0) |
| 6876 | return posix_error(); |
| 6877 | Py_INCREF(Py_None); |
| 6878 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6881 | #ifdef HAVE_LOCKF |
| 6882 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6883 | "lockf(fd, cmd, len)\n\n\ |
| 6884 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6885 | fd is an open file descriptor.\n\ |
| 6886 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6887 | F_TEST.\n\ |
| 6888 | len specifies the section of the file to lock."); |
| 6889 | |
| 6890 | static PyObject * |
| 6891 | posix_lockf(PyObject *self, PyObject *args) |
| 6892 | { |
| 6893 | int fd, cmd, res; |
| 6894 | off_t len; |
| 6895 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6896 | &fd, &cmd, _parse_off_t, &len)) |
| 6897 | return NULL; |
| 6898 | |
| 6899 | Py_BEGIN_ALLOW_THREADS |
| 6900 | res = lockf(fd, cmd, len); |
| 6901 | Py_END_ALLOW_THREADS |
| 6902 | |
| 6903 | if (res < 0) |
| 6904 | return posix_error(); |
| 6905 | |
| 6906 | Py_RETURN_NONE; |
| 6907 | } |
| 6908 | #endif |
| 6909 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6911 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6912 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6913 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6914 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6915 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6916 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6917 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6918 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6919 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6920 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6921 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6922 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6923 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6924 | PyObject *posobj; |
| 6925 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6926 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6927 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6928 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6929 | switch (how) { |
| 6930 | case 0: how = SEEK_SET; break; |
| 6931 | case 1: how = SEEK_CUR; break; |
| 6932 | case 2: how = SEEK_END; break; |
| 6933 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6934 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6935 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6936 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6937 | pos = PyLong_AsLong(posobj); |
| 6938 | #else |
| 6939 | pos = PyLong_AsLongLong(posobj); |
| 6940 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6941 | if (PyErr_Occurred()) |
| 6942 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | if (!_PyVerify_fd(fd)) |
| 6945 | return posix_error(); |
| 6946 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6947 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6948 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6949 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6950 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6951 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6952 | Py_END_ALLOW_THREADS |
| 6953 | if (res < 0) |
| 6954 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6955 | |
| 6956 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6957 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6958 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6960 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6964 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6965 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6966 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6967 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6968 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6969 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6970 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6971 | int fd, size; |
| 6972 | Py_ssize_t n; |
| 6973 | PyObject *buffer; |
| 6974 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6975 | return NULL; |
| 6976 | if (size < 0) { |
| 6977 | errno = EINVAL; |
| 6978 | return posix_error(); |
| 6979 | } |
| 6980 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6981 | if (buffer == NULL) |
| 6982 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6983 | if (!_PyVerify_fd(fd)) { |
| 6984 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6985 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6986 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6987 | Py_BEGIN_ALLOW_THREADS |
| 6988 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6989 | Py_END_ALLOW_THREADS |
| 6990 | if (n < 0) { |
| 6991 | Py_DECREF(buffer); |
| 6992 | return posix_error(); |
| 6993 | } |
| 6994 | if (n != size) |
| 6995 | _PyBytes_Resize(&buffer, n); |
| 6996 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6999 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7000 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7001 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7002 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7003 | { |
| 7004 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7005 | Py_ssize_t blen, total = 0; |
| 7006 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7007 | *iov = PyMem_New(struct iovec, cnt); |
| 7008 | if (*iov == NULL) { |
| 7009 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7010 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7011 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7012 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7013 | *buf = PyMem_New(Py_buffer, cnt); |
| 7014 | if (*buf == NULL) { |
| 7015 | PyMem_Del(*iov); |
| 7016 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7017 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7021 | PyObject *item = PySequence_GetItem(seq, i); |
| 7022 | if (item == NULL) |
| 7023 | goto fail; |
| 7024 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7025 | Py_DECREF(item); |
| 7026 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7027 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7028 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7029 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7030 | blen = (*buf)[i].len; |
| 7031 | (*iov)[i].iov_len = blen; |
| 7032 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7033 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7034 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7035 | |
| 7036 | fail: |
| 7037 | PyMem_Del(*iov); |
| 7038 | for (j = 0; j < i; j++) { |
| 7039 | PyBuffer_Release(&(*buf)[j]); |
| 7040 | } |
| 7041 | PyMem_Del(*buf); |
| 7042 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | static void |
| 7046 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7047 | { |
| 7048 | int i; |
| 7049 | PyMem_Del(iov); |
| 7050 | for (i = 0; i < cnt; i++) { |
| 7051 | PyBuffer_Release(&buf[i]); |
| 7052 | } |
| 7053 | PyMem_Del(buf); |
| 7054 | } |
| 7055 | #endif |
| 7056 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7057 | #ifdef HAVE_READV |
| 7058 | PyDoc_STRVAR(posix_readv__doc__, |
| 7059 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7060 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7061 | is an arbitrary sequence of writable buffers.\n\ |
| 7062 | Returns the total number of bytes read."); |
| 7063 | |
| 7064 | static PyObject * |
| 7065 | posix_readv(PyObject *self, PyObject *args) |
| 7066 | { |
| 7067 | int fd, cnt; |
| 7068 | Py_ssize_t n; |
| 7069 | PyObject *seq; |
| 7070 | struct iovec *iov; |
| 7071 | Py_buffer *buf; |
| 7072 | |
| 7073 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7074 | return NULL; |
| 7075 | if (!PySequence_Check(seq)) { |
| 7076 | PyErr_SetString(PyExc_TypeError, |
| 7077 | "readv() arg 2 must be a sequence"); |
| 7078 | return NULL; |
| 7079 | } |
| 7080 | cnt = PySequence_Size(seq); |
| 7081 | |
| 7082 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7083 | return NULL; |
| 7084 | |
| 7085 | Py_BEGIN_ALLOW_THREADS |
| 7086 | n = readv(fd, iov, cnt); |
| 7087 | Py_END_ALLOW_THREADS |
| 7088 | |
| 7089 | iov_cleanup(iov, buf, cnt); |
| 7090 | return PyLong_FromSsize_t(n); |
| 7091 | } |
| 7092 | #endif |
| 7093 | |
| 7094 | #ifdef HAVE_PREAD |
| 7095 | PyDoc_STRVAR(posix_pread__doc__, |
| 7096 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7097 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7098 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7099 | |
| 7100 | static PyObject * |
| 7101 | posix_pread(PyObject *self, PyObject *args) |
| 7102 | { |
| 7103 | int fd, size; |
| 7104 | off_t offset; |
| 7105 | Py_ssize_t n; |
| 7106 | PyObject *buffer; |
| 7107 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7108 | return NULL; |
| 7109 | |
| 7110 | if (size < 0) { |
| 7111 | errno = EINVAL; |
| 7112 | return posix_error(); |
| 7113 | } |
| 7114 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7115 | if (buffer == NULL) |
| 7116 | return NULL; |
| 7117 | if (!_PyVerify_fd(fd)) { |
| 7118 | Py_DECREF(buffer); |
| 7119 | return posix_error(); |
| 7120 | } |
| 7121 | Py_BEGIN_ALLOW_THREADS |
| 7122 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7123 | Py_END_ALLOW_THREADS |
| 7124 | if (n < 0) { |
| 7125 | Py_DECREF(buffer); |
| 7126 | return posix_error(); |
| 7127 | } |
| 7128 | if (n != size) |
| 7129 | _PyBytes_Resize(&buffer, n); |
| 7130 | return buffer; |
| 7131 | } |
| 7132 | #endif |
| 7133 | |
| 7134 | PyDoc_STRVAR(posix_write__doc__, |
| 7135 | "write(fd, string) -> byteswritten\n\n\ |
| 7136 | Write a string to a file descriptor."); |
| 7137 | |
| 7138 | static PyObject * |
| 7139 | posix_write(PyObject *self, PyObject *args) |
| 7140 | { |
| 7141 | Py_buffer pbuf; |
| 7142 | int fd; |
| 7143 | Py_ssize_t size, len; |
| 7144 | |
| 7145 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7146 | return NULL; |
| 7147 | if (!_PyVerify_fd(fd)) { |
| 7148 | PyBuffer_Release(&pbuf); |
| 7149 | return posix_error(); |
| 7150 | } |
| 7151 | len = pbuf.len; |
| 7152 | Py_BEGIN_ALLOW_THREADS |
| 7153 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7154 | if (len > INT_MAX) |
| 7155 | len = INT_MAX; |
| 7156 | size = write(fd, pbuf.buf, (int)len); |
| 7157 | #else |
| 7158 | size = write(fd, pbuf.buf, len); |
| 7159 | #endif |
| 7160 | Py_END_ALLOW_THREADS |
| 7161 | PyBuffer_Release(&pbuf); |
| 7162 | if (size < 0) |
| 7163 | return posix_error(); |
| 7164 | return PyLong_FromSsize_t(size); |
| 7165 | } |
| 7166 | |
| 7167 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7168 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7169 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7170 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7171 | -> byteswritten\n\ |
| 7172 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7173 | |
| 7174 | static PyObject * |
| 7175 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7176 | { |
| 7177 | int in, out; |
| 7178 | Py_ssize_t ret; |
| 7179 | off_t offset; |
| 7180 | |
| 7181 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7182 | #ifndef __APPLE__ |
| 7183 | Py_ssize_t len; |
| 7184 | #endif |
| 7185 | PyObject *headers = NULL, *trailers = NULL; |
| 7186 | Py_buffer *hbuf, *tbuf; |
| 7187 | off_t sbytes; |
| 7188 | struct sf_hdtr sf; |
| 7189 | int flags = 0; |
| 7190 | sf.headers = NULL; |
| 7191 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7192 | static char *keywords[] = {"out", "in", |
| 7193 | "offset", "count", |
| 7194 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7195 | |
| 7196 | #ifdef __APPLE__ |
| 7197 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7198 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7199 | #else |
| 7200 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7201 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7202 | #endif |
| 7203 | &headers, &trailers, &flags)) |
| 7204 | return NULL; |
| 7205 | if (headers != NULL) { |
| 7206 | if (!PySequence_Check(headers)) { |
| 7207 | PyErr_SetString(PyExc_TypeError, |
| 7208 | "sendfile() headers must be a sequence or None"); |
| 7209 | return NULL; |
| 7210 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7211 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7212 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7213 | if (sf.hdr_cnt > 0 && |
| 7214 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7215 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7216 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7217 | #ifdef __APPLE__ |
| 7218 | sbytes += i; |
| 7219 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7220 | } |
| 7221 | } |
| 7222 | if (trailers != NULL) { |
| 7223 | if (!PySequence_Check(trailers)) { |
| 7224 | PyErr_SetString(PyExc_TypeError, |
| 7225 | "sendfile() trailers must be a sequence or None"); |
| 7226 | return NULL; |
| 7227 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7228 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7229 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7230 | if (sf.trl_cnt > 0 && |
| 7231 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7232 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7233 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7234 | #ifdef __APPLE__ |
| 7235 | sbytes += i; |
| 7236 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7237 | } |
| 7238 | } |
| 7239 | |
| 7240 | Py_BEGIN_ALLOW_THREADS |
| 7241 | #ifdef __APPLE__ |
| 7242 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7243 | #else |
| 7244 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7245 | #endif |
| 7246 | Py_END_ALLOW_THREADS |
| 7247 | |
| 7248 | if (sf.headers != NULL) |
| 7249 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7250 | if (sf.trailers != NULL) |
| 7251 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7252 | |
| 7253 | if (ret < 0) { |
| 7254 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7255 | if (sbytes != 0) { |
| 7256 | // some data has been sent |
| 7257 | goto done; |
| 7258 | } |
| 7259 | else { |
| 7260 | // no data has been sent; upper application is supposed |
| 7261 | // to retry on EAGAIN or EBUSY |
| 7262 | return posix_error(); |
| 7263 | } |
| 7264 | } |
| 7265 | return posix_error(); |
| 7266 | } |
| 7267 | goto done; |
| 7268 | |
| 7269 | done: |
| 7270 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7271 | return Py_BuildValue("l", sbytes); |
| 7272 | #else |
| 7273 | return Py_BuildValue("L", sbytes); |
| 7274 | #endif |
| 7275 | |
| 7276 | #else |
| 7277 | Py_ssize_t count; |
| 7278 | PyObject *offobj; |
| 7279 | static char *keywords[] = {"out", "in", |
| 7280 | "offset", "count", NULL}; |
| 7281 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7282 | keywords, &out, &in, &offobj, &count)) |
| 7283 | return NULL; |
| 7284 | #ifdef linux |
| 7285 | if (offobj == Py_None) { |
| 7286 | Py_BEGIN_ALLOW_THREADS |
| 7287 | ret = sendfile(out, in, NULL, count); |
| 7288 | Py_END_ALLOW_THREADS |
| 7289 | if (ret < 0) |
| 7290 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7291 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7292 | } |
| 7293 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7294 | if (!_parse_off_t(offobj, &offset)) |
| 7295 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7296 | Py_BEGIN_ALLOW_THREADS |
| 7297 | ret = sendfile(out, in, &offset, count); |
| 7298 | Py_END_ALLOW_THREADS |
| 7299 | if (ret < 0) |
| 7300 | return posix_error(); |
| 7301 | return Py_BuildValue("n", ret); |
| 7302 | #endif |
| 7303 | } |
| 7304 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7306 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7307 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7308 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7309 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7311 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | int fd; |
| 7314 | STRUCT_STAT st; |
| 7315 | int res; |
| 7316 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7317 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7318 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7319 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7320 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7321 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7322 | if (!_PyVerify_fd(fd)) |
| 7323 | return posix_error(); |
| 7324 | Py_BEGIN_ALLOW_THREADS |
| 7325 | res = FSTAT(fd, &st); |
| 7326 | Py_END_ALLOW_THREADS |
| 7327 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7328 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7329 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7330 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7331 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7332 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7333 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7335 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7338 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7339 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7340 | 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] | 7341 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7342 | |
| 7343 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7344 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7345 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7346 | int fd; |
| 7347 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7348 | return NULL; |
| 7349 | if (!_PyVerify_fd(fd)) |
| 7350 | return PyBool_FromLong(0); |
| 7351 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7352 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7353 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7354 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7356 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7357 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7358 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7359 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7360 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7361 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7362 | #if defined(PYOS_OS2) |
| 7363 | HFILE read, write; |
| 7364 | APIRET rc; |
| 7365 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7366 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7367 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7368 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7369 | |
| 7370 | return Py_BuildValue("(ii)", read, write); |
| 7371 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7372 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7373 | int fds[2]; |
| 7374 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7375 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7376 | if (res != 0) |
| 7377 | return posix_error(); |
| 7378 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7379 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7380 | HANDLE read, write; |
| 7381 | int read_fd, write_fd; |
| 7382 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7383 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7384 | if (!ok) |
| 7385 | return win32_error("CreatePipe", NULL); |
| 7386 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7387 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7388 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7389 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7390 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7391 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7392 | #endif /* HAVE_PIPE */ |
| 7393 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7394 | #ifdef HAVE_PIPE2 |
| 7395 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7396 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7397 | Create a pipe with flags set atomically.\n\ |
| 7398 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7399 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7400 | "); |
| 7401 | |
| 7402 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7403 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7404 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7405 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7406 | int fds[2]; |
| 7407 | int res; |
| 7408 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7409 | flags = PyLong_AsLong(arg); |
| 7410 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7411 | return NULL; |
| 7412 | |
| 7413 | res = pipe2(fds, flags); |
| 7414 | if (res != 0) |
| 7415 | return posix_error(); |
| 7416 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7417 | } |
| 7418 | #endif /* HAVE_PIPE2 */ |
| 7419 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7420 | #ifdef HAVE_WRITEV |
| 7421 | PyDoc_STRVAR(posix_writev__doc__, |
| 7422 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7423 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7424 | arbitrary sequence of buffers.\n\ |
| 7425 | Returns the total bytes written."); |
| 7426 | |
| 7427 | static PyObject * |
| 7428 | posix_writev(PyObject *self, PyObject *args) |
| 7429 | { |
| 7430 | int fd, cnt; |
| 7431 | Py_ssize_t res; |
| 7432 | PyObject *seq; |
| 7433 | struct iovec *iov; |
| 7434 | Py_buffer *buf; |
| 7435 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7436 | return NULL; |
| 7437 | if (!PySequence_Check(seq)) { |
| 7438 | PyErr_SetString(PyExc_TypeError, |
| 7439 | "writev() arg 2 must be a sequence"); |
| 7440 | return NULL; |
| 7441 | } |
| 7442 | cnt = PySequence_Size(seq); |
| 7443 | |
| 7444 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7445 | return NULL; |
| 7446 | } |
| 7447 | |
| 7448 | Py_BEGIN_ALLOW_THREADS |
| 7449 | res = writev(fd, iov, cnt); |
| 7450 | Py_END_ALLOW_THREADS |
| 7451 | |
| 7452 | iov_cleanup(iov, buf, cnt); |
| 7453 | return PyLong_FromSsize_t(res); |
| 7454 | } |
| 7455 | #endif |
| 7456 | |
| 7457 | #ifdef HAVE_PWRITE |
| 7458 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7459 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7460 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7461 | offset unchanged."); |
| 7462 | |
| 7463 | static PyObject * |
| 7464 | posix_pwrite(PyObject *self, PyObject *args) |
| 7465 | { |
| 7466 | Py_buffer pbuf; |
| 7467 | int fd; |
| 7468 | off_t offset; |
| 7469 | Py_ssize_t size; |
| 7470 | |
| 7471 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7472 | return NULL; |
| 7473 | |
| 7474 | if (!_PyVerify_fd(fd)) { |
| 7475 | PyBuffer_Release(&pbuf); |
| 7476 | return posix_error(); |
| 7477 | } |
| 7478 | Py_BEGIN_ALLOW_THREADS |
| 7479 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7480 | Py_END_ALLOW_THREADS |
| 7481 | PyBuffer_Release(&pbuf); |
| 7482 | if (size < 0) |
| 7483 | return posix_error(); |
| 7484 | return PyLong_FromSsize_t(size); |
| 7485 | } |
| 7486 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7487 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7488 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7489 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7490 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7491 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7492 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7493 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7494 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7495 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7496 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7497 | char *filename; |
| 7498 | int mode = 0666; |
| 7499 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7500 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7501 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7503 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7504 | Py_BEGIN_ALLOW_THREADS |
| 7505 | res = mkfifo(filename, mode); |
| 7506 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7507 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7508 | if (res < 0) |
| 7509 | return posix_error(); |
| 7510 | Py_INCREF(Py_None); |
| 7511 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7512 | } |
| 7513 | #endif |
| 7514 | |
| 7515 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7516 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7517 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7518 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7519 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7520 | named filename. mode specifies both the permissions to use and the\n\ |
| 7521 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7522 | 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] | 7523 | device defines the newly created device special file (probably using\n\ |
| 7524 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7525 | |
| 7526 | |
| 7527 | static PyObject * |
| 7528 | posix_mknod(PyObject *self, PyObject *args) |
| 7529 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7530 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7531 | char *filename; |
| 7532 | int mode = 0600; |
| 7533 | int device = 0; |
| 7534 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7535 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7536 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7537 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7538 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7539 | Py_BEGIN_ALLOW_THREADS |
| 7540 | res = mknod(filename, mode, device); |
| 7541 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7542 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7543 | if (res < 0) |
| 7544 | return posix_error(); |
| 7545 | Py_INCREF(Py_None); |
| 7546 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7547 | } |
| 7548 | #endif |
| 7549 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7550 | #ifdef HAVE_DEVICE_MACROS |
| 7551 | PyDoc_STRVAR(posix_major__doc__, |
| 7552 | "major(device) -> major number\n\ |
| 7553 | Extracts a device major number from a raw device number."); |
| 7554 | |
| 7555 | static PyObject * |
| 7556 | posix_major(PyObject *self, PyObject *args) |
| 7557 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7558 | int device; |
| 7559 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7560 | return NULL; |
| 7561 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | PyDoc_STRVAR(posix_minor__doc__, |
| 7565 | "minor(device) -> minor number\n\ |
| 7566 | Extracts a device minor number from a raw device number."); |
| 7567 | |
| 7568 | static PyObject * |
| 7569 | posix_minor(PyObject *self, PyObject *args) |
| 7570 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7571 | int device; |
| 7572 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7573 | return NULL; |
| 7574 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7578 | "makedev(major, minor) -> device number\n\ |
| 7579 | Composes a raw device number from the major and minor device numbers."); |
| 7580 | |
| 7581 | static PyObject * |
| 7582 | posix_makedev(PyObject *self, PyObject *args) |
| 7583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | int major, minor; |
| 7585 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7586 | return NULL; |
| 7587 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7588 | } |
| 7589 | #endif /* device macros */ |
| 7590 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7591 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7592 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7593 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7594 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7595 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7596 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7597 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7598 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7599 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | int fd; |
| 7601 | off_t length; |
| 7602 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7603 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7604 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7605 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7607 | Py_BEGIN_ALLOW_THREADS |
| 7608 | res = ftruncate(fd, length); |
| 7609 | Py_END_ALLOW_THREADS |
| 7610 | if (res < 0) |
| 7611 | return posix_error(); |
| 7612 | Py_INCREF(Py_None); |
| 7613 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7614 | } |
| 7615 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7616 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7617 | #ifdef HAVE_TRUNCATE |
| 7618 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7619 | "truncate(path, length)\n\n\ |
| 7620 | Truncate the file given by path to length bytes."); |
| 7621 | |
| 7622 | static PyObject * |
| 7623 | posix_truncate(PyObject *self, PyObject *args) |
| 7624 | { |
| 7625 | PyObject *opath; |
| 7626 | const char *path; |
| 7627 | off_t length; |
| 7628 | int res; |
| 7629 | |
| 7630 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7631 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7632 | return NULL; |
| 7633 | path = PyBytes_AsString(opath); |
| 7634 | |
| 7635 | Py_BEGIN_ALLOW_THREADS |
| 7636 | res = truncate(path, length); |
| 7637 | Py_END_ALLOW_THREADS |
| 7638 | Py_DECREF(opath); |
| 7639 | if (res < 0) |
| 7640 | return posix_error(); |
| 7641 | Py_RETURN_NONE; |
| 7642 | } |
| 7643 | #endif |
| 7644 | |
| 7645 | #ifdef HAVE_POSIX_FALLOCATE |
| 7646 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7647 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7648 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7649 | starting from offset and continuing for len bytes."); |
| 7650 | |
| 7651 | static PyObject * |
| 7652 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7653 | { |
| 7654 | off_t len, offset; |
| 7655 | int res, fd; |
| 7656 | |
| 7657 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7658 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7659 | return NULL; |
| 7660 | |
| 7661 | Py_BEGIN_ALLOW_THREADS |
| 7662 | res = posix_fallocate(fd, offset, len); |
| 7663 | Py_END_ALLOW_THREADS |
| 7664 | if (res != 0) { |
| 7665 | errno = res; |
| 7666 | return posix_error(); |
| 7667 | } |
| 7668 | Py_RETURN_NONE; |
| 7669 | } |
| 7670 | #endif |
| 7671 | |
| 7672 | #ifdef HAVE_POSIX_FADVISE |
| 7673 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7674 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7675 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7676 | the kernel to make optimizations.\n\ |
| 7677 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7678 | offset and continuing for len bytes.\n\ |
| 7679 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7680 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7681 | POSIX_FADV_DONTNEED."); |
| 7682 | |
| 7683 | static PyObject * |
| 7684 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7685 | { |
| 7686 | off_t len, offset; |
| 7687 | int res, fd, advice; |
| 7688 | |
| 7689 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7690 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7691 | return NULL; |
| 7692 | |
| 7693 | Py_BEGIN_ALLOW_THREADS |
| 7694 | res = posix_fadvise(fd, offset, len, advice); |
| 7695 | Py_END_ALLOW_THREADS |
| 7696 | if (res != 0) { |
| 7697 | errno = res; |
| 7698 | return posix_error(); |
| 7699 | } |
| 7700 | Py_RETURN_NONE; |
| 7701 | } |
| 7702 | #endif |
| 7703 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7704 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7705 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7706 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7707 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7708 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7709 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7710 | * get re-set with another call for the same key. */ |
| 7711 | static PyObject *posix_putenv_garbage; |
| 7712 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7713 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7714 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7715 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7716 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7717 | wchar_t *s1, *s2; |
| 7718 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7719 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7720 | PyObject *os1, *os2; |
| 7721 | char *s1, *s2; |
| 7722 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7723 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7724 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7725 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7726 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7727 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | if (!PyArg_ParseTuple(args, |
| 7729 | "uu:putenv", |
| 7730 | &s1, &s2)) |
| 7731 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7732 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7733 | if (!PyArg_ParseTuple(args, |
| 7734 | "O&O&:putenv", |
| 7735 | PyUnicode_FSConverter, &os1, |
| 7736 | PyUnicode_FSConverter, &os2)) |
| 7737 | return NULL; |
| 7738 | s1 = PyBytes_AsString(os1); |
| 7739 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7740 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7741 | |
| 7742 | #if defined(PYOS_OS2) |
| 7743 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7744 | APIRET rc; |
| 7745 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7746 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7747 | if (rc != NO_ERROR) { |
| 7748 | os2_error(rc); |
| 7749 | goto error; |
| 7750 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7751 | |
| 7752 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7753 | APIRET rc; |
| 7754 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7755 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7756 | if (rc != NO_ERROR) { |
| 7757 | os2_error(rc); |
| 7758 | goto error; |
| 7759 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7760 | } else { |
| 7761 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7762 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7763 | /* len includes space for a trailing \0; the size arg to |
| 7764 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7765 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7766 | len = wcslen(s1) + wcslen(s2) + 2; |
| 7767 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7768 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7769 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7771 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7772 | if (newstr == NULL) { |
| 7773 | PyErr_NoMemory(); |
| 7774 | goto error; |
| 7775 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7776 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7777 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7778 | if (newenv == NULL) |
| 7779 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7780 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 7781 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7782 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7783 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7785 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7786 | newenv = PyBytes_AS_STRING(newstr); |
| 7787 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7788 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7789 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7790 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7792 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7793 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7794 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7795 | * this will cause previous value to be collected. This has to |
| 7796 | * happen after the real putenv() call because the old value |
| 7797 | * was still accessible until then. */ |
| 7798 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7799 | #ifdef MS_WINDOWS |
| 7800 | PyTuple_GET_ITEM(args, 0), |
| 7801 | #else |
| 7802 | os1, |
| 7803 | #endif |
| 7804 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7805 | /* really not much we can do; just leak */ |
| 7806 | PyErr_Clear(); |
| 7807 | } |
| 7808 | else { |
| 7809 | Py_DECREF(newstr); |
| 7810 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7811 | |
| 7812 | #if defined(PYOS_OS2) |
| 7813 | } |
| 7814 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7815 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7816 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7817 | Py_DECREF(os1); |
| 7818 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7819 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7820 | Py_RETURN_NONE; |
| 7821 | |
| 7822 | error: |
| 7823 | #ifndef MS_WINDOWS |
| 7824 | Py_DECREF(os1); |
| 7825 | Py_DECREF(os2); |
| 7826 | #endif |
| 7827 | Py_XDECREF(newstr); |
| 7828 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7829 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7830 | #endif /* putenv */ |
| 7831 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7832 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7833 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7834 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7835 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7836 | |
| 7837 | static PyObject * |
| 7838 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7839 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7840 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7841 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7843 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7844 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7845 | #else |
| 7846 | PyObject *os1; |
| 7847 | char *s1; |
| 7848 | |
| 7849 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7850 | PyUnicode_FSConverter, &os1)) |
| 7851 | return NULL; |
| 7852 | s1 = PyBytes_AsString(os1); |
| 7853 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7855 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | /* Remove the key from posix_putenv_garbage; |
| 7858 | * this will cause it to be collected. This has to |
| 7859 | * happen after the real unsetenv() call because the |
| 7860 | * old value was still accessible until then. |
| 7861 | */ |
| 7862 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7863 | #ifdef MS_WINDOWS |
| 7864 | PyTuple_GET_ITEM(args, 0) |
| 7865 | #else |
| 7866 | os1 |
| 7867 | #endif |
| 7868 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7869 | /* really not much we can do; just leak */ |
| 7870 | PyErr_Clear(); |
| 7871 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7872 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7873 | #ifndef MS_WINDOWS |
| 7874 | Py_DECREF(os1); |
| 7875 | #endif |
| 7876 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7877 | } |
| 7878 | #endif /* unsetenv */ |
| 7879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7880 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7881 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7882 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7883 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7884 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7885 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7886 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7887 | int code; |
| 7888 | char *message; |
| 7889 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7890 | return NULL; |
| 7891 | message = strerror(code); |
| 7892 | if (message == NULL) { |
| 7893 | PyErr_SetString(PyExc_ValueError, |
| 7894 | "strerror() argument out of range"); |
| 7895 | return NULL; |
| 7896 | } |
| 7897 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7898 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7899 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7900 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7901 | #ifdef HAVE_SYS_WAIT_H |
| 7902 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7903 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7904 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7905 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7906 | 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] | 7907 | |
| 7908 | static PyObject * |
| 7909 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7910 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7911 | WAIT_TYPE status; |
| 7912 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7914 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7915 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7916 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7917 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7918 | } |
| 7919 | #endif /* WCOREDUMP */ |
| 7920 | |
| 7921 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7922 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7923 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7924 | 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] | 7925 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7926 | |
| 7927 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7928 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7929 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7930 | WAIT_TYPE status; |
| 7931 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7933 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7934 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7935 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7936 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7937 | } |
| 7938 | #endif /* WIFCONTINUED */ |
| 7939 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7940 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7941 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7942 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7943 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7944 | |
| 7945 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7946 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7947 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7948 | WAIT_TYPE status; |
| 7949 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7950 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7951 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7952 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7954 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7955 | } |
| 7956 | #endif /* WIFSTOPPED */ |
| 7957 | |
| 7958 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7959 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7960 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7961 | 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] | 7962 | |
| 7963 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7964 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7965 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7966 | WAIT_TYPE status; |
| 7967 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7969 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7970 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7971 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7972 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7973 | } |
| 7974 | #endif /* WIFSIGNALED */ |
| 7975 | |
| 7976 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7977 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7978 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7979 | 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] | 7980 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7981 | |
| 7982 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7983 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7984 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | WAIT_TYPE status; |
| 7986 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7987 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7988 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7989 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7990 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7991 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7992 | } |
| 7993 | #endif /* WIFEXITED */ |
| 7994 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7995 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7996 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7997 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7998 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7999 | |
| 8000 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8001 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8003 | WAIT_TYPE status; |
| 8004 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8006 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8007 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8009 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8010 | } |
| 8011 | #endif /* WEXITSTATUS */ |
| 8012 | |
| 8013 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8014 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8015 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8016 | 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] | 8017 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8018 | |
| 8019 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8020 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8022 | WAIT_TYPE status; |
| 8023 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8025 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8026 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8027 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8028 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8029 | } |
| 8030 | #endif /* WTERMSIG */ |
| 8031 | |
| 8032 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8033 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8034 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8035 | Return the signal that stopped the process that provided\n\ |
| 8036 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8037 | |
| 8038 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8039 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8040 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8041 | WAIT_TYPE status; |
| 8042 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8044 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8045 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8046 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8047 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8048 | } |
| 8049 | #endif /* WSTOPSIG */ |
| 8050 | |
| 8051 | #endif /* HAVE_SYS_WAIT_H */ |
| 8052 | |
| 8053 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8054 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8055 | #ifdef _SCO_DS |
| 8056 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8057 | needed definitions in sys/statvfs.h */ |
| 8058 | #define _SVID3 |
| 8059 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8060 | #include <sys/statvfs.h> |
| 8061 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8062 | static PyObject* |
| 8063 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8064 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8065 | if (v == NULL) |
| 8066 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8067 | |
| 8068 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8069 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8070 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8071 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8072 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8073 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8074 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8075 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8076 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8077 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8078 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8079 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8080 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8081 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8082 | PyStructSequence_SET_ITEM(v, 2, |
| 8083 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8084 | PyStructSequence_SET_ITEM(v, 3, |
| 8085 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8086 | PyStructSequence_SET_ITEM(v, 4, |
| 8087 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8088 | PyStructSequence_SET_ITEM(v, 5, |
| 8089 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8090 | PyStructSequence_SET_ITEM(v, 6, |
| 8091 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8092 | PyStructSequence_SET_ITEM(v, 7, |
| 8093 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8094 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8095 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8096 | #endif |
| 8097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8101 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8102 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8103 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8104 | |
| 8105 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8106 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8107 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8108 | int fd, res; |
| 8109 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8111 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8112 | return NULL; |
| 8113 | Py_BEGIN_ALLOW_THREADS |
| 8114 | res = fstatvfs(fd, &st); |
| 8115 | Py_END_ALLOW_THREADS |
| 8116 | if (res != 0) |
| 8117 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8118 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8119 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8120 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8121 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8122 | |
| 8123 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8124 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8125 | #include <sys/statvfs.h> |
| 8126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8127 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8128 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8129 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8130 | |
| 8131 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8132 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8133 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8134 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8135 | int res; |
| 8136 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8137 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8138 | return NULL; |
| 8139 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8140 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8141 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8142 | if (res != 0) { |
| 8143 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8144 | Py_DECREF(path); |
| 8145 | return NULL; |
| 8146 | } |
| 8147 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8148 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8149 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8150 | } |
| 8151 | #endif /* HAVE_STATVFS */ |
| 8152 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8153 | #ifdef MS_WINDOWS |
| 8154 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8155 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8156 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8157 | |
| 8158 | static PyObject * |
| 8159 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8160 | { |
| 8161 | BOOL retval; |
| 8162 | ULARGE_INTEGER _, total, free; |
| 8163 | LPCTSTR path; |
| 8164 | |
| 8165 | if (! PyArg_ParseTuple(args, "s", &path)) |
| 8166 | return NULL; |
| 8167 | |
| 8168 | Py_BEGIN_ALLOW_THREADS |
| 8169 | retval = GetDiskFreeSpaceEx(path, &_, &total, &free); |
| 8170 | Py_END_ALLOW_THREADS |
| 8171 | if (retval == 0) |
| 8172 | return PyErr_SetFromWindowsErr(0); |
| 8173 | |
| 8174 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8175 | } |
| 8176 | #endif |
| 8177 | |
| 8178 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8179 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8180 | * It maps strings representing configuration variable names to |
| 8181 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8182 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8183 | * rarely-used constants. There are three separate tables that use |
| 8184 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8185 | * |
| 8186 | * This code is always included, even if none of the interfaces that |
| 8187 | * need it are included. The #if hackery needed to avoid it would be |
| 8188 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8189 | */ |
| 8190 | struct constdef { |
| 8191 | char *name; |
| 8192 | long value; |
| 8193 | }; |
| 8194 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8195 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8196 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8197 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8198 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8199 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8200 | *valuep = PyLong_AS_LONG(arg); |
| 8201 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8202 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8203 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8204 | /* look up the value in the table using a binary search */ |
| 8205 | size_t lo = 0; |
| 8206 | size_t mid; |
| 8207 | size_t hi = tablesize; |
| 8208 | int cmp; |
| 8209 | const char *confname; |
| 8210 | if (!PyUnicode_Check(arg)) { |
| 8211 | PyErr_SetString(PyExc_TypeError, |
| 8212 | "configuration names must be strings or integers"); |
| 8213 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8214 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8215 | confname = _PyUnicode_AsString(arg); |
| 8216 | if (confname == NULL) |
| 8217 | return 0; |
| 8218 | while (lo < hi) { |
| 8219 | mid = (lo + hi) / 2; |
| 8220 | cmp = strcmp(confname, table[mid].name); |
| 8221 | if (cmp < 0) |
| 8222 | hi = mid; |
| 8223 | else if (cmp > 0) |
| 8224 | lo = mid + 1; |
| 8225 | else { |
| 8226 | *valuep = table[mid].value; |
| 8227 | return 1; |
| 8228 | } |
| 8229 | } |
| 8230 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8231 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8232 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8233 | } |
| 8234 | |
| 8235 | |
| 8236 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8237 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8238 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8239 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8240 | #endif |
| 8241 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8242 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8243 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8244 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8245 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8246 | #endif |
| 8247 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8248 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8249 | #endif |
| 8250 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8251 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8252 | #endif |
| 8253 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8255 | #endif |
| 8256 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8257 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8258 | #endif |
| 8259 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8260 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8261 | #endif |
| 8262 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8263 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8264 | #endif |
| 8265 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8266 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8267 | #endif |
| 8268 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8269 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8270 | #endif |
| 8271 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8272 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8273 | #endif |
| 8274 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8275 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8276 | #endif |
| 8277 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8278 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8279 | #endif |
| 8280 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8281 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8282 | #endif |
| 8283 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8284 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8285 | #endif |
| 8286 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8287 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8288 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8289 | #ifdef _PC_ACL_ENABLED |
| 8290 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8291 | #endif |
| 8292 | #ifdef _PC_MIN_HOLE_SIZE |
| 8293 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8294 | #endif |
| 8295 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8296 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8297 | #endif |
| 8298 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8299 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8300 | #endif |
| 8301 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8302 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8303 | #endif |
| 8304 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8305 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8306 | #endif |
| 8307 | #ifdef _PC_REC_XFER_ALIGN |
| 8308 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8309 | #endif |
| 8310 | #ifdef _PC_SYMLINK_MAX |
| 8311 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8312 | #endif |
| 8313 | #ifdef _PC_XATTR_ENABLED |
| 8314 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8315 | #endif |
| 8316 | #ifdef _PC_XATTR_EXISTS |
| 8317 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8318 | #endif |
| 8319 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8320 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8321 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8322 | }; |
| 8323 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8324 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8325 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8326 | { |
| 8327 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8328 | sizeof(posix_constants_pathconf) |
| 8329 | / sizeof(struct constdef)); |
| 8330 | } |
| 8331 | #endif |
| 8332 | |
| 8333 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8334 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8335 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8336 | 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] | 8337 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8338 | |
| 8339 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8340 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8341 | { |
| 8342 | PyObject *result = NULL; |
| 8343 | int name, fd; |
| 8344 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8345 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8346 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8347 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8349 | errno = 0; |
| 8350 | limit = fpathconf(fd, name); |
| 8351 | if (limit == -1 && errno != 0) |
| 8352 | posix_error(); |
| 8353 | else |
| 8354 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8355 | } |
| 8356 | return result; |
| 8357 | } |
| 8358 | #endif |
| 8359 | |
| 8360 | |
| 8361 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8362 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8363 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8364 | 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] | 8365 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8366 | |
| 8367 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8368 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8369 | { |
| 8370 | PyObject *result = NULL; |
| 8371 | int name; |
| 8372 | char *path; |
| 8373 | |
| 8374 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8375 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8376 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8377 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8378 | errno = 0; |
| 8379 | limit = pathconf(path, name); |
| 8380 | if (limit == -1 && errno != 0) { |
| 8381 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8382 | /* could be a path or name problem */ |
| 8383 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8384 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8385 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8386 | } |
| 8387 | else |
| 8388 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8389 | } |
| 8390 | return result; |
| 8391 | } |
| 8392 | #endif |
| 8393 | |
| 8394 | #ifdef HAVE_CONFSTR |
| 8395 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8396 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8397 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8398 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8399 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8401 | #endif |
| 8402 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8404 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8405 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8407 | #endif |
| 8408 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8410 | #endif |
| 8411 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8413 | #endif |
| 8414 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8415 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8416 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8417 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8419 | #endif |
| 8420 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8421 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8422 | #endif |
| 8423 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8425 | #endif |
| 8426 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8428 | #endif |
| 8429 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8431 | #endif |
| 8432 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8433 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8434 | #endif |
| 8435 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8436 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8437 | #endif |
| 8438 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8440 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8441 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8442 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8443 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8444 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8445 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8446 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8447 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8448 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8449 | #endif |
| 8450 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8451 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8452 | #endif |
| 8453 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8454 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8455 | #endif |
| 8456 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8458 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8459 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8461 | #endif |
| 8462 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8463 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8464 | #endif |
| 8465 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8467 | #endif |
| 8468 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8470 | #endif |
| 8471 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8473 | #endif |
| 8474 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8475 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8476 | #endif |
| 8477 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8479 | #endif |
| 8480 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8482 | #endif |
| 8483 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8484 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8485 | #endif |
| 8486 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8487 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8488 | #endif |
| 8489 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8490 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8491 | #endif |
| 8492 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8494 | #endif |
| 8495 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8497 | #endif |
| 8498 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8500 | #endif |
| 8501 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8502 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8503 | #endif |
| 8504 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8505 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8506 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8507 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8509 | #endif |
| 8510 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8511 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8512 | #endif |
| 8513 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8515 | #endif |
| 8516 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8517 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8518 | #endif |
| 8519 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8520 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8521 | #endif |
| 8522 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8523 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8524 | #endif |
| 8525 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8526 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8527 | #endif |
| 8528 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8529 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8530 | #endif |
| 8531 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8532 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8533 | #endif |
| 8534 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8536 | #endif |
| 8537 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8538 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8539 | #endif |
| 8540 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8541 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8545 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8546 | }; |
| 8547 | |
| 8548 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8549 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8550 | { |
| 8551 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8552 | sizeof(posix_constants_confstr) |
| 8553 | / sizeof(struct constdef)); |
| 8554 | } |
| 8555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8556 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8557 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8558 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8559 | |
| 8560 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8561 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8562 | { |
| 8563 | PyObject *result = NULL; |
| 8564 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8565 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8566 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8567 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8568 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8569 | return NULL; |
| 8570 | |
| 8571 | errno = 0; |
| 8572 | len = confstr(name, buffer, sizeof(buffer)); |
| 8573 | if (len == 0) { |
| 8574 | if (errno) { |
| 8575 | posix_error(); |
| 8576 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8577 | } |
| 8578 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8579 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8580 | } |
| 8581 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8582 | |
| 8583 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8584 | char *buf = PyMem_Malloc(len); |
| 8585 | if (buf == NULL) |
| 8586 | return PyErr_NoMemory(); |
| 8587 | confstr(name, buf, len); |
| 8588 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8589 | PyMem_Free(buf); |
| 8590 | } |
| 8591 | else |
| 8592 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8593 | return result; |
| 8594 | } |
| 8595 | #endif |
| 8596 | |
| 8597 | |
| 8598 | #ifdef HAVE_SYSCONF |
| 8599 | static struct constdef posix_constants_sysconf[] = { |
| 8600 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8602 | #endif |
| 8603 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8611 | #endif |
| 8612 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8614 | #endif |
| 8615 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8617 | #endif |
| 8618 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8619 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8622 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | #endif |
| 8627 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8629 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8630 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8632 | #endif |
| 8633 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8635 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8636 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8638 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8639 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8640 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8641 | #endif |
| 8642 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8643 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8644 | #endif |
| 8645 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8646 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | #endif |
| 8648 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8649 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8650 | #endif |
| 8651 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8653 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8654 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8655 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8656 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8657 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8659 | #endif |
| 8660 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8662 | #endif |
| 8663 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8672 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8674 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8675 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
| 8702 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
| 8708 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
| 8717 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8719 | #endif |
| 8720 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8722 | #endif |
| 8723 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | #endif |
| 8726 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | #endif |
| 8729 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8734 | #endif |
| 8735 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8737 | #endif |
| 8738 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8739 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8740 | #endif |
| 8741 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8742 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8744 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8746 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8747 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #endif |
| 8750 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8752 | #endif |
| 8753 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8756 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8758 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8759 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8761 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8762 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8764 | #endif |
| 8765 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8767 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8768 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8770 | #endif |
| 8771 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | #endif |
| 8774 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #endif |
| 8777 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8778 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8779 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8780 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8782 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8783 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8785 | #endif |
| 8786 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8788 | #endif |
| 8789 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8791 | #endif |
| 8792 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8793 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8794 | #endif |
| 8795 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #endif |
| 8798 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8799 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8800 | #endif |
| 8801 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8804 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8805 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8806 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8807 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8809 | #endif |
| 8810 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8811 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8812 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8813 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8814 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8815 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8816 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8818 | #endif |
| 8819 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8821 | #endif |
| 8822 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8824 | #endif |
| 8825 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8830 | #endif |
| 8831 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8832 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8836 | #endif |
| 8837 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8843 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8845 | #endif |
| 8846 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8848 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8849 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8851 | #endif |
| 8852 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8853 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8854 | #endif |
| 8855 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8857 | #endif |
| 8858 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8862 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8866 | #endif |
| 8867 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8869 | #endif |
| 8870 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8871 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8872 | #endif |
| 8873 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8875 | #endif |
| 8876 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8877 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8878 | #endif |
| 8879 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8881 | #endif |
| 8882 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | #endif |
| 8885 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #endif |
| 8894 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8899 | #endif |
| 8900 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | #endif |
| 8906 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8908 | #endif |
| 8909 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8911 | #endif |
| 8912 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8914 | #endif |
| 8915 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8916 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8920 | #endif |
| 8921 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8922 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8925 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8929 | #endif |
| 8930 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #endif |
| 8933 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8934 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8935 | #endif |
| 8936 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8937 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8938 | #endif |
| 8939 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8941 | #endif |
| 8942 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8947 | #endif |
| 8948 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8950 | #endif |
| 8951 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8953 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8954 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8955 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8956 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8957 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8959 | #endif |
| 8960 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | #endif |
| 8972 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8973 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #endif |
| 8975 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #endif |
| 8978 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | #endif |
| 8981 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8985 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8986 | #endif |
| 8987 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8992 | #endif |
| 8993 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #endif |
| 8996 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #endif |
| 8999 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | #endif |
| 9002 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9012 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #endif |
| 9017 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9018 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | #endif |
| 9020 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9022 | #endif |
| 9023 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9030 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | #endif |
| 9032 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9033 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9034 | #endif |
| 9035 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9037 | #endif |
| 9038 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9039 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9040 | #endif |
| 9041 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9042 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9043 | #endif |
| 9044 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9046 | #endif |
| 9047 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9048 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9049 | #endif |
| 9050 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9051 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9052 | #endif |
| 9053 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9054 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9055 | #endif |
| 9056 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9057 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9058 | #endif |
| 9059 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9060 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9061 | #endif |
| 9062 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9064 | #endif |
| 9065 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9067 | #endif |
| 9068 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #endif |
| 9071 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9072 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9073 | #endif |
| 9074 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9075 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9076 | #endif |
| 9077 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9078 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9079 | #endif |
| 9080 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9082 | #endif |
| 9083 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9084 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9085 | #endif |
| 9086 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9087 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9088 | #endif |
| 9089 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9090 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9091 | #endif |
| 9092 | }; |
| 9093 | |
| 9094 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9095 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9096 | { |
| 9097 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9098 | sizeof(posix_constants_sysconf) |
| 9099 | / sizeof(struct constdef)); |
| 9100 | } |
| 9101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9102 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9103 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9104 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9105 | |
| 9106 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9107 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | { |
| 9109 | PyObject *result = NULL; |
| 9110 | int name; |
| 9111 | |
| 9112 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9113 | int value; |
| 9114 | |
| 9115 | errno = 0; |
| 9116 | value = sysconf(name); |
| 9117 | if (value == -1 && errno != 0) |
| 9118 | posix_error(); |
| 9119 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9120 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9121 | } |
| 9122 | return result; |
| 9123 | } |
| 9124 | #endif |
| 9125 | |
| 9126 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9127 | /* This code is used to ensure that the tables of configuration value names |
| 9128 | * are in sorted order as required by conv_confname(), and also to build the |
| 9129 | * the exported dictionaries that are used to publish information about the |
| 9130 | * names available on the host platform. |
| 9131 | * |
| 9132 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9133 | * when used, even for platforms we're not able to test on. It also makes |
| 9134 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9135 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9136 | |
| 9137 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9138 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9139 | { |
| 9140 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9141 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9142 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9143 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9144 | |
| 9145 | return strcmp(c1->name, c2->name); |
| 9146 | } |
| 9147 | |
| 9148 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9149 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9150 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9151 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9152 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9153 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9154 | |
| 9155 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9156 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9157 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9158 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9159 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9160 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9162 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9163 | Py_XDECREF(o); |
| 9164 | Py_DECREF(d); |
| 9165 | return -1; |
| 9166 | } |
| 9167 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9168 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9169 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9170 | } |
| 9171 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9172 | /* Return -1 on failure, 0 on success. */ |
| 9173 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9174 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9175 | { |
| 9176 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9177 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9178 | sizeof(posix_constants_pathconf) |
| 9179 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9180 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9181 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9182 | #endif |
| 9183 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9184 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9185 | sizeof(posix_constants_confstr) |
| 9186 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9187 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9188 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9189 | #endif |
| 9190 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9191 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9192 | sizeof(posix_constants_sysconf) |
| 9193 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9194 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9195 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9196 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9197 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9198 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9199 | |
| 9200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9201 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9202 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9203 | 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] | 9204 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9205 | |
| 9206 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9207 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9208 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9209 | abort(); |
| 9210 | /*NOTREACHED*/ |
| 9211 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9212 | return NULL; |
| 9213 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9214 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9215 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9216 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9217 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9218 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9219 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9220 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9221 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9222 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9223 | application (if any) its extension is associated.\n\ |
| 9224 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9225 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9226 | \n\ |
| 9227 | startfile returns as soon as the associated application is launched.\n\ |
| 9228 | There is no option to wait for the application to close, and no way\n\ |
| 9229 | to retrieve the application's exit status.\n\ |
| 9230 | \n\ |
| 9231 | The filepath is relative to the current directory. If you want to use\n\ |
| 9232 | 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] | 9233 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9234 | |
| 9235 | static PyObject * |
| 9236 | win32_startfile(PyObject *self, PyObject *args) |
| 9237 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9238 | PyObject *ofilepath; |
| 9239 | char *filepath; |
| 9240 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9241 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9242 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9243 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9244 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9246 | &unipath, &operation)) { |
| 9247 | PyErr_Clear(); |
| 9248 | goto normal; |
| 9249 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9252 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9253 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9254 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9255 | PyErr_Clear(); |
| 9256 | operation = NULL; |
| 9257 | goto normal; |
| 9258 | } |
| 9259 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9260 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9261 | wpath = PyUnicode_AsUnicode(unipath); |
| 9262 | if (wpath == NULL) |
| 9263 | goto normal; |
| 9264 | if (uoperation) { |
| 9265 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9266 | if (woperation == NULL) |
| 9267 | goto normal; |
| 9268 | } |
| 9269 | else |
| 9270 | woperation = NULL; |
| 9271 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9272 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9273 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9274 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | Py_END_ALLOW_THREADS |
| 9276 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9277 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9279 | win32_error_object("startfile", unipath); |
| 9280 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | } |
| 9282 | Py_INCREF(Py_None); |
| 9283 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9284 | |
| 9285 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9286 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9287 | PyUnicode_FSConverter, &ofilepath, |
| 9288 | &operation)) |
| 9289 | return NULL; |
| 9290 | filepath = PyBytes_AsString(ofilepath); |
| 9291 | Py_BEGIN_ALLOW_THREADS |
| 9292 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9293 | NULL, NULL, SW_SHOWNORMAL); |
| 9294 | Py_END_ALLOW_THREADS |
| 9295 | if (rc <= (HINSTANCE)32) { |
| 9296 | PyObject *errval = win32_error("startfile", filepath); |
| 9297 | Py_DECREF(ofilepath); |
| 9298 | return errval; |
| 9299 | } |
| 9300 | Py_DECREF(ofilepath); |
| 9301 | Py_INCREF(Py_None); |
| 9302 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9303 | } |
| 9304 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9305 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9306 | #ifdef HAVE_GETLOADAVG |
| 9307 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9308 | "getloadavg() -> (float, float, float)\n\n\ |
| 9309 | Return the number of processes in the system run queue averaged over\n\ |
| 9310 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9311 | was unobtainable"); |
| 9312 | |
| 9313 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9314 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9315 | { |
| 9316 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9317 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9318 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9319 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9320 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9321 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9322 | } |
| 9323 | #endif |
| 9324 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9325 | #ifdef MS_WINDOWS |
| 9326 | |
| 9327 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9328 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9329 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9330 | |
| 9331 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9332 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9333 | DWORD dwFlags ); |
| 9334 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9335 | BYTE *pbBuffer ); |
| 9336 | |
| 9337 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9338 | /* This handle is never explicitly released. Instead, the operating |
| 9339 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9340 | static HCRYPTPROV hCryptProv = 0; |
| 9341 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9342 | static PyObject* |
| 9343 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9344 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | int howMany; |
| 9346 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9348 | /* Read arguments */ |
| 9349 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9350 | return NULL; |
| 9351 | if (howMany < 0) |
| 9352 | return PyErr_Format(PyExc_ValueError, |
| 9353 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9354 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9355 | if (hCryptProv == 0) { |
| 9356 | HINSTANCE hAdvAPI32 = NULL; |
| 9357 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
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 | /* Obtain handle to the DLL containing CryptoAPI |
| 9360 | This should not fail */ |
| 9361 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 9362 | if(hAdvAPI32 == NULL) |
| 9363 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9364 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | /* Obtain pointers to the CryptoAPI functions |
| 9366 | This will fail on some early versions of Win95 */ |
| 9367 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9368 | hAdvAPI32, |
| 9369 | "CryptAcquireContextA"); |
| 9370 | if (pCryptAcquireContext == NULL) |
| 9371 | return PyErr_Format(PyExc_NotImplementedError, |
| 9372 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9375 | hAdvAPI32, "CryptGenRandom"); |
| 9376 | if (pCryptGenRandom == NULL) |
| 9377 | return PyErr_Format(PyExc_NotImplementedError, |
| 9378 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9380 | /* Acquire context */ |
| 9381 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9382 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9383 | return win32_error("CryptAcquireContext", NULL); |
| 9384 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | /* Allocate bytes */ |
| 9387 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9388 | if (result != NULL) { |
| 9389 | /* Get random data */ |
| 9390 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9391 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9392 | PyBytes_AS_STRING(result))) { |
| 9393 | Py_DECREF(result); |
| 9394 | return win32_error("CryptGenRandom", NULL); |
| 9395 | } |
| 9396 | } |
| 9397 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9398 | } |
| 9399 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9400 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9401 | PyDoc_STRVAR(device_encoding__doc__, |
| 9402 | "device_encoding(fd) -> str\n\n\ |
| 9403 | Return a string describing the encoding of the device\n\ |
| 9404 | if the output is a terminal; else return None."); |
| 9405 | |
| 9406 | static PyObject * |
| 9407 | device_encoding(PyObject *self, PyObject *args) |
| 9408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9409 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9410 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9411 | UINT cp; |
| 9412 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9413 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9414 | return NULL; |
| 9415 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9416 | Py_INCREF(Py_None); |
| 9417 | return Py_None; |
| 9418 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9419 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9420 | if (fd == 0) |
| 9421 | cp = GetConsoleCP(); |
| 9422 | else if (fd == 1 || fd == 2) |
| 9423 | cp = GetConsoleOutputCP(); |
| 9424 | else |
| 9425 | cp = 0; |
| 9426 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9427 | has no console */ |
| 9428 | if (cp != 0) |
| 9429 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9430 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9431 | { |
| 9432 | char *codeset = nl_langinfo(CODESET); |
| 9433 | if (codeset != NULL && codeset[0] != 0) |
| 9434 | return PyUnicode_FromString(codeset); |
| 9435 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9436 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9437 | Py_INCREF(Py_None); |
| 9438 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9439 | } |
| 9440 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9441 | #ifdef __VMS |
| 9442 | /* Use openssl random routine */ |
| 9443 | #include <openssl/rand.h> |
| 9444 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9445 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9446 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9447 | |
| 9448 | static PyObject* |
| 9449 | vms_urandom(PyObject *self, PyObject *args) |
| 9450 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9451 | int howMany; |
| 9452 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9453 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9454 | /* Read arguments */ |
| 9455 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9456 | return NULL; |
| 9457 | if (howMany < 0) |
| 9458 | return PyErr_Format(PyExc_ValueError, |
| 9459 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9460 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | /* Allocate bytes */ |
| 9462 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9463 | if (result != NULL) { |
| 9464 | /* Get random data */ |
| 9465 | if (RAND_pseudo_bytes((unsigned char*) |
| 9466 | PyBytes_AS_STRING(result), |
| 9467 | howMany) < 0) { |
| 9468 | Py_DECREF(result); |
| 9469 | return PyErr_Format(PyExc_ValueError, |
| 9470 | "RAND_pseudo_bytes"); |
| 9471 | } |
| 9472 | } |
| 9473 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9474 | } |
| 9475 | #endif |
| 9476 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9477 | #ifdef HAVE_SETRESUID |
| 9478 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9479 | "setresuid(ruid, euid, suid)\n\n\ |
| 9480 | Set the current process's real, effective, and saved user ids."); |
| 9481 | |
| 9482 | static PyObject* |
| 9483 | posix_setresuid (PyObject *self, PyObject *args) |
| 9484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9485 | /* We assume uid_t is no larger than a long. */ |
| 9486 | long ruid, euid, suid; |
| 9487 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9488 | return NULL; |
| 9489 | if (setresuid(ruid, euid, suid) < 0) |
| 9490 | return posix_error(); |
| 9491 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9492 | } |
| 9493 | #endif |
| 9494 | |
| 9495 | #ifdef HAVE_SETRESGID |
| 9496 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9497 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9498 | Set the current process's real, effective, and saved group ids."); |
| 9499 | |
| 9500 | static PyObject* |
| 9501 | posix_setresgid (PyObject *self, PyObject *args) |
| 9502 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9503 | /* We assume uid_t is no larger than a long. */ |
| 9504 | long rgid, egid, sgid; |
| 9505 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9506 | return NULL; |
| 9507 | if (setresgid(rgid, egid, sgid) < 0) |
| 9508 | return posix_error(); |
| 9509 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9510 | } |
| 9511 | #endif |
| 9512 | |
| 9513 | #ifdef HAVE_GETRESUID |
| 9514 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9515 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9516 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9517 | |
| 9518 | static PyObject* |
| 9519 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9520 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9521 | uid_t ruid, euid, suid; |
| 9522 | long l_ruid, l_euid, l_suid; |
| 9523 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9524 | return posix_error(); |
| 9525 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9526 | l_ruid = ruid; |
| 9527 | l_euid = euid; |
| 9528 | l_suid = suid; |
| 9529 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9530 | } |
| 9531 | #endif |
| 9532 | |
| 9533 | #ifdef HAVE_GETRESGID |
| 9534 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9535 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9536 | 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] | 9537 | |
| 9538 | static PyObject* |
| 9539 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9540 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9541 | uid_t rgid, egid, sgid; |
| 9542 | long l_rgid, l_egid, l_sgid; |
| 9543 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9544 | return posix_error(); |
| 9545 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9546 | l_rgid = rgid; |
| 9547 | l_egid = egid; |
| 9548 | l_sgid = sgid; |
| 9549 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9550 | } |
| 9551 | #endif |
| 9552 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9553 | /* Posix *at family of functions: |
| 9554 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9555 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9556 | unlinkat, utimensat, mkfifoat */ |
| 9557 | |
| 9558 | #ifdef HAVE_FACCESSAT |
| 9559 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9560 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9561 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9562 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9563 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9564 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9565 | is interpreted relative to the current working directory."); |
| 9566 | |
| 9567 | static PyObject * |
| 9568 | posix_faccessat(PyObject *self, PyObject *args) |
| 9569 | { |
| 9570 | PyObject *opath; |
| 9571 | char *path; |
| 9572 | int mode; |
| 9573 | int res; |
| 9574 | int dirfd, flags = 0; |
| 9575 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9576 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9577 | return NULL; |
| 9578 | path = PyBytes_AsString(opath); |
| 9579 | Py_BEGIN_ALLOW_THREADS |
| 9580 | res = faccessat(dirfd, path, mode, flags); |
| 9581 | Py_END_ALLOW_THREADS |
| 9582 | Py_DECREF(opath); |
| 9583 | return PyBool_FromLong(res == 0); |
| 9584 | } |
| 9585 | #endif |
| 9586 | |
| 9587 | #ifdef HAVE_FCHMODAT |
| 9588 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9589 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9590 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9591 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9592 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9593 | is interpreted relative to the current working directory."); |
| 9594 | |
| 9595 | static PyObject * |
| 9596 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9597 | { |
| 9598 | int dirfd, mode, res; |
| 9599 | int flags = 0; |
| 9600 | PyObject *opath; |
| 9601 | char *path; |
| 9602 | |
| 9603 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9604 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9605 | return NULL; |
| 9606 | |
| 9607 | path = PyBytes_AsString(opath); |
| 9608 | |
| 9609 | Py_BEGIN_ALLOW_THREADS |
| 9610 | res = fchmodat(dirfd, path, mode, flags); |
| 9611 | Py_END_ALLOW_THREADS |
| 9612 | Py_DECREF(opath); |
| 9613 | if (res < 0) |
| 9614 | return posix_error(); |
| 9615 | Py_RETURN_NONE; |
| 9616 | } |
| 9617 | #endif /* HAVE_FCHMODAT */ |
| 9618 | |
| 9619 | #ifdef HAVE_FCHOWNAT |
| 9620 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9621 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9622 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9623 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9624 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9625 | is interpreted relative to the current working directory."); |
| 9626 | |
| 9627 | static PyObject * |
| 9628 | posix_fchownat(PyObject *self, PyObject *args) |
| 9629 | { |
| 9630 | PyObject *opath; |
| 9631 | int dirfd, res; |
| 9632 | long uid, gid; |
| 9633 | int flags = 0; |
| 9634 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9635 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9636 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9637 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9638 | return NULL; |
| 9639 | |
| 9640 | path = PyBytes_AsString(opath); |
| 9641 | |
| 9642 | Py_BEGIN_ALLOW_THREADS |
| 9643 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9644 | Py_END_ALLOW_THREADS |
| 9645 | Py_DECREF(opath); |
| 9646 | if (res < 0) |
| 9647 | return posix_error(); |
| 9648 | Py_RETURN_NONE; |
| 9649 | } |
| 9650 | #endif /* HAVE_FCHOWNAT */ |
| 9651 | |
| 9652 | #ifdef HAVE_FSTATAT |
| 9653 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9654 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9655 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9656 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9657 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9658 | is interpreted relative to the current working directory."); |
| 9659 | |
| 9660 | static PyObject * |
| 9661 | posix_fstatat(PyObject *self, PyObject *args) |
| 9662 | { |
| 9663 | PyObject *opath; |
| 9664 | char *path; |
| 9665 | STRUCT_STAT st; |
| 9666 | int dirfd, res, flags = 0; |
| 9667 | |
| 9668 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9669 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9670 | return NULL; |
| 9671 | path = PyBytes_AsString(opath); |
| 9672 | |
| 9673 | Py_BEGIN_ALLOW_THREADS |
| 9674 | res = fstatat(dirfd, path, &st, flags); |
| 9675 | Py_END_ALLOW_THREADS |
| 9676 | Py_DECREF(opath); |
| 9677 | if (res != 0) |
| 9678 | return posix_error(); |
| 9679 | |
| 9680 | return _pystat_fromstructstat(&st); |
| 9681 | } |
| 9682 | #endif |
| 9683 | |
| 9684 | #ifdef HAVE_FUTIMESAT |
| 9685 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 9686 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 9687 | futimesat(dirfd, path, None)\n\n\ |
| 9688 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9689 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9690 | is interpreted relative to the current working directory."); |
| 9691 | |
| 9692 | static PyObject * |
| 9693 | posix_futimesat(PyObject *self, PyObject *args) |
| 9694 | { |
| 9695 | PyObject *opath; |
| 9696 | char *path; |
| 9697 | int res, dirfd; |
| 9698 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9699 | time_t atime, mtime; |
| 9700 | long ausec, musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9701 | |
| 9702 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 9703 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9704 | return NULL; |
| 9705 | path = PyBytes_AsString(opath); |
| 9706 | if (arg == Py_None) { |
| 9707 | /* optional time values not given */ |
| 9708 | Py_BEGIN_ALLOW_THREADS |
| 9709 | res = futimesat(dirfd, path, NULL); |
| 9710 | Py_END_ALLOW_THREADS |
| 9711 | } |
| 9712 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9713 | PyErr_SetString(PyExc_TypeError, |
| 9714 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9715 | Py_DECREF(opath); |
| 9716 | return NULL; |
| 9717 | } |
| 9718 | else { |
| 9719 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9720 | &atime, &ausec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9721 | Py_DECREF(opath); |
| 9722 | return NULL; |
| 9723 | } |
| 9724 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9725 | &mtime, &musec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9726 | Py_DECREF(opath); |
| 9727 | return NULL; |
| 9728 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9729 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9730 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9731 | { |
| 9732 | #ifdef HAVE_UTIMENSAT |
| 9733 | struct timespec buf[2]; |
| 9734 | buf[0].tv_sec = atime; |
| 9735 | buf[0].tv_nsec = ausec; |
| 9736 | buf[1].tv_sec = mtime; |
| 9737 | buf[1].tv_nsec = musec; |
| 9738 | res = utimensat(dirfd, path, buf, 0); |
| 9739 | #else |
| 9740 | struct timeval buf[2]; |
| 9741 | buf[0].tv_sec = atime; |
| 9742 | buf[0].tv_usec = ausec; |
| 9743 | buf[1].tv_sec = mtime; |
| 9744 | buf[1].tv_usec = musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9745 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9746 | #endif |
| 9747 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9748 | Py_END_ALLOW_THREADS |
| 9749 | } |
| 9750 | Py_DECREF(opath); |
| 9751 | if (res < 0) { |
| 9752 | return posix_error(); |
| 9753 | } |
| 9754 | Py_RETURN_NONE; |
| 9755 | } |
| 9756 | #endif |
| 9757 | |
| 9758 | #ifdef HAVE_LINKAT |
| 9759 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9760 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9761 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9762 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9763 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9764 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9765 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9766 | also applies for dstpath."); |
| 9767 | |
| 9768 | static PyObject * |
| 9769 | posix_linkat(PyObject *self, PyObject *args) |
| 9770 | { |
| 9771 | PyObject *osrc, *odst; |
| 9772 | char *src, *dst; |
| 9773 | int res, srcfd, dstfd; |
| 9774 | int flags = 0; |
| 9775 | |
| 9776 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9777 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9778 | return NULL; |
| 9779 | src = PyBytes_AsString(osrc); |
| 9780 | dst = PyBytes_AsString(odst); |
| 9781 | Py_BEGIN_ALLOW_THREADS |
| 9782 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9783 | Py_END_ALLOW_THREADS |
| 9784 | Py_DECREF(osrc); |
| 9785 | Py_DECREF(odst); |
| 9786 | if (res < 0) |
| 9787 | return posix_error(); |
| 9788 | Py_RETURN_NONE; |
| 9789 | } |
| 9790 | #endif /* HAVE_LINKAT */ |
| 9791 | |
| 9792 | #ifdef HAVE_MKDIRAT |
| 9793 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9794 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9795 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9796 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9797 | is interpreted relative to the current working directory."); |
| 9798 | |
| 9799 | static PyObject * |
| 9800 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9801 | { |
| 9802 | int res, dirfd; |
| 9803 | PyObject *opath; |
| 9804 | char *path; |
| 9805 | int mode = 0777; |
| 9806 | |
| 9807 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9808 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9809 | return NULL; |
| 9810 | path = PyBytes_AsString(opath); |
| 9811 | Py_BEGIN_ALLOW_THREADS |
| 9812 | res = mkdirat(dirfd, path, mode); |
| 9813 | Py_END_ALLOW_THREADS |
| 9814 | Py_DECREF(opath); |
| 9815 | if (res < 0) |
| 9816 | return posix_error(); |
| 9817 | Py_RETURN_NONE; |
| 9818 | } |
| 9819 | #endif |
| 9820 | |
| 9821 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9822 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9823 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9824 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9825 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9826 | is interpreted relative to the current working directory."); |
| 9827 | |
| 9828 | static PyObject * |
| 9829 | posix_mknodat(PyObject *self, PyObject *args) |
| 9830 | { |
| 9831 | PyObject *opath; |
| 9832 | char *filename; |
| 9833 | int mode = 0600; |
| 9834 | int device = 0; |
| 9835 | int res, dirfd; |
| 9836 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9837 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9838 | return NULL; |
| 9839 | filename = PyBytes_AS_STRING(opath); |
| 9840 | Py_BEGIN_ALLOW_THREADS |
| 9841 | res = mknodat(dirfd, filename, mode, device); |
| 9842 | Py_END_ALLOW_THREADS |
| 9843 | Py_DECREF(opath); |
| 9844 | if (res < 0) |
| 9845 | return posix_error(); |
| 9846 | Py_RETURN_NONE; |
| 9847 | } |
| 9848 | #endif |
| 9849 | |
| 9850 | #ifdef HAVE_OPENAT |
| 9851 | PyDoc_STRVAR(posix_openat__doc__, |
| 9852 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9853 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9854 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9855 | is interpreted relative to the current working directory."); |
| 9856 | |
| 9857 | static PyObject * |
| 9858 | posix_openat(PyObject *self, PyObject *args) |
| 9859 | { |
| 9860 | PyObject *ofile; |
| 9861 | char *file; |
| 9862 | int flag, dirfd, fd; |
| 9863 | int mode = 0777; |
| 9864 | |
| 9865 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9866 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9867 | &flag, &mode)) |
| 9868 | return NULL; |
| 9869 | file = PyBytes_AsString(ofile); |
| 9870 | Py_BEGIN_ALLOW_THREADS |
| 9871 | fd = openat(dirfd, file, flag, mode); |
| 9872 | Py_END_ALLOW_THREADS |
| 9873 | Py_DECREF(ofile); |
| 9874 | if (fd < 0) |
| 9875 | return posix_error(); |
| 9876 | return PyLong_FromLong((long)fd); |
| 9877 | } |
| 9878 | #endif |
| 9879 | |
| 9880 | #ifdef HAVE_READLINKAT |
| 9881 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9882 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9883 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9884 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9885 | is interpreted relative to the current working directory."); |
| 9886 | |
| 9887 | static PyObject * |
| 9888 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9889 | { |
| 9890 | PyObject *v, *opath; |
| 9891 | char buf[MAXPATHLEN]; |
| 9892 | char *path; |
| 9893 | int n, dirfd; |
| 9894 | int arg_is_unicode = 0; |
| 9895 | |
| 9896 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9897 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9898 | return NULL; |
| 9899 | path = PyBytes_AsString(opath); |
| 9900 | v = PySequence_GetItem(args, 1); |
| 9901 | if (v == NULL) { |
| 9902 | Py_DECREF(opath); |
| 9903 | return NULL; |
| 9904 | } |
| 9905 | |
| 9906 | if (PyUnicode_Check(v)) { |
| 9907 | arg_is_unicode = 1; |
| 9908 | } |
| 9909 | Py_DECREF(v); |
| 9910 | |
| 9911 | Py_BEGIN_ALLOW_THREADS |
| 9912 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9913 | Py_END_ALLOW_THREADS |
| 9914 | Py_DECREF(opath); |
| 9915 | if (n < 0) |
| 9916 | return posix_error(); |
| 9917 | |
| 9918 | if (arg_is_unicode) |
| 9919 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9920 | else |
| 9921 | return PyBytes_FromStringAndSize(buf, n); |
| 9922 | } |
| 9923 | #endif /* HAVE_READLINKAT */ |
| 9924 | |
| 9925 | #ifdef HAVE_RENAMEAT |
| 9926 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9927 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9928 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9929 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9930 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9931 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9932 | also applies for newpath."); |
| 9933 | |
| 9934 | static PyObject * |
| 9935 | posix_renameat(PyObject *self, PyObject *args) |
| 9936 | { |
| 9937 | int res; |
| 9938 | PyObject *opathold, *opathnew; |
| 9939 | char *opath, *npath; |
| 9940 | int oldfd, newfd; |
| 9941 | |
| 9942 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9943 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9944 | return NULL; |
| 9945 | opath = PyBytes_AsString(opathold); |
| 9946 | npath = PyBytes_AsString(opathnew); |
| 9947 | Py_BEGIN_ALLOW_THREADS |
| 9948 | res = renameat(oldfd, opath, newfd, npath); |
| 9949 | Py_END_ALLOW_THREADS |
| 9950 | Py_DECREF(opathold); |
| 9951 | Py_DECREF(opathnew); |
| 9952 | if (res < 0) |
| 9953 | return posix_error(); |
| 9954 | Py_RETURN_NONE; |
| 9955 | } |
| 9956 | #endif |
| 9957 | |
| 9958 | #if HAVE_SYMLINKAT |
| 9959 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9960 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9961 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9962 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9963 | is interpreted relative to the current working directory."); |
| 9964 | |
| 9965 | static PyObject * |
| 9966 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9967 | { |
| 9968 | int res, dstfd; |
| 9969 | PyObject *osrc, *odst; |
| 9970 | char *src, *dst; |
| 9971 | |
| 9972 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9973 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9974 | return NULL; |
| 9975 | src = PyBytes_AsString(osrc); |
| 9976 | dst = PyBytes_AsString(odst); |
| 9977 | Py_BEGIN_ALLOW_THREADS |
| 9978 | res = symlinkat(src, dstfd, dst); |
| 9979 | Py_END_ALLOW_THREADS |
| 9980 | Py_DECREF(osrc); |
| 9981 | Py_DECREF(odst); |
| 9982 | if (res < 0) |
| 9983 | return posix_error(); |
| 9984 | Py_RETURN_NONE; |
| 9985 | } |
| 9986 | #endif /* HAVE_SYMLINKAT */ |
| 9987 | |
| 9988 | #ifdef HAVE_UNLINKAT |
| 9989 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9990 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9991 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9992 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9993 | specified, unlinkat() behaves like rmdir().\n\ |
| 9994 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9995 | is interpreted relative to the current working directory."); |
| 9996 | |
| 9997 | static PyObject * |
| 9998 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9999 | { |
| 10000 | int dirfd, res, flags = 0; |
| 10001 | PyObject *opath; |
| 10002 | char *path; |
| 10003 | |
| 10004 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 10005 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 10006 | return NULL; |
| 10007 | path = PyBytes_AsString(opath); |
| 10008 | Py_BEGIN_ALLOW_THREADS |
| 10009 | res = unlinkat(dirfd, path, flags); |
| 10010 | Py_END_ALLOW_THREADS |
| 10011 | Py_DECREF(opath); |
| 10012 | if (res < 0) |
| 10013 | return posix_error(); |
| 10014 | Py_RETURN_NONE; |
| 10015 | } |
| 10016 | #endif |
| 10017 | |
| 10018 | #ifdef HAVE_UTIMENSAT |
| 10019 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 10020 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 10021 | (mtime_sec, mtime_nsec), flags)\n\ |
| 10022 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 10023 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 10024 | relative, it is taken as relative to dirfd.\n\ |
| 10025 | The second form sets atime and mtime to the current time.\n\ |
| 10026 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 10027 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10028 | is interpreted relative to the current working directory.\n\ |
| 10029 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 10030 | current time.\n\ |
| 10031 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 10032 | |
| 10033 | static PyObject * |
| 10034 | posix_utimensat(PyObject *self, PyObject *args) |
| 10035 | { |
| 10036 | PyObject *opath; |
| 10037 | char *path; |
| 10038 | int res, dirfd, flags = 0; |
| 10039 | PyObject *atime, *mtime; |
| 10040 | |
| 10041 | struct timespec buf[2]; |
| 10042 | |
| 10043 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 10044 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10045 | return NULL; |
| 10046 | path = PyBytes_AsString(opath); |
| 10047 | if (atime == Py_None && mtime == Py_None) { |
| 10048 | /* optional time values not given */ |
| 10049 | Py_BEGIN_ALLOW_THREADS |
| 10050 | res = utimensat(dirfd, path, NULL, flags); |
| 10051 | Py_END_ALLOW_THREADS |
| 10052 | } |
| 10053 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10054 | PyErr_SetString(PyExc_TypeError, |
| 10055 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10056 | Py_DECREF(opath); |
| 10057 | return NULL; |
| 10058 | } |
| 10059 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10060 | PyErr_SetString(PyExc_TypeError, |
| 10061 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10062 | Py_DECREF(opath); |
| 10063 | return NULL; |
| 10064 | } |
| 10065 | else { |
| 10066 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10067 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10068 | Py_DECREF(opath); |
| 10069 | return NULL; |
| 10070 | } |
| 10071 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10072 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10073 | Py_DECREF(opath); |
| 10074 | return NULL; |
| 10075 | } |
| 10076 | Py_BEGIN_ALLOW_THREADS |
| 10077 | res = utimensat(dirfd, path, buf, flags); |
| 10078 | Py_END_ALLOW_THREADS |
| 10079 | } |
| 10080 | Py_DECREF(opath); |
| 10081 | if (res < 0) { |
| 10082 | return posix_error(); |
| 10083 | } |
| 10084 | Py_RETURN_NONE; |
| 10085 | } |
| 10086 | #endif |
| 10087 | |
| 10088 | #ifdef HAVE_MKFIFOAT |
| 10089 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10090 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10091 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10092 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10093 | is interpreted relative to the current working directory."); |
| 10094 | |
| 10095 | static PyObject * |
| 10096 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10097 | { |
| 10098 | PyObject *opath; |
| 10099 | char *filename; |
| 10100 | int mode = 0666; |
| 10101 | int res, dirfd; |
| 10102 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10103 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10104 | return NULL; |
| 10105 | filename = PyBytes_AS_STRING(opath); |
| 10106 | Py_BEGIN_ALLOW_THREADS |
| 10107 | res = mkfifoat(dirfd, filename, mode); |
| 10108 | Py_END_ALLOW_THREADS |
| 10109 | Py_DECREF(opath); |
| 10110 | if (res < 0) |
| 10111 | return posix_error(); |
| 10112 | Py_RETURN_NONE; |
| 10113 | } |
| 10114 | #endif |
| 10115 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10116 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10117 | |
| 10118 | static int |
| 10119 | try_getxattr(const char *path, const char *name, |
| 10120 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10121 | Py_ssize_t buf_size, PyObject **res) |
| 10122 | { |
| 10123 | PyObject *value; |
| 10124 | Py_ssize_t len; |
| 10125 | |
| 10126 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10127 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10128 | if (!value) |
| 10129 | return 0; |
| 10130 | Py_BEGIN_ALLOW_THREADS; |
| 10131 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10132 | Py_END_ALLOW_THREADS; |
| 10133 | if (len < 0) { |
| 10134 | Py_DECREF(value); |
| 10135 | if (errno == ERANGE) { |
| 10136 | value = NULL; |
| 10137 | } |
| 10138 | else { |
| 10139 | posix_error(); |
| 10140 | return 0; |
| 10141 | } |
| 10142 | } |
| 10143 | else if (len != buf_size) { |
| 10144 | /* Can only shrink. */ |
| 10145 | _PyBytes_Resize(&value, len); |
| 10146 | } |
| 10147 | *res = value; |
| 10148 | return 1; |
| 10149 | } |
| 10150 | |
| 10151 | static PyObject * |
| 10152 | getxattr_common(const char *path, PyObject *name_obj, |
| 10153 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10154 | { |
| 10155 | PyObject *value; |
| 10156 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10157 | |
| 10158 | /* Try a small value first. */ |
| 10159 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10160 | return NULL; |
| 10161 | if (value) |
| 10162 | return value; |
| 10163 | /* Now the maximum possible one. */ |
| 10164 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10165 | return NULL; |
| 10166 | assert(value); |
| 10167 | return value; |
| 10168 | } |
| 10169 | |
| 10170 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10171 | "getxattr(path, attr) -> value\n\n\ |
| 10172 | Return the value of extended attribute *name* on *path*."); |
| 10173 | |
| 10174 | static PyObject * |
| 10175 | posix_getxattr(PyObject *self, PyObject *args) |
| 10176 | { |
| 10177 | PyObject *path, *res, *name; |
| 10178 | |
| 10179 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10180 | PyUnicode_FSConverter, &name)) |
| 10181 | return NULL; |
| 10182 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10183 | Py_DECREF(path); |
| 10184 | Py_DECREF(name); |
| 10185 | return res; |
| 10186 | } |
| 10187 | |
| 10188 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10189 | "lgetxattr(path, attr) -> value\n\n\ |
| 10190 | Like getxattr but don't follow symlinks."); |
| 10191 | |
| 10192 | static PyObject * |
| 10193 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10194 | { |
| 10195 | PyObject *path, *res, *name; |
| 10196 | |
| 10197 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10198 | PyUnicode_FSConverter, &name)) |
| 10199 | return NULL; |
| 10200 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10201 | Py_DECREF(path); |
| 10202 | Py_DECREF(name); |
| 10203 | return res; |
| 10204 | } |
| 10205 | |
| 10206 | static ssize_t |
| 10207 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10208 | { |
| 10209 | /* Hack to share code. */ |
| 10210 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10211 | } |
| 10212 | |
| 10213 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10214 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10215 | Like getxattr but operate on a fd instead of a path."); |
| 10216 | |
| 10217 | static PyObject * |
| 10218 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10219 | { |
| 10220 | PyObject *res, *name; |
| 10221 | int fd; |
| 10222 | |
| 10223 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10224 | return NULL; |
| 10225 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10226 | Py_DECREF(name); |
| 10227 | return res; |
| 10228 | } |
| 10229 | |
| 10230 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10231 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10232 | Set extended attribute *attr* on *path* to *value*."); |
| 10233 | |
| 10234 | static PyObject * |
| 10235 | posix_setxattr(PyObject *self, PyObject *args) |
| 10236 | { |
| 10237 | PyObject *path, *name; |
| 10238 | Py_buffer data; |
| 10239 | int flags = 0, err; |
| 10240 | |
| 10241 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10242 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10243 | return NULL; |
| 10244 | Py_BEGIN_ALLOW_THREADS; |
| 10245 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10246 | data.buf, data.len, flags); |
| 10247 | Py_END_ALLOW_THREADS; |
| 10248 | Py_DECREF(path); |
| 10249 | Py_DECREF(name); |
| 10250 | PyBuffer_Release(&data); |
| 10251 | if (err) |
| 10252 | return posix_error(); |
| 10253 | Py_RETURN_NONE; |
| 10254 | } |
| 10255 | |
| 10256 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10257 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10258 | Like setxattr but don't follow symlinks."); |
| 10259 | |
| 10260 | static PyObject * |
| 10261 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10262 | { |
| 10263 | PyObject *path, *name; |
| 10264 | Py_buffer data; |
| 10265 | int flags = 0, err; |
| 10266 | |
| 10267 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10268 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10269 | return NULL; |
| 10270 | Py_BEGIN_ALLOW_THREADS; |
| 10271 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10272 | data.buf, data.len, flags); |
| 10273 | Py_END_ALLOW_THREADS; |
| 10274 | Py_DECREF(path); |
| 10275 | Py_DECREF(name); |
| 10276 | PyBuffer_Release(&data); |
| 10277 | if (err) |
| 10278 | return posix_error(); |
| 10279 | Py_RETURN_NONE; |
| 10280 | } |
| 10281 | |
| 10282 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10283 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10284 | Like setxattr but operates on *fd* instead of a path."); |
| 10285 | |
| 10286 | static PyObject * |
| 10287 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10288 | { |
| 10289 | Py_buffer data; |
| 10290 | const char *name; |
| 10291 | int fd, flags = 0, err; |
| 10292 | |
| 10293 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10294 | &name, &data, &flags)) |
| 10295 | return NULL; |
| 10296 | Py_BEGIN_ALLOW_THREADS; |
| 10297 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10298 | Py_END_ALLOW_THREADS; |
| 10299 | Py_DECREF(name); |
| 10300 | PyBuffer_Release(&data); |
| 10301 | if (err) |
| 10302 | return posix_error(); |
| 10303 | Py_RETURN_NONE; |
| 10304 | } |
| 10305 | |
| 10306 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10307 | "removexattr(path, attr)\n\n\ |
| 10308 | Remove extended attribute *attr* on *path*."); |
| 10309 | |
| 10310 | static PyObject * |
| 10311 | posix_removexattr(PyObject *self, PyObject *args) |
| 10312 | { |
| 10313 | PyObject *path, *name; |
| 10314 | int err; |
| 10315 | |
| 10316 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10317 | PyUnicode_FSConverter, &name)) |
| 10318 | return NULL; |
| 10319 | Py_BEGIN_ALLOW_THREADS; |
| 10320 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10321 | Py_END_ALLOW_THREADS; |
| 10322 | Py_DECREF(path); |
| 10323 | Py_DECREF(name); |
| 10324 | if (err) |
| 10325 | return posix_error(); |
| 10326 | Py_RETURN_NONE; |
| 10327 | } |
| 10328 | |
| 10329 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10330 | "lremovexattr(path, attr)\n\n\ |
| 10331 | Like removexattr but don't follow symlinks."); |
| 10332 | |
| 10333 | static PyObject * |
| 10334 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10335 | { |
| 10336 | PyObject *path, *name; |
| 10337 | int err; |
| 10338 | |
| 10339 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10340 | PyUnicode_FSConverter, &name)) |
| 10341 | return NULL; |
| 10342 | Py_BEGIN_ALLOW_THREADS; |
| 10343 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10344 | Py_END_ALLOW_THREADS; |
| 10345 | Py_DECREF(path); |
| 10346 | Py_DECREF(name); |
| 10347 | if (err) |
| 10348 | return posix_error(); |
| 10349 | Py_RETURN_NONE; |
| 10350 | } |
| 10351 | |
| 10352 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10353 | "fremovexattr(fd, attr)\n\n\ |
| 10354 | Like removexattr but operates on a file descriptor."); |
| 10355 | |
| 10356 | static PyObject * |
| 10357 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10358 | { |
| 10359 | PyObject *name; |
| 10360 | int fd, err; |
| 10361 | |
| 10362 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10363 | PyUnicode_FSConverter, &name)) |
| 10364 | return NULL; |
| 10365 | Py_BEGIN_ALLOW_THREADS; |
| 10366 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10367 | Py_END_ALLOW_THREADS; |
| 10368 | Py_DECREF(name); |
| 10369 | if (err) |
| 10370 | return posix_error(); |
| 10371 | Py_RETURN_NONE; |
| 10372 | } |
| 10373 | |
| 10374 | static Py_ssize_t |
| 10375 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10376 | Py_ssize_t buf_size, char **buf) |
| 10377 | { |
| 10378 | Py_ssize_t len; |
| 10379 | |
| 10380 | *buf = PyMem_MALLOC(buf_size); |
| 10381 | if (!*buf) { |
| 10382 | PyErr_NoMemory(); |
| 10383 | return -1; |
| 10384 | } |
| 10385 | Py_BEGIN_ALLOW_THREADS; |
| 10386 | len = list(path, *buf, buf_size); |
| 10387 | Py_END_ALLOW_THREADS; |
| 10388 | if (len < 0) { |
| 10389 | PyMem_FREE(*buf); |
| 10390 | if (errno != ERANGE) |
| 10391 | posix_error(); |
| 10392 | return -1; |
| 10393 | } |
| 10394 | return len; |
| 10395 | } |
| 10396 | |
| 10397 | static PyObject * |
| 10398 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10399 | { |
| 10400 | PyObject *res, *attr; |
| 10401 | Py_ssize_t len, err, start, i; |
| 10402 | char *buf; |
| 10403 | |
| 10404 | len = try_listxattr(path, list, 256, &buf); |
| 10405 | if (len < 0) { |
| 10406 | if (PyErr_Occurred()) |
| 10407 | return NULL; |
| 10408 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10409 | if (len < 0) |
| 10410 | return NULL; |
| 10411 | } |
| 10412 | res = PyList_New(0); |
| 10413 | if (!res) { |
| 10414 | PyMem_FREE(buf); |
| 10415 | return NULL; |
| 10416 | } |
| 10417 | for (start = i = 0; i < len; i++) { |
| 10418 | if (!buf[i]) { |
| 10419 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10420 | if (!attr) { |
| 10421 | Py_DECREF(res); |
| 10422 | PyMem_FREE(buf); |
| 10423 | return NULL; |
| 10424 | } |
| 10425 | err = PyList_Append(res, attr); |
| 10426 | Py_DECREF(attr); |
| 10427 | if (err) { |
| 10428 | Py_DECREF(res); |
| 10429 | PyMem_FREE(buf); |
| 10430 | return NULL; |
| 10431 | } |
| 10432 | start = i + 1; |
| 10433 | } |
| 10434 | } |
| 10435 | PyMem_FREE(buf); |
| 10436 | return res; |
| 10437 | } |
| 10438 | |
| 10439 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10440 | "listxattr(path)\n\n\ |
| 10441 | Return a list of extended attributes on *path*."); |
| 10442 | |
| 10443 | static PyObject * |
| 10444 | posix_listxattr(PyObject *self, PyObject *args) |
| 10445 | { |
| 10446 | PyObject *path, *res; |
| 10447 | |
| 10448 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10449 | return NULL; |
| 10450 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10451 | Py_DECREF(path); |
| 10452 | return res; |
| 10453 | } |
| 10454 | |
| 10455 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10456 | "llistxattr(path)\n\n\ |
| 10457 | Like listxattr but don't follow symlinks.."); |
| 10458 | |
| 10459 | static PyObject * |
| 10460 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10461 | { |
| 10462 | PyObject *path, *res; |
| 10463 | |
| 10464 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10465 | return NULL; |
| 10466 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10467 | Py_DECREF(path); |
| 10468 | return res; |
| 10469 | } |
| 10470 | |
| 10471 | static ssize_t |
| 10472 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10473 | { |
| 10474 | /* Hack to share code. */ |
| 10475 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10476 | } |
| 10477 | |
| 10478 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10479 | "flistxattr(path)\n\n\ |
| 10480 | Like flistxattr but operates on a file descriptor."); |
| 10481 | |
| 10482 | static PyObject * |
| 10483 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10484 | { |
| 10485 | long fd; |
| 10486 | |
| 10487 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10488 | return NULL; |
| 10489 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10490 | } |
| 10491 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10492 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10493 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10494 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10495 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10496 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10497 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10498 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10499 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10500 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10501 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10502 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10504 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10505 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10506 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10507 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10508 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10509 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10510 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10511 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10512 | #endif /* HAVE_LCHMOD */ |
| 10513 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10514 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10515 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10516 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10517 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10518 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10519 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10520 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10521 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10522 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10524 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10525 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10526 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10527 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10528 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10529 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10530 | METH_NOARGS, posix_getcwd__doc__}, |
| 10531 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10532 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10533 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10534 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10535 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10536 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10538 | #ifdef HAVE_FDOPENDIR |
| 10539 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10540 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10542 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10543 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10544 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10545 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10546 | #ifdef HAVE_GETPRIORITY |
| 10547 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10548 | #endif /* HAVE_GETPRIORITY */ |
| 10549 | #ifdef HAVE_SETPRIORITY |
| 10550 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10551 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10552 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10553 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10554 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10555 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10556 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10557 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10558 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10559 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10560 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10561 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10562 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10564 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10565 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10566 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10567 | win_symlink__doc__}, |
| 10568 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10569 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10570 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10571 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10572 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10573 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10574 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10575 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10577 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10578 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10579 | #ifdef HAVE_FUTIMES |
| 10580 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10581 | #endif |
| 10582 | #ifdef HAVE_LUTIMES |
| 10583 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10584 | #endif |
| 10585 | #ifdef HAVE_FUTIMENS |
| 10586 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10587 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10588 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10589 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10590 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10591 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10592 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10593 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10594 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10595 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10596 | #ifdef HAVE_FEXECVE |
| 10597 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10598 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10599 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10600 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10601 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10602 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10603 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10604 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10605 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10606 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10607 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10608 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10609 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10610 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10611 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10612 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10613 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10614 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10615 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10616 | {"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] | 10617 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10618 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10619 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10620 | #endif |
| 10621 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10622 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10623 | #endif |
| 10624 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10625 | {"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] | 10626 | #endif |
| 10627 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10628 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10629 | #endif |
| 10630 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10631 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10632 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10633 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10634 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10635 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10636 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10637 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10638 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10639 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10640 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10641 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10642 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10643 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10644 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10645 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10647 | #endif /* HAVE_GETEGID */ |
| 10648 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10649 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10650 | #endif /* HAVE_GETEUID */ |
| 10651 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10653 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10654 | #ifdef HAVE_GETGROUPLIST |
| 10655 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10656 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10657 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10658 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10659 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10661 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10663 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10664 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10666 | #endif /* HAVE_GETPPID */ |
| 10667 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10668 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10669 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10670 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10672 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10673 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10674 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10675 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10676 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10678 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10679 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10680 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10681 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10682 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10684 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10685 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10686 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10687 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10688 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10689 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10690 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10691 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10692 | #endif /* HAVE_SETEUID */ |
| 10693 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10694 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10695 | #endif /* HAVE_SETEGID */ |
| 10696 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10697 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10698 | #endif /* HAVE_SETREUID */ |
| 10699 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10700 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10701 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10702 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10703 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10704 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10705 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10706 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10707 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10708 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10710 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10711 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10712 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10713 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10714 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10716 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10717 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10718 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10719 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10720 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10721 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10722 | #endif /* HAVE_WAIT3 */ |
| 10723 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10724 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10725 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10726 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10727 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10728 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10729 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10731 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10732 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10733 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10734 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10735 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10737 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10738 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10740 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10741 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10743 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10744 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10745 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10746 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10747 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10748 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10749 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10750 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10751 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10752 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10753 | #ifdef HAVE_LOCKF |
| 10754 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10755 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10756 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10757 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10758 | #ifdef HAVE_READV |
| 10759 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10760 | #endif |
| 10761 | #ifdef HAVE_PREAD |
| 10762 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10763 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10764 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10765 | #ifdef HAVE_WRITEV |
| 10766 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10767 | #endif |
| 10768 | #ifdef HAVE_PWRITE |
| 10769 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10770 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10771 | #ifdef HAVE_SENDFILE |
| 10772 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10773 | posix_sendfile__doc__}, |
| 10774 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10775 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10776 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10777 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10778 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10779 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10780 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10781 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10782 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10783 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10785 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10786 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10787 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10788 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10789 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10790 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10791 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10792 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10793 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10794 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10795 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10796 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10797 | #ifdef HAVE_TRUNCATE |
| 10798 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10799 | #endif |
| 10800 | #ifdef HAVE_POSIX_FALLOCATE |
| 10801 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10802 | #endif |
| 10803 | #ifdef HAVE_POSIX_FADVISE |
| 10804 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10805 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10806 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10807 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10808 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10809 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10810 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10811 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10813 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10815 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10816 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10818 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10819 | #ifdef HAVE_SYNC |
| 10820 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10821 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10822 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10824 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10825 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10826 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10828 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10829 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10831 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10832 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10834 | #endif /* WIFSTOPPED */ |
| 10835 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10837 | #endif /* WIFSIGNALED */ |
| 10838 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10840 | #endif /* WIFEXITED */ |
| 10841 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10843 | #endif /* WEXITSTATUS */ |
| 10844 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10846 | #endif /* WTERMSIG */ |
| 10847 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10849 | #endif /* WSTOPSIG */ |
| 10850 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10851 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10852 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10853 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10854 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10855 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10856 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10857 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10859 | #endif |
| 10860 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10862 | #endif |
| 10863 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10864 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10865 | #endif |
| 10866 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10868 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10869 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10870 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10872 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10873 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10874 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10875 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10876 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10877 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10878 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10879 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10880 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10882 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10883 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10884 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10885 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10886 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10887 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10888 | #endif |
| 10889 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10890 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10891 | #endif |
| 10892 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10894 | #endif |
| 10895 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10897 | #endif |
| 10898 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10899 | /* posix *at family of functions */ |
| 10900 | #ifdef HAVE_FACCESSAT |
| 10901 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10902 | #endif |
| 10903 | #ifdef HAVE_FCHMODAT |
| 10904 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10905 | #endif /* HAVE_FCHMODAT */ |
| 10906 | #ifdef HAVE_FCHOWNAT |
| 10907 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10908 | #endif /* HAVE_FCHOWNAT */ |
| 10909 | #ifdef HAVE_FSTATAT |
| 10910 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10911 | #endif |
| 10912 | #ifdef HAVE_FUTIMESAT |
| 10913 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10914 | #endif |
| 10915 | #ifdef HAVE_LINKAT |
| 10916 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10917 | #endif /* HAVE_LINKAT */ |
| 10918 | #ifdef HAVE_MKDIRAT |
| 10919 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10920 | #endif |
| 10921 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10922 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10923 | #endif |
| 10924 | #ifdef HAVE_OPENAT |
| 10925 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10926 | #endif |
| 10927 | #ifdef HAVE_READLINKAT |
| 10928 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10929 | #endif /* HAVE_READLINKAT */ |
| 10930 | #ifdef HAVE_RENAMEAT |
| 10931 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10932 | #endif |
| 10933 | #if HAVE_SYMLINKAT |
| 10934 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10935 | #endif /* HAVE_SYMLINKAT */ |
| 10936 | #ifdef HAVE_UNLINKAT |
| 10937 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10938 | #endif |
| 10939 | #ifdef HAVE_UTIMENSAT |
| 10940 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 10941 | #endif |
| 10942 | #ifdef HAVE_MKFIFOAT |
| 10943 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10944 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10945 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10946 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10947 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10948 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10949 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10950 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10951 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10952 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10953 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10954 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10955 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10956 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10957 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10958 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10960 | }; |
| 10961 | |
| 10962 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10963 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10964 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10965 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10966 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10967 | } |
| 10968 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10969 | #if defined(PYOS_OS2) |
| 10970 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10971 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10972 | { |
| 10973 | APIRET rc; |
| 10974 | ULONG values[QSV_MAX+1]; |
| 10975 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10976 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10977 | |
| 10978 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10979 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10980 | Py_END_ALLOW_THREADS |
| 10981 | |
| 10982 | if (rc != NO_ERROR) { |
| 10983 | os2_error(rc); |
| 10984 | return -1; |
| 10985 | } |
| 10986 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10987 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10988 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10989 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10990 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10991 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10992 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10993 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10994 | |
| 10995 | switch (values[QSV_VERSION_MINOR]) { |
| 10996 | case 0: ver = "2.00"; break; |
| 10997 | case 10: ver = "2.10"; break; |
| 10998 | case 11: ver = "2.11"; break; |
| 10999 | case 30: ver = "3.00"; break; |
| 11000 | case 40: ver = "4.00"; break; |
| 11001 | case 50: ver = "5.00"; break; |
| 11002 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11003 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11005 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11006 | ver = &tmp[0]; |
| 11007 | } |
| 11008 | |
| 11009 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11010 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11011 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11012 | |
| 11013 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11014 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11015 | tmp[1] = ':'; |
| 11016 | tmp[2] = '\0'; |
| 11017 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11018 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11019 | } |
| 11020 | #endif |
| 11021 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11022 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11023 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11024 | enable_symlink() |
| 11025 | { |
| 11026 | HANDLE tok; |
| 11027 | TOKEN_PRIVILEGES tok_priv; |
| 11028 | LUID luid; |
| 11029 | int meth_idx = 0; |
| 11030 | |
| 11031 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11032 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11033 | |
| 11034 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11035 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11036 | |
| 11037 | tok_priv.PrivilegeCount = 1; |
| 11038 | tok_priv.Privileges[0].Luid = luid; |
| 11039 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11040 | |
| 11041 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11042 | sizeof(TOKEN_PRIVILEGES), |
| 11043 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11044 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11045 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11046 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11047 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11048 | } |
| 11049 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11050 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11051 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11052 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11053 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11054 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11056 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11057 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11059 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11060 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11062 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11063 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11065 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11069 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11071 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11072 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11074 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11075 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11077 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11078 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11080 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11081 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11095 | #endif |
| 11096 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11098 | #endif |
| 11099 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11119 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11120 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11125 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11126 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11128 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11129 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11134 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11135 | #ifdef PRIO_PROCESS |
| 11136 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11137 | #endif |
| 11138 | #ifdef PRIO_PGRP |
| 11139 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11140 | #endif |
| 11141 | #ifdef PRIO_USER |
| 11142 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11143 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11144 | #ifdef O_CLOEXEC |
| 11145 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11146 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11147 | /* posix - constants for *at functions */ |
| 11148 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11149 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11150 | #endif |
| 11151 | #ifdef AT_EACCESS |
| 11152 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11153 | #endif |
| 11154 | #ifdef AT_FDCWD |
| 11155 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11156 | #endif |
| 11157 | #ifdef AT_REMOVEDIR |
| 11158 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11159 | #endif |
| 11160 | #ifdef AT_SYMLINK_FOLLOW |
| 11161 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11162 | #endif |
| 11163 | #ifdef UTIME_NOW |
| 11164 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11165 | #endif |
| 11166 | #ifdef UTIME_OMIT |
| 11167 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11168 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11169 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11170 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11171 | /* MS Windows */ |
| 11172 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | /* Don't inherit in child processes. */ |
| 11174 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11175 | #endif |
| 11176 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | /* Optimize for short life (keep in memory). */ |
| 11178 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11179 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11180 | #endif |
| 11181 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | /* Automatically delete when last handle is closed. */ |
| 11183 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11184 | #endif |
| 11185 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11186 | /* Optimize for random access. */ |
| 11187 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | /* Optimize for sequential access. */ |
| 11191 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11194 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11195 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11196 | /* Send a SIGIO signal whenever input or output |
| 11197 | becomes available on file descriptor */ |
| 11198 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11199 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11200 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11201 | /* Direct disk access. */ |
| 11202 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11203 | #endif |
| 11204 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | /* Must be a directory. */ |
| 11206 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11207 | #endif |
| 11208 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | /* Do not follow links. */ |
| 11210 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11211 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11212 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11213 | /* Do not update the access time. */ |
| 11214 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11215 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11216 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11218 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11220 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11221 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11222 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11223 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11224 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11226 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11227 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11228 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11229 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11230 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11231 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11232 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11233 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11235 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11236 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11238 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11239 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11241 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11242 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11243 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11244 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11245 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11246 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11247 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11248 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11250 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11251 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11253 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11254 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11255 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11256 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11257 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11258 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11259 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11260 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11262 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11263 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11265 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11266 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11267 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11268 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11269 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11270 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11271 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11272 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11273 | #endif /* ST_RDONLY */ |
| 11274 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11275 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11276 | #endif /* ST_NOSUID */ |
| 11277 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11278 | /* FreeBSD sendfile() constants */ |
| 11279 | #ifdef SF_NODISKIO |
| 11280 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11281 | #endif |
| 11282 | #ifdef SF_MNOWAIT |
| 11283 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11284 | #endif |
| 11285 | #ifdef SF_SYNC |
| 11286 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11287 | #endif |
| 11288 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11289 | /* constants for posix_fadvise */ |
| 11290 | #ifdef POSIX_FADV_NORMAL |
| 11291 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11292 | #endif |
| 11293 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11294 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11295 | #endif |
| 11296 | #ifdef POSIX_FADV_RANDOM |
| 11297 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11298 | #endif |
| 11299 | #ifdef POSIX_FADV_NOREUSE |
| 11300 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11301 | #endif |
| 11302 | #ifdef POSIX_FADV_WILLNEED |
| 11303 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11304 | #endif |
| 11305 | #ifdef POSIX_FADV_DONTNEED |
| 11306 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11307 | #endif |
| 11308 | |
| 11309 | /* constants for waitid */ |
| 11310 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11311 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11312 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11313 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11314 | #endif |
| 11315 | #ifdef WEXITED |
| 11316 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11317 | #endif |
| 11318 | #ifdef WNOWAIT |
| 11319 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11320 | #endif |
| 11321 | #ifdef WSTOPPED |
| 11322 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11323 | #endif |
| 11324 | #ifdef CLD_EXITED |
| 11325 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11326 | #endif |
| 11327 | #ifdef CLD_DUMPED |
| 11328 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11329 | #endif |
| 11330 | #ifdef CLD_TRAPPED |
| 11331 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11332 | #endif |
| 11333 | #ifdef CLD_CONTINUED |
| 11334 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11335 | #endif |
| 11336 | |
| 11337 | /* constants for lockf */ |
| 11338 | #ifdef F_LOCK |
| 11339 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11340 | #endif |
| 11341 | #ifdef F_TLOCK |
| 11342 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11343 | #endif |
| 11344 | #ifdef F_ULOCK |
| 11345 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11346 | #endif |
| 11347 | #ifdef F_TEST |
| 11348 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11349 | #endif |
| 11350 | |
| 11351 | /* constants for futimens */ |
| 11352 | #ifdef UTIME_NOW |
| 11353 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11354 | #endif |
| 11355 | #ifdef UTIME_OMIT |
| 11356 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11357 | #endif |
| 11358 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11359 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11360 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11362 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11363 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11364 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11365 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11366 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11367 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11368 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11369 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11370 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11371 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11372 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11373 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11374 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11375 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11376 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11377 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11378 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11379 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11380 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11381 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11383 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11384 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11385 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11386 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11387 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11388 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11389 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11390 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11391 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11392 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11393 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11394 | #ifdef SCHED_SPORADIC |
| 11395 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11396 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11397 | #ifdef SCHED_BATCH |
| 11398 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11399 | #endif |
| 11400 | #ifdef SCHED_IDLE |
| 11401 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11402 | #endif |
| 11403 | #ifdef SCHED_RESET_ON_FORK |
| 11404 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11405 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11406 | #ifdef SCHED_SYS |
| 11407 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11408 | #endif |
| 11409 | #ifdef SCHED_IA |
| 11410 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11411 | #endif |
| 11412 | #ifdef SCHED_FSS |
| 11413 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11414 | #endif |
| 11415 | #ifdef SCHED_FX |
| 11416 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11417 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11418 | #endif |
| 11419 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11420 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11421 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11422 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11423 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11424 | #endif |
| 11425 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11426 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11428 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11429 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11430 | } |
| 11431 | |
| 11432 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11433 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11434 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11435 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11436 | |
| 11437 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11438 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11439 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11440 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11441 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11442 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11443 | #define MODNAME "posix" |
| 11444 | #endif |
| 11445 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11446 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11447 | PyModuleDef_HEAD_INIT, |
| 11448 | MODNAME, |
| 11449 | posix__doc__, |
| 11450 | -1, |
| 11451 | posix_methods, |
| 11452 | NULL, |
| 11453 | NULL, |
| 11454 | NULL, |
| 11455 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11456 | }; |
| 11457 | |
| 11458 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11459 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11460 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11462 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11463 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11464 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11465 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11466 | #endif |
| 11467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11468 | m = PyModule_Create(&posixmodule); |
| 11469 | if (m == NULL) |
| 11470 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11471 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | /* Initialize environ dictionary */ |
| 11473 | v = convertenviron(); |
| 11474 | Py_XINCREF(v); |
| 11475 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11476 | return NULL; |
| 11477 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | if (all_ins(m)) |
| 11480 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11481 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11482 | if (setup_confname_tables(m)) |
| 11483 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11485 | Py_INCREF(PyExc_OSError); |
| 11486 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11487 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11488 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11489 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11490 | return NULL; |
| 11491 | Py_INCREF(&cpu_set_type); |
| 11492 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11493 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11494 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11495 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | if (posix_putenv_garbage == NULL) |
| 11497 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11498 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11499 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11501 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11502 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11503 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11504 | #endif |
| 11505 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11507 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11508 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11509 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11510 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11511 | structseq_new = StatResultType.tp_new; |
| 11512 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11513 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11515 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11516 | #ifdef NEED_TICKS_PER_SECOND |
| 11517 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11519 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11520 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11521 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11522 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11523 | # endif |
| 11524 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11525 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11526 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11527 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11528 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11529 | SchedParamType.tp_new = sched_param_new; |
| 11530 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11531 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11532 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11533 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11534 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11535 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | Py_INCREF((PyObject*) &StatResultType); |
| 11537 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11538 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11539 | PyModule_AddObject(m, "statvfs_result", |
| 11540 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11541 | |
| 11542 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11543 | Py_INCREF(&SchedParamType); |
| 11544 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11545 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11546 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11547 | |
| 11548 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11549 | /* |
| 11550 | * Step 2 of weak-linking support on Mac OS X. |
| 11551 | * |
| 11552 | * The code below removes functions that are not available on the |
| 11553 | * currently active platform. |
| 11554 | * |
| 11555 | * This block allow one to use a python binary that was build on |
| 11556 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11557 | * OSX 10.4. |
| 11558 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11559 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | if (fstatvfs == NULL) { |
| 11561 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11562 | return NULL; |
| 11563 | } |
| 11564 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11565 | #endif /* HAVE_FSTATVFS */ |
| 11566 | |
| 11567 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11568 | if (statvfs == NULL) { |
| 11569 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11570 | return NULL; |
| 11571 | } |
| 11572 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11573 | #endif /* HAVE_STATVFS */ |
| 11574 | |
| 11575 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11576 | if (lchown == NULL) { |
| 11577 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11578 | return NULL; |
| 11579 | } |
| 11580 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11581 | #endif /* HAVE_LCHOWN */ |
| 11582 | |
| 11583 | |
| 11584 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11585 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11586 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11587 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11588 | |
| 11589 | #ifdef __cplusplus |
| 11590 | } |
| 11591 | #endif |