Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 31 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #endif /* defined(__VMS) */ |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 47 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #define INCL_DOS |
| 49 | #define INCL_DOSERRORS |
| 50 | #define INCL_DOSPROCESS |
| 51 | #define INCL_NOPMAPI |
| 52 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 53 | #if defined(PYCC_GCC) |
| 54 | #include <ctype.h> |
| 55 | #include <io.h> |
| 56 | #include <stdio.h> |
| 57 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 59 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 62 | #ifdef HAVE_SYS_UIO_H |
| 63 | #include <sys/uio.h> |
| 64 | #endif |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_TYPES_H */ |
| 69 | |
| 70 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 75 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #ifdef HAVE_FCNTL_H |
| 83 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 84 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #endif |
| 89 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYSEXITS_H |
| 91 | #include <sysexits.h> |
| 92 | #endif /* HAVE_SYSEXITS_H */ |
| 93 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_LOADAVG_H |
| 95 | #include <sys/loadavg.h> |
| 96 | #endif |
| 97 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 98 | #ifdef HAVE_LANGINFO_H |
| 99 | #include <langinfo.h> |
| 100 | #endif |
| 101 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 102 | #ifdef HAVE_SYS_SENDFILE_H |
| 103 | #include <sys/sendfile.h> |
| 104 | #endif |
| 105 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 106 | #ifdef HAVE_SCHED_H |
| 107 | #include <sched.h> |
| 108 | #endif |
| 109 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 110 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 111 | #define USE_XATTRS |
| 112 | #endif |
| 113 | |
| 114 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 115 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 116 | #endif |
| 117 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 118 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 119 | #ifdef HAVE_SYS_SOCKET_H |
| 120 | #include <sys/socket.h> |
| 121 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 124 | #ifdef HAVE_DLFCN_H |
| 125 | #include <dlfcn.h> |
| 126 | #endif |
| 127 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 128 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 129 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 130 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 131 | #include <process.h> |
| 132 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 133 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 134 | #define HAVE_GETCWD 1 |
| 135 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 136 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #if defined(__OS2__) |
| 138 | #define HAVE_EXECV 1 |
| 139 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 140 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 141 | #include <process.h> |
| 142 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 143 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 144 | #define HAVE_EXECV 1 |
| 145 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 146 | #define HAVE_OPENDIR 1 |
| 147 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 148 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 149 | #define HAVE_WAIT 1 |
| 150 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 152 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 153 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 154 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 155 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 156 | #define HAVE_EXECV 1 |
| 157 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 158 | #define HAVE_SYSTEM 1 |
| 159 | #define HAVE_CWAIT 1 |
| 160 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 161 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 162 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 163 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 164 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 166 | /* Unix functions that the configure script doesn't check for */ |
| 167 | #define HAVE_EXECV 1 |
| 168 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 169 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 170 | #define HAVE_FORK1 1 |
| 171 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 172 | #define HAVE_GETCWD 1 |
| 173 | #define HAVE_GETEGID 1 |
| 174 | #define HAVE_GETEUID 1 |
| 175 | #define HAVE_GETGID 1 |
| 176 | #define HAVE_GETPPID 1 |
| 177 | #define HAVE_GETUID 1 |
| 178 | #define HAVE_KILL 1 |
| 179 | #define HAVE_OPENDIR 1 |
| 180 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 181 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 183 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 184 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 185 | #endif /* _MSC_VER */ |
| 186 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 187 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 188 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 190 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 191 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 192 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 193 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 194 | (default) */ |
| 195 | extern char *ctermid_r(char *); |
| 196 | #endif |
| 197 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 198 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 200 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 201 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 202 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 204 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 205 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 206 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #endif |
| 208 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 209 | extern int chdir(char *); |
| 210 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int chdir(const char *); |
| 213 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 214 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 215 | #ifdef __BORLANDC__ |
| 216 | extern int chmod(const char *, int); |
| 217 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 218 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 219 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 220 | /*#ifdef HAVE_FCHMOD |
| 221 | extern int fchmod(int, mode_t); |
| 222 | #endif*/ |
| 223 | /*#ifdef HAVE_LCHMOD |
| 224 | extern int lchmod(const char *, mode_t); |
| 225 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 226 | extern int chown(const char *, uid_t, gid_t); |
| 227 | extern char *getcwd(char *, int); |
| 228 | extern char *strerror(int); |
| 229 | extern int link(const char *, const char *); |
| 230 | extern int rename(const char *, const char *); |
| 231 | extern int stat(const char *, struct stat *); |
| 232 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 234 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 235 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 237 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 238 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 241 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | #ifdef HAVE_UTIME_H |
| 244 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 245 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 247 | #ifdef HAVE_SYS_UTIME_H |
| 248 | #include <sys/utime.h> |
| 249 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 250 | #endif /* HAVE_SYS_UTIME_H */ |
| 251 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #ifdef HAVE_SYS_TIMES_H |
| 253 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
| 256 | #ifdef HAVE_SYS_PARAM_H |
| 257 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 258 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
| 260 | #ifdef HAVE_SYS_UTSNAME_H |
| 261 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 262 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 264 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 266 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 267 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 268 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 269 | #include <direct.h> |
| 270 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 271 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 274 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 276 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 277 | #endif |
| 278 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 286 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 289 | #endif |
| 290 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 296 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 297 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 300 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 301 | #endif |
| 302 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 303 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 304 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 305 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 306 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 307 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 308 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 309 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 310 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 311 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 312 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 313 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 314 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 316 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 317 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 318 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 322 | #define MAXPATHLEN PATH_MAX |
| 323 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 326 | #endif /* MAXPATHLEN */ |
| 327 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 328 | #ifdef UNION_WAIT |
| 329 | /* Emulate some macros on systems that have a union instead of macros */ |
| 330 | |
| 331 | #ifndef WIFEXITED |
| 332 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WEXITSTATUS |
| 336 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WTERMSIG |
| 340 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 341 | #endif |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | #define WAIT_TYPE union wait |
| 344 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 345 | |
| 346 | #else /* !UNION_WAIT */ |
| 347 | #define WAIT_TYPE int |
| 348 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 349 | #endif /* UNION_WAIT */ |
| 350 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 351 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 352 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 353 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 354 | #define USE_CTERMID_R |
| 355 | #endif |
| 356 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 357 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 358 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 359 | #undef FSTAT |
| 360 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 361 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT win32_stat |
| 363 | # define FSTAT win32_fstat |
| 364 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT stat |
| 367 | # define FSTAT fstat |
| 368 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 369 | #endif |
| 370 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 371 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #include <sys/mkdev.h> |
| 373 | #else |
| 374 | #if defined(MAJOR_IN_SYSMACROS) |
| 375 | #include <sys/sysmacros.h> |
| 376 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 377 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 378 | #include <sys/mkdev.h> |
| 379 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 380 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 381 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 382 | /* A helper used by a number of POSIX-only functions */ |
| 383 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 384 | static int |
| 385 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 386 | { |
| 387 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 388 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 389 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 390 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 391 | #endif |
| 392 | if (PyErr_Occurred()) |
| 393 | return 0; |
| 394 | return 1; |
| 395 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 396 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 397 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 398 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 399 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 400 | * valid and throw an assertion if it isn't. |
| 401 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 402 | * an assertion can be useful, but it does contradict the POSIX standard |
| 403 | * which for write(2) states: |
| 404 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 405 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 406 | * writing." |
| 407 | * Furthermore, python allows the user to enter any old integer |
| 408 | * as a fd and should merely raise a python exception on error. |
| 409 | * The Microsoft CRT doesn't provide an official way to check for the |
| 410 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 411 | * by using the exported __pinfo data member and knowledge of the |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 412 | * internal structures involved. |
| 413 | * The structures below must be updated for each version of visual studio |
| 414 | * according to the file internal.h in the CRT source, until MS comes |
| 415 | * up with a less hacky way to do this. |
| 416 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 417 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 418 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 419 | /* The actual size of the structure is determined at runtime. |
| 420 | * Only the first items must be present. |
| 421 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 422 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 423 | intptr_t osfhnd; |
| 424 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 425 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 426 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 427 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 428 | #define IOINFO_L2E 5 |
| 429 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 430 | #define IOINFO_ARRAYS 64 |
| 431 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 432 | #define FOPEN 0x01 |
| 433 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 434 | |
| 435 | /* This function emulates what the windows CRT does to validate file handles */ |
| 436 | int |
| 437 | _PyVerify_fd(int fd) |
| 438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 439 | const int i1 = fd >> IOINFO_L2E; |
| 440 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 442 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 444 | /* Determine the actual size of the ioinfo structure, |
| 445 | * as used by the CRT loaded in memory |
| 446 | */ |
| 447 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 448 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 449 | } |
| 450 | if (sizeof_ioinfo == 0) { |
| 451 | /* This should not happen... */ |
| 452 | goto fail; |
| 453 | } |
| 454 | |
| 455 | /* See that it isn't a special CLEAR fileno */ |
| 456 | if (fd != _NO_CONSOLE_FILENO) { |
| 457 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 458 | * we check pointer validity and other info |
| 459 | */ |
| 460 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 461 | /* finally, check that the file is open */ |
| 462 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 463 | if (info->osfile & FOPEN) { |
| 464 | return 1; |
| 465 | } |
| 466 | } |
| 467 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 468 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 469 | errno = EBADF; |
| 470 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 474 | static int |
| 475 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 476 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 477 | if (!_PyVerify_fd(fd1)) |
| 478 | return 0; |
| 479 | if (fd2 == _NO_CONSOLE_FILENO) |
| 480 | return 0; |
| 481 | if ((unsigned)fd2 < _NHANDLE_) |
| 482 | return 1; |
| 483 | else |
| 484 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 485 | } |
| 486 | #else |
| 487 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 488 | #define _PyVerify_fd_dup2(A, B) (1) |
| 489 | #endif |
| 490 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 491 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 492 | /* The following structure was copied from |
| 493 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 494 | include doesn't seem to be present in the Windows SDK (at least as included |
| 495 | with Visual Studio Express). */ |
| 496 | typedef struct _REPARSE_DATA_BUFFER { |
| 497 | ULONG ReparseTag; |
| 498 | USHORT ReparseDataLength; |
| 499 | USHORT Reserved; |
| 500 | union { |
| 501 | struct { |
| 502 | USHORT SubstituteNameOffset; |
| 503 | USHORT SubstituteNameLength; |
| 504 | USHORT PrintNameOffset; |
| 505 | USHORT PrintNameLength; |
| 506 | ULONG Flags; |
| 507 | WCHAR PathBuffer[1]; |
| 508 | } SymbolicLinkReparseBuffer; |
| 509 | |
| 510 | struct { |
| 511 | USHORT SubstituteNameOffset; |
| 512 | USHORT SubstituteNameLength; |
| 513 | USHORT PrintNameOffset; |
| 514 | USHORT PrintNameLength; |
| 515 | WCHAR PathBuffer[1]; |
| 516 | } MountPointReparseBuffer; |
| 517 | |
| 518 | struct { |
| 519 | UCHAR DataBuffer[1]; |
| 520 | } GenericReparseBuffer; |
| 521 | }; |
| 522 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 523 | |
| 524 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 525 | GenericReparseBuffer) |
| 526 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 527 | |
| 528 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 529 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 530 | { |
| 531 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 532 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 533 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 534 | |
| 535 | if (0 == DeviceIoControl( |
| 536 | reparse_point_handle, |
| 537 | FSCTL_GET_REPARSE_POINT, |
| 538 | NULL, 0, /* in buffer */ |
| 539 | target_buffer, sizeof(target_buffer), |
| 540 | &n_bytes_returned, |
| 541 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 542 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 543 | |
| 544 | if (reparse_tag) |
| 545 | *reparse_tag = rdb->ReparseTag; |
| 546 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 547 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 548 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 549 | |
| 550 | static int |
| 551 | win32_warn_bytes_api() |
| 552 | { |
| 553 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 554 | "The Windows bytes API has been deprecated, " |
| 555 | "use Unicode filenames instead", |
| 556 | 1); |
| 557 | } |
| 558 | |
| 559 | static PyObject* |
| 560 | win32_decode_filename(PyObject *obj) |
| 561 | { |
| 562 | PyObject *unicode; |
| 563 | if (PyUnicode_Check(obj)) { |
| 564 | if (PyUnicode_READY(obj)) |
| 565 | return NULL; |
| 566 | Py_INCREF(obj); |
| 567 | return obj; |
| 568 | } |
| 569 | if (!PyUnicode_FSDecoder(obj, &unicode)) |
| 570 | return NULL; |
| 571 | if (win32_warn_bytes_api()) { |
| 572 | Py_DECREF(unicode); |
| 573 | return NULL; |
| 574 | } |
| 575 | return unicode; |
| 576 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 577 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 579 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 580 | #ifdef WITH_NEXT_FRAMEWORK |
| 581 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 582 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 583 | */ |
| 584 | #include <crt_externs.h> |
| 585 | static char **environ; |
| 586 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 587 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 588 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 589 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 590 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 591 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 593 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 594 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 595 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 596 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 597 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 598 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 599 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 600 | APIRET rc; |
| 601 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 602 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 604 | d = PyDict_New(); |
| 605 | if (d == NULL) |
| 606 | return NULL; |
| 607 | #ifdef WITH_NEXT_FRAMEWORK |
| 608 | if (environ == NULL) |
| 609 | environ = *_NSGetEnviron(); |
| 610 | #endif |
| 611 | #ifdef MS_WINDOWS |
| 612 | /* _wenviron must be initialized in this way if the program is started |
| 613 | through main() instead of wmain(). */ |
| 614 | _wgetenv(L""); |
| 615 | if (_wenviron == NULL) |
| 616 | return d; |
| 617 | /* This part ignores errors */ |
| 618 | for (e = _wenviron; *e != NULL; e++) { |
| 619 | PyObject *k; |
| 620 | PyObject *v; |
| 621 | wchar_t *p = wcschr(*e, L'='); |
| 622 | if (p == NULL) |
| 623 | continue; |
| 624 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 625 | if (k == NULL) { |
| 626 | PyErr_Clear(); |
| 627 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 628 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 629 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 630 | if (v == NULL) { |
| 631 | PyErr_Clear(); |
| 632 | Py_DECREF(k); |
| 633 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 634 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 635 | if (PyDict_GetItem(d, k) == NULL) { |
| 636 | if (PyDict_SetItem(d, k, v) != 0) |
| 637 | PyErr_Clear(); |
| 638 | } |
| 639 | Py_DECREF(k); |
| 640 | Py_DECREF(v); |
| 641 | } |
| 642 | #else |
| 643 | if (environ == NULL) |
| 644 | return d; |
| 645 | /* This part ignores errors */ |
| 646 | for (e = environ; *e != NULL; e++) { |
| 647 | PyObject *k; |
| 648 | PyObject *v; |
| 649 | char *p = strchr(*e, '='); |
| 650 | if (p == NULL) |
| 651 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 652 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 653 | if (k == NULL) { |
| 654 | PyErr_Clear(); |
| 655 | continue; |
| 656 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 657 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 658 | if (v == NULL) { |
| 659 | PyErr_Clear(); |
| 660 | Py_DECREF(k); |
| 661 | continue; |
| 662 | } |
| 663 | if (PyDict_GetItem(d, k) == NULL) { |
| 664 | if (PyDict_SetItem(d, k, v) != 0) |
| 665 | PyErr_Clear(); |
| 666 | } |
| 667 | Py_DECREF(k); |
| 668 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 669 | } |
| 670 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 671 | #if defined(PYOS_OS2) |
| 672 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 673 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 674 | PyObject *v = PyBytes_FromString(buffer); |
| 675 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 676 | Py_DECREF(v); |
| 677 | } |
| 678 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 679 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 680 | PyObject *v = PyBytes_FromString(buffer); |
| 681 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 682 | Py_DECREF(v); |
| 683 | } |
| 684 | #endif |
| 685 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 688 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 689 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 690 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 691 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 692 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 693 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 694 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 695 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 696 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 698 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 701 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 702 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 703 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 704 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 705 | PyObject *name_str, *rc; |
| 706 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 707 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 708 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 709 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 710 | name_str); |
| 711 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 712 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 715 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 716 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 717 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 718 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 719 | /* XXX We should pass the function name along in the future. |
| 720 | (winreg.c also wants to pass the function name.) |
| 721 | This would however require an additional param to the |
| 722 | Windows error object, which is non-trivial. |
| 723 | */ |
| 724 | errno = GetLastError(); |
| 725 | if (filename) |
| 726 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 727 | else |
| 728 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 729 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 730 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 731 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 732 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 733 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 734 | /* XXX - see win32_error for comments on 'function' */ |
| 735 | errno = GetLastError(); |
| 736 | if (filename) |
| 737 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 738 | else |
| 739 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 742 | static PyObject * |
| 743 | win32_error_object(char* function, PyObject* filename) |
| 744 | { |
| 745 | /* XXX - see win32_error for comments on 'function' */ |
| 746 | errno = GetLastError(); |
| 747 | if (filename) |
| 748 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 749 | PyExc_WindowsError, |
| 750 | errno, |
| 751 | filename); |
| 752 | else |
| 753 | return PyErr_SetFromWindowsErr(errno); |
| 754 | } |
| 755 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 756 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 757 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 758 | #if defined(PYOS_OS2) |
| 759 | /********************************************************************** |
| 760 | * Helper Function to Trim and Format OS/2 Messages |
| 761 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 762 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 763 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 764 | { |
| 765 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 766 | |
| 767 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 768 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 769 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 770 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 771 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 772 | } |
| 773 | |
| 774 | /* Add Optional Reason Text */ |
| 775 | if (reason) { |
| 776 | strcat(msgbuf, " : "); |
| 777 | strcat(msgbuf, reason); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /********************************************************************** |
| 782 | * Decode an OS/2 Operating System Error Code |
| 783 | * |
| 784 | * A convenience function to lookup an OS/2 error code and return a |
| 785 | * text message we can use to raise a Python exception. |
| 786 | * |
| 787 | * Notes: |
| 788 | * The messages for errors returned from the OS/2 kernel reside in |
| 789 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 790 | * |
| 791 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 792 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 793 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 794 | { |
| 795 | APIRET rc; |
| 796 | ULONG msglen; |
| 797 | |
| 798 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 799 | Py_BEGIN_ALLOW_THREADS |
| 800 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 801 | errorcode, "oso001.msg", &msglen); |
| 802 | Py_END_ALLOW_THREADS |
| 803 | |
| 804 | if (rc == NO_ERROR) |
| 805 | os2_formatmsg(msgbuf, msglen, reason); |
| 806 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 807 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 808 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 809 | |
| 810 | return msgbuf; |
| 811 | } |
| 812 | |
| 813 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 814 | errors are not in a global variable e.g. 'errno' nor are |
| 815 | they congruent with posix error numbers. */ |
| 816 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 817 | static PyObject * |
| 818 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 819 | { |
| 820 | char text[1024]; |
| 821 | PyObject *v; |
| 822 | |
| 823 | os2_strerror(text, sizeof(text), code, ""); |
| 824 | |
| 825 | v = Py_BuildValue("(is)", code, text); |
| 826 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 827 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 828 | Py_DECREF(v); |
| 829 | } |
| 830 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 831 | } |
| 832 | |
| 833 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | |
| 835 | /* POSIX generic methods */ |
| 836 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 837 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 838 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 839 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 840 | int fd; |
| 841 | int res; |
| 842 | fd = PyObject_AsFileDescriptor(fdobj); |
| 843 | if (fd < 0) |
| 844 | return NULL; |
| 845 | if (!_PyVerify_fd(fd)) |
| 846 | return posix_error(); |
| 847 | Py_BEGIN_ALLOW_THREADS |
| 848 | res = (*func)(fd); |
| 849 | Py_END_ALLOW_THREADS |
| 850 | if (res < 0) |
| 851 | return posix_error(); |
| 852 | Py_INCREF(Py_None); |
| 853 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 854 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 855 | |
| 856 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 857 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 858 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 859 | PyObject *opath1 = NULL; |
| 860 | char *path1; |
| 861 | int res; |
| 862 | if (!PyArg_ParseTuple(args, format, |
| 863 | PyUnicode_FSConverter, &opath1)) |
| 864 | return NULL; |
| 865 | path1 = PyBytes_AsString(opath1); |
| 866 | Py_BEGIN_ALLOW_THREADS |
| 867 | res = (*func)(path1); |
| 868 | Py_END_ALLOW_THREADS |
| 869 | if (res < 0) |
| 870 | return posix_error_with_allocated_filename(opath1); |
| 871 | Py_DECREF(opath1); |
| 872 | Py_INCREF(Py_None); |
| 873 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 876 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 877 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 878 | char *format, |
| 879 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 880 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 881 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 882 | char *path1, *path2; |
| 883 | int res; |
| 884 | if (!PyArg_ParseTuple(args, format, |
| 885 | PyUnicode_FSConverter, &opath1, |
| 886 | PyUnicode_FSConverter, &opath2)) { |
| 887 | return NULL; |
| 888 | } |
| 889 | path1 = PyBytes_AsString(opath1); |
| 890 | path2 = PyBytes_AsString(opath2); |
| 891 | Py_BEGIN_ALLOW_THREADS |
| 892 | res = (*func)(path1, path2); |
| 893 | Py_END_ALLOW_THREADS |
| 894 | Py_DECREF(opath1); |
| 895 | Py_DECREF(opath2); |
| 896 | if (res != 0) |
| 897 | /* XXX how to report both path1 and path2??? */ |
| 898 | return posix_error(); |
| 899 | Py_INCREF(Py_None); |
| 900 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 903 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 904 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 905 | win32_1str(PyObject* args, char* func, |
| 906 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 907 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 908 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 909 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 910 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 911 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 912 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 913 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 914 | { |
| 915 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 916 | if (wstr == NULL) |
| 917 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 918 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 919 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 920 | Py_END_ALLOW_THREADS |
| 921 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 922 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 923 | Py_INCREF(Py_None); |
| 924 | return Py_None; |
| 925 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 926 | PyErr_Clear(); |
| 927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 928 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 929 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 930 | if (win32_warn_bytes_api()) |
| 931 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 932 | Py_BEGIN_ALLOW_THREADS |
| 933 | result = funcA(ansi); |
| 934 | Py_END_ALLOW_THREADS |
| 935 | if (!result) |
| 936 | return win32_error(func, ansi); |
| 937 | Py_INCREF(Py_None); |
| 938 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | |
| 940 | } |
| 941 | |
| 942 | /* This is a reimplementation of the C library's chdir function, |
| 943 | but one that produces Win32 errors instead of DOS error codes. |
| 944 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 945 | it also needs to set "magic" environment variables indicating |
| 946 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 947 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 948 | win32_chdir(LPCSTR path) |
| 949 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 950 | char new_path[MAX_PATH+1]; |
| 951 | int result; |
| 952 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 954 | if(!SetCurrentDirectoryA(path)) |
| 955 | return FALSE; |
| 956 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 957 | if (!result) |
| 958 | return FALSE; |
| 959 | /* In the ANSI API, there should not be any paths longer |
| 960 | than MAX_PATH. */ |
| 961 | assert(result <= MAX_PATH+1); |
| 962 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 963 | strncmp(new_path, "//", 2) == 0) |
| 964 | /* UNC path, nothing to do. */ |
| 965 | return TRUE; |
| 966 | env[1] = new_path[0]; |
| 967 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* The Unicode version differs from the ANSI version |
| 971 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 972 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 973 | win32_wchdir(LPCWSTR path) |
| 974 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 975 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 976 | int result; |
| 977 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 978 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 979 | if(!SetCurrentDirectoryW(path)) |
| 980 | return FALSE; |
| 981 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 982 | if (!result) |
| 983 | return FALSE; |
| 984 | if (result > MAX_PATH+1) { |
| 985 | new_path = malloc(result * sizeof(wchar_t)); |
| 986 | if (!new_path) { |
| 987 | SetLastError(ERROR_OUTOFMEMORY); |
| 988 | return FALSE; |
| 989 | } |
| 990 | result = GetCurrentDirectoryW(result, new_path); |
| 991 | if (!result) { |
| 992 | free(new_path); |
| 993 | return FALSE; |
| 994 | } |
| 995 | } |
| 996 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 997 | wcsncmp(new_path, L"//", 2) == 0) |
| 998 | /* UNC path, nothing to do. */ |
| 999 | return TRUE; |
| 1000 | env[1] = new_path[0]; |
| 1001 | result = SetEnvironmentVariableW(env, new_path); |
| 1002 | if (new_path != _new_path) |
| 1003 | free(new_path); |
| 1004 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1005 | } |
| 1006 | #endif |
| 1007 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1008 | #ifdef MS_WINDOWS |
| 1009 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1010 | - time stamps are restricted to second resolution |
| 1011 | - file modification times suffer from forth-and-back conversions between |
| 1012 | UTC and local time |
| 1013 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1014 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1015 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1016 | |
| 1017 | struct win32_stat{ |
| 1018 | int st_dev; |
| 1019 | __int64 st_ino; |
| 1020 | unsigned short st_mode; |
| 1021 | int st_nlink; |
| 1022 | int st_uid; |
| 1023 | int st_gid; |
| 1024 | int st_rdev; |
| 1025 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1026 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1027 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1028 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1029 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1030 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1031 | int st_ctime_nsec; |
| 1032 | }; |
| 1033 | |
| 1034 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1035 | |
| 1036 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1037 | 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] | 1038 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1039 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1040 | /* Cannot simply cast and dereference in_ptr, |
| 1041 | since it might not be aligned properly */ |
| 1042 | __int64 in; |
| 1043 | memcpy(&in, in_ptr, sizeof(in)); |
| 1044 | *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] | 1045 | *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] | 1046 | } |
| 1047 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1048 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1049 | 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] | 1050 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1051 | /* XXX endianness */ |
| 1052 | __int64 out; |
| 1053 | out = time_in + secs_between_epochs; |
| 1054 | out = out * 10000000 + nsec_in / 100; |
| 1055 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1058 | /* Below, we *know* that ugo+r is 0444 */ |
| 1059 | #if _S_IREAD != 0400 |
| 1060 | #error Unsupported C library |
| 1061 | #endif |
| 1062 | static int |
| 1063 | attributes_to_mode(DWORD attr) |
| 1064 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1065 | int m = 0; |
| 1066 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1067 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1068 | else |
| 1069 | m |= _S_IFREG; |
| 1070 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1071 | m |= 0444; |
| 1072 | else |
| 1073 | m |= 0666; |
| 1074 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1078 | 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] | 1079 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1080 | memset(result, 0, sizeof(*result)); |
| 1081 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1082 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1083 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1084 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1085 | 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] | 1086 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1087 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1088 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1089 | /* first clear the S_IFMT bits */ |
| 1090 | result->st_mode ^= (result->st_mode & 0170000); |
| 1091 | /* now set the bits that make this a symlink */ |
| 1092 | result->st_mode |= 0120000; |
| 1093 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1095 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1099 | 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] | 1100 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1101 | HANDLE hFindFile; |
| 1102 | WIN32_FIND_DATAA FileData; |
| 1103 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1104 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1105 | return FALSE; |
| 1106 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1107 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1108 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1109 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1110 | info->ftCreationTime = FileData.ftCreationTime; |
| 1111 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1112 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1113 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1114 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1115 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1116 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1117 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1118 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1122 | 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] | 1123 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1124 | HANDLE hFindFile; |
| 1125 | WIN32_FIND_DATAW FileData; |
| 1126 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1127 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1128 | return FALSE; |
| 1129 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1130 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1131 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1132 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1133 | info->ftCreationTime = FileData.ftCreationTime; |
| 1134 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1135 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1136 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1137 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1138 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1139 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1140 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1141 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1144 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1145 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1146 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1147 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1148 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1149 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1150 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1151 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1152 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1153 | DWORD); |
| 1154 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1155 | /* only recheck */ |
| 1156 | if (!has_GetFinalPathNameByHandle) |
| 1157 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1158 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1159 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1160 | "GetFinalPathNameByHandleA"); |
| 1161 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1162 | "GetFinalPathNameByHandleW"); |
| 1163 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1164 | Py_GetFinalPathNameByHandleW; |
| 1165 | } |
| 1166 | return has_GetFinalPathNameByHandle; |
| 1167 | } |
| 1168 | |
| 1169 | static BOOL |
| 1170 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1171 | { |
| 1172 | int buf_size, result_length; |
| 1173 | wchar_t *buf; |
| 1174 | |
| 1175 | /* We have a good handle to the target, use it to determine |
| 1176 | the target path name (then we'll call lstat on it). */ |
| 1177 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1178 | VOLUME_NAME_DOS); |
| 1179 | if(!buf_size) |
| 1180 | return FALSE; |
| 1181 | |
| 1182 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1183 | if (!buf) { |
| 1184 | SetLastError(ERROR_OUTOFMEMORY); |
| 1185 | return FALSE; |
| 1186 | } |
| 1187 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1188 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1189 | buf, buf_size, VOLUME_NAME_DOS); |
| 1190 | |
| 1191 | if(!result_length) { |
| 1192 | free(buf); |
| 1193 | return FALSE; |
| 1194 | } |
| 1195 | |
| 1196 | if(!CloseHandle(hdl)) { |
| 1197 | free(buf); |
| 1198 | return FALSE; |
| 1199 | } |
| 1200 | |
| 1201 | buf[result_length] = 0; |
| 1202 | |
| 1203 | *target_path = buf; |
| 1204 | return TRUE; |
| 1205 | } |
| 1206 | |
| 1207 | static int |
| 1208 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1209 | BOOL traverse); |
| 1210 | static int |
| 1211 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1212 | BOOL traverse) |
| 1213 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1214 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1215 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1216 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1217 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1218 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1219 | const char *dot; |
| 1220 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1221 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1222 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1223 | traverse reparse point. */ |
| 1224 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1227 | hFile = CreateFileA( |
| 1228 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1229 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1230 | 0, /* share mode */ |
| 1231 | NULL, /* security attributes */ |
| 1232 | OPEN_EXISTING, |
| 1233 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1234 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1235 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1236 | the symlink path agin and not the actual final path. */ |
| 1237 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1238 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1239 | NULL); |
| 1240 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1241 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1242 | /* Either the target doesn't exist, or we don't have access to |
| 1243 | get a handle to it. If the former, we need to return an error. |
| 1244 | If the latter, we can use attributes_from_dir. */ |
| 1245 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1246 | return -1; |
| 1247 | /* Could not get attributes on open file. Fall back to |
| 1248 | reading the directory. */ |
| 1249 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1250 | /* Very strange. This should not fail now */ |
| 1251 | return -1; |
| 1252 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1253 | if (traverse) { |
| 1254 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1255 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1256 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1257 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1258 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1259 | } else { |
| 1260 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1261 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1262 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1263 | } |
| 1264 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1265 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1266 | return -1; |
| 1267 | |
| 1268 | /* Close the outer open file handle now that we're about to |
| 1269 | reopen it with different flags. */ |
| 1270 | if (!CloseHandle(hFile)) |
| 1271 | return -1; |
| 1272 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1273 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1274 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1275 | the file without the reparse handling flag set. */ |
| 1276 | hFile2 = CreateFileA( |
| 1277 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1278 | NULL, OPEN_EXISTING, |
| 1279 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1280 | NULL); |
| 1281 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1282 | return -1; |
| 1283 | |
| 1284 | if (!get_target_path(hFile2, &target_path)) |
| 1285 | return -1; |
| 1286 | |
| 1287 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1288 | free(target_path); |
| 1289 | return code; |
| 1290 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1291 | } else |
| 1292 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1293 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1294 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1297 | dot = strrchr(path, '.'); |
| 1298 | if (dot) { |
| 1299 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1300 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1301 | result->st_mode |= 0111; |
| 1302 | } |
| 1303 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1307 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1308 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1309 | { |
| 1310 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1311 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1312 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1313 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1314 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1315 | const wchar_t *dot; |
| 1316 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1317 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1318 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1319 | traverse reparse point. */ |
| 1320 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1323 | hFile = CreateFileW( |
| 1324 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1325 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1326 | 0, /* share mode */ |
| 1327 | NULL, /* security attributes */ |
| 1328 | OPEN_EXISTING, |
| 1329 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1330 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1331 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1332 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1333 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1334 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1335 | NULL); |
| 1336 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1337 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1338 | /* Either the target doesn't exist, or we don't have access to |
| 1339 | get a handle to it. If the former, we need to return an error. |
| 1340 | If the latter, we can use attributes_from_dir. */ |
| 1341 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1342 | return -1; |
| 1343 | /* Could not get attributes on open file. Fall back to |
| 1344 | reading the directory. */ |
| 1345 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1346 | /* Very strange. This should not fail now */ |
| 1347 | return -1; |
| 1348 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1349 | if (traverse) { |
| 1350 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1351 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1352 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1353 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1354 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1355 | } else { |
| 1356 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1357 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1358 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1359 | } |
| 1360 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1361 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1362 | return -1; |
| 1363 | |
| 1364 | /* Close the outer open file handle now that we're about to |
| 1365 | reopen it with different flags. */ |
| 1366 | if (!CloseHandle(hFile)) |
| 1367 | return -1; |
| 1368 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1369 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1370 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1371 | the file without the reparse handling flag set. */ |
| 1372 | hFile2 = CreateFileW( |
| 1373 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1374 | NULL, OPEN_EXISTING, |
| 1375 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1376 | NULL); |
| 1377 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1378 | return -1; |
| 1379 | |
| 1380 | if (!get_target_path(hFile2, &target_path)) |
| 1381 | return -1; |
| 1382 | |
| 1383 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1384 | free(target_path); |
| 1385 | return code; |
| 1386 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1387 | } else |
| 1388 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1389 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1390 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1391 | |
| 1392 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1393 | dot = wcsrchr(path, '.'); |
| 1394 | if (dot) { |
| 1395 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1396 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1397 | result->st_mode |= 0111; |
| 1398 | } |
| 1399 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1403 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1404 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1405 | /* Protocol violation: we explicitly clear errno, instead of |
| 1406 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1407 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1408 | errno = 0; |
| 1409 | return code; |
| 1410 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1411 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1412 | static int |
| 1413 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1414 | { |
| 1415 | /* Protocol violation: we explicitly clear errno, instead of |
| 1416 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1417 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1418 | errno = 0; |
| 1419 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1420 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1421 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1422 | |
| 1423 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1424 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1425 | default does not traverse symlinks and instead returns attributes for |
| 1426 | the symlink. |
| 1427 | |
| 1428 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1429 | win32_stat will first explicitly resolve the symlink target and then will |
| 1430 | call win32_lstat on that result. |
| 1431 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1432 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1433 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1434 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1435 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1436 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1437 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1440 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1441 | 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] | 1442 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1443 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | static int |
| 1447 | win32_stat(const char* path, struct win32_stat *result) |
| 1448 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1449 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1452 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1453 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1454 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1455 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static int |
| 1459 | win32_fstat(int file_number, struct win32_stat *result) |
| 1460 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1461 | BY_HANDLE_FILE_INFORMATION info; |
| 1462 | HANDLE h; |
| 1463 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1464 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1465 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1467 | /* Protocol violation: we explicitly clear errno, instead of |
| 1468 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1469 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1471 | if (h == INVALID_HANDLE_VALUE) { |
| 1472 | /* This is really a C library error (invalid file handle). |
| 1473 | We set the Win32 error to the closes one matching. */ |
| 1474 | SetLastError(ERROR_INVALID_HANDLE); |
| 1475 | return -1; |
| 1476 | } |
| 1477 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1479 | type = GetFileType(h); |
| 1480 | if (type == FILE_TYPE_UNKNOWN) { |
| 1481 | DWORD error = GetLastError(); |
| 1482 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1483 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | } |
| 1485 | /* else: valid but unknown file */ |
| 1486 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1488 | if (type != FILE_TYPE_DISK) { |
| 1489 | if (type == FILE_TYPE_CHAR) |
| 1490 | result->st_mode = _S_IFCHR; |
| 1491 | else if (type == FILE_TYPE_PIPE) |
| 1492 | result->st_mode = _S_IFIFO; |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | if (!GetFileInformationByHandle(h, &info)) { |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1500 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1503 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | #endif /* MS_WINDOWS */ |
| 1507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1508 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1509 | "stat_result: Result from stat or lstat.\n\n\ |
| 1510 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1511 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1512 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1513 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1514 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1515 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1516 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1517 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1518 | |
| 1519 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1520 | {"st_mode", "protection bits"}, |
| 1521 | {"st_ino", "inode"}, |
| 1522 | {"st_dev", "device"}, |
| 1523 | {"st_nlink", "number of hard links"}, |
| 1524 | {"st_uid", "user ID of owner"}, |
| 1525 | {"st_gid", "group ID of owner"}, |
| 1526 | {"st_size", "total size, in bytes"}, |
| 1527 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1528 | {NULL, "integer time of last access"}, |
| 1529 | {NULL, "integer time of last modification"}, |
| 1530 | {NULL, "integer time of last change"}, |
| 1531 | {"st_atime", "time of last access"}, |
| 1532 | {"st_mtime", "time of last modification"}, |
| 1533 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1534 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1535 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1536 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1537 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1538 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1539 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1540 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1541 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1542 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1543 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1544 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1545 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1546 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1547 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1548 | #endif |
| 1549 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1550 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1551 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1552 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1553 | }; |
| 1554 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1555 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1556 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1557 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1558 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1559 | #endif |
| 1560 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1561 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1562 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1563 | #else |
| 1564 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1565 | #endif |
| 1566 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1567 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1568 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1569 | #else |
| 1570 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1571 | #endif |
| 1572 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1573 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1574 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1575 | #else |
| 1576 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1577 | #endif |
| 1578 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1579 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1580 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1581 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1582 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1583 | #endif |
| 1584 | |
| 1585 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1586 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1587 | #else |
| 1588 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1589 | #endif |
| 1590 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1591 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1592 | "stat_result", /* name */ |
| 1593 | stat_result__doc__, /* doc */ |
| 1594 | stat_result_fields, |
| 1595 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1596 | }; |
| 1597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1598 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1599 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1600 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1601 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1602 | 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] | 1603 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1604 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1605 | |
| 1606 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1607 | {"f_bsize", }, |
| 1608 | {"f_frsize", }, |
| 1609 | {"f_blocks", }, |
| 1610 | {"f_bfree", }, |
| 1611 | {"f_bavail", }, |
| 1612 | {"f_files", }, |
| 1613 | {"f_ffree", }, |
| 1614 | {"f_favail", }, |
| 1615 | {"f_flag", }, |
| 1616 | {"f_namemax",}, |
| 1617 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1618 | }; |
| 1619 | |
| 1620 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1621 | "statvfs_result", /* name */ |
| 1622 | statvfs_result__doc__, /* doc */ |
| 1623 | statvfs_result_fields, |
| 1624 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1625 | }; |
| 1626 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1627 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1628 | PyDoc_STRVAR(waitid_result__doc__, |
| 1629 | "waitid_result: Result from waitid.\n\n\ |
| 1630 | This object may be accessed either as a tuple of\n\ |
| 1631 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1632 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1633 | \n\ |
| 1634 | See os.waitid for more information."); |
| 1635 | |
| 1636 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1637 | {"si_pid", }, |
| 1638 | {"si_uid", }, |
| 1639 | {"si_signo", }, |
| 1640 | {"si_status", }, |
| 1641 | {"si_code", }, |
| 1642 | {0} |
| 1643 | }; |
| 1644 | |
| 1645 | static PyStructSequence_Desc waitid_result_desc = { |
| 1646 | "waitid_result", /* name */ |
| 1647 | waitid_result__doc__, /* doc */ |
| 1648 | waitid_result_fields, |
| 1649 | 5 |
| 1650 | }; |
| 1651 | static PyTypeObject WaitidResultType; |
| 1652 | #endif |
| 1653 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1654 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1655 | static PyTypeObject StatResultType; |
| 1656 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1657 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1658 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1659 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1660 | static newfunc structseq_new; |
| 1661 | |
| 1662 | static PyObject * |
| 1663 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1664 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1665 | PyStructSequence *result; |
| 1666 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1667 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1668 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1669 | if (!result) |
| 1670 | return NULL; |
| 1671 | /* If we have been initialized from a tuple, |
| 1672 | st_?time might be set to None. Initialize it |
| 1673 | from the int slots. */ |
| 1674 | for (i = 7; i <= 9; i++) { |
| 1675 | if (result->ob_item[i+3] == Py_None) { |
| 1676 | Py_DECREF(Py_None); |
| 1677 | Py_INCREF(result->ob_item[i]); |
| 1678 | result->ob_item[i+3] = result->ob_item[i]; |
| 1679 | } |
| 1680 | } |
| 1681 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | |
| 1686 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1687 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1688 | |
| 1689 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1690 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1691 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1692 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1693 | future calls return ints. \n\ |
| 1694 | If newval is omitted, return the current setting.\n"); |
| 1695 | |
| 1696 | static PyObject* |
| 1697 | stat_float_times(PyObject* self, PyObject *args) |
| 1698 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1699 | int newval = -1; |
| 1700 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1701 | return NULL; |
| 1702 | if (newval == -1) |
| 1703 | /* Return old value */ |
| 1704 | return PyBool_FromLong(_stat_float_times); |
| 1705 | _stat_float_times = newval; |
| 1706 | Py_INCREF(Py_None); |
| 1707 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1708 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1709 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1710 | static void |
| 1711 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1712 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1713 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1714 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1715 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1716 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1717 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1718 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1719 | if (!ival) |
| 1720 | return; |
| 1721 | if (_stat_float_times) { |
| 1722 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1723 | } else { |
| 1724 | fval = ival; |
| 1725 | Py_INCREF(fval); |
| 1726 | } |
| 1727 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1728 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1731 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1732 | (used by posix_stat() and posix_fstat()) */ |
| 1733 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1734 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1735 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1736 | unsigned long ansec, mnsec, cnsec; |
| 1737 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1738 | if (v == NULL) |
| 1739 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1741 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1742 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1743 | PyStructSequence_SET_ITEM(v, 1, |
| 1744 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1745 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1747 | #endif |
| 1748 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1749 | PyStructSequence_SET_ITEM(v, 2, |
| 1750 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1751 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1753 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1754 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1755 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1756 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1757 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1758 | PyStructSequence_SET_ITEM(v, 6, |
| 1759 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1760 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1761 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1762 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1763 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1764 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1765 | ansec = st->st_atim.tv_nsec; |
| 1766 | mnsec = st->st_mtim.tv_nsec; |
| 1767 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1768 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | ansec = st->st_atimespec.tv_nsec; |
| 1770 | mnsec = st->st_mtimespec.tv_nsec; |
| 1771 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1772 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1773 | ansec = st->st_atime_nsec; |
| 1774 | mnsec = st->st_mtime_nsec; |
| 1775 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1776 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1779 | fill_time(v, 7, st->st_atime, ansec); |
| 1780 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1781 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1782 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1783 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1784 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1785 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1786 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1787 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1788 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1789 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1790 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1791 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1793 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1794 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1795 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1796 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1797 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1798 | #endif |
| 1799 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1800 | { |
| 1801 | PyObject *val; |
| 1802 | unsigned long bsec,bnsec; |
| 1803 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1804 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1805 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1806 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1807 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1808 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1809 | if (_stat_float_times) { |
| 1810 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1811 | } else { |
| 1812 | val = PyLong_FromLong((long)bsec); |
| 1813 | } |
| 1814 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1815 | val); |
| 1816 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1817 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1818 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1819 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1820 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1821 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1823 | if (PyErr_Occurred()) { |
| 1824 | Py_DECREF(v); |
| 1825 | return NULL; |
| 1826 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1828 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1831 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1832 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1833 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1834 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1835 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1836 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1837 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1838 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1840 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1841 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1842 | STRUCT_STAT st; |
| 1843 | PyObject *opath; |
| 1844 | char *path; |
| 1845 | int res; |
| 1846 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1847 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1848 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1849 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1851 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 1852 | if (wpath == NULL) |
| 1853 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1855 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1856 | res = wstatfunc(wpath, &st); |
| 1857 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1860 | return win32_error_object("stat", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1861 | return _pystat_fromstructstat(&st); |
| 1862 | } |
| 1863 | /* Drop the argument parsing error as narrow strings |
| 1864 | are also valid. */ |
| 1865 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1866 | #endif |
| 1867 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1868 | if (!PyArg_ParseTuple(args, format, |
| 1869 | PyUnicode_FSConverter, &opath)) |
| 1870 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1871 | #ifdef MS_WINDOWS |
| 1872 | if (win32_warn_bytes_api()) { |
| 1873 | Py_DECREF(opath); |
| 1874 | return NULL; |
| 1875 | } |
| 1876 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1877 | path = PyBytes_AsString(opath); |
| 1878 | Py_BEGIN_ALLOW_THREADS |
| 1879 | res = (*statfunc)(path, &st); |
| 1880 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1881 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1882 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1883 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1884 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1885 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1886 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1887 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1888 | } |
| 1889 | else |
| 1890 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1891 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1892 | Py_DECREF(opath); |
| 1893 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1896 | /* POSIX methods */ |
| 1897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1898 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1899 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1900 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1901 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1902 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1903 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1904 | 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] | 1905 | |
| 1906 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1907 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1908 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1909 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1910 | int mode; |
| 1911 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1912 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1913 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1914 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1915 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1916 | wchar_t* wpath = PyUnicode_AsUnicode(po); |
| 1917 | if (wpath == NULL) |
| 1918 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1919 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1920 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1921 | Py_END_ALLOW_THREADS |
| 1922 | goto finish; |
| 1923 | } |
| 1924 | /* Drop the argument parsing error as narrow strings |
| 1925 | are also valid. */ |
| 1926 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1927 | if (!PyArg_ParseTuple(args, "yi:access", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1928 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1929 | if (win32_warn_bytes_api()) |
| 1930 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | Py_BEGIN_ALLOW_THREADS |
| 1932 | attr = GetFileAttributesA(path); |
| 1933 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1934 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1935 | if (attr == 0xFFFFFFFF) |
| 1936 | /* File does not exist, or cannot read attributes */ |
| 1937 | return PyBool_FromLong(0); |
| 1938 | /* Access is possible if either write access wasn't requested, or |
| 1939 | the file isn't read-only, or if it's a directory, as there are |
| 1940 | no read-only directories on Windows. */ |
| 1941 | return PyBool_FromLong(!(mode & 2) |
| 1942 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1943 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1944 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1945 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1946 | int res; |
| 1947 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1948 | PyUnicode_FSConverter, &opath, &mode)) |
| 1949 | return NULL; |
| 1950 | path = PyBytes_AsString(opath); |
| 1951 | Py_BEGIN_ALLOW_THREADS |
| 1952 | res = access(path, mode); |
| 1953 | Py_END_ALLOW_THREADS |
| 1954 | Py_DECREF(opath); |
| 1955 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1956 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1959 | #ifndef F_OK |
| 1960 | #define F_OK 0 |
| 1961 | #endif |
| 1962 | #ifndef R_OK |
| 1963 | #define R_OK 4 |
| 1964 | #endif |
| 1965 | #ifndef W_OK |
| 1966 | #define W_OK 2 |
| 1967 | #endif |
| 1968 | #ifndef X_OK |
| 1969 | #define X_OK 1 |
| 1970 | #endif |
| 1971 | |
| 1972 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1973 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1974 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1975 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1976 | |
| 1977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1978 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1980 | int id; |
| 1981 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1982 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1983 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1984 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1985 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1986 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1987 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1988 | if (id == 0) { |
| 1989 | ret = ttyname(); |
| 1990 | } |
| 1991 | else { |
| 1992 | ret = NULL; |
| 1993 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1994 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1995 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1996 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | if (ret == NULL) |
| 1998 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1999 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2000 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2001 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2002 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2003 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2005 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2006 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2007 | |
| 2008 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2009 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2010 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2011 | char *ret; |
| 2012 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2013 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2014 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2016 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2017 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2018 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | if (ret == NULL) |
| 2020 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2021 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2022 | } |
| 2023 | #endif |
| 2024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2025 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2026 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2027 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2028 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2029 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2030 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2031 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2032 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2033 | 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] | 2034 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2035 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2036 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2037 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2038 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2039 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2040 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2043 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2044 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2045 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2046 | 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] | 2047 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2048 | |
| 2049 | static PyObject * |
| 2050 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2051 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2053 | } |
| 2054 | #endif /* HAVE_FCHDIR */ |
| 2055 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2057 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2058 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2059 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2061 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2062 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2064 | PyObject *opath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2065 | const char *path = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2066 | int i; |
| 2067 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2068 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2070 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2071 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2072 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2073 | if (wpath == NULL) |
| 2074 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2075 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2076 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | if (attr != 0xFFFFFFFF) { |
| 2078 | if (i & _S_IWRITE) |
| 2079 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2080 | else |
| 2081 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2082 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2083 | } |
| 2084 | else |
| 2085 | res = 0; |
| 2086 | Py_END_ALLOW_THREADS |
| 2087 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2088 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2089 | Py_INCREF(Py_None); |
| 2090 | return Py_None; |
| 2091 | } |
| 2092 | /* Drop the argument parsing error as narrow strings |
| 2093 | are also valid. */ |
| 2094 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2095 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2096 | if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2097 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2098 | if (win32_warn_bytes_api()) |
| 2099 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2100 | Py_BEGIN_ALLOW_THREADS |
| 2101 | attr = GetFileAttributesA(path); |
| 2102 | if (attr != 0xFFFFFFFF) { |
| 2103 | if (i & _S_IWRITE) |
| 2104 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2105 | else |
| 2106 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2107 | res = SetFileAttributesA(path, attr); |
| 2108 | } |
| 2109 | else |
| 2110 | res = 0; |
| 2111 | Py_END_ALLOW_THREADS |
| 2112 | if (!res) { |
| 2113 | win32_error("chmod", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2114 | return NULL; |
| 2115 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2116 | Py_INCREF(Py_None); |
| 2117 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2118 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2119 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2120 | &opath, &i)) |
| 2121 | return NULL; |
| 2122 | path = PyBytes_AsString(opath); |
| 2123 | Py_BEGIN_ALLOW_THREADS |
| 2124 | res = chmod(path, i); |
| 2125 | Py_END_ALLOW_THREADS |
| 2126 | if (res < 0) |
| 2127 | return posix_error_with_allocated_filename(opath); |
| 2128 | Py_DECREF(opath); |
| 2129 | Py_INCREF(Py_None); |
| 2130 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2131 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_FCHMOD |
| 2135 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2136 | "fchmod(fd, mode)\n\n\ |
| 2137 | Change the access permissions of the file given by file\n\ |
| 2138 | descriptor fd."); |
| 2139 | |
| 2140 | static PyObject * |
| 2141 | posix_fchmod(PyObject *self, PyObject *args) |
| 2142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2143 | int fd, mode, res; |
| 2144 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2145 | return NULL; |
| 2146 | Py_BEGIN_ALLOW_THREADS |
| 2147 | res = fchmod(fd, mode); |
| 2148 | Py_END_ALLOW_THREADS |
| 2149 | if (res < 0) |
| 2150 | return posix_error(); |
| 2151 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2152 | } |
| 2153 | #endif /* HAVE_FCHMOD */ |
| 2154 | |
| 2155 | #ifdef HAVE_LCHMOD |
| 2156 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2157 | "lchmod(path, mode)\n\n\ |
| 2158 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2159 | affects the link itself rather than the target."); |
| 2160 | |
| 2161 | static PyObject * |
| 2162 | posix_lchmod(PyObject *self, PyObject *args) |
| 2163 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2164 | PyObject *opath; |
| 2165 | char *path; |
| 2166 | int i; |
| 2167 | int res; |
| 2168 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2169 | &opath, &i)) |
| 2170 | return NULL; |
| 2171 | path = PyBytes_AsString(opath); |
| 2172 | Py_BEGIN_ALLOW_THREADS |
| 2173 | res = lchmod(path, i); |
| 2174 | Py_END_ALLOW_THREADS |
| 2175 | if (res < 0) |
| 2176 | return posix_error_with_allocated_filename(opath); |
| 2177 | Py_DECREF(opath); |
| 2178 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2179 | } |
| 2180 | #endif /* HAVE_LCHMOD */ |
| 2181 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2182 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2183 | #ifdef HAVE_CHFLAGS |
| 2184 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2185 | "chflags(path, flags)\n\n\ |
| 2186 | Set file flags."); |
| 2187 | |
| 2188 | static PyObject * |
| 2189 | posix_chflags(PyObject *self, PyObject *args) |
| 2190 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2191 | PyObject *opath; |
| 2192 | char *path; |
| 2193 | unsigned long flags; |
| 2194 | int res; |
| 2195 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2196 | PyUnicode_FSConverter, &opath, &flags)) |
| 2197 | return NULL; |
| 2198 | path = PyBytes_AsString(opath); |
| 2199 | Py_BEGIN_ALLOW_THREADS |
| 2200 | res = chflags(path, flags); |
| 2201 | Py_END_ALLOW_THREADS |
| 2202 | if (res < 0) |
| 2203 | return posix_error_with_allocated_filename(opath); |
| 2204 | Py_DECREF(opath); |
| 2205 | Py_INCREF(Py_None); |
| 2206 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2207 | } |
| 2208 | #endif /* HAVE_CHFLAGS */ |
| 2209 | |
| 2210 | #ifdef HAVE_LCHFLAGS |
| 2211 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2212 | "lchflags(path, flags)\n\n\ |
| 2213 | Set file flags.\n\ |
| 2214 | This function will not follow symbolic links."); |
| 2215 | |
| 2216 | static PyObject * |
| 2217 | posix_lchflags(PyObject *self, PyObject *args) |
| 2218 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2219 | PyObject *opath; |
| 2220 | char *path; |
| 2221 | unsigned long flags; |
| 2222 | int res; |
| 2223 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2224 | PyUnicode_FSConverter, &opath, &flags)) |
| 2225 | return NULL; |
| 2226 | path = PyBytes_AsString(opath); |
| 2227 | Py_BEGIN_ALLOW_THREADS |
| 2228 | res = lchflags(path, flags); |
| 2229 | Py_END_ALLOW_THREADS |
| 2230 | if (res < 0) |
| 2231 | return posix_error_with_allocated_filename(opath); |
| 2232 | Py_DECREF(opath); |
| 2233 | Py_INCREF(Py_None); |
| 2234 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2235 | } |
| 2236 | #endif /* HAVE_LCHFLAGS */ |
| 2237 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2238 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2239 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2240 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2241 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2242 | |
| 2243 | static PyObject * |
| 2244 | posix_chroot(PyObject *self, PyObject *args) |
| 2245 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2246 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2247 | } |
| 2248 | #endif |
| 2249 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2250 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2251 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2252 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2253 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2254 | |
| 2255 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2256 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2257 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2258 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2259 | } |
| 2260 | #endif /* HAVE_FSYNC */ |
| 2261 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2262 | #ifdef HAVE_SYNC |
| 2263 | PyDoc_STRVAR(posix_sync__doc__, |
| 2264 | "sync()\n\n\ |
| 2265 | Force write of everything to disk."); |
| 2266 | |
| 2267 | static PyObject * |
| 2268 | posix_sync(PyObject *self, PyObject *noargs) |
| 2269 | { |
| 2270 | Py_BEGIN_ALLOW_THREADS |
| 2271 | sync(); |
| 2272 | Py_END_ALLOW_THREADS |
| 2273 | Py_RETURN_NONE; |
| 2274 | } |
| 2275 | #endif |
| 2276 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2277 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2278 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2279 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2280 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2281 | #endif |
| 2282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2283 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2284 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2285 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2287 | |
| 2288 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2289 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2290 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2291 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2292 | } |
| 2293 | #endif /* HAVE_FDATASYNC */ |
| 2294 | |
| 2295 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2296 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2297 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2298 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2299 | 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] | 2300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2301 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2302 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2303 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2304 | PyObject *opath; |
| 2305 | char *path; |
| 2306 | long uid, gid; |
| 2307 | int res; |
| 2308 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2309 | PyUnicode_FSConverter, &opath, |
| 2310 | &uid, &gid)) |
| 2311 | return NULL; |
| 2312 | path = PyBytes_AsString(opath); |
| 2313 | Py_BEGIN_ALLOW_THREADS |
| 2314 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2315 | Py_END_ALLOW_THREADS |
| 2316 | if (res < 0) |
| 2317 | return posix_error_with_allocated_filename(opath); |
| 2318 | Py_DECREF(opath); |
| 2319 | Py_INCREF(Py_None); |
| 2320 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2321 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2322 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2323 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2324 | #ifdef HAVE_FCHOWN |
| 2325 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2326 | "fchown(fd, uid, gid)\n\n\ |
| 2327 | Change the owner and group id of the file given by file descriptor\n\ |
| 2328 | fd to the numeric uid and gid."); |
| 2329 | |
| 2330 | static PyObject * |
| 2331 | posix_fchown(PyObject *self, PyObject *args) |
| 2332 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2333 | int fd; |
| 2334 | long uid, gid; |
| 2335 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2336 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2337 | return NULL; |
| 2338 | Py_BEGIN_ALLOW_THREADS |
| 2339 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2340 | Py_END_ALLOW_THREADS |
| 2341 | if (res < 0) |
| 2342 | return posix_error(); |
| 2343 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2344 | } |
| 2345 | #endif /* HAVE_FCHOWN */ |
| 2346 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2347 | #ifdef HAVE_LCHOWN |
| 2348 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2349 | "lchown(path, uid, gid)\n\n\ |
| 2350 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2351 | This function will not follow symbolic links."); |
| 2352 | |
| 2353 | static PyObject * |
| 2354 | posix_lchown(PyObject *self, PyObject *args) |
| 2355 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2356 | PyObject *opath; |
| 2357 | char *path; |
| 2358 | long uid, gid; |
| 2359 | int res; |
| 2360 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2361 | PyUnicode_FSConverter, &opath, |
| 2362 | &uid, &gid)) |
| 2363 | return NULL; |
| 2364 | path = PyBytes_AsString(opath); |
| 2365 | Py_BEGIN_ALLOW_THREADS |
| 2366 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2367 | Py_END_ALLOW_THREADS |
| 2368 | if (res < 0) |
| 2369 | return posix_error_with_allocated_filename(opath); |
| 2370 | Py_DECREF(opath); |
| 2371 | Py_INCREF(Py_None); |
| 2372 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2373 | } |
| 2374 | #endif /* HAVE_LCHOWN */ |
| 2375 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2376 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2377 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2378 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2379 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2381 | char buf[1026]; |
| 2382 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2383 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2384 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2385 | if (!use_bytes) { |
| 2386 | wchar_t wbuf[1026]; |
| 2387 | wchar_t *wbuf2 = wbuf; |
| 2388 | PyObject *resobj; |
| 2389 | DWORD len; |
| 2390 | Py_BEGIN_ALLOW_THREADS |
| 2391 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2392 | /* If the buffer is large enough, len does not include the |
| 2393 | terminating \0. If the buffer is too small, len includes |
| 2394 | the space needed for the terminator. */ |
| 2395 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2396 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2397 | if (wbuf2) |
| 2398 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2399 | } |
| 2400 | Py_END_ALLOW_THREADS |
| 2401 | if (!wbuf2) { |
| 2402 | PyErr_NoMemory(); |
| 2403 | return NULL; |
| 2404 | } |
| 2405 | if (!len) { |
| 2406 | if (wbuf2 != wbuf) free(wbuf2); |
| 2407 | return win32_error("getcwdu", NULL); |
| 2408 | } |
| 2409 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2410 | if (wbuf2 != wbuf) free(wbuf2); |
| 2411 | return resobj; |
| 2412 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2413 | |
| 2414 | if (win32_warn_bytes_api()) |
| 2415 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2416 | #endif |
| 2417 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2418 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2419 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2420 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2421 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2422 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2423 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2424 | Py_END_ALLOW_THREADS |
| 2425 | if (res == NULL) |
| 2426 | return posix_error(); |
| 2427 | if (use_bytes) |
| 2428 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2429 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2430 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2431 | |
| 2432 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2433 | "getcwd() -> path\n\n\ |
| 2434 | Return a unicode string representing the current working directory."); |
| 2435 | |
| 2436 | static PyObject * |
| 2437 | posix_getcwd_unicode(PyObject *self) |
| 2438 | { |
| 2439 | return posix_getcwd(0); |
| 2440 | } |
| 2441 | |
| 2442 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2443 | "getcwdb() -> path\n\n\ |
| 2444 | Return a bytes string representing the current working directory."); |
| 2445 | |
| 2446 | static PyObject * |
| 2447 | posix_getcwd_bytes(PyObject *self) |
| 2448 | { |
| 2449 | return posix_getcwd(1); |
| 2450 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2451 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2452 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2453 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2454 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2455 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2456 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2457 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2458 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2459 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2460 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2462 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2463 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2464 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2465 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2466 | #ifdef MS_WINDOWS |
| 2467 | PyDoc_STRVAR(win32_link__doc__, |
| 2468 | "link(src, dst)\n\n\ |
| 2469 | Create a hard link to a file."); |
| 2470 | |
| 2471 | static PyObject * |
| 2472 | win32_link(PyObject *self, PyObject *args) |
| 2473 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2474 | PyObject *src, *dst; |
| 2475 | BOOL ok; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2476 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2477 | if (PyArg_ParseTuple(args, "UU:link", &src, &dst)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2478 | { |
| 2479 | wchar_t *wsrc, *wdst; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2480 | |
| 2481 | wsrc = PyUnicode_AsUnicode(src); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2482 | if (wsrc == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2483 | goto error; |
| 2484 | wdst = PyUnicode_AsUnicode(dst); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2485 | if (wdst == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2486 | goto error; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2487 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2488 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2489 | ok = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2490 | Py_END_ALLOW_THREADS |
| 2491 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2492 | if (!ok) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2493 | return win32_error("link", NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2494 | Py_RETURN_NONE; |
| 2495 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2496 | else { |
| 2497 | PyErr_Clear(); |
| 2498 | if (!PyArg_ParseTuple(args, "O&O&:link", |
| 2499 | PyUnicode_FSConverter, &src, |
| 2500 | PyUnicode_FSConverter, &dst)) |
| 2501 | return NULL; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2502 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2503 | if (win32_warn_bytes_api()) |
| 2504 | goto error; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2505 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2506 | Py_BEGIN_ALLOW_THREADS |
| 2507 | ok = CreateHardLinkA(PyBytes_AS_STRING(dst), |
| 2508 | PyBytes_AS_STRING(src), |
| 2509 | NULL); |
| 2510 | Py_END_ALLOW_THREADS |
| 2511 | |
| 2512 | Py_XDECREF(src); |
| 2513 | Py_XDECREF(dst); |
| 2514 | |
| 2515 | if (!ok) |
| 2516 | return win32_error("link", NULL); |
| 2517 | Py_RETURN_NONE; |
| 2518 | |
| 2519 | error: |
| 2520 | Py_XDECREF(src); |
| 2521 | Py_XDECREF(dst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2522 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2523 | } |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2524 | } |
| 2525 | #endif /* MS_WINDOWS */ |
| 2526 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2528 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2529 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2530 | Return a list containing the names of the entries in the directory.\n\ |
| 2531 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2532 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2533 | \n\ |
| 2534 | 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] | 2535 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2536 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2537 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2538 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2539 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2540 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2541 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2542 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2543 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2544 | PyObject *d, *v; |
| 2545 | HANDLE hFindFile; |
| 2546 | BOOL result; |
| 2547 | WIN32_FIND_DATA FileData; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2548 | const char *path; |
| 2549 | Py_ssize_t pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2550 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2551 | char *bufptr = namebuf; |
| 2552 | 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] | 2553 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2554 | PyObject *po = NULL; |
| 2555 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2556 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2557 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2559 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2560 | po_wchars = L"."; |
| 2561 | len = 1; |
| 2562 | } else { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 2563 | po_wchars = PyUnicode_AsUnicodeAndSize(po, &len); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2564 | if (po_wchars == NULL) |
| 2565 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2566 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2567 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2568 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2569 | if (!wnamebuf) { |
| 2570 | PyErr_NoMemory(); |
| 2571 | return NULL; |
| 2572 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2573 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2574 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2575 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2576 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2577 | wnamebuf[len++] = L'\\'; |
| 2578 | wcscpy(wnamebuf + len, L"*.*"); |
| 2579 | } |
| 2580 | if ((d = PyList_New(0)) == NULL) { |
| 2581 | free(wnamebuf); |
| 2582 | return NULL; |
| 2583 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2584 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2585 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2586 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2587 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2588 | int error = GetLastError(); |
| 2589 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2590 | free(wnamebuf); |
| 2591 | return d; |
| 2592 | } |
| 2593 | Py_DECREF(d); |
| 2594 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2595 | free(wnamebuf); |
| 2596 | return NULL; |
| 2597 | } |
| 2598 | do { |
| 2599 | /* Skip over . and .. */ |
| 2600 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2601 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2602 | v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2603 | if (v == NULL) { |
| 2604 | Py_DECREF(d); |
| 2605 | d = NULL; |
| 2606 | break; |
| 2607 | } |
| 2608 | if (PyList_Append(d, v) != 0) { |
| 2609 | Py_DECREF(v); |
| 2610 | Py_DECREF(d); |
| 2611 | d = NULL; |
| 2612 | break; |
| 2613 | } |
| 2614 | Py_DECREF(v); |
| 2615 | } |
| 2616 | Py_BEGIN_ALLOW_THREADS |
| 2617 | result = FindNextFileW(hFindFile, &wFileData); |
| 2618 | Py_END_ALLOW_THREADS |
| 2619 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2620 | it got to the end of the directory. */ |
| 2621 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2622 | Py_DECREF(d); |
| 2623 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2624 | FindClose(hFindFile); |
| 2625 | free(wnamebuf); |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2629 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2630 | if (FindClose(hFindFile) == FALSE) { |
| 2631 | Py_DECREF(d); |
| 2632 | win32_error_unicode("FindClose", wnamebuf); |
| 2633 | free(wnamebuf); |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | free(wnamebuf); |
| 2637 | return d; |
| 2638 | } |
| 2639 | /* Drop the argument parsing error as narrow strings |
| 2640 | are also valid. */ |
| 2641 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2642 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2643 | if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2644 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2645 | if (win32_warn_bytes_api()) |
| 2646 | return NULL; |
| 2647 | if (pathlen+1 > MAX_PATH) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2648 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2649 | return NULL; |
| 2650 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2651 | strcpy(namebuf, path); |
| 2652 | len = pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2653 | if (len > 0) { |
| 2654 | char ch = namebuf[len-1]; |
| 2655 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2656 | namebuf[len++] = '/'; |
| 2657 | strcpy(namebuf + len, "*.*"); |
| 2658 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2659 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2660 | if ((d = PyList_New(0)) == NULL) |
| 2661 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2663 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2664 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2665 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2666 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2667 | int error = GetLastError(); |
| 2668 | if (error == ERROR_FILE_NOT_FOUND) |
| 2669 | return d; |
| 2670 | Py_DECREF(d); |
| 2671 | return win32_error("FindFirstFile", namebuf); |
| 2672 | } |
| 2673 | do { |
| 2674 | /* Skip over . and .. */ |
| 2675 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2676 | strcmp(FileData.cFileName, "..") != 0) { |
| 2677 | v = PyBytes_FromString(FileData.cFileName); |
| 2678 | if (v == NULL) { |
| 2679 | Py_DECREF(d); |
| 2680 | d = NULL; |
| 2681 | break; |
| 2682 | } |
| 2683 | if (PyList_Append(d, v) != 0) { |
| 2684 | Py_DECREF(v); |
| 2685 | Py_DECREF(d); |
| 2686 | d = NULL; |
| 2687 | break; |
| 2688 | } |
| 2689 | Py_DECREF(v); |
| 2690 | } |
| 2691 | Py_BEGIN_ALLOW_THREADS |
| 2692 | result = FindNextFile(hFindFile, &FileData); |
| 2693 | Py_END_ALLOW_THREADS |
| 2694 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2695 | it got to the end of the directory. */ |
| 2696 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2697 | Py_DECREF(d); |
| 2698 | win32_error("FindNextFile", namebuf); |
| 2699 | FindClose(hFindFile); |
| 2700 | return NULL; |
| 2701 | } |
| 2702 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2703 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2704 | if (FindClose(hFindFile) == FALSE) { |
| 2705 | Py_DECREF(d); |
| 2706 | return win32_error("FindClose", namebuf); |
| 2707 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2709 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2710 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2711 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2712 | |
| 2713 | #ifndef MAX_PATH |
| 2714 | #define MAX_PATH CCHMAXPATH |
| 2715 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2716 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2717 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2718 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2719 | PyObject *d, *v; |
| 2720 | char namebuf[MAX_PATH+5]; |
| 2721 | HDIR hdir = 1; |
| 2722 | ULONG srchcnt = 1; |
| 2723 | FILEFINDBUF3 ep; |
| 2724 | APIRET rc; |
| 2725 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2726 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2727 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2728 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2729 | name = PyBytes_AsString(oname); |
| 2730 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2731 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2732 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2733 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2734 | return NULL; |
| 2735 | } |
| 2736 | strcpy(namebuf, name); |
| 2737 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2738 | if (*pt == ALTSEP) |
| 2739 | *pt = SEP; |
| 2740 | if (namebuf[len-1] != SEP) |
| 2741 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2742 | strcpy(namebuf + len, "*.*"); |
| 2743 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2744 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2745 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2746 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2747 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2748 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2749 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2750 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2751 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2752 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2753 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2754 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2755 | |
| 2756 | if (rc != NO_ERROR) { |
| 2757 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2758 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2761 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2762 | do { |
| 2763 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2764 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2765 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2766 | |
| 2767 | strcpy(namebuf, ep.achName); |
| 2768 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2769 | /* Leave Case of Name Alone -- In Native Form */ |
| 2770 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2771 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2772 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2773 | if (v == NULL) { |
| 2774 | Py_DECREF(d); |
| 2775 | d = NULL; |
| 2776 | break; |
| 2777 | } |
| 2778 | if (PyList_Append(d, v) != 0) { |
| 2779 | Py_DECREF(v); |
| 2780 | Py_DECREF(d); |
| 2781 | d = NULL; |
| 2782 | break; |
| 2783 | } |
| 2784 | Py_DECREF(v); |
| 2785 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2786 | } |
| 2787 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2788 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2789 | return d; |
| 2790 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2791 | PyObject *oname; |
| 2792 | char *name; |
| 2793 | PyObject *d, *v; |
| 2794 | DIR *dirp; |
| 2795 | struct dirent *ep; |
| 2796 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2797 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2798 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2799 | /* v is never read, so it does not need to be initialized yet. */ |
| 2800 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | arg_is_unicode = 0; |
| 2802 | PyErr_Clear(); |
| 2803 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2804 | oname = NULL; |
| 2805 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2806 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2807 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2808 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2809 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2810 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2811 | Py_BEGIN_ALLOW_THREADS |
| 2812 | dirp = opendir(name); |
| 2813 | Py_END_ALLOW_THREADS |
| 2814 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2815 | return posix_error_with_allocated_filename(oname); |
| 2816 | } |
| 2817 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2818 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2819 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2820 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2821 | Py_DECREF(oname); |
| 2822 | return NULL; |
| 2823 | } |
| 2824 | for (;;) { |
| 2825 | errno = 0; |
| 2826 | Py_BEGIN_ALLOW_THREADS |
| 2827 | ep = readdir(dirp); |
| 2828 | Py_END_ALLOW_THREADS |
| 2829 | if (ep == NULL) { |
| 2830 | if (errno == 0) { |
| 2831 | break; |
| 2832 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2833 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2834 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2835 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2836 | Py_DECREF(d); |
| 2837 | return posix_error_with_allocated_filename(oname); |
| 2838 | } |
| 2839 | } |
| 2840 | if (ep->d_name[0] == '.' && |
| 2841 | (NAMLEN(ep) == 1 || |
| 2842 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2843 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2844 | if (arg_is_unicode) |
| 2845 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2846 | else |
| 2847 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2848 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2849 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2850 | break; |
| 2851 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2852 | if (PyList_Append(d, v) != 0) { |
| 2853 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2854 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | } |
| 2857 | Py_DECREF(v); |
| 2858 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2859 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2860 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2861 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2862 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2864 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2865 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2866 | #endif /* which OS */ |
| 2867 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2868 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2869 | #ifdef HAVE_FDOPENDIR |
| 2870 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2871 | "fdlistdir(fd) -> list_of_strings\n\n\ |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2872 | Like listdir(), but uses a file descriptor instead."); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2873 | |
| 2874 | static PyObject * |
| 2875 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2876 | { |
| 2877 | PyObject *d, *v; |
| 2878 | DIR *dirp; |
| 2879 | struct dirent *ep; |
| 2880 | int fd; |
| 2881 | |
| 2882 | errno = 0; |
| 2883 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2884 | return NULL; |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2885 | /* closedir() closes the FD, so we duplicate it */ |
| 2886 | fd = dup(fd); |
| 2887 | if (fd < 0) |
| 2888 | return posix_error(); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2889 | Py_BEGIN_ALLOW_THREADS |
| 2890 | dirp = fdopendir(fd); |
| 2891 | Py_END_ALLOW_THREADS |
| 2892 | if (dirp == NULL) { |
| 2893 | close(fd); |
| 2894 | return posix_error(); |
| 2895 | } |
| 2896 | if ((d = PyList_New(0)) == NULL) { |
| 2897 | Py_BEGIN_ALLOW_THREADS |
| 2898 | closedir(dirp); |
| 2899 | Py_END_ALLOW_THREADS |
| 2900 | return NULL; |
| 2901 | } |
| 2902 | for (;;) { |
| 2903 | errno = 0; |
| 2904 | Py_BEGIN_ALLOW_THREADS |
| 2905 | ep = readdir(dirp); |
| 2906 | Py_END_ALLOW_THREADS |
| 2907 | if (ep == NULL) { |
| 2908 | if (errno == 0) { |
| 2909 | break; |
| 2910 | } else { |
| 2911 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2912 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2913 | closedir(dirp); |
| 2914 | Py_END_ALLOW_THREADS |
| 2915 | Py_DECREF(d); |
| 2916 | return posix_error(); |
| 2917 | } |
| 2918 | } |
| 2919 | if (ep->d_name[0] == '.' && |
| 2920 | (NAMLEN(ep) == 1 || |
| 2921 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2922 | continue; |
| 2923 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2924 | if (v == NULL) { |
| 2925 | Py_CLEAR(d); |
| 2926 | break; |
| 2927 | } |
| 2928 | if (PyList_Append(d, v) != 0) { |
| 2929 | Py_DECREF(v); |
| 2930 | Py_CLEAR(d); |
| 2931 | break; |
| 2932 | } |
| 2933 | Py_DECREF(v); |
| 2934 | } |
| 2935 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2936 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2937 | closedir(dirp); |
| 2938 | Py_END_ALLOW_THREADS |
| 2939 | |
| 2940 | return d; |
| 2941 | } |
| 2942 | #endif |
| 2943 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2944 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2945 | /* A helper function for abspath on win32 */ |
| 2946 | static PyObject * |
| 2947 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2948 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2949 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2950 | char outbuf[MAX_PATH*2]; |
| 2951 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2952 | PyObject *po; |
| 2953 | |
| 2954 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 2955 | { |
| 2956 | wchar_t *wpath; |
| 2957 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2958 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2959 | DWORD result; |
| 2960 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2961 | |
| 2962 | wpath = PyUnicode_AsUnicode(po); |
| 2963 | if (wpath == NULL) |
| 2964 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2965 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2966 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2967 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2968 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2969 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2970 | if (!woutbufp) |
| 2971 | return PyErr_NoMemory(); |
| 2972 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2973 | } |
| 2974 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2975 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2976 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2977 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2978 | if (woutbufp != woutbuf) |
| 2979 | free(woutbufp); |
| 2980 | return v; |
| 2981 | } |
| 2982 | /* Drop the argument parsing error as narrow strings |
| 2983 | are also valid. */ |
| 2984 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2985 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2986 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 2987 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2988 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2989 | if (win32_warn_bytes_api()) |
| 2990 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2991 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2992 | outbuf, &temp)) { |
| 2993 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2996 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2997 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2998 | Py_FileSystemDefaultEncoding, NULL); |
| 2999 | } |
| 3000 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3001 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3002 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3003 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3004 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3005 | /* A helper function for samepath on windows */ |
| 3006 | static PyObject * |
| 3007 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3008 | { |
| 3009 | HANDLE hFile; |
| 3010 | int buf_size; |
| 3011 | wchar_t *target_path; |
| 3012 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3013 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3014 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3015 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3016 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3017 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3018 | path = PyUnicode_AsUnicode(po); |
| 3019 | if (path == NULL) |
| 3020 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3021 | |
| 3022 | if(!check_GetFinalPathNameByHandle()) { |
| 3023 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3024 | NotImplementedError. */ |
| 3025 | return PyErr_Format(PyExc_NotImplementedError, |
| 3026 | "GetFinalPathNameByHandle not available on this platform"); |
| 3027 | } |
| 3028 | |
| 3029 | hFile = CreateFileW( |
| 3030 | path, |
| 3031 | 0, /* desired access */ |
| 3032 | 0, /* share mode */ |
| 3033 | NULL, /* security attributes */ |
| 3034 | OPEN_EXISTING, |
| 3035 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3036 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3037 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3038 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3039 | if(hFile == INVALID_HANDLE_VALUE) |
| 3040 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3041 | |
| 3042 | /* We have a good handle to the target, use it to determine the |
| 3043 | target path name. */ |
| 3044 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3045 | |
| 3046 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3047 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3048 | |
| 3049 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3050 | if(!target_path) |
| 3051 | return PyErr_NoMemory(); |
| 3052 | |
| 3053 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3054 | buf_size, VOLUME_NAME_DOS); |
| 3055 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3056 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3057 | |
| 3058 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3059 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3060 | |
| 3061 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3062 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3063 | free(target_path); |
| 3064 | return result; |
| 3065 | |
| 3066 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3067 | |
| 3068 | static PyObject * |
| 3069 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3070 | { |
| 3071 | HANDLE hFile; |
| 3072 | BY_HANDLE_FILE_INFORMATION info; |
| 3073 | int fd; |
| 3074 | |
| 3075 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3076 | return NULL; |
| 3077 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3078 | if (!_PyVerify_fd(fd)) |
| 3079 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3080 | |
| 3081 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3082 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3083 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3084 | |
| 3085 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3086 | return win32_error("_getfileinformation", NULL); |
| 3087 | |
| 3088 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3089 | info.nFileIndexHigh, |
| 3090 | info.nFileIndexLow); |
| 3091 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3092 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3093 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3094 | "Return true if the pathname refers to an existing directory."); |
| 3095 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3096 | static PyObject * |
| 3097 | posix__isdir(PyObject *self, PyObject *args) |
| 3098 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3099 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3100 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3101 | DWORD attributes; |
| 3102 | |
| 3103 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3104 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3105 | if (wpath == NULL) |
| 3106 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3107 | |
| 3108 | attributes = GetFileAttributesW(wpath); |
| 3109 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3110 | Py_RETURN_FALSE; |
| 3111 | goto check; |
| 3112 | } |
| 3113 | /* Drop the argument parsing error as narrow strings |
| 3114 | are also valid. */ |
| 3115 | PyErr_Clear(); |
| 3116 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3117 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3118 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3119 | if (win32_warn_bytes_api()) |
| 3120 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3121 | attributes = GetFileAttributesA(path); |
| 3122 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3123 | Py_RETURN_FALSE; |
| 3124 | |
| 3125 | check: |
| 3126 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3127 | Py_RETURN_TRUE; |
| 3128 | else |
| 3129 | Py_RETURN_FALSE; |
| 3130 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3131 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3133 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3134 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3135 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3136 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3137 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3138 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3139 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3140 | int res; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3141 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3142 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3143 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3144 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3145 | PyObject *po; |
| 3146 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3147 | { |
| 3148 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3149 | if (wpath == NULL) |
| 3150 | return NULL; |
| 3151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3153 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | Py_END_ALLOW_THREADS |
| 3155 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3156 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | Py_INCREF(Py_None); |
| 3158 | return Py_None; |
| 3159 | } |
| 3160 | /* Drop the argument parsing error as narrow strings |
| 3161 | are also valid. */ |
| 3162 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3163 | if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3164 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3165 | if (win32_warn_bytes_api()) |
| 3166 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3167 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3168 | res = CreateDirectoryA(path, NULL); |
| 3169 | Py_END_ALLOW_THREADS |
| 3170 | if (!res) { |
| 3171 | win32_error("mkdir", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3172 | return NULL; |
| 3173 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3174 | Py_INCREF(Py_None); |
| 3175 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3176 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3177 | PyObject *opath; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3179 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3180 | PyUnicode_FSConverter, &opath, &mode)) |
| 3181 | return NULL; |
| 3182 | path = PyBytes_AsString(opath); |
| 3183 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3184 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3185 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3186 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3187 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3188 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3189 | Py_END_ALLOW_THREADS |
| 3190 | if (res < 0) |
| 3191 | return posix_error_with_allocated_filename(opath); |
| 3192 | Py_DECREF(opath); |
| 3193 | Py_INCREF(Py_None); |
| 3194 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3195 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3198 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3199 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3200 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3201 | #include <sys/resource.h> |
| 3202 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3203 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3204 | |
| 3205 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3206 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3207 | "nice(inc) -> new_priority\n\n\ |
| 3208 | 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] | 3209 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3210 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3211 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3212 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3213 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3215 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3216 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3218 | /* There are two flavours of 'nice': one that returns the new |
| 3219 | priority (as required by almost all standards out there) and the |
| 3220 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3221 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3222 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3223 | If we are of the nice family that returns the new priority, we |
| 3224 | need to clear errno before the call, and check if errno is filled |
| 3225 | before calling posix_error() on a returnvalue of -1, because the |
| 3226 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3227 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3228 | errno = 0; |
| 3229 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3230 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3231 | if (value == 0) |
| 3232 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3233 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3234 | if (value == -1 && errno != 0) |
| 3235 | /* either nice() or getpriority() returned an error */ |
| 3236 | return posix_error(); |
| 3237 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3238 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3239 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3240 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3241 | |
| 3242 | #ifdef HAVE_GETPRIORITY |
| 3243 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3244 | "getpriority(which, who) -> current_priority\n\n\ |
| 3245 | Get program scheduling priority."); |
| 3246 | |
| 3247 | static PyObject * |
| 3248 | posix_getpriority(PyObject *self, PyObject *args) |
| 3249 | { |
| 3250 | int which, who, retval; |
| 3251 | |
| 3252 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3253 | return NULL; |
| 3254 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3255 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3256 | if (errno != 0) |
| 3257 | return posix_error(); |
| 3258 | return PyLong_FromLong((long)retval); |
| 3259 | } |
| 3260 | #endif /* HAVE_GETPRIORITY */ |
| 3261 | |
| 3262 | |
| 3263 | #ifdef HAVE_SETPRIORITY |
| 3264 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3265 | "setpriority(which, who, prio) -> None\n\n\ |
| 3266 | Set program scheduling priority."); |
| 3267 | |
| 3268 | static PyObject * |
| 3269 | posix_setpriority(PyObject *self, PyObject *args) |
| 3270 | { |
| 3271 | int which, who, prio, retval; |
| 3272 | |
| 3273 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3274 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3275 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3276 | if (retval == -1) |
| 3277 | return posix_error(); |
| 3278 | Py_RETURN_NONE; |
| 3279 | } |
| 3280 | #endif /* HAVE_SETPRIORITY */ |
| 3281 | |
| 3282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3283 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3284 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3285 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3286 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3287 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3288 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3289 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3290 | #ifdef MS_WINDOWS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3291 | PyObject *src, *dst; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3292 | BOOL result; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3293 | if (PyArg_ParseTuple(args, "UU:rename", &src, &dst)) |
| 3294 | { |
| 3295 | wchar_t *wsrc, *wdst; |
| 3296 | |
| 3297 | wsrc = PyUnicode_AsUnicode(src); |
| 3298 | if (wsrc == NULL) |
| 3299 | return NULL; |
| 3300 | wdst = PyUnicode_AsUnicode(dst); |
| 3301 | if (wdst == NULL) |
| 3302 | return NULL; |
| 3303 | Py_BEGIN_ALLOW_THREADS |
| 3304 | result = MoveFileW(wsrc, wdst); |
| 3305 | Py_END_ALLOW_THREADS |
| 3306 | if (!result) |
| 3307 | return win32_error("rename", NULL); |
| 3308 | Py_INCREF(Py_None); |
| 3309 | return Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3310 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3311 | else { |
| 3312 | PyErr_Clear(); |
| 3313 | if (!PyArg_ParseTuple(args, "O&O&:rename", |
| 3314 | PyUnicode_FSConverter, &src, |
| 3315 | PyUnicode_FSConverter, &dst)) |
| 3316 | return NULL; |
| 3317 | |
| 3318 | if (win32_warn_bytes_api()) |
| 3319 | goto error; |
| 3320 | |
| 3321 | Py_BEGIN_ALLOW_THREADS |
| 3322 | result = MoveFileA(PyBytes_AS_STRING(src), |
| 3323 | PyBytes_AS_STRING(dst)); |
| 3324 | Py_END_ALLOW_THREADS |
| 3325 | |
| 3326 | Py_XDECREF(src); |
| 3327 | Py_XDECREF(dst); |
| 3328 | |
| 3329 | if (!result) |
| 3330 | return win32_error("rename", NULL); |
| 3331 | Py_INCREF(Py_None); |
| 3332 | return Py_None; |
| 3333 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3334 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3335 | Py_XDECREF(src); |
| 3336 | Py_XDECREF(dst); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3337 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3338 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3339 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3341 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3345 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3346 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3347 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3348 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3349 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3350 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3351 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3352 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3353 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3354 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3355 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3356 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3360 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3361 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3362 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3363 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3364 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3365 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3366 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3367 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3368 | 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] | 3369 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3370 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3371 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3374 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3375 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3376 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3377 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3378 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3379 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3380 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3381 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3382 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3383 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3384 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3385 | wchar_t *command; |
| 3386 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3387 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3389 | Py_BEGIN_ALLOW_THREADS |
| 3390 | sts = _wsystem(command); |
| 3391 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3392 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3393 | PyObject *command_obj; |
| 3394 | char *command; |
| 3395 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3396 | PyUnicode_FSConverter, &command_obj)) |
| 3397 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3398 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3399 | command = PyBytes_AsString(command_obj); |
| 3400 | Py_BEGIN_ALLOW_THREADS |
| 3401 | sts = system(command); |
| 3402 | Py_END_ALLOW_THREADS |
| 3403 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3404 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3405 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3406 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3407 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3408 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3410 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3411 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3412 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3413 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3415 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3417 | int i; |
| 3418 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3419 | return NULL; |
| 3420 | i = (int)umask(i); |
| 3421 | if (i < 0) |
| 3422 | return posix_error(); |
| 3423 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3426 | #ifdef MS_WINDOWS |
| 3427 | |
| 3428 | /* override the default DeleteFileW behavior so that directory |
| 3429 | symlinks can be removed with this function, the same as with |
| 3430 | Unix symlinks */ |
| 3431 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3432 | { |
| 3433 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3434 | WIN32_FIND_DATAW find_data; |
| 3435 | HANDLE find_data_handle; |
| 3436 | int is_directory = 0; |
| 3437 | int is_link = 0; |
| 3438 | |
| 3439 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3440 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3441 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3442 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3443 | it is a symlink */ |
| 3444 | if(is_directory && |
| 3445 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3446 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3447 | |
| 3448 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3449 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3450 | FindClose(find_data_handle); |
| 3451 | } |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | if (is_directory && is_link) |
| 3456 | return RemoveDirectoryW(lpFileName); |
| 3457 | |
| 3458 | return DeleteFileW(lpFileName); |
| 3459 | } |
| 3460 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3461 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3462 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3463 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3464 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3465 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3466 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3467 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3468 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3469 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3470 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3471 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3472 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3473 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3474 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3475 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3476 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3477 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3478 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3481 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3482 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3483 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3484 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3485 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3486 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3487 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3488 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3489 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3490 | struct utsname u; |
| 3491 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3492 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3493 | Py_BEGIN_ALLOW_THREADS |
| 3494 | res = uname(&u); |
| 3495 | Py_END_ALLOW_THREADS |
| 3496 | if (res < 0) |
| 3497 | return posix_error(); |
| 3498 | return Py_BuildValue("(sssss)", |
| 3499 | u.sysname, |
| 3500 | u.nodename, |
| 3501 | u.release, |
| 3502 | u.version, |
| 3503 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3504 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3505 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3506 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3507 | |
| 3508 | /* |
| 3509 | * Classic POSIX utime functions supported microseconds (1m/sec). |
| 3510 | * Newer POSIX functions support nanoseconds (1 billion per sec). |
| 3511 | * posixmodule now uses the new functions where possible. |
| 3512 | * This improves accuracy in many situations, for example shutil.copy2(). |
| 3513 | * |
| 3514 | * The implementation isn't currently sophisticated enough to handle |
| 3515 | * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false. |
| 3516 | * Specifically, posix_futimes() would break. |
| 3517 | * |
| 3518 | * Supporting such a platform wouldn't be impossible; you'd need two |
| 3519 | * extract_time() functions, or make its precision a parameter. |
| 3520 | * Since such a platform seems unlikely we haven't bothered. |
| 3521 | */ |
| 3522 | #if defined(HAVE_UTIMENSAT) |
| 3523 | #define EXTRACT_TIME_PRECISION (1e9) |
| 3524 | #if !defined(HAVE_FUTIMENS) |
| 3525 | #error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment. |
| 3526 | #endif |
| 3527 | #else |
| 3528 | #define EXTRACT_TIME_PRECISION (1e6) |
| 3529 | #endif |
| 3530 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3531 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3532 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3533 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3534 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | if (PyFloat_Check(t)) { |
| 3536 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3537 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3538 | if (!intobj) |
| 3539 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3540 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3541 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3542 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3543 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3544 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3545 | Py_DECREF(intobj); |
| 3546 | if (intval == -1 && PyErr_Occurred()) |
| 3547 | return -1; |
| 3548 | *sec = intval; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3549 | |
| 3550 | *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | if (*usec < 0) |
| 3552 | /* If rounding gave us a negative number, |
| 3553 | truncate. */ |
| 3554 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3555 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3556 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3557 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3558 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3559 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3561 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3562 | if (intval == -1 && PyErr_Occurred()) |
| 3563 | return -1; |
| 3564 | *sec = intval; |
| 3565 | *usec = 0; |
| 3566 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3567 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3569 | PyDoc_STRVAR(posix_utime__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3570 | "utime(path[, (atime, mtime)])\n\ |
| 3571 | Set the access and modified time of the file to the given values.\n\ |
| 3572 | If no second argument is used, set the access and modified times to\n\ |
| 3573 | the current time."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3574 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3575 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3576 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3577 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3578 | #ifdef MS_WINDOWS |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3579 | PyObject *arg = Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3580 | PyObject *obwpath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | wchar_t *wpath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3582 | const char *apath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3583 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3584 | time_t atimesec, mtimesec; |
| 3585 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3586 | FILETIME atime, mtime; |
| 3587 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3588 | |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 3589 | if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3590 | wpath = PyUnicode_AsUnicode(obwpath); |
| 3591 | if (wpath == NULL) |
| 3592 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3593 | Py_BEGIN_ALLOW_THREADS |
| 3594 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3595 | NULL, OPEN_EXISTING, |
| 3596 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3597 | Py_END_ALLOW_THREADS |
| 3598 | if (hFile == INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3599 | return win32_error_object("utime", obwpath); |
| 3600 | } |
| 3601 | else { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3602 | /* Drop the argument parsing error as narrow strings |
| 3603 | are also valid. */ |
| 3604 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3605 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3606 | if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg)) |
| 3607 | return NULL; |
| 3608 | if (win32_warn_bytes_api()) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3609 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3610 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3611 | Py_BEGIN_ALLOW_THREADS |
| 3612 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3613 | NULL, OPEN_EXISTING, |
| 3614 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3615 | Py_END_ALLOW_THREADS |
| 3616 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3617 | win32_error("utime", apath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3618 | return NULL; |
| 3619 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3622 | if (arg == Py_None) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3623 | SYSTEMTIME now; |
| 3624 | GetSystemTime(&now); |
| 3625 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3626 | !SystemTimeToFileTime(&now, &atime)) { |
| 3627 | win32_error("utime", NULL); |
| 3628 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3629 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | } |
| 3631 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3632 | PyErr_SetString(PyExc_TypeError, |
| 3633 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3634 | goto done; |
| 3635 | } |
| 3636 | else { |
| 3637 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3638 | &atimesec, &ausec) == -1) |
| 3639 | goto done; |
| 3640 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3641 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3642 | &mtimesec, &musec) == -1) |
| 3643 | goto done; |
| 3644 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3645 | } |
| 3646 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3647 | /* Avoid putting the file name into the error here, |
| 3648 | as that may confuse the user into believing that |
| 3649 | something is wrong with the file, when it also |
| 3650 | could be the time stamp that gives a problem. */ |
| 3651 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3652 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3653 | } |
| 3654 | Py_INCREF(Py_None); |
| 3655 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3656 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3657 | CloseHandle(hFile); |
| 3658 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3659 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3660 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3661 | PyObject *opath; |
| 3662 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3663 | time_t atime, mtime; |
| 3664 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3665 | int res; |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3666 | PyObject* arg = Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3667 | |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 3668 | if (!PyArg_ParseTuple(args, "O&|O:utime", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3669 | PyUnicode_FSConverter, &opath, &arg)) |
| 3670 | return NULL; |
| 3671 | path = PyBytes_AsString(opath); |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3672 | if (arg == Py_None) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3673 | /* optional time values not given */ |
| 3674 | Py_BEGIN_ALLOW_THREADS |
| 3675 | res = utime(path, NULL); |
| 3676 | Py_END_ALLOW_THREADS |
| 3677 | } |
| 3678 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3679 | PyErr_SetString(PyExc_TypeError, |
| 3680 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3681 | Py_DECREF(opath); |
| 3682 | return NULL; |
| 3683 | } |
| 3684 | else { |
| 3685 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3686 | &atime, &ausec) == -1) { |
| 3687 | Py_DECREF(opath); |
| 3688 | return NULL; |
| 3689 | } |
| 3690 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3691 | &mtime, &musec) == -1) { |
| 3692 | Py_DECREF(opath); |
| 3693 | return NULL; |
| 3694 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3695 | |
| 3696 | Py_BEGIN_ALLOW_THREADS |
| 3697 | { |
| 3698 | #ifdef HAVE_UTIMENSAT |
| 3699 | struct timespec buf[2]; |
| 3700 | buf[0].tv_sec = atime; |
| 3701 | buf[0].tv_nsec = ausec; |
| 3702 | buf[1].tv_sec = mtime; |
| 3703 | buf[1].tv_nsec = musec; |
| 3704 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3705 | #elif defined(HAVE_UTIMES) |
| 3706 | struct timeval buf[2]; |
| 3707 | buf[0].tv_sec = atime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3708 | buf[0].tv_usec = ausec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3709 | buf[1].tv_sec = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3710 | buf[1].tv_usec = musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3711 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3712 | #elif defined(HAVE_UTIME_H) |
| 3713 | /* XXX should define struct utimbuf instead, above */ |
| 3714 | struct utimbuf buf; |
| 3715 | buf.actime = atime; |
| 3716 | buf.modtime = mtime; |
| 3717 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3718 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3719 | time_t buf[2]; |
| 3720 | buf[0] = atime; |
| 3721 | buf[1] = mtime; |
| 3722 | res = utime(path, buf); |
| 3723 | #endif |
| 3724 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3725 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3726 | } |
| 3727 | if (res < 0) { |
| 3728 | return posix_error_with_allocated_filename(opath); |
| 3729 | } |
| 3730 | Py_DECREF(opath); |
| 3731 | Py_INCREF(Py_None); |
| 3732 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3733 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3734 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3737 | #ifdef HAVE_FUTIMES |
| 3738 | PyDoc_STRVAR(posix_futimes__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3739 | "futimes(fd[, (atime, mtime)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3740 | Set the access and modified time of the file specified by the file\n\ |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3741 | descriptor fd to the given values. If no second argument is used, set the\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3742 | access and modified times to the current time."); |
| 3743 | |
| 3744 | static PyObject * |
| 3745 | posix_futimes(PyObject *self, PyObject *args) |
| 3746 | { |
| 3747 | int res, fd; |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3748 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3749 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3750 | long ausec, musec; |
| 3751 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3752 | if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3753 | return NULL; |
| 3754 | |
| 3755 | if (arg == Py_None) { |
| 3756 | /* optional time values not given */ |
| 3757 | Py_BEGIN_ALLOW_THREADS |
| 3758 | res = futimes(fd, NULL); |
| 3759 | Py_END_ALLOW_THREADS |
| 3760 | } |
| 3761 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3762 | PyErr_SetString(PyExc_TypeError, |
| 3763 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3764 | return NULL; |
| 3765 | } |
| 3766 | else { |
| 3767 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3768 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3769 | return NULL; |
| 3770 | } |
| 3771 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3772 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3773 | return NULL; |
| 3774 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3775 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3776 | { |
| 3777 | #ifdef HAVE_FUTIMENS |
| 3778 | struct timespec buf[2]; |
| 3779 | buf[0].tv_sec = atime; |
| 3780 | buf[0].tv_nsec = ausec; |
| 3781 | buf[1].tv_sec = mtime; |
| 3782 | buf[1].tv_nsec = musec; |
| 3783 | res = futimens(fd, buf); |
| 3784 | #else |
| 3785 | struct timeval buf[2]; |
| 3786 | buf[0].tv_sec = atime; |
| 3787 | buf[0].tv_usec = ausec; |
| 3788 | buf[1].tv_sec = mtime; |
| 3789 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3790 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3791 | #endif |
| 3792 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3793 | Py_END_ALLOW_THREADS |
| 3794 | } |
| 3795 | if (res < 0) |
| 3796 | return posix_error(); |
| 3797 | Py_RETURN_NONE; |
| 3798 | } |
| 3799 | #endif |
| 3800 | |
| 3801 | #ifdef HAVE_LUTIMES |
| 3802 | PyDoc_STRVAR(posix_lutimes__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3803 | "lutimes(path[, (atime, mtime)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3804 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3805 | |
| 3806 | static PyObject * |
| 3807 | posix_lutimes(PyObject *self, PyObject *args) |
| 3808 | { |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3809 | PyObject *opath; |
| 3810 | PyObject *arg = Py_None; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3811 | const char *path; |
| 3812 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3813 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3814 | long ausec, musec; |
| 3815 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3816 | if (!PyArg_ParseTuple(args, "O&|O:lutimes", |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3817 | PyUnicode_FSConverter, &opath, &arg)) |
| 3818 | return NULL; |
| 3819 | path = PyBytes_AsString(opath); |
| 3820 | if (arg == Py_None) { |
| 3821 | /* optional time values not given */ |
| 3822 | Py_BEGIN_ALLOW_THREADS |
| 3823 | res = lutimes(path, NULL); |
| 3824 | Py_END_ALLOW_THREADS |
| 3825 | } |
| 3826 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3827 | PyErr_SetString(PyExc_TypeError, |
| 3828 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3829 | Py_DECREF(opath); |
| 3830 | return NULL; |
| 3831 | } |
| 3832 | else { |
| 3833 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3834 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3835 | Py_DECREF(opath); |
| 3836 | return NULL; |
| 3837 | } |
| 3838 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3839 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3840 | Py_DECREF(opath); |
| 3841 | return NULL; |
| 3842 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3843 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3844 | { |
| 3845 | #ifdef HAVE_UTIMENSAT |
| 3846 | struct timespec buf[2]; |
| 3847 | buf[0].tv_sec = atime; |
| 3848 | buf[0].tv_nsec = ausec; |
| 3849 | buf[1].tv_sec = mtime; |
| 3850 | buf[1].tv_nsec = musec; |
| 3851 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3852 | #else |
| 3853 | struct timeval buf[2]; |
| 3854 | buf[0].tv_sec = atime; |
| 3855 | buf[0].tv_usec = ausec; |
| 3856 | buf[1].tv_sec = mtime; |
| 3857 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3858 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3859 | #endif |
| 3860 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3861 | Py_END_ALLOW_THREADS |
| 3862 | } |
| 3863 | Py_DECREF(opath); |
| 3864 | if (res < 0) |
| 3865 | return posix_error(); |
| 3866 | Py_RETURN_NONE; |
| 3867 | } |
| 3868 | #endif |
| 3869 | |
| 3870 | #ifdef HAVE_FUTIMENS |
| 3871 | PyDoc_STRVAR(posix_futimens__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3872 | "futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3873 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3874 | nanosecond precision.\n\ |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3875 | If no second argument is given, set atime and mtime to the current time.\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3876 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3877 | current time.\n\ |
| 3878 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3879 | |
| 3880 | static PyObject * |
| 3881 | posix_futimens(PyObject *self, PyObject *args) |
| 3882 | { |
| 3883 | int res, fd; |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3884 | PyObject *atime = Py_None; |
| 3885 | PyObject *mtime = Py_None; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3886 | struct timespec buf[2]; |
| 3887 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3888 | if (!PyArg_ParseTuple(args, "i|OO:futimens", |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3889 | &fd, &atime, &mtime)) |
| 3890 | return NULL; |
| 3891 | if (atime == Py_None && mtime == Py_None) { |
| 3892 | /* optional time values not given */ |
| 3893 | Py_BEGIN_ALLOW_THREADS |
| 3894 | res = futimens(fd, NULL); |
| 3895 | Py_END_ALLOW_THREADS |
| 3896 | } |
| 3897 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3898 | PyErr_SetString(PyExc_TypeError, |
| 3899 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3900 | return NULL; |
| 3901 | } |
| 3902 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3903 | PyErr_SetString(PyExc_TypeError, |
| 3904 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3905 | return NULL; |
| 3906 | } |
| 3907 | else { |
| 3908 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3909 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3910 | return NULL; |
| 3911 | } |
| 3912 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3913 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3914 | return NULL; |
| 3915 | } |
| 3916 | Py_BEGIN_ALLOW_THREADS |
| 3917 | res = futimens(fd, buf); |
| 3918 | Py_END_ALLOW_THREADS |
| 3919 | } |
| 3920 | if (res < 0) |
| 3921 | return posix_error(); |
| 3922 | Py_RETURN_NONE; |
| 3923 | } |
| 3924 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3925 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3926 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3928 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3929 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3930 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3931 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3932 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3933 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3934 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3935 | int sts; |
| 3936 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3937 | return NULL; |
| 3938 | _exit(sts); |
| 3939 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3942 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3943 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3944 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3945 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3946 | Py_ssize_t i; |
| 3947 | for (i = 0; i < count; i++) |
| 3948 | PyMem_Free(array[i]); |
| 3949 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3950 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3951 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3952 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3953 | int fsconvert_strdup(PyObject *o, char**out) |
| 3954 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3955 | PyObject *bytes; |
| 3956 | Py_ssize_t size; |
| 3957 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3958 | return 0; |
| 3959 | size = PyBytes_GET_SIZE(bytes); |
| 3960 | *out = PyMem_Malloc(size+1); |
| 3961 | if (!*out) |
| 3962 | return 0; |
| 3963 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3964 | Py_DECREF(bytes); |
| 3965 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3966 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3967 | #endif |
| 3968 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3969 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3970 | static char** |
| 3971 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3972 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3973 | char **envlist; |
| 3974 | Py_ssize_t i, pos, envc; |
| 3975 | PyObject *keys=NULL, *vals=NULL; |
| 3976 | PyObject *key, *val, *key2, *val2; |
| 3977 | char *p, *k, *v; |
| 3978 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3980 | i = PyMapping_Size(env); |
| 3981 | if (i < 0) |
| 3982 | return NULL; |
| 3983 | envlist = PyMem_NEW(char *, i + 1); |
| 3984 | if (envlist == NULL) { |
| 3985 | PyErr_NoMemory(); |
| 3986 | return NULL; |
| 3987 | } |
| 3988 | envc = 0; |
| 3989 | keys = PyMapping_Keys(env); |
| 3990 | vals = PyMapping_Values(env); |
| 3991 | if (!keys || !vals) |
| 3992 | goto error; |
| 3993 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3994 | PyErr_Format(PyExc_TypeError, |
| 3995 | "env.keys() or env.values() is not a list"); |
| 3996 | goto error; |
| 3997 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3998 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3999 | for (pos = 0; pos < i; pos++) { |
| 4000 | key = PyList_GetItem(keys, pos); |
| 4001 | val = PyList_GetItem(vals, pos); |
| 4002 | if (!key || !val) |
| 4003 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4005 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 4006 | goto error; |
| 4007 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 4008 | Py_DECREF(key2); |
| 4009 | goto error; |
| 4010 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4011 | |
| 4012 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4013 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4014 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4015 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4016 | k = PyBytes_AsString(key2); |
| 4017 | v = PyBytes_AsString(val2); |
| 4018 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4019 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4020 | p = PyMem_NEW(char, len); |
| 4021 | if (p == NULL) { |
| 4022 | PyErr_NoMemory(); |
| 4023 | Py_DECREF(key2); |
| 4024 | Py_DECREF(val2); |
| 4025 | goto error; |
| 4026 | } |
| 4027 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4028 | envlist[envc++] = p; |
| 4029 | Py_DECREF(key2); |
| 4030 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4031 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4032 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4033 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4034 | } |
| 4035 | Py_DECREF(vals); |
| 4036 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4038 | envlist[envc] = 0; |
| 4039 | *envc_ptr = envc; |
| 4040 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4041 | |
| 4042 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4043 | Py_XDECREF(keys); |
| 4044 | Py_XDECREF(vals); |
| 4045 | while (--envc >= 0) |
| 4046 | PyMem_DEL(envlist[envc]); |
| 4047 | PyMem_DEL(envlist); |
| 4048 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4049 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4050 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4051 | static char** |
| 4052 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4053 | { |
| 4054 | int i; |
| 4055 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4056 | if (argvlist == NULL) { |
| 4057 | PyErr_NoMemory(); |
| 4058 | return NULL; |
| 4059 | } |
| 4060 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4061 | PyObject* item = PySequence_ITEM(argv, i); |
| 4062 | if (item == NULL) |
| 4063 | goto fail; |
| 4064 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4065 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4066 | goto fail; |
| 4067 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4068 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4069 | } |
| 4070 | argvlist[*argc] = NULL; |
| 4071 | return argvlist; |
| 4072 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4073 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4074 | free_string_array(argvlist, *argc); |
| 4075 | return NULL; |
| 4076 | } |
| 4077 | #endif |
| 4078 | |
| 4079 | #ifdef HAVE_EXECV |
| 4080 | PyDoc_STRVAR(posix_execv__doc__, |
| 4081 | "execv(path, args)\n\n\ |
| 4082 | Execute an executable path with arguments, replacing current process.\n\ |
| 4083 | \n\ |
| 4084 | path: path of executable file\n\ |
| 4085 | args: tuple or list of strings"); |
| 4086 | |
| 4087 | static PyObject * |
| 4088 | posix_execv(PyObject *self, PyObject *args) |
| 4089 | { |
| 4090 | PyObject *opath; |
| 4091 | char *path; |
| 4092 | PyObject *argv; |
| 4093 | char **argvlist; |
| 4094 | Py_ssize_t argc; |
| 4095 | |
| 4096 | /* execv has two arguments: (path, argv), where |
| 4097 | argv is a list or tuple of strings. */ |
| 4098 | |
| 4099 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4100 | PyUnicode_FSConverter, |
| 4101 | &opath, &argv)) |
| 4102 | return NULL; |
| 4103 | path = PyBytes_AsString(opath); |
| 4104 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4105 | PyErr_SetString(PyExc_TypeError, |
| 4106 | "execv() arg 2 must be a tuple or list"); |
| 4107 | Py_DECREF(opath); |
| 4108 | return NULL; |
| 4109 | } |
| 4110 | argc = PySequence_Size(argv); |
| 4111 | if (argc < 1) { |
| 4112 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4113 | Py_DECREF(opath); |
| 4114 | return NULL; |
| 4115 | } |
| 4116 | |
| 4117 | argvlist = parse_arglist(argv, &argc); |
| 4118 | if (argvlist == NULL) { |
| 4119 | Py_DECREF(opath); |
| 4120 | return NULL; |
| 4121 | } |
| 4122 | |
| 4123 | execv(path, argvlist); |
| 4124 | |
| 4125 | /* If we get here it's definitely an error */ |
| 4126 | |
| 4127 | free_string_array(argvlist, argc); |
| 4128 | Py_DECREF(opath); |
| 4129 | return posix_error(); |
| 4130 | } |
| 4131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4132 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4133 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4134 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4135 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4136 | path: path of executable file\n\ |
| 4137 | args: tuple or list of arguments\n\ |
| 4138 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4140 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4141 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4143 | PyObject *opath; |
| 4144 | char *path; |
| 4145 | PyObject *argv, *env; |
| 4146 | char **argvlist; |
| 4147 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4148 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4149 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4150 | /* execve has three arguments: (path, argv, env), where |
| 4151 | argv is a list or tuple of strings and env is a dictionary |
| 4152 | like posix.environ. */ |
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 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4155 | PyUnicode_FSConverter, |
| 4156 | &opath, &argv, &env)) |
| 4157 | return NULL; |
| 4158 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4159 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4160 | PyErr_SetString(PyExc_TypeError, |
| 4161 | "execve() arg 2 must be a tuple or list"); |
| 4162 | goto fail_0; |
| 4163 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4164 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4165 | if (!PyMapping_Check(env)) { |
| 4166 | PyErr_SetString(PyExc_TypeError, |
| 4167 | "execve() arg 3 must be a mapping object"); |
| 4168 | goto fail_0; |
| 4169 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4170 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4171 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4172 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | goto fail_0; |
| 4174 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4176 | envlist = parse_envlist(env, &envc); |
| 4177 | if (envlist == NULL) |
| 4178 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4179 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4182 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4184 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4185 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4186 | while (--envc >= 0) |
| 4187 | PyMem_DEL(envlist[envc]); |
| 4188 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4189 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4190 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4191 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4192 | Py_DECREF(opath); |
| 4193 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4194 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4195 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4196 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4197 | #ifdef HAVE_FEXECVE |
| 4198 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4199 | "fexecve(fd, args, env)\n\n\ |
| 4200 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4201 | environment, replacing the current process.\n\ |
| 4202 | \n\ |
| 4203 | fd: file descriptor of executable\n\ |
| 4204 | args: tuple or list of arguments\n\ |
| 4205 | env: dictionary of strings mapping to strings"); |
| 4206 | |
| 4207 | static PyObject * |
| 4208 | posix_fexecve(PyObject *self, PyObject *args) |
| 4209 | { |
| 4210 | int fd; |
| 4211 | PyObject *argv, *env; |
| 4212 | char **argvlist; |
| 4213 | char **envlist; |
| 4214 | Py_ssize_t argc, envc; |
| 4215 | |
| 4216 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4217 | &fd, &argv, &env)) |
| 4218 | return NULL; |
| 4219 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4220 | PyErr_SetString(PyExc_TypeError, |
| 4221 | "fexecve() arg 2 must be a tuple or list"); |
| 4222 | return NULL; |
| 4223 | } |
| 4224 | argc = PySequence_Size(argv); |
| 4225 | if (!PyMapping_Check(env)) { |
| 4226 | PyErr_SetString(PyExc_TypeError, |
| 4227 | "fexecve() arg 3 must be a mapping object"); |
| 4228 | return NULL; |
| 4229 | } |
| 4230 | |
| 4231 | argvlist = parse_arglist(argv, &argc); |
| 4232 | if (argvlist == NULL) |
| 4233 | return NULL; |
| 4234 | |
| 4235 | envlist = parse_envlist(env, &envc); |
| 4236 | if (envlist == NULL) |
| 4237 | goto fail; |
| 4238 | |
| 4239 | fexecve(fd, argvlist, envlist); |
| 4240 | |
| 4241 | /* If we get here it's definitely an error */ |
| 4242 | |
| 4243 | (void) posix_error(); |
| 4244 | |
| 4245 | while (--envc >= 0) |
| 4246 | PyMem_DEL(envlist[envc]); |
| 4247 | PyMem_DEL(envlist); |
| 4248 | fail: |
| 4249 | free_string_array(argvlist, argc); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4253 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4254 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4255 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4256 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4257 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4258 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4259 | mode: mode of process creation\n\ |
| 4260 | path: path of executable file\n\ |
| 4261 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4262 | |
| 4263 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4264 | posix_spawnv(PyObject *self, PyObject *args) |
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 | PyObject *opath; |
| 4267 | char *path; |
| 4268 | PyObject *argv; |
| 4269 | char **argvlist; |
| 4270 | int mode, i; |
| 4271 | Py_ssize_t argc; |
| 4272 | Py_intptr_t spawnval; |
| 4273 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4275 | /* spawnv has three arguments: (mode, path, argv), where |
| 4276 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4278 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4279 | PyUnicode_FSConverter, |
| 4280 | &opath, &argv)) |
| 4281 | return NULL; |
| 4282 | path = PyBytes_AsString(opath); |
| 4283 | if (PyList_Check(argv)) { |
| 4284 | argc = PyList_Size(argv); |
| 4285 | getitem = PyList_GetItem; |
| 4286 | } |
| 4287 | else if (PyTuple_Check(argv)) { |
| 4288 | argc = PyTuple_Size(argv); |
| 4289 | getitem = PyTuple_GetItem; |
| 4290 | } |
| 4291 | else { |
| 4292 | PyErr_SetString(PyExc_TypeError, |
| 4293 | "spawnv() arg 2 must be a tuple or list"); |
| 4294 | Py_DECREF(opath); |
| 4295 | return NULL; |
| 4296 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4298 | argvlist = PyMem_NEW(char *, argc+1); |
| 4299 | if (argvlist == NULL) { |
| 4300 | Py_DECREF(opath); |
| 4301 | return PyErr_NoMemory(); |
| 4302 | } |
| 4303 | for (i = 0; i < argc; i++) { |
| 4304 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4305 | &argvlist[i])) { |
| 4306 | free_string_array(argvlist, i); |
| 4307 | PyErr_SetString( |
| 4308 | PyExc_TypeError, |
| 4309 | "spawnv() arg 2 must contain only strings"); |
| 4310 | Py_DECREF(opath); |
| 4311 | return NULL; |
| 4312 | } |
| 4313 | } |
| 4314 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4315 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4316 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4317 | Py_BEGIN_ALLOW_THREADS |
| 4318 | spawnval = spawnv(mode, path, argvlist); |
| 4319 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4320 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4321 | if (mode == _OLD_P_OVERLAY) |
| 4322 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4324 | Py_BEGIN_ALLOW_THREADS |
| 4325 | spawnval = _spawnv(mode, path, argvlist); |
| 4326 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4327 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4328 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4329 | free_string_array(argvlist, argc); |
| 4330 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4331 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4332 | if (spawnval == -1) |
| 4333 | return posix_error(); |
| 4334 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4335 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4336 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4337 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4338 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4339 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4343 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4344 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4345 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4346 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4347 | mode: mode of process creation\n\ |
| 4348 | path: path of executable file\n\ |
| 4349 | args: tuple or list of arguments\n\ |
| 4350 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4351 | |
| 4352 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4353 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4355 | PyObject *opath; |
| 4356 | char *path; |
| 4357 | PyObject *argv, *env; |
| 4358 | char **argvlist; |
| 4359 | char **envlist; |
| 4360 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4361 | int mode; |
| 4362 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4363 | Py_intptr_t spawnval; |
| 4364 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4365 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4367 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4368 | argv is a list or tuple of strings and env is a dictionary |
| 4369 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4370 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4371 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4372 | PyUnicode_FSConverter, |
| 4373 | &opath, &argv, &env)) |
| 4374 | return NULL; |
| 4375 | path = PyBytes_AsString(opath); |
| 4376 | if (PyList_Check(argv)) { |
| 4377 | argc = PyList_Size(argv); |
| 4378 | getitem = PyList_GetItem; |
| 4379 | } |
| 4380 | else if (PyTuple_Check(argv)) { |
| 4381 | argc = PyTuple_Size(argv); |
| 4382 | getitem = PyTuple_GetItem; |
| 4383 | } |
| 4384 | else { |
| 4385 | PyErr_SetString(PyExc_TypeError, |
| 4386 | "spawnve() arg 2 must be a tuple or list"); |
| 4387 | goto fail_0; |
| 4388 | } |
| 4389 | if (!PyMapping_Check(env)) { |
| 4390 | PyErr_SetString(PyExc_TypeError, |
| 4391 | "spawnve() arg 3 must be a mapping object"); |
| 4392 | goto fail_0; |
| 4393 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4395 | argvlist = PyMem_NEW(char *, argc+1); |
| 4396 | if (argvlist == NULL) { |
| 4397 | PyErr_NoMemory(); |
| 4398 | goto fail_0; |
| 4399 | } |
| 4400 | for (i = 0; i < argc; i++) { |
| 4401 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4402 | &argvlist[i])) |
| 4403 | { |
| 4404 | lastarg = i; |
| 4405 | goto fail_1; |
| 4406 | } |
| 4407 | } |
| 4408 | lastarg = argc; |
| 4409 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4410 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4411 | envlist = parse_envlist(env, &envc); |
| 4412 | if (envlist == NULL) |
| 4413 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4414 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4415 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4416 | Py_BEGIN_ALLOW_THREADS |
| 4417 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4418 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4419 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4420 | if (mode == _OLD_P_OVERLAY) |
| 4421 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4423 | Py_BEGIN_ALLOW_THREADS |
| 4424 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4425 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4426 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4427 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4428 | if (spawnval == -1) |
| 4429 | (void) posix_error(); |
| 4430 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4431 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4432 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4433 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4434 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4435 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4437 | while (--envc >= 0) |
| 4438 | PyMem_DEL(envlist[envc]); |
| 4439 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4440 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4441 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4442 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4443 | Py_DECREF(opath); |
| 4444 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4445 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4446 | |
| 4447 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4448 | #if defined(PYOS_OS2) |
| 4449 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4450 | "spawnvp(mode, file, args)\n\n\ |
| 4451 | Execute the program 'file' in a new process, using the environment\n\ |
| 4452 | search path to find the file.\n\ |
| 4453 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4454 | mode: mode of process creation\n\ |
| 4455 | file: executable file name\n\ |
| 4456 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4457 | |
| 4458 | static PyObject * |
| 4459 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4460 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4461 | PyObject *opath; |
| 4462 | char *path; |
| 4463 | PyObject *argv; |
| 4464 | char **argvlist; |
| 4465 | int mode, i, argc; |
| 4466 | Py_intptr_t spawnval; |
| 4467 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4468 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4469 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4470 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4471 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4472 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4473 | PyUnicode_FSConverter, |
| 4474 | &opath, &argv)) |
| 4475 | return NULL; |
| 4476 | path = PyBytes_AsString(opath); |
| 4477 | if (PyList_Check(argv)) { |
| 4478 | argc = PyList_Size(argv); |
| 4479 | getitem = PyList_GetItem; |
| 4480 | } |
| 4481 | else if (PyTuple_Check(argv)) { |
| 4482 | argc = PyTuple_Size(argv); |
| 4483 | getitem = PyTuple_GetItem; |
| 4484 | } |
| 4485 | else { |
| 4486 | PyErr_SetString(PyExc_TypeError, |
| 4487 | "spawnvp() arg 2 must be a tuple or list"); |
| 4488 | Py_DECREF(opath); |
| 4489 | return NULL; |
| 4490 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4492 | argvlist = PyMem_NEW(char *, argc+1); |
| 4493 | if (argvlist == NULL) { |
| 4494 | Py_DECREF(opath); |
| 4495 | return PyErr_NoMemory(); |
| 4496 | } |
| 4497 | for (i = 0; i < argc; i++) { |
| 4498 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4499 | &argvlist[i])) { |
| 4500 | free_string_array(argvlist, i); |
| 4501 | PyErr_SetString( |
| 4502 | PyExc_TypeError, |
| 4503 | "spawnvp() arg 2 must contain only strings"); |
| 4504 | Py_DECREF(opath); |
| 4505 | return NULL; |
| 4506 | } |
| 4507 | } |
| 4508 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4509 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4510 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4511 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4512 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4513 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4514 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4515 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4516 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4518 | free_string_array(argvlist, argc); |
| 4519 | Py_DECREF(opath); |
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 | if (spawnval == -1) |
| 4522 | return posix_error(); |
| 4523 | else |
| 4524 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | |
| 4528 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4529 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4530 | Execute the program 'file' in a new process, using the environment\n\ |
| 4531 | search path to find the file.\n\ |
| 4532 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4533 | mode: mode of process creation\n\ |
| 4534 | file: executable file name\n\ |
| 4535 | args: tuple or list of arguments\n\ |
| 4536 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4537 | |
| 4538 | static PyObject * |
| 4539 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4540 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 4541 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4542 | char *path; |
| 4543 | PyObject *argv, *env; |
| 4544 | char **argvlist; |
| 4545 | char **envlist; |
| 4546 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4547 | int mode; |
| 4548 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4549 | Py_intptr_t spawnval; |
| 4550 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4551 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4552 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4553 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4554 | argv is a list or tuple of strings and env is a dictionary |
| 4555 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4557 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4558 | PyUnicode_FSConverter, |
| 4559 | &opath, &argv, &env)) |
| 4560 | return NULL; |
| 4561 | path = PyBytes_AsString(opath); |
| 4562 | if (PyList_Check(argv)) { |
| 4563 | argc = PyList_Size(argv); |
| 4564 | getitem = PyList_GetItem; |
| 4565 | } |
| 4566 | else if (PyTuple_Check(argv)) { |
| 4567 | argc = PyTuple_Size(argv); |
| 4568 | getitem = PyTuple_GetItem; |
| 4569 | } |
| 4570 | else { |
| 4571 | PyErr_SetString(PyExc_TypeError, |
| 4572 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4573 | goto fail_0; |
| 4574 | } |
| 4575 | if (!PyMapping_Check(env)) { |
| 4576 | PyErr_SetString(PyExc_TypeError, |
| 4577 | "spawnvpe() arg 3 must be a mapping object"); |
| 4578 | goto fail_0; |
| 4579 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4581 | argvlist = PyMem_NEW(char *, argc+1); |
| 4582 | if (argvlist == NULL) { |
| 4583 | PyErr_NoMemory(); |
| 4584 | goto fail_0; |
| 4585 | } |
| 4586 | for (i = 0; i < argc; i++) { |
| 4587 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4588 | &argvlist[i])) |
| 4589 | { |
| 4590 | lastarg = i; |
| 4591 | goto fail_1; |
| 4592 | } |
| 4593 | } |
| 4594 | lastarg = argc; |
| 4595 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4596 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4597 | envlist = parse_envlist(env, &envc); |
| 4598 | if (envlist == NULL) |
| 4599 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4601 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4602 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4603 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4604 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4605 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4606 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4607 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4608 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4609 | if (spawnval == -1) |
| 4610 | (void) posix_error(); |
| 4611 | else |
| 4612 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4613 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4614 | while (--envc >= 0) |
| 4615 | PyMem_DEL(envlist[envc]); |
| 4616 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4617 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4618 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4619 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4620 | Py_DECREF(opath); |
| 4621 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4622 | } |
| 4623 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4624 | #endif /* HAVE_SPAWNV */ |
| 4625 | |
| 4626 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4627 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4628 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4629 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4630 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4631 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4632 | 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] | 4633 | |
| 4634 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4635 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4636 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4637 | pid_t pid; |
| 4638 | int result = 0; |
| 4639 | _PyImport_AcquireLock(); |
| 4640 | pid = fork1(); |
| 4641 | if (pid == 0) { |
| 4642 | /* child: this clobbers and resets the import lock. */ |
| 4643 | PyOS_AfterFork(); |
| 4644 | } else { |
| 4645 | /* parent: release the import lock. */ |
| 4646 | result = _PyImport_ReleaseLock(); |
| 4647 | } |
| 4648 | if (pid == -1) |
| 4649 | return posix_error(); |
| 4650 | if (result < 0) { |
| 4651 | /* Don't clobber the OSError if the fork failed. */ |
| 4652 | PyErr_SetString(PyExc_RuntimeError, |
| 4653 | "not holding the import lock"); |
| 4654 | return NULL; |
| 4655 | } |
| 4656 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4657 | } |
| 4658 | #endif |
| 4659 | |
| 4660 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4661 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4662 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4663 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4664 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4665 | 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] | 4666 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4667 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4668 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4669 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4670 | pid_t pid; |
| 4671 | int result = 0; |
| 4672 | _PyImport_AcquireLock(); |
| 4673 | pid = fork(); |
| 4674 | if (pid == 0) { |
| 4675 | /* child: this clobbers and resets the import lock. */ |
| 4676 | PyOS_AfterFork(); |
| 4677 | } else { |
| 4678 | /* parent: release the import lock. */ |
| 4679 | result = _PyImport_ReleaseLock(); |
| 4680 | } |
| 4681 | if (pid == -1) |
| 4682 | return posix_error(); |
| 4683 | if (result < 0) { |
| 4684 | /* Don't clobber the OSError if the fork failed. */ |
| 4685 | PyErr_SetString(PyExc_RuntimeError, |
| 4686 | "not holding the import lock"); |
| 4687 | return NULL; |
| 4688 | } |
| 4689 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4690 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4691 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4692 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4693 | #ifdef HAVE_SCHED_H |
| 4694 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4695 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4696 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4697 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4698 | "sched_get_priority_max(policy)\n\n\ |
| 4699 | Get the maximum scheduling priority for *policy*."); |
| 4700 | |
| 4701 | static PyObject * |
| 4702 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4703 | { |
| 4704 | int policy, max; |
| 4705 | |
| 4706 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4707 | return NULL; |
| 4708 | max = sched_get_priority_max(policy); |
| 4709 | if (max < 0) |
| 4710 | return posix_error(); |
| 4711 | return PyLong_FromLong(max); |
| 4712 | } |
| 4713 | |
| 4714 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4715 | "sched_get_priority_min(policy)\n\n\ |
| 4716 | Get the minimum scheduling priority for *policy*."); |
| 4717 | |
| 4718 | static PyObject * |
| 4719 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4720 | { |
| 4721 | int policy, min; |
| 4722 | |
| 4723 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4724 | return NULL; |
| 4725 | min = sched_get_priority_min(policy); |
| 4726 | if (min < 0) |
| 4727 | return posix_error(); |
| 4728 | return PyLong_FromLong(min); |
| 4729 | } |
| 4730 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4731 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4732 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4733 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4734 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4735 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4736 | "sched_getscheduler(pid)\n\n\ |
| 4737 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4738 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4739 | |
| 4740 | static PyObject * |
| 4741 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4742 | { |
| 4743 | pid_t pid; |
| 4744 | int policy; |
| 4745 | |
| 4746 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4747 | return NULL; |
| 4748 | policy = sched_getscheduler(pid); |
| 4749 | if (policy < 0) |
| 4750 | return posix_error(); |
| 4751 | return PyLong_FromLong(policy); |
| 4752 | } |
| 4753 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4754 | #endif |
| 4755 | |
| 4756 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4757 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4758 | static PyObject * |
| 4759 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4760 | { |
| 4761 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4762 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4763 | |
| 4764 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4765 | return NULL; |
| 4766 | res = PyStructSequence_New(type); |
| 4767 | if (!res) |
| 4768 | return NULL; |
| 4769 | Py_INCREF(priority); |
| 4770 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4771 | return res; |
| 4772 | } |
| 4773 | |
| 4774 | PyDoc_STRVAR(sched_param__doc__, |
| 4775 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4776 | Current has only one field: sched_priority"); |
| 4777 | |
| 4778 | static PyStructSequence_Field sched_param_fields[] = { |
| 4779 | {"sched_priority", "the scheduling priority"}, |
| 4780 | {0} |
| 4781 | }; |
| 4782 | |
| 4783 | static PyStructSequence_Desc sched_param_desc = { |
| 4784 | "sched_param", /* name */ |
| 4785 | sched_param__doc__, /* doc */ |
| 4786 | sched_param_fields, |
| 4787 | 1 |
| 4788 | }; |
| 4789 | |
| 4790 | static int |
| 4791 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4792 | { |
| 4793 | long priority; |
| 4794 | |
| 4795 | if (Py_TYPE(param) != &SchedParamType) { |
| 4796 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4797 | return 0; |
| 4798 | } |
| 4799 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4800 | if (priority == -1 && PyErr_Occurred()) |
| 4801 | return 0; |
| 4802 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4803 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4804 | return 0; |
| 4805 | } |
| 4806 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4807 | return 1; |
| 4808 | } |
| 4809 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4810 | #endif |
| 4811 | |
| 4812 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4813 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4814 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4815 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4816 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4817 | If *pid* is 0, the calling process is changed.\n\ |
| 4818 | *param* is an instance of sched_param."); |
| 4819 | |
| 4820 | static PyObject * |
| 4821 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4822 | { |
| 4823 | pid_t pid; |
| 4824 | int policy; |
| 4825 | struct sched_param param; |
| 4826 | |
| 4827 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4828 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4829 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4830 | |
| 4831 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4832 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4833 | ** scheduling policy under Solaris/Illumos, and others. |
| 4834 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4835 | */ |
| 4836 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4837 | return posix_error(); |
| 4838 | Py_RETURN_NONE; |
| 4839 | } |
| 4840 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4841 | #endif |
| 4842 | |
| 4843 | #ifdef HAVE_SCHED_SETPARAM |
| 4844 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4845 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4846 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4847 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4848 | sched_param class. A PID of 0 means the calling process."); |
| 4849 | |
| 4850 | static PyObject * |
| 4851 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4852 | { |
| 4853 | pid_t pid; |
| 4854 | struct sched_param param; |
| 4855 | PyObject *res, *priority; |
| 4856 | |
| 4857 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4858 | return NULL; |
| 4859 | if (sched_getparam(pid, ¶m)) |
| 4860 | return posix_error(); |
| 4861 | res = PyStructSequence_New(&SchedParamType); |
| 4862 | if (!res) |
| 4863 | return NULL; |
| 4864 | priority = PyLong_FromLong(param.sched_priority); |
| 4865 | if (!priority) { |
| 4866 | Py_DECREF(res); |
| 4867 | return NULL; |
| 4868 | } |
| 4869 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4870 | return res; |
| 4871 | } |
| 4872 | |
| 4873 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4874 | "sched_setparam(pid, param)\n\n\ |
| 4875 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4876 | A PID of 0 means the calling process."); |
| 4877 | |
| 4878 | static PyObject * |
| 4879 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4880 | { |
| 4881 | pid_t pid; |
| 4882 | struct sched_param param; |
| 4883 | |
| 4884 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4885 | &pid, &convert_sched_param, ¶m)) |
| 4886 | return NULL; |
| 4887 | if (sched_setparam(pid, ¶m)) |
| 4888 | return posix_error(); |
| 4889 | Py_RETURN_NONE; |
| 4890 | } |
| 4891 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4892 | #endif |
| 4893 | |
| 4894 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4895 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4896 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4897 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4898 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4899 | |
| 4900 | static PyObject * |
| 4901 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4902 | { |
| 4903 | pid_t pid; |
| 4904 | struct timespec interval; |
| 4905 | |
| 4906 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4907 | return NULL; |
| 4908 | if (sched_rr_get_interval(pid, &interval)) |
| 4909 | return posix_error(); |
| 4910 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4911 | } |
| 4912 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4913 | #endif |
| 4914 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4915 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4916 | "sched_yield()\n\n\ |
| 4917 | Voluntarily relinquish the CPU."); |
| 4918 | |
| 4919 | static PyObject * |
| 4920 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4921 | { |
| 4922 | if (sched_yield()) |
| 4923 | return posix_error(); |
| 4924 | Py_RETURN_NONE; |
| 4925 | } |
| 4926 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4927 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4928 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4929 | typedef struct { |
| 4930 | PyObject_HEAD; |
| 4931 | Py_ssize_t size; |
| 4932 | int ncpus; |
| 4933 | cpu_set_t *set; |
| 4934 | } Py_cpu_set; |
| 4935 | |
| 4936 | static PyTypeObject cpu_set_type; |
| 4937 | |
| 4938 | static void |
| 4939 | cpu_set_dealloc(Py_cpu_set *set) |
| 4940 | { |
| 4941 | assert(set->set); |
| 4942 | CPU_FREE(set->set); |
| 4943 | Py_TYPE(set)->tp_free(set); |
| 4944 | } |
| 4945 | |
| 4946 | static Py_cpu_set * |
| 4947 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4948 | { |
| 4949 | Py_cpu_set *set; |
| 4950 | |
| 4951 | if (size < 0) { |
| 4952 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4953 | return NULL; |
| 4954 | } |
| 4955 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4956 | if (!set) |
| 4957 | return NULL; |
| 4958 | set->ncpus = size; |
| 4959 | set->size = CPU_ALLOC_SIZE(size); |
| 4960 | set->set = CPU_ALLOC(size); |
| 4961 | if (!set->set) { |
| 4962 | type->tp_free(set); |
| 4963 | PyErr_NoMemory(); |
| 4964 | return NULL; |
| 4965 | } |
| 4966 | CPU_ZERO_S(set->size, set->set); |
| 4967 | return set; |
| 4968 | } |
| 4969 | |
| 4970 | static PyObject * |
| 4971 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4972 | { |
| 4973 | int size; |
| 4974 | |
| 4975 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4976 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4977 | return NULL; |
| 4978 | return (PyObject *)make_new_cpu_set(type, size); |
| 4979 | } |
| 4980 | |
| 4981 | static PyObject * |
| 4982 | cpu_set_repr(Py_cpu_set *set) |
| 4983 | { |
| 4984 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4985 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4986 | |
| 4987 | static Py_ssize_t |
| 4988 | cpu_set_len(Py_cpu_set *set) |
| 4989 | { |
| 4990 | return set->ncpus; |
| 4991 | } |
| 4992 | |
| 4993 | static int |
| 4994 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4995 | { |
| 4996 | int cpu; |
| 4997 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4998 | return -1; |
| 4999 | if (cpu < 0) { |
| 5000 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 5001 | return -1; |
| 5002 | } |
| 5003 | if (cpu >= set->ncpus) { |
| 5004 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 5005 | return -1; |
| 5006 | } |
| 5007 | return cpu; |
| 5008 | } |
| 5009 | |
| 5010 | PyDoc_STRVAR(cpu_set_set_doc, |
| 5011 | "cpu_set.set(i)\n\n\ |
| 5012 | Add CPU *i* to the set."); |
| 5013 | |
| 5014 | static PyObject * |
| 5015 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 5016 | { |
| 5017 | int cpu = _get_cpu(set, "i|set", args); |
| 5018 | if (cpu == -1) |
| 5019 | return NULL; |
| 5020 | CPU_SET_S(cpu, set->size, set->set); |
| 5021 | Py_RETURN_NONE; |
| 5022 | } |
| 5023 | |
| 5024 | PyDoc_STRVAR(cpu_set_count_doc, |
| 5025 | "cpu_set.count() -> int\n\n\ |
| 5026 | Return the number of CPUs active in the set."); |
| 5027 | |
| 5028 | static PyObject * |
| 5029 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5030 | { |
| 5031 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5032 | } |
| 5033 | |
| 5034 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5035 | "cpu_set.clear(i)\n\n\ |
| 5036 | Remove CPU *i* from the set."); |
| 5037 | |
| 5038 | static PyObject * |
| 5039 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5040 | { |
| 5041 | int cpu = _get_cpu(set, "i|clear", args); |
| 5042 | if (cpu == -1) |
| 5043 | return NULL; |
| 5044 | CPU_CLR_S(cpu, set->size, set->set); |
| 5045 | Py_RETURN_NONE; |
| 5046 | } |
| 5047 | |
| 5048 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5049 | "cpu_set.isset(i) -> bool\n\n\ |
| 5050 | Test if CPU *i* is in the set."); |
| 5051 | |
| 5052 | static PyObject * |
| 5053 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5054 | { |
| 5055 | int cpu = _get_cpu(set, "i|isset", args); |
| 5056 | if (cpu == -1) |
| 5057 | return NULL; |
| 5058 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5059 | Py_RETURN_TRUE; |
| 5060 | Py_RETURN_FALSE; |
| 5061 | } |
| 5062 | |
| 5063 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5064 | "cpu_set.zero()\n\n\ |
| 5065 | Clear the cpu_set."); |
| 5066 | |
| 5067 | static PyObject * |
| 5068 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5069 | { |
| 5070 | CPU_ZERO_S(set->size, set->set); |
| 5071 | Py_RETURN_NONE; |
| 5072 | } |
| 5073 | |
| 5074 | static PyObject * |
| 5075 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5076 | { |
| 5077 | int eq; |
| 5078 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5079 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5080 | Py_RETURN_NOTIMPLEMENTED; |
| 5081 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5082 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5083 | if ((op == Py_EQ) ? eq : !eq) |
| 5084 | Py_RETURN_TRUE; |
| 5085 | else |
| 5086 | Py_RETURN_FALSE; |
| 5087 | } |
| 5088 | |
| 5089 | #define CPU_SET_BINOP(name, op) \ |
| 5090 | static PyObject * \ |
| 5091 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5092 | if (res) { \ |
| 5093 | Py_INCREF(res); \ |
| 5094 | } \ |
| 5095 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5096 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5097 | if (!res) \ |
| 5098 | return NULL; \ |
| 5099 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5100 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5101 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5102 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5103 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5104 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5105 | op(res->size, res->set, left->set, right->set); \ |
| 5106 | return (PyObject *)res; \ |
| 5107 | } \ |
| 5108 | static PyObject * \ |
| 5109 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5110 | return do_cpu_set_##name(left, right, NULL); \ |
| 5111 | } \ |
| 5112 | static PyObject * \ |
| 5113 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5114 | return do_cpu_set_##name(left, right, left); \ |
| 5115 | } \ |
| 5116 | |
| 5117 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5118 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5119 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5120 | #undef CPU_SET_BINOP |
| 5121 | |
| 5122 | PyDoc_STRVAR(cpu_set_doc, |
| 5123 | "cpu_set(size)\n\n\ |
| 5124 | Create an empty mask of CPUs."); |
| 5125 | |
| 5126 | static PyNumberMethods cpu_set_as_number = { |
| 5127 | 0, /*nb_add*/ |
| 5128 | 0, /*nb_subtract*/ |
| 5129 | 0, /*nb_multiply*/ |
| 5130 | 0, /*nb_remainder*/ |
| 5131 | 0, /*nb_divmod*/ |
| 5132 | 0, /*nb_power*/ |
| 5133 | 0, /*nb_negative*/ |
| 5134 | 0, /*nb_positive*/ |
| 5135 | 0, /*nb_absolute*/ |
| 5136 | 0, /*nb_bool*/ |
| 5137 | 0, /*nb_invert*/ |
| 5138 | 0, /*nb_lshift*/ |
| 5139 | 0, /*nb_rshift*/ |
| 5140 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5141 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5142 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5143 | 0, /*nb_int*/ |
| 5144 | 0, /*nb_reserved*/ |
| 5145 | 0, /*nb_float*/ |
| 5146 | 0, /*nb_inplace_add*/ |
| 5147 | 0, /*nb_inplace_subtract*/ |
| 5148 | 0, /*nb_inplace_multiply*/ |
| 5149 | 0, /*nb_inplace_remainder*/ |
| 5150 | 0, /*nb_inplace_power*/ |
| 5151 | 0, /*nb_inplace_lshift*/ |
| 5152 | 0, /*nb_inplace_rshift*/ |
| 5153 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5154 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5155 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5156 | }; |
| 5157 | |
| 5158 | static PySequenceMethods cpu_set_as_sequence = { |
| 5159 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5160 | }; |
| 5161 | |
| 5162 | static PyMethodDef cpu_set_methods[] = { |
| 5163 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5164 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5165 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5166 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5167 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5168 | {NULL, NULL} /* sentinel */ |
| 5169 | }; |
| 5170 | |
| 5171 | static PyTypeObject cpu_set_type = { |
| 5172 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5173 | "posix.cpu_set", /* tp_name */ |
| 5174 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5175 | 0, /* tp_itemsize */ |
| 5176 | /* methods */ |
| 5177 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5178 | 0, /* tp_print */ |
| 5179 | 0, /* tp_getattr */ |
| 5180 | 0, /* tp_setattr */ |
| 5181 | 0, /* tp_reserved */ |
| 5182 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5183 | &cpu_set_as_number, /* tp_as_number */ |
| 5184 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5185 | 0, /* tp_as_mapping */ |
| 5186 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5187 | 0, /* tp_call */ |
| 5188 | 0, /* tp_str */ |
| 5189 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5190 | 0, /* tp_setattro */ |
| 5191 | 0, /* tp_as_buffer */ |
| 5192 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5193 | cpu_set_doc, /* tp_doc */ |
| 5194 | 0, /* tp_traverse */ |
| 5195 | 0, /* tp_clear */ |
| 5196 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5197 | 0, /* tp_weaklistoffset */ |
| 5198 | 0, /* tp_iter */ |
| 5199 | 0, /* tp_iternext */ |
| 5200 | cpu_set_methods, /* tp_methods */ |
| 5201 | 0, /* tp_members */ |
| 5202 | 0, /* tp_getset */ |
| 5203 | 0, /* tp_base */ |
| 5204 | 0, /* tp_dict */ |
| 5205 | 0, /* tp_descr_get */ |
| 5206 | 0, /* tp_descr_set */ |
| 5207 | 0, /* tp_dictoffset */ |
| 5208 | 0, /* tp_init */ |
| 5209 | PyType_GenericAlloc, /* tp_alloc */ |
| 5210 | cpu_set_new, /* tp_new */ |
| 5211 | PyObject_Del, /* tp_free */ |
| 5212 | }; |
| 5213 | |
| 5214 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5215 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5216 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5217 | |
| 5218 | static PyObject * |
| 5219 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5220 | { |
| 5221 | pid_t pid; |
| 5222 | Py_cpu_set *cpu_set; |
| 5223 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5224 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5225 | &pid, &cpu_set_type, &cpu_set)) |
| 5226 | return NULL; |
| 5227 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5228 | return posix_error(); |
| 5229 | Py_RETURN_NONE; |
| 5230 | } |
| 5231 | |
| 5232 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5233 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5234 | Return the affinity of the process with PID *pid*.\n\ |
| 5235 | The returned cpu_set will be of size *ncpus*."); |
| 5236 | |
| 5237 | static PyObject * |
| 5238 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5239 | { |
| 5240 | pid_t pid; |
| 5241 | int ncpus; |
| 5242 | Py_cpu_set *res; |
| 5243 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5244 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5245 | &pid, &ncpus)) |
| 5246 | return NULL; |
| 5247 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5248 | if (!res) |
| 5249 | return NULL; |
| 5250 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5251 | Py_DECREF(res); |
| 5252 | return posix_error(); |
| 5253 | } |
| 5254 | return (PyObject *)res; |
| 5255 | } |
| 5256 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5257 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5258 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5259 | #endif /* HAVE_SCHED_H */ |
| 5260 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5261 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5262 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5263 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5264 | #define DEV_PTY_FILE "/dev/ptc" |
| 5265 | #define HAVE_DEV_PTMX |
| 5266 | #else |
| 5267 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5268 | #endif |
| 5269 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5270 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5271 | #ifdef HAVE_PTY_H |
| 5272 | #include <pty.h> |
| 5273 | #else |
| 5274 | #ifdef HAVE_LIBUTIL_H |
| 5275 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5276 | #else |
| 5277 | #ifdef HAVE_UTIL_H |
| 5278 | #include <util.h> |
| 5279 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5280 | #endif /* HAVE_LIBUTIL_H */ |
| 5281 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5282 | #ifdef HAVE_STROPTS_H |
| 5283 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5284 | #endif |
| 5285 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5286 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5287 | #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] | 5288 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5289 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5290 | 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] | 5291 | |
| 5292 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5293 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5294 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5295 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5296 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5298 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5299 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5300 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5301 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5302 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5303 | #endif |
| 5304 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5305 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5306 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5307 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5308 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5309 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5310 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5311 | if (slave_name == NULL) |
| 5312 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5313 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5314 | slave_fd = open(slave_name, O_RDWR); |
| 5315 | if (slave_fd < 0) |
| 5316 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5317 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5318 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5319 | if (master_fd < 0) |
| 5320 | return posix_error(); |
| 5321 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5322 | /* change permission of slave */ |
| 5323 | if (grantpt(master_fd) < 0) { |
| 5324 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5325 | return posix_error(); |
| 5326 | } |
| 5327 | /* unlock slave */ |
| 5328 | if (unlockpt(master_fd) < 0) { |
| 5329 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5330 | return posix_error(); |
| 5331 | } |
| 5332 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5333 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5334 | if (slave_name == NULL) |
| 5335 | return posix_error(); |
| 5336 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5337 | if (slave_fd < 0) |
| 5338 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5339 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5340 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5341 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5342 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5343 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5344 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5345 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5346 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5348 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5349 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5350 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5351 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5352 | |
| 5353 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5354 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5355 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5356 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5357 | 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] | 5358 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5359 | |
| 5360 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5361 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5363 | int master_fd = -1, result = 0; |
| 5364 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5366 | _PyImport_AcquireLock(); |
| 5367 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5368 | if (pid == 0) { |
| 5369 | /* child: this clobbers and resets the import lock. */ |
| 5370 | PyOS_AfterFork(); |
| 5371 | } else { |
| 5372 | /* parent: release the import lock. */ |
| 5373 | result = _PyImport_ReleaseLock(); |
| 5374 | } |
| 5375 | if (pid == -1) |
| 5376 | return posix_error(); |
| 5377 | if (result < 0) { |
| 5378 | /* Don't clobber the OSError if the fork failed. */ |
| 5379 | PyErr_SetString(PyExc_RuntimeError, |
| 5380 | "not holding the import lock"); |
| 5381 | return NULL; |
| 5382 | } |
| 5383 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5384 | } |
| 5385 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5386 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5387 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5388 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5390 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5391 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5392 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5393 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5394 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5395 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5396 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5397 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5398 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5399 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5400 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5401 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5402 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5403 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5404 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5405 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5406 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5407 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5409 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5410 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5411 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5412 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5413 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5414 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5415 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5416 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5417 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5418 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5419 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5420 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5421 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5422 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5423 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5424 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5425 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5427 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5428 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5429 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5430 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5431 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5432 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5434 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5437 | #ifdef HAVE_GETGROUPLIST |
| 5438 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5439 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5440 | Returns a list of groups to which a user belongs.\n\n\ |
| 5441 | user: username to lookup\n\ |
| 5442 | group: base group id of the user"); |
| 5443 | |
| 5444 | static PyObject * |
| 5445 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5446 | { |
| 5447 | #ifdef NGROUPS_MAX |
| 5448 | #define MAX_GROUPS NGROUPS_MAX |
| 5449 | #else |
| 5450 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5451 | #define MAX_GROUPS 64 |
| 5452 | #endif |
| 5453 | |
| 5454 | const char *user; |
| 5455 | int i, ngroups; |
| 5456 | PyObject *list; |
| 5457 | #ifdef __APPLE__ |
| 5458 | int *groups, basegid; |
| 5459 | #else |
| 5460 | gid_t *groups, basegid; |
| 5461 | #endif |
| 5462 | ngroups = MAX_GROUPS; |
| 5463 | |
| 5464 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5465 | return NULL; |
| 5466 | |
| 5467 | #ifdef __APPLE__ |
| 5468 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5469 | #else |
| 5470 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5471 | #endif |
| 5472 | if (groups == NULL) |
| 5473 | return PyErr_NoMemory(); |
| 5474 | |
| 5475 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5476 | PyMem_Del(groups); |
| 5477 | return posix_error(); |
| 5478 | } |
| 5479 | |
| 5480 | list = PyList_New(ngroups); |
| 5481 | if (list == NULL) { |
| 5482 | PyMem_Del(groups); |
| 5483 | return NULL; |
| 5484 | } |
| 5485 | |
| 5486 | for (i = 0; i < ngroups; i++) { |
| 5487 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5488 | if (o == NULL) { |
| 5489 | Py_DECREF(list); |
| 5490 | PyMem_Del(groups); |
| 5491 | return NULL; |
| 5492 | } |
| 5493 | PyList_SET_ITEM(list, i, o); |
| 5494 | } |
| 5495 | |
| 5496 | PyMem_Del(groups); |
| 5497 | |
| 5498 | return list; |
| 5499 | } |
| 5500 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5501 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5502 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5503 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5504 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5505 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5506 | |
| 5507 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5508 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5509 | { |
| 5510 | PyObject *result = NULL; |
| 5511 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5512 | #ifdef NGROUPS_MAX |
| 5513 | #define MAX_GROUPS NGROUPS_MAX |
| 5514 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5515 | /* 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] | 5516 | #define MAX_GROUPS 64 |
| 5517 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5518 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5519 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5520 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5521 | * This is a helper variable to store the intermediate result when |
| 5522 | * that happens. |
| 5523 | * |
| 5524 | * To keep the code readable the OSX behaviour is unconditional, |
| 5525 | * according to the POSIX spec this should be safe on all unix-y |
| 5526 | * systems. |
| 5527 | */ |
| 5528 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5529 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5530 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5531 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5532 | if (n < 0) { |
| 5533 | if (errno == EINVAL) { |
| 5534 | n = getgroups(0, NULL); |
| 5535 | if (n == -1) { |
| 5536 | return posix_error(); |
| 5537 | } |
| 5538 | if (n == 0) { |
| 5539 | /* Avoid malloc(0) */ |
| 5540 | alt_grouplist = grouplist; |
| 5541 | } else { |
| 5542 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5543 | if (alt_grouplist == NULL) { |
| 5544 | errno = EINVAL; |
| 5545 | return posix_error(); |
| 5546 | } |
| 5547 | n = getgroups(n, alt_grouplist); |
| 5548 | if (n == -1) { |
| 5549 | PyMem_Free(alt_grouplist); |
| 5550 | return posix_error(); |
| 5551 | } |
| 5552 | } |
| 5553 | } else { |
| 5554 | return posix_error(); |
| 5555 | } |
| 5556 | } |
| 5557 | result = PyList_New(n); |
| 5558 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5559 | int i; |
| 5560 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5561 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5562 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5563 | Py_DECREF(result); |
| 5564 | result = NULL; |
| 5565 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5566 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5567 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5568 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | if (alt_grouplist != grouplist) { |
| 5572 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5573 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5574 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5575 | return result; |
| 5576 | } |
| 5577 | #endif |
| 5578 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5579 | #ifdef HAVE_INITGROUPS |
| 5580 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5581 | "initgroups(username, gid) -> None\n\n\ |
| 5582 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5583 | the groups of which the specified username is a member, plus the specified\n\ |
| 5584 | group id."); |
| 5585 | |
| 5586 | static PyObject * |
| 5587 | posix_initgroups(PyObject *self, PyObject *args) |
| 5588 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5589 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5590 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5591 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5592 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5593 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5594 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5595 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5596 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5597 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5598 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5599 | res = initgroups(username, (gid_t) gid); |
| 5600 | Py_DECREF(oname); |
| 5601 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5602 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5604 | Py_INCREF(Py_None); |
| 5605 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5606 | } |
| 5607 | #endif |
| 5608 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5609 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5610 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5611 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5612 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5613 | |
| 5614 | static PyObject * |
| 5615 | posix_getpgid(PyObject *self, PyObject *args) |
| 5616 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5617 | pid_t pid, pgid; |
| 5618 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5619 | return NULL; |
| 5620 | pgid = getpgid(pid); |
| 5621 | if (pgid < 0) |
| 5622 | return posix_error(); |
| 5623 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5624 | } |
| 5625 | #endif /* HAVE_GETPGID */ |
| 5626 | |
| 5627 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5628 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5629 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5630 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5631 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5632 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5633 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5634 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5635 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5636 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5637 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5638 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5639 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5640 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5641 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5642 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5643 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5644 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5645 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5646 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5647 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5648 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5649 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5650 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5651 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5652 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5653 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5654 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5655 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5656 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5657 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5658 | return posix_error(); |
| 5659 | Py_INCREF(Py_None); |
| 5660 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5663 | #endif /* HAVE_SETPGRP */ |
| 5664 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5665 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5666 | |
| 5667 | #ifdef MS_WINDOWS |
| 5668 | #include <tlhelp32.h> |
| 5669 | |
| 5670 | static PyObject* |
| 5671 | win32_getppid() |
| 5672 | { |
| 5673 | HANDLE snapshot; |
| 5674 | pid_t mypid; |
| 5675 | PyObject* result = NULL; |
| 5676 | BOOL have_record; |
| 5677 | PROCESSENTRY32 pe; |
| 5678 | |
| 5679 | mypid = getpid(); /* This function never fails */ |
| 5680 | |
| 5681 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5682 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5683 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5684 | |
| 5685 | pe.dwSize = sizeof(pe); |
| 5686 | have_record = Process32First(snapshot, &pe); |
| 5687 | while (have_record) { |
| 5688 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5689 | /* We could cache the ulong value in a static variable. */ |
| 5690 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5691 | break; |
| 5692 | } |
| 5693 | |
| 5694 | have_record = Process32Next(snapshot, &pe); |
| 5695 | } |
| 5696 | |
| 5697 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5698 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5699 | * error anyway, so let's raise it. */ |
| 5700 | if (!result) |
| 5701 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5702 | |
| 5703 | CloseHandle(snapshot); |
| 5704 | |
| 5705 | return result; |
| 5706 | } |
| 5707 | #endif /*MS_WINDOWS*/ |
| 5708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5709 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5710 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5711 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5712 | Windows machines will still return its id; others systems will return the id\n\ |
| 5713 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5714 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5715 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5716 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5717 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5718 | #ifdef MS_WINDOWS |
| 5719 | return win32_getppid(); |
| 5720 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5721 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5722 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5723 | } |
| 5724 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5725 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5726 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5727 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5728 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5729 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5730 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5731 | |
| 5732 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5733 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5734 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5735 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5736 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5737 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5738 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5739 | |
| 5740 | if (GetUserNameW(user_name, &num_chars)) { |
| 5741 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5742 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5743 | } |
| 5744 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5745 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5746 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5747 | char *name; |
| 5748 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5750 | errno = 0; |
| 5751 | name = getlogin(); |
| 5752 | if (name == NULL) { |
| 5753 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5754 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5755 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5756 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5757 | } |
| 5758 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5759 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5760 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5761 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5762 | return result; |
| 5763 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5764 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5765 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5766 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5767 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5768 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5769 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5770 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5771 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5772 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5773 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5774 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5775 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5776 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5777 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5778 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5779 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5780 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5781 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5782 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5783 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5784 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5785 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5787 | pid_t pid; |
| 5788 | int sig; |
| 5789 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5790 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5791 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5792 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5793 | APIRET rc; |
| 5794 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5795 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5796 | |
| 5797 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5798 | APIRET rc; |
| 5799 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5800 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5801 | |
| 5802 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5803 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5804 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5805 | if (kill(pid, sig) == -1) |
| 5806 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5807 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5808 | Py_INCREF(Py_None); |
| 5809 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5810 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5811 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5812 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5813 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5814 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5815 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5816 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5817 | |
| 5818 | static PyObject * |
| 5819 | posix_killpg(PyObject *self, PyObject *args) |
| 5820 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5821 | int sig; |
| 5822 | pid_t pgid; |
| 5823 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5824 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5825 | take the same type. Moreover, pid_t is always at least as wide as |
| 5826 | int (else compilation of this module fails), which is safe. */ |
| 5827 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5828 | return NULL; |
| 5829 | if (killpg(pgid, sig) == -1) |
| 5830 | return posix_error(); |
| 5831 | Py_INCREF(Py_None); |
| 5832 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5833 | } |
| 5834 | #endif |
| 5835 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5836 | #ifdef MS_WINDOWS |
| 5837 | PyDoc_STRVAR(win32_kill__doc__, |
| 5838 | "kill(pid, sig)\n\n\ |
| 5839 | Kill a process with a signal."); |
| 5840 | |
| 5841 | static PyObject * |
| 5842 | win32_kill(PyObject *self, PyObject *args) |
| 5843 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5844 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5845 | DWORD pid, sig, err; |
| 5846 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5848 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5849 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5850 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5851 | /* Console processes which share a common console can be sent CTRL+C or |
| 5852 | CTRL+BREAK events, provided they handle said events. */ |
| 5853 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5854 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5855 | err = GetLastError(); |
| 5856 | PyErr_SetFromWindowsErr(err); |
| 5857 | } |
| 5858 | else |
| 5859 | Py_RETURN_NONE; |
| 5860 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5861 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5862 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5863 | attempt to open and terminate the process. */ |
| 5864 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5865 | if (handle == NULL) { |
| 5866 | err = GetLastError(); |
| 5867 | return PyErr_SetFromWindowsErr(err); |
| 5868 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5869 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5870 | if (TerminateProcess(handle, sig) == 0) { |
| 5871 | err = GetLastError(); |
| 5872 | result = PyErr_SetFromWindowsErr(err); |
| 5873 | } else { |
| 5874 | Py_INCREF(Py_None); |
| 5875 | result = Py_None; |
| 5876 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5877 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5878 | CloseHandle(handle); |
| 5879 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5880 | } |
| 5881 | #endif /* MS_WINDOWS */ |
| 5882 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5883 | #ifdef HAVE_PLOCK |
| 5884 | |
| 5885 | #ifdef HAVE_SYS_LOCK_H |
| 5886 | #include <sys/lock.h> |
| 5887 | #endif |
| 5888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5889 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5890 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5891 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5892 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5893 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5894 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5895 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5896 | int op; |
| 5897 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5898 | return NULL; |
| 5899 | if (plock(op) == -1) |
| 5900 | return posix_error(); |
| 5901 | Py_INCREF(Py_None); |
| 5902 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5903 | } |
| 5904 | #endif |
| 5905 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5906 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5907 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5908 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5909 | Set the current process's user id."); |
| 5910 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5911 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5912 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5913 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5914 | long uid_arg; |
| 5915 | uid_t uid; |
| 5916 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5917 | return NULL; |
| 5918 | uid = uid_arg; |
| 5919 | if (uid != uid_arg) { |
| 5920 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5921 | return NULL; |
| 5922 | } |
| 5923 | if (setuid(uid) < 0) |
| 5924 | return posix_error(); |
| 5925 | Py_INCREF(Py_None); |
| 5926 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5927 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5928 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5929 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5930 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5931 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5932 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5933 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5934 | Set the current process's effective user id."); |
| 5935 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5936 | static PyObject * |
| 5937 | posix_seteuid (PyObject *self, PyObject *args) |
| 5938 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5939 | long euid_arg; |
| 5940 | uid_t euid; |
| 5941 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5942 | return NULL; |
| 5943 | euid = euid_arg; |
| 5944 | if (euid != euid_arg) { |
| 5945 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5946 | return NULL; |
| 5947 | } |
| 5948 | if (seteuid(euid) < 0) { |
| 5949 | return posix_error(); |
| 5950 | } else { |
| 5951 | Py_INCREF(Py_None); |
| 5952 | return Py_None; |
| 5953 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5954 | } |
| 5955 | #endif /* HAVE_SETEUID */ |
| 5956 | |
| 5957 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5958 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5959 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5960 | Set the current process's effective group id."); |
| 5961 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5962 | static PyObject * |
| 5963 | posix_setegid (PyObject *self, PyObject *args) |
| 5964 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5965 | long egid_arg; |
| 5966 | gid_t egid; |
| 5967 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5968 | return NULL; |
| 5969 | egid = egid_arg; |
| 5970 | if (egid != egid_arg) { |
| 5971 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5972 | return NULL; |
| 5973 | } |
| 5974 | if (setegid(egid) < 0) { |
| 5975 | return posix_error(); |
| 5976 | } else { |
| 5977 | Py_INCREF(Py_None); |
| 5978 | return Py_None; |
| 5979 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5980 | } |
| 5981 | #endif /* HAVE_SETEGID */ |
| 5982 | |
| 5983 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5984 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5985 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5986 | Set the current process's real and effective user ids."); |
| 5987 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5988 | static PyObject * |
| 5989 | posix_setreuid (PyObject *self, PyObject *args) |
| 5990 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5991 | long ruid_arg, euid_arg; |
| 5992 | uid_t ruid, euid; |
| 5993 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5994 | return NULL; |
| 5995 | if (ruid_arg == -1) |
| 5996 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5997 | else |
| 5998 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5999 | if (euid_arg == -1) |
| 6000 | euid = (uid_t)-1; |
| 6001 | else |
| 6002 | euid = euid_arg; |
| 6003 | if ((euid_arg != -1 && euid != euid_arg) || |
| 6004 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 6005 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 6006 | return NULL; |
| 6007 | } |
| 6008 | if (setreuid(ruid, euid) < 0) { |
| 6009 | return posix_error(); |
| 6010 | } else { |
| 6011 | Py_INCREF(Py_None); |
| 6012 | return Py_None; |
| 6013 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6014 | } |
| 6015 | #endif /* HAVE_SETREUID */ |
| 6016 | |
| 6017 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6018 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6019 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6020 | Set the current process's real and effective group ids."); |
| 6021 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6022 | static PyObject * |
| 6023 | posix_setregid (PyObject *self, PyObject *args) |
| 6024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6025 | long rgid_arg, egid_arg; |
| 6026 | gid_t rgid, egid; |
| 6027 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6028 | return NULL; |
| 6029 | if (rgid_arg == -1) |
| 6030 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6031 | else |
| 6032 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6033 | if (egid_arg == -1) |
| 6034 | egid = (gid_t)-1; |
| 6035 | else |
| 6036 | egid = egid_arg; |
| 6037 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6038 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6039 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6040 | return NULL; |
| 6041 | } |
| 6042 | if (setregid(rgid, egid) < 0) { |
| 6043 | return posix_error(); |
| 6044 | } else { |
| 6045 | Py_INCREF(Py_None); |
| 6046 | return Py_None; |
| 6047 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6048 | } |
| 6049 | #endif /* HAVE_SETREGID */ |
| 6050 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6051 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6052 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6053 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6054 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6055 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6056 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6057 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6058 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | long gid_arg; |
| 6060 | gid_t gid; |
| 6061 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6062 | return NULL; |
| 6063 | gid = gid_arg; |
| 6064 | if (gid != gid_arg) { |
| 6065 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6066 | return NULL; |
| 6067 | } |
| 6068 | if (setgid(gid) < 0) |
| 6069 | return posix_error(); |
| 6070 | Py_INCREF(Py_None); |
| 6071 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6072 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6073 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6074 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6075 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6076 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6077 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6078 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6079 | |
| 6080 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6081 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | int i, len; |
| 6084 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6085 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6086 | if (!PySequence_Check(groups)) { |
| 6087 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6088 | return NULL; |
| 6089 | } |
| 6090 | len = PySequence_Size(groups); |
| 6091 | if (len > MAX_GROUPS) { |
| 6092 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6093 | return NULL; |
| 6094 | } |
| 6095 | for(i = 0; i < len; i++) { |
| 6096 | PyObject *elem; |
| 6097 | elem = PySequence_GetItem(groups, i); |
| 6098 | if (!elem) |
| 6099 | return NULL; |
| 6100 | if (!PyLong_Check(elem)) { |
| 6101 | PyErr_SetString(PyExc_TypeError, |
| 6102 | "groups must be integers"); |
| 6103 | Py_DECREF(elem); |
| 6104 | return NULL; |
| 6105 | } else { |
| 6106 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6107 | if (PyErr_Occurred()) { |
| 6108 | PyErr_SetString(PyExc_TypeError, |
| 6109 | "group id too big"); |
| 6110 | Py_DECREF(elem); |
| 6111 | return NULL; |
| 6112 | } |
| 6113 | grouplist[i] = x; |
| 6114 | /* read back the value to see if it fitted in gid_t */ |
| 6115 | if (grouplist[i] != x) { |
| 6116 | PyErr_SetString(PyExc_TypeError, |
| 6117 | "group id too big"); |
| 6118 | Py_DECREF(elem); |
| 6119 | return NULL; |
| 6120 | } |
| 6121 | } |
| 6122 | Py_DECREF(elem); |
| 6123 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6125 | if (setgroups(len, grouplist) < 0) |
| 6126 | return posix_error(); |
| 6127 | Py_INCREF(Py_None); |
| 6128 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6129 | } |
| 6130 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6131 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6132 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6133 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6134 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6135 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6136 | PyObject *result; |
| 6137 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6138 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6139 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6140 | if (pid == -1) |
| 6141 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6143 | if (struct_rusage == NULL) { |
| 6144 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6145 | if (m == NULL) |
| 6146 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6147 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6148 | Py_DECREF(m); |
| 6149 | if (struct_rusage == NULL) |
| 6150 | return NULL; |
| 6151 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6152 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6153 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6154 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6155 | if (!result) |
| 6156 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6157 | |
| 6158 | #ifndef doubletime |
| 6159 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6160 | #endif |
| 6161 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6162 | PyStructSequence_SET_ITEM(result, 0, |
| 6163 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6164 | PyStructSequence_SET_ITEM(result, 1, |
| 6165 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6166 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6167 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6168 | SET_INT(result, 2, ru->ru_maxrss); |
| 6169 | SET_INT(result, 3, ru->ru_ixrss); |
| 6170 | SET_INT(result, 4, ru->ru_idrss); |
| 6171 | SET_INT(result, 5, ru->ru_isrss); |
| 6172 | SET_INT(result, 6, ru->ru_minflt); |
| 6173 | SET_INT(result, 7, ru->ru_majflt); |
| 6174 | SET_INT(result, 8, ru->ru_nswap); |
| 6175 | SET_INT(result, 9, ru->ru_inblock); |
| 6176 | SET_INT(result, 10, ru->ru_oublock); |
| 6177 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6178 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6179 | SET_INT(result, 13, ru->ru_nsignals); |
| 6180 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6181 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6182 | #undef SET_INT |
| 6183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6184 | if (PyErr_Occurred()) { |
| 6185 | Py_DECREF(result); |
| 6186 | return NULL; |
| 6187 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6188 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6189 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6190 | } |
| 6191 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6192 | |
| 6193 | #ifdef HAVE_WAIT3 |
| 6194 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6195 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6196 | Wait for completion of a child process."); |
| 6197 | |
| 6198 | static PyObject * |
| 6199 | posix_wait3(PyObject *self, PyObject *args) |
| 6200 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6201 | pid_t pid; |
| 6202 | int options; |
| 6203 | struct rusage ru; |
| 6204 | WAIT_TYPE status; |
| 6205 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6207 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6208 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6210 | Py_BEGIN_ALLOW_THREADS |
| 6211 | pid = wait3(&status, options, &ru); |
| 6212 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6213 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6214 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6215 | } |
| 6216 | #endif /* HAVE_WAIT3 */ |
| 6217 | |
| 6218 | #ifdef HAVE_WAIT4 |
| 6219 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6220 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6221 | Wait for completion of a given child process."); |
| 6222 | |
| 6223 | static PyObject * |
| 6224 | posix_wait4(PyObject *self, PyObject *args) |
| 6225 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6226 | pid_t pid; |
| 6227 | int options; |
| 6228 | struct rusage ru; |
| 6229 | WAIT_TYPE status; |
| 6230 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6231 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6232 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6233 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6234 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6235 | Py_BEGIN_ALLOW_THREADS |
| 6236 | pid = wait4(pid, &status, options, &ru); |
| 6237 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6238 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6239 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6240 | } |
| 6241 | #endif /* HAVE_WAIT4 */ |
| 6242 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6243 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6244 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6245 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6246 | Wait for the completion of one or more child processes.\n\n\ |
| 6247 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6248 | id specifies the pid to wait on.\n\ |
| 6249 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6250 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6251 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6252 | no children in a waitable state."); |
| 6253 | |
| 6254 | static PyObject * |
| 6255 | posix_waitid(PyObject *self, PyObject *args) |
| 6256 | { |
| 6257 | PyObject *result; |
| 6258 | idtype_t idtype; |
| 6259 | id_t id; |
| 6260 | int options, res; |
| 6261 | siginfo_t si; |
| 6262 | si.si_pid = 0; |
| 6263 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6264 | return NULL; |
| 6265 | Py_BEGIN_ALLOW_THREADS |
| 6266 | res = waitid(idtype, id, &si, options); |
| 6267 | Py_END_ALLOW_THREADS |
| 6268 | if (res == -1) |
| 6269 | return posix_error(); |
| 6270 | |
| 6271 | if (si.si_pid == 0) |
| 6272 | Py_RETURN_NONE; |
| 6273 | |
| 6274 | result = PyStructSequence_New(&WaitidResultType); |
| 6275 | if (!result) |
| 6276 | return NULL; |
| 6277 | |
| 6278 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6279 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6280 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6281 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6282 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6283 | if (PyErr_Occurred()) { |
| 6284 | Py_DECREF(result); |
| 6285 | return NULL; |
| 6286 | } |
| 6287 | |
| 6288 | return result; |
| 6289 | } |
| 6290 | #endif |
| 6291 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6292 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6293 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6294 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6295 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6297 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6298 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6299 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | pid_t pid; |
| 6301 | int options; |
| 6302 | WAIT_TYPE status; |
| 6303 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6304 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6305 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6306 | return NULL; |
| 6307 | Py_BEGIN_ALLOW_THREADS |
| 6308 | pid = waitpid(pid, &status, options); |
| 6309 | Py_END_ALLOW_THREADS |
| 6310 | if (pid == -1) |
| 6311 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6312 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6314 | } |
| 6315 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6316 | #elif defined(HAVE_CWAIT) |
| 6317 | |
| 6318 | /* 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] | 6319 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6320 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6321 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6322 | |
| 6323 | static PyObject * |
| 6324 | posix_waitpid(PyObject *self, PyObject *args) |
| 6325 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6326 | Py_intptr_t pid; |
| 6327 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6328 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6329 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6330 | return NULL; |
| 6331 | Py_BEGIN_ALLOW_THREADS |
| 6332 | pid = _cwait(&status, pid, options); |
| 6333 | Py_END_ALLOW_THREADS |
| 6334 | if (pid == -1) |
| 6335 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6336 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6337 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6338 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6339 | } |
| 6340 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6341 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6342 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6343 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6344 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6345 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6346 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6347 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6348 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6350 | pid_t pid; |
| 6351 | WAIT_TYPE status; |
| 6352 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6353 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6354 | Py_BEGIN_ALLOW_THREADS |
| 6355 | pid = wait(&status); |
| 6356 | Py_END_ALLOW_THREADS |
| 6357 | if (pid == -1) |
| 6358 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6359 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6360 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6361 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6362 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6363 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6365 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6366 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6367 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6368 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6369 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6370 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6371 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6372 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6373 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6374 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6375 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6376 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6377 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6378 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6379 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6380 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6381 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6384 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6385 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6386 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6387 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6388 | 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] | 6389 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6390 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6391 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6392 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6393 | PyObject* v; |
| 6394 | char buf[MAXPATHLEN]; |
| 6395 | PyObject *opath; |
| 6396 | char *path; |
| 6397 | int n; |
| 6398 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6399 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6400 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6401 | PyUnicode_FSConverter, &opath)) |
| 6402 | return NULL; |
| 6403 | path = PyBytes_AsString(opath); |
| 6404 | v = PySequence_GetItem(args, 0); |
| 6405 | if (v == NULL) { |
| 6406 | Py_DECREF(opath); |
| 6407 | return NULL; |
| 6408 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6410 | if (PyUnicode_Check(v)) { |
| 6411 | arg_is_unicode = 1; |
| 6412 | } |
| 6413 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6415 | Py_BEGIN_ALLOW_THREADS |
| 6416 | n = readlink(path, buf, (int) sizeof buf); |
| 6417 | Py_END_ALLOW_THREADS |
| 6418 | if (n < 0) |
| 6419 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6421 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6422 | if (arg_is_unicode) |
| 6423 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6424 | else |
| 6425 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6426 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6427 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6428 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6429 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6430 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6431 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6432 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6433 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6434 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6435 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6436 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6437 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6438 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6439 | } |
| 6440 | #endif /* HAVE_SYMLINK */ |
| 6441 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6442 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6443 | |
| 6444 | PyDoc_STRVAR(win_readlink__doc__, |
| 6445 | "readlink(path) -> path\n\n\ |
| 6446 | Return a string representing the path to which the symbolic link points."); |
| 6447 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6448 | /* Windows readlink implementation */ |
| 6449 | static PyObject * |
| 6450 | win_readlink(PyObject *self, PyObject *args) |
| 6451 | { |
| 6452 | wchar_t *path; |
| 6453 | DWORD n_bytes_returned; |
| 6454 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6455 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6456 | HANDLE reparse_point_handle; |
| 6457 | |
| 6458 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6459 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6460 | wchar_t *print_name; |
| 6461 | |
| 6462 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6463 | "U:readlink", |
| 6464 | &po)) |
| 6465 | return NULL; |
| 6466 | path = PyUnicode_AsUnicode(po); |
| 6467 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6468 | return NULL; |
| 6469 | |
| 6470 | /* First get a handle to the reparse point */ |
| 6471 | Py_BEGIN_ALLOW_THREADS |
| 6472 | reparse_point_handle = CreateFileW( |
| 6473 | path, |
| 6474 | 0, |
| 6475 | 0, |
| 6476 | 0, |
| 6477 | OPEN_EXISTING, |
| 6478 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6479 | 0); |
| 6480 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6481 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6482 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6483 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6484 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6485 | Py_BEGIN_ALLOW_THREADS |
| 6486 | /* New call DeviceIoControl to read the reparse point */ |
| 6487 | io_result = DeviceIoControl( |
| 6488 | reparse_point_handle, |
| 6489 | FSCTL_GET_REPARSE_POINT, |
| 6490 | 0, 0, /* in buffer */ |
| 6491 | target_buffer, sizeof(target_buffer), |
| 6492 | &n_bytes_returned, |
| 6493 | 0 /* we're not using OVERLAPPED_IO */ |
| 6494 | ); |
| 6495 | CloseHandle(reparse_point_handle); |
| 6496 | Py_END_ALLOW_THREADS |
| 6497 | |
| 6498 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6499 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6500 | |
| 6501 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6502 | { |
| 6503 | PyErr_SetString(PyExc_ValueError, |
| 6504 | "not a symbolic link"); |
| 6505 | return NULL; |
| 6506 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6507 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6508 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6509 | |
| 6510 | result = PyUnicode_FromWideChar(print_name, |
| 6511 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6512 | return result; |
| 6513 | } |
| 6514 | |
| 6515 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6516 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6517 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6518 | |
| 6519 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6520 | static int has_CreateSymbolicLinkW = 0; |
| 6521 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6522 | static int |
| 6523 | check_CreateSymbolicLinkW() |
| 6524 | { |
| 6525 | HINSTANCE hKernel32; |
| 6526 | /* only recheck */ |
| 6527 | if (has_CreateSymbolicLinkW) |
| 6528 | return has_CreateSymbolicLinkW; |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 6529 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6530 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6531 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6532 | if (Py_CreateSymbolicLinkW) |
| 6533 | has_CreateSymbolicLinkW = 1; |
| 6534 | return has_CreateSymbolicLinkW; |
| 6535 | } |
| 6536 | |
| 6537 | PyDoc_STRVAR(win_symlink__doc__, |
| 6538 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6539 | Create a symbolic link pointing to src named dst.\n\ |
| 6540 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6541 | a directory.\n\ |
| 6542 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6543 | NotImplementedError otherwise."); |
| 6544 | |
| 6545 | static PyObject * |
| 6546 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6547 | { |
| 6548 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6549 | PyObject *osrc, *odest; |
| 6550 | PyObject *usrc = NULL, *udest = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6551 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6552 | int target_is_directory = 0; |
| 6553 | DWORD res; |
| 6554 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6555 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6556 | if (!check_CreateSymbolicLinkW()) |
| 6557 | { |
| 6558 | /* raise NotImplementedError */ |
| 6559 | return PyErr_Format(PyExc_NotImplementedError, |
| 6560 | "CreateSymbolicLinkW not found"); |
| 6561 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6562 | if (!PyArg_ParseTupleAndKeywords( |
| 6563 | args, kwargs, "OO|i:symlink", kwlist, |
| 6564 | &osrc, &odest, &target_is_directory)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6565 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6566 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6567 | usrc = win32_decode_filename(osrc); |
| 6568 | if (!usrc) |
| 6569 | return NULL; |
| 6570 | udest = win32_decode_filename(odest); |
| 6571 | if (!udest) |
| 6572 | goto error; |
| 6573 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6574 | if (win32_can_symlink == 0) |
| 6575 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6576 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6577 | wsrc = PyUnicode_AsUnicode(usrc); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6578 | if (wsrc == NULL) |
| 6579 | goto error; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6580 | wdest = PyUnicode_AsUnicode(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6581 | if (wsrc == NULL) |
| 6582 | goto error; |
| 6583 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6584 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6585 | if( |
| 6586 | GetFileAttributesExW( |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6587 | wsrc, GetFileExInfoStandard, &src_info |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6588 | )) |
| 6589 | { |
| 6590 | target_is_directory = target_is_directory || |
| 6591 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6592 | } |
| 6593 | |
| 6594 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6595 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6596 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6597 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6598 | Py_DECREF(usrc); |
| 6599 | Py_DECREF(udest); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6600 | if (!res) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6601 | return win32_error_object("symlink", osrc); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6602 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6603 | Py_INCREF(Py_None); |
| 6604 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6605 | |
| 6606 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6607 | Py_XDECREF(usrc); |
| 6608 | Py_XDECREF(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6609 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6610 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6611 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6612 | |
| 6613 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6614 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6615 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6616 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6617 | { |
| 6618 | ULONG value = 0; |
| 6619 | |
| 6620 | Py_BEGIN_ALLOW_THREADS |
| 6621 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6622 | Py_END_ALLOW_THREADS |
| 6623 | |
| 6624 | return value; |
| 6625 | } |
| 6626 | |
| 6627 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6628 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6629 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6630 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | return Py_BuildValue("ddddd", |
| 6632 | (double)0 /* t.tms_utime / HZ */, |
| 6633 | (double)0 /* t.tms_stime / HZ */, |
| 6634 | (double)0 /* t.tms_cutime / HZ */, |
| 6635 | (double)0 /* t.tms_cstime / HZ */, |
| 6636 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6637 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6638 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6639 | #define NEED_TICKS_PER_SECOND |
| 6640 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6641 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6642 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6643 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6644 | struct tms t; |
| 6645 | clock_t c; |
| 6646 | errno = 0; |
| 6647 | c = times(&t); |
| 6648 | if (c == (clock_t) -1) |
| 6649 | return posix_error(); |
| 6650 | return Py_BuildValue("ddddd", |
| 6651 | (double)t.tms_utime / ticks_per_second, |
| 6652 | (double)t.tms_stime / ticks_per_second, |
| 6653 | (double)t.tms_cutime / ticks_per_second, |
| 6654 | (double)t.tms_cstime / ticks_per_second, |
| 6655 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6656 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6657 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6658 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6659 | |
| 6660 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6661 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6662 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6663 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6664 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6666 | FILETIME create, exit, kernel, user; |
| 6667 | HANDLE hProc; |
| 6668 | hProc = GetCurrentProcess(); |
| 6669 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6670 | /* The fields of a FILETIME structure are the hi and lo part |
| 6671 | of a 64-bit value expressed in 100 nanosecond units. |
| 6672 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6673 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6674 | */ |
| 6675 | return Py_BuildValue( |
| 6676 | "ddddd", |
| 6677 | (double)(user.dwHighDateTime*429.4967296 + |
| 6678 | user.dwLowDateTime*1e-7), |
| 6679 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6680 | kernel.dwLowDateTime*1e-7), |
| 6681 | (double)0, |
| 6682 | (double)0, |
| 6683 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6684 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6685 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6686 | |
| 6687 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6688 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6689 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6690 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6691 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6692 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6693 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6694 | #ifdef HAVE_GETSID |
| 6695 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6696 | "getsid(pid) -> sid\n\n\ |
| 6697 | Call the system call getsid()."); |
| 6698 | |
| 6699 | static PyObject * |
| 6700 | posix_getsid(PyObject *self, PyObject *args) |
| 6701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6702 | pid_t pid; |
| 6703 | int sid; |
| 6704 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6705 | return NULL; |
| 6706 | sid = getsid(pid); |
| 6707 | if (sid < 0) |
| 6708 | return posix_error(); |
| 6709 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6710 | } |
| 6711 | #endif /* HAVE_GETSID */ |
| 6712 | |
| 6713 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6714 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6716 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6717 | Call the system call setsid()."); |
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 * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6720 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | if (setsid() < 0) |
| 6723 | return posix_error(); |
| 6724 | Py_INCREF(Py_None); |
| 6725 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6726 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6727 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6728 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6729 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6730 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6731 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6732 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6733 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6734 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6735 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6736 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6737 | pid_t pid; |
| 6738 | int pgrp; |
| 6739 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6740 | return NULL; |
| 6741 | if (setpgid(pid, pgrp) < 0) |
| 6742 | return posix_error(); |
| 6743 | Py_INCREF(Py_None); |
| 6744 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6745 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6746 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6747 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6748 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6749 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6750 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6751 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6752 | 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] | 6753 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6754 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6755 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6756 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6757 | int fd; |
| 6758 | pid_t pgid; |
| 6759 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6760 | return NULL; |
| 6761 | pgid = tcgetpgrp(fd); |
| 6762 | if (pgid < 0) |
| 6763 | return posix_error(); |
| 6764 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6765 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6766 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6767 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6768 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6769 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6770 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6771 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6772 | 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] | 6773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6774 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6775 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6776 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6777 | int fd; |
| 6778 | pid_t pgid; |
| 6779 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6780 | return NULL; |
| 6781 | if (tcsetpgrp(fd, pgid) < 0) |
| 6782 | return posix_error(); |
| 6783 | Py_INCREF(Py_None); |
| 6784 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6785 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6786 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6787 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6788 | /* Functions acting on file descriptors */ |
| 6789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6790 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6791 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6792 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6793 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6794 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6795 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6796 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | PyObject *ofile; |
| 6798 | char *file; |
| 6799 | int flag; |
| 6800 | int mode = 0777; |
| 6801 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6802 | |
| 6803 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6804 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6805 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6806 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 6807 | if (wpath == NULL) |
| 6808 | return NULL; |
| 6809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6810 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6811 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6812 | Py_END_ALLOW_THREADS |
| 6813 | if (fd < 0) |
| 6814 | return posix_error(); |
| 6815 | return PyLong_FromLong((long)fd); |
| 6816 | } |
| 6817 | /* Drop the argument parsing error as narrow strings |
| 6818 | are also valid. */ |
| 6819 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6820 | #endif |
| 6821 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6822 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6823 | PyUnicode_FSConverter, &ofile, |
| 6824 | &flag, &mode)) |
| 6825 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6826 | #ifdef MS_WINDOWS |
| 6827 | if (win32_warn_bytes_api()) { |
| 6828 | Py_DECREF(ofile); |
| 6829 | return NULL; |
| 6830 | } |
| 6831 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6832 | file = PyBytes_AsString(ofile); |
| 6833 | Py_BEGIN_ALLOW_THREADS |
| 6834 | fd = open(file, flag, mode); |
| 6835 | Py_END_ALLOW_THREADS |
| 6836 | if (fd < 0) |
| 6837 | return posix_error_with_allocated_filename(ofile); |
| 6838 | Py_DECREF(ofile); |
| 6839 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6843 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6844 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6845 | Close a file descriptor (for low level IO)."); |
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_close(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, res; |
| 6851 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6852 | return NULL; |
| 6853 | if (!_PyVerify_fd(fd)) |
| 6854 | return posix_error(); |
| 6855 | Py_BEGIN_ALLOW_THREADS |
| 6856 | res = close(fd); |
| 6857 | Py_END_ALLOW_THREADS |
| 6858 | if (res < 0) |
| 6859 | return posix_error(); |
| 6860 | Py_INCREF(Py_None); |
| 6861 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6865 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6866 | "closerange(fd_low, fd_high)\n\n\ |
| 6867 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6868 | |
| 6869 | static PyObject * |
| 6870 | posix_closerange(PyObject *self, PyObject *args) |
| 6871 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | int fd_from, fd_to, i; |
| 6873 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6874 | return NULL; |
| 6875 | Py_BEGIN_ALLOW_THREADS |
| 6876 | for (i = fd_from; i < fd_to; i++) |
| 6877 | if (_PyVerify_fd(i)) |
| 6878 | close(i); |
| 6879 | Py_END_ALLOW_THREADS |
| 6880 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6884 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6885 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6886 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6887 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6888 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6889 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6890 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6891 | int fd; |
| 6892 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6893 | return NULL; |
| 6894 | if (!_PyVerify_fd(fd)) |
| 6895 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6896 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6897 | if (fd < 0) |
| 6898 | return posix_error(); |
| 6899 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6903 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6904 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6905 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6907 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6908 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6909 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6910 | int fd, fd2, res; |
| 6911 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6912 | return NULL; |
| 6913 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6914 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6915 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6916 | if (res < 0) |
| 6917 | return posix_error(); |
| 6918 | Py_INCREF(Py_None); |
| 6919 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6922 | #ifdef HAVE_LOCKF |
| 6923 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6924 | "lockf(fd, cmd, len)\n\n\ |
| 6925 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6926 | fd is an open file descriptor.\n\ |
| 6927 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6928 | F_TEST.\n\ |
| 6929 | len specifies the section of the file to lock."); |
| 6930 | |
| 6931 | static PyObject * |
| 6932 | posix_lockf(PyObject *self, PyObject *args) |
| 6933 | { |
| 6934 | int fd, cmd, res; |
| 6935 | off_t len; |
| 6936 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6937 | &fd, &cmd, _parse_off_t, &len)) |
| 6938 | return NULL; |
| 6939 | |
| 6940 | Py_BEGIN_ALLOW_THREADS |
| 6941 | res = lockf(fd, cmd, len); |
| 6942 | Py_END_ALLOW_THREADS |
| 6943 | |
| 6944 | if (res < 0) |
| 6945 | return posix_error(); |
| 6946 | |
| 6947 | Py_RETURN_NONE; |
| 6948 | } |
| 6949 | #endif |
| 6950 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6952 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6953 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 6954 | Set the current position of a file descriptor.\n\ |
| 6955 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6956 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6957 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6958 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6959 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6960 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6961 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6962 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6963 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6964 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6965 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6966 | PyObject *posobj; |
| 6967 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6968 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6969 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6970 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6971 | switch (how) { |
| 6972 | case 0: how = SEEK_SET; break; |
| 6973 | case 1: how = SEEK_CUR; break; |
| 6974 | case 2: how = SEEK_END; break; |
| 6975 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6976 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6977 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6978 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6979 | pos = PyLong_AsLong(posobj); |
| 6980 | #else |
| 6981 | pos = PyLong_AsLongLong(posobj); |
| 6982 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6983 | if (PyErr_Occurred()) |
| 6984 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6986 | if (!_PyVerify_fd(fd)) |
| 6987 | return posix_error(); |
| 6988 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6989 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6990 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6991 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6992 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6993 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6994 | Py_END_ALLOW_THREADS |
| 6995 | if (res < 0) |
| 6996 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6997 | |
| 6998 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6999 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7000 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7001 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 7002 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7003 | } |
| 7004 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7005 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7006 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7007 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7008 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7009 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7010 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7011 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7012 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7013 | int fd, size; |
| 7014 | Py_ssize_t n; |
| 7015 | PyObject *buffer; |
| 7016 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 7017 | return NULL; |
| 7018 | if (size < 0) { |
| 7019 | errno = EINVAL; |
| 7020 | return posix_error(); |
| 7021 | } |
| 7022 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7023 | if (buffer == NULL) |
| 7024 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7025 | if (!_PyVerify_fd(fd)) { |
| 7026 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7027 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7028 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7029 | Py_BEGIN_ALLOW_THREADS |
| 7030 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7031 | Py_END_ALLOW_THREADS |
| 7032 | if (n < 0) { |
| 7033 | Py_DECREF(buffer); |
| 7034 | return posix_error(); |
| 7035 | } |
| 7036 | if (n != size) |
| 7037 | _PyBytes_Resize(&buffer, n); |
| 7038 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7041 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7042 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7043 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7044 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7045 | { |
| 7046 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7047 | Py_ssize_t blen, total = 0; |
| 7048 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7049 | *iov = PyMem_New(struct iovec, cnt); |
| 7050 | if (*iov == NULL) { |
| 7051 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7052 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7053 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7054 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7055 | *buf = PyMem_New(Py_buffer, cnt); |
| 7056 | if (*buf == NULL) { |
| 7057 | PyMem_Del(*iov); |
| 7058 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7059 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7063 | PyObject *item = PySequence_GetItem(seq, i); |
| 7064 | if (item == NULL) |
| 7065 | goto fail; |
| 7066 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7067 | Py_DECREF(item); |
| 7068 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7069 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7070 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7071 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7072 | blen = (*buf)[i].len; |
| 7073 | (*iov)[i].iov_len = blen; |
| 7074 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7075 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7076 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7077 | |
| 7078 | fail: |
| 7079 | PyMem_Del(*iov); |
| 7080 | for (j = 0; j < i; j++) { |
| 7081 | PyBuffer_Release(&(*buf)[j]); |
| 7082 | } |
| 7083 | PyMem_Del(*buf); |
| 7084 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | static void |
| 7088 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7089 | { |
| 7090 | int i; |
| 7091 | PyMem_Del(iov); |
| 7092 | for (i = 0; i < cnt; i++) { |
| 7093 | PyBuffer_Release(&buf[i]); |
| 7094 | } |
| 7095 | PyMem_Del(buf); |
| 7096 | } |
| 7097 | #endif |
| 7098 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7099 | #ifdef HAVE_READV |
| 7100 | PyDoc_STRVAR(posix_readv__doc__, |
| 7101 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7102 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7103 | is an arbitrary sequence of writable buffers.\n\ |
| 7104 | Returns the total number of bytes read."); |
| 7105 | |
| 7106 | static PyObject * |
| 7107 | posix_readv(PyObject *self, PyObject *args) |
| 7108 | { |
| 7109 | int fd, cnt; |
| 7110 | Py_ssize_t n; |
| 7111 | PyObject *seq; |
| 7112 | struct iovec *iov; |
| 7113 | Py_buffer *buf; |
| 7114 | |
| 7115 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7116 | return NULL; |
| 7117 | if (!PySequence_Check(seq)) { |
| 7118 | PyErr_SetString(PyExc_TypeError, |
| 7119 | "readv() arg 2 must be a sequence"); |
| 7120 | return NULL; |
| 7121 | } |
| 7122 | cnt = PySequence_Size(seq); |
| 7123 | |
| 7124 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7125 | return NULL; |
| 7126 | |
| 7127 | Py_BEGIN_ALLOW_THREADS |
| 7128 | n = readv(fd, iov, cnt); |
| 7129 | Py_END_ALLOW_THREADS |
| 7130 | |
| 7131 | iov_cleanup(iov, buf, cnt); |
| 7132 | return PyLong_FromSsize_t(n); |
| 7133 | } |
| 7134 | #endif |
| 7135 | |
| 7136 | #ifdef HAVE_PREAD |
| 7137 | PyDoc_STRVAR(posix_pread__doc__, |
| 7138 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7139 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7140 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7141 | |
| 7142 | static PyObject * |
| 7143 | posix_pread(PyObject *self, PyObject *args) |
| 7144 | { |
| 7145 | int fd, size; |
| 7146 | off_t offset; |
| 7147 | Py_ssize_t n; |
| 7148 | PyObject *buffer; |
| 7149 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7150 | return NULL; |
| 7151 | |
| 7152 | if (size < 0) { |
| 7153 | errno = EINVAL; |
| 7154 | return posix_error(); |
| 7155 | } |
| 7156 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7157 | if (buffer == NULL) |
| 7158 | return NULL; |
| 7159 | if (!_PyVerify_fd(fd)) { |
| 7160 | Py_DECREF(buffer); |
| 7161 | return posix_error(); |
| 7162 | } |
| 7163 | Py_BEGIN_ALLOW_THREADS |
| 7164 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7165 | Py_END_ALLOW_THREADS |
| 7166 | if (n < 0) { |
| 7167 | Py_DECREF(buffer); |
| 7168 | return posix_error(); |
| 7169 | } |
| 7170 | if (n != size) |
| 7171 | _PyBytes_Resize(&buffer, n); |
| 7172 | return buffer; |
| 7173 | } |
| 7174 | #endif |
| 7175 | |
| 7176 | PyDoc_STRVAR(posix_write__doc__, |
| 7177 | "write(fd, string) -> byteswritten\n\n\ |
| 7178 | Write a string to a file descriptor."); |
| 7179 | |
| 7180 | static PyObject * |
| 7181 | posix_write(PyObject *self, PyObject *args) |
| 7182 | { |
| 7183 | Py_buffer pbuf; |
| 7184 | int fd; |
| 7185 | Py_ssize_t size, len; |
| 7186 | |
| 7187 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7188 | return NULL; |
| 7189 | if (!_PyVerify_fd(fd)) { |
| 7190 | PyBuffer_Release(&pbuf); |
| 7191 | return posix_error(); |
| 7192 | } |
| 7193 | len = pbuf.len; |
| 7194 | Py_BEGIN_ALLOW_THREADS |
| 7195 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7196 | if (len > INT_MAX) |
| 7197 | len = INT_MAX; |
| 7198 | size = write(fd, pbuf.buf, (int)len); |
| 7199 | #else |
| 7200 | size = write(fd, pbuf.buf, len); |
| 7201 | #endif |
| 7202 | Py_END_ALLOW_THREADS |
| 7203 | PyBuffer_Release(&pbuf); |
| 7204 | if (size < 0) |
| 7205 | return posix_error(); |
| 7206 | return PyLong_FromSsize_t(size); |
| 7207 | } |
| 7208 | |
| 7209 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7210 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7211 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7212 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7213 | -> byteswritten\n\ |
| 7214 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7215 | |
| 7216 | static PyObject * |
| 7217 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7218 | { |
| 7219 | int in, out; |
| 7220 | Py_ssize_t ret; |
| 7221 | off_t offset; |
| 7222 | |
| 7223 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7224 | #ifndef __APPLE__ |
| 7225 | Py_ssize_t len; |
| 7226 | #endif |
| 7227 | PyObject *headers = NULL, *trailers = NULL; |
| 7228 | Py_buffer *hbuf, *tbuf; |
| 7229 | off_t sbytes; |
| 7230 | struct sf_hdtr sf; |
| 7231 | int flags = 0; |
| 7232 | sf.headers = NULL; |
| 7233 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7234 | static char *keywords[] = {"out", "in", |
| 7235 | "offset", "count", |
| 7236 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7237 | |
| 7238 | #ifdef __APPLE__ |
| 7239 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7240 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7241 | #else |
| 7242 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7243 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7244 | #endif |
| 7245 | &headers, &trailers, &flags)) |
| 7246 | return NULL; |
| 7247 | if (headers != NULL) { |
| 7248 | if (!PySequence_Check(headers)) { |
| 7249 | PyErr_SetString(PyExc_TypeError, |
| 7250 | "sendfile() headers must be a sequence or None"); |
| 7251 | return NULL; |
| 7252 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7253 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7254 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7255 | if (sf.hdr_cnt > 0 && |
| 7256 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7257 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7258 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7259 | #ifdef __APPLE__ |
| 7260 | sbytes += i; |
| 7261 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7262 | } |
| 7263 | } |
| 7264 | if (trailers != NULL) { |
| 7265 | if (!PySequence_Check(trailers)) { |
| 7266 | PyErr_SetString(PyExc_TypeError, |
| 7267 | "sendfile() trailers must be a sequence or None"); |
| 7268 | return NULL; |
| 7269 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7270 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7271 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7272 | if (sf.trl_cnt > 0 && |
| 7273 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7274 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7275 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7276 | #ifdef __APPLE__ |
| 7277 | sbytes += i; |
| 7278 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7279 | } |
| 7280 | } |
| 7281 | |
| 7282 | Py_BEGIN_ALLOW_THREADS |
| 7283 | #ifdef __APPLE__ |
| 7284 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7285 | #else |
| 7286 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7287 | #endif |
| 7288 | Py_END_ALLOW_THREADS |
| 7289 | |
| 7290 | if (sf.headers != NULL) |
| 7291 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7292 | if (sf.trailers != NULL) |
| 7293 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7294 | |
| 7295 | if (ret < 0) { |
| 7296 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7297 | if (sbytes != 0) { |
| 7298 | // some data has been sent |
| 7299 | goto done; |
| 7300 | } |
| 7301 | else { |
| 7302 | // no data has been sent; upper application is supposed |
| 7303 | // to retry on EAGAIN or EBUSY |
| 7304 | return posix_error(); |
| 7305 | } |
| 7306 | } |
| 7307 | return posix_error(); |
| 7308 | } |
| 7309 | goto done; |
| 7310 | |
| 7311 | done: |
| 7312 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7313 | return Py_BuildValue("l", sbytes); |
| 7314 | #else |
| 7315 | return Py_BuildValue("L", sbytes); |
| 7316 | #endif |
| 7317 | |
| 7318 | #else |
| 7319 | Py_ssize_t count; |
| 7320 | PyObject *offobj; |
| 7321 | static char *keywords[] = {"out", "in", |
| 7322 | "offset", "count", NULL}; |
| 7323 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7324 | keywords, &out, &in, &offobj, &count)) |
| 7325 | return NULL; |
| 7326 | #ifdef linux |
| 7327 | if (offobj == Py_None) { |
| 7328 | Py_BEGIN_ALLOW_THREADS |
| 7329 | ret = sendfile(out, in, NULL, count); |
| 7330 | Py_END_ALLOW_THREADS |
| 7331 | if (ret < 0) |
| 7332 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7333 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7334 | } |
| 7335 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7336 | if (!_parse_off_t(offobj, &offset)) |
| 7337 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7338 | Py_BEGIN_ALLOW_THREADS |
| 7339 | ret = sendfile(out, in, &offset, count); |
| 7340 | Py_END_ALLOW_THREADS |
| 7341 | if (ret < 0) |
| 7342 | return posix_error(); |
| 7343 | return Py_BuildValue("n", ret); |
| 7344 | #endif |
| 7345 | } |
| 7346 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7348 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7349 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7350 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7352 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7353 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7355 | int fd; |
| 7356 | STRUCT_STAT st; |
| 7357 | int res; |
| 7358 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7359 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7360 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7361 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7362 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7363 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7364 | if (!_PyVerify_fd(fd)) |
| 7365 | return posix_error(); |
| 7366 | Py_BEGIN_ALLOW_THREADS |
| 7367 | res = FSTAT(fd, &st); |
| 7368 | Py_END_ALLOW_THREADS |
| 7369 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7370 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7371 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7372 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7373 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7374 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7375 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7377 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7378 | } |
| 7379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7380 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7381 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7382 | 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] | 7383 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7384 | |
| 7385 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7386 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7387 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7388 | int fd; |
| 7389 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7390 | return NULL; |
| 7391 | if (!_PyVerify_fd(fd)) |
| 7392 | return PyBool_FromLong(0); |
| 7393 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7394 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7395 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7396 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7397 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7398 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7399 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7400 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7401 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7402 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7403 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7404 | #if defined(PYOS_OS2) |
| 7405 | HFILE read, write; |
| 7406 | APIRET rc; |
| 7407 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7408 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7409 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7410 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7411 | |
| 7412 | return Py_BuildValue("(ii)", read, write); |
| 7413 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7414 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7415 | int fds[2]; |
| 7416 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7417 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7418 | if (res != 0) |
| 7419 | return posix_error(); |
| 7420 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7421 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7422 | HANDLE read, write; |
| 7423 | int read_fd, write_fd; |
| 7424 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7425 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7426 | if (!ok) |
| 7427 | return win32_error("CreatePipe", NULL); |
| 7428 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7429 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7430 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7431 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7432 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7433 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7434 | #endif /* HAVE_PIPE */ |
| 7435 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7436 | #ifdef HAVE_PIPE2 |
| 7437 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7438 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7439 | Create a pipe with flags set atomically.\n\ |
| 7440 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7441 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7442 | "); |
| 7443 | |
| 7444 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7445 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7446 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7447 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7448 | int fds[2]; |
| 7449 | int res; |
| 7450 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7451 | flags = PyLong_AsLong(arg); |
| 7452 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7453 | return NULL; |
| 7454 | |
| 7455 | res = pipe2(fds, flags); |
| 7456 | if (res != 0) |
| 7457 | return posix_error(); |
| 7458 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7459 | } |
| 7460 | #endif /* HAVE_PIPE2 */ |
| 7461 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7462 | #ifdef HAVE_WRITEV |
| 7463 | PyDoc_STRVAR(posix_writev__doc__, |
| 7464 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7465 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7466 | arbitrary sequence of buffers.\n\ |
| 7467 | Returns the total bytes written."); |
| 7468 | |
| 7469 | static PyObject * |
| 7470 | posix_writev(PyObject *self, PyObject *args) |
| 7471 | { |
| 7472 | int fd, cnt; |
| 7473 | Py_ssize_t res; |
| 7474 | PyObject *seq; |
| 7475 | struct iovec *iov; |
| 7476 | Py_buffer *buf; |
| 7477 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7478 | return NULL; |
| 7479 | if (!PySequence_Check(seq)) { |
| 7480 | PyErr_SetString(PyExc_TypeError, |
| 7481 | "writev() arg 2 must be a sequence"); |
| 7482 | return NULL; |
| 7483 | } |
| 7484 | cnt = PySequence_Size(seq); |
| 7485 | |
| 7486 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7487 | return NULL; |
| 7488 | } |
| 7489 | |
| 7490 | Py_BEGIN_ALLOW_THREADS |
| 7491 | res = writev(fd, iov, cnt); |
| 7492 | Py_END_ALLOW_THREADS |
| 7493 | |
| 7494 | iov_cleanup(iov, buf, cnt); |
| 7495 | return PyLong_FromSsize_t(res); |
| 7496 | } |
| 7497 | #endif |
| 7498 | |
| 7499 | #ifdef HAVE_PWRITE |
| 7500 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7501 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7502 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7503 | offset unchanged."); |
| 7504 | |
| 7505 | static PyObject * |
| 7506 | posix_pwrite(PyObject *self, PyObject *args) |
| 7507 | { |
| 7508 | Py_buffer pbuf; |
| 7509 | int fd; |
| 7510 | off_t offset; |
| 7511 | Py_ssize_t size; |
| 7512 | |
| 7513 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7514 | return NULL; |
| 7515 | |
| 7516 | if (!_PyVerify_fd(fd)) { |
| 7517 | PyBuffer_Release(&pbuf); |
| 7518 | return posix_error(); |
| 7519 | } |
| 7520 | Py_BEGIN_ALLOW_THREADS |
| 7521 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7522 | Py_END_ALLOW_THREADS |
| 7523 | PyBuffer_Release(&pbuf); |
| 7524 | if (size < 0) |
| 7525 | return posix_error(); |
| 7526 | return PyLong_FromSsize_t(size); |
| 7527 | } |
| 7528 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7529 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7530 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7531 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7532 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7533 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7534 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7535 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7536 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7537 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7538 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7539 | char *filename; |
| 7540 | int mode = 0666; |
| 7541 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7542 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7543 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7544 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7545 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7546 | Py_BEGIN_ALLOW_THREADS |
| 7547 | res = mkfifo(filename, mode); |
| 7548 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7549 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7550 | if (res < 0) |
| 7551 | return posix_error(); |
| 7552 | Py_INCREF(Py_None); |
| 7553 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7554 | } |
| 7555 | #endif |
| 7556 | |
| 7557 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7558 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7559 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7560 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7561 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7562 | named filename. mode specifies both the permissions to use and the\n\ |
| 7563 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7564 | 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] | 7565 | device defines the newly created device special file (probably using\n\ |
| 7566 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7567 | |
| 7568 | |
| 7569 | static PyObject * |
| 7570 | posix_mknod(PyObject *self, PyObject *args) |
| 7571 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7572 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7573 | char *filename; |
| 7574 | int mode = 0600; |
| 7575 | int device = 0; |
| 7576 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7577 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7578 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7579 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7580 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7581 | Py_BEGIN_ALLOW_THREADS |
| 7582 | res = mknod(filename, mode, device); |
| 7583 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7584 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7585 | if (res < 0) |
| 7586 | return posix_error(); |
| 7587 | Py_INCREF(Py_None); |
| 7588 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7589 | } |
| 7590 | #endif |
| 7591 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7592 | #ifdef HAVE_DEVICE_MACROS |
| 7593 | PyDoc_STRVAR(posix_major__doc__, |
| 7594 | "major(device) -> major number\n\ |
| 7595 | Extracts a device major number from a raw device number."); |
| 7596 | |
| 7597 | static PyObject * |
| 7598 | posix_major(PyObject *self, PyObject *args) |
| 7599 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | int device; |
| 7601 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7602 | return NULL; |
| 7603 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7604 | } |
| 7605 | |
| 7606 | PyDoc_STRVAR(posix_minor__doc__, |
| 7607 | "minor(device) -> minor number\n\ |
| 7608 | Extracts a device minor number from a raw device number."); |
| 7609 | |
| 7610 | static PyObject * |
| 7611 | posix_minor(PyObject *self, PyObject *args) |
| 7612 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | int device; |
| 7614 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7615 | return NULL; |
| 7616 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7620 | "makedev(major, minor) -> device number\n\ |
| 7621 | Composes a raw device number from the major and minor device numbers."); |
| 7622 | |
| 7623 | static PyObject * |
| 7624 | posix_makedev(PyObject *self, PyObject *args) |
| 7625 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7626 | int major, minor; |
| 7627 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7628 | return NULL; |
| 7629 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7630 | } |
| 7631 | #endif /* device macros */ |
| 7632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7633 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7634 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7635 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7636 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7637 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7638 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7639 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7640 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7641 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | int fd; |
| 7643 | off_t length; |
| 7644 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7645 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7646 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7647 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7648 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7649 | Py_BEGIN_ALLOW_THREADS |
| 7650 | res = ftruncate(fd, length); |
| 7651 | Py_END_ALLOW_THREADS |
| 7652 | if (res < 0) |
| 7653 | return posix_error(); |
| 7654 | Py_INCREF(Py_None); |
| 7655 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7656 | } |
| 7657 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7658 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7659 | #ifdef HAVE_TRUNCATE |
| 7660 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7661 | "truncate(path, length)\n\n\ |
| 7662 | Truncate the file given by path to length bytes."); |
| 7663 | |
| 7664 | static PyObject * |
| 7665 | posix_truncate(PyObject *self, PyObject *args) |
| 7666 | { |
| 7667 | PyObject *opath; |
| 7668 | const char *path; |
| 7669 | off_t length; |
| 7670 | int res; |
| 7671 | |
| 7672 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7673 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7674 | return NULL; |
| 7675 | path = PyBytes_AsString(opath); |
| 7676 | |
| 7677 | Py_BEGIN_ALLOW_THREADS |
| 7678 | res = truncate(path, length); |
| 7679 | Py_END_ALLOW_THREADS |
| 7680 | Py_DECREF(opath); |
| 7681 | if (res < 0) |
| 7682 | return posix_error(); |
| 7683 | Py_RETURN_NONE; |
| 7684 | } |
| 7685 | #endif |
| 7686 | |
| 7687 | #ifdef HAVE_POSIX_FALLOCATE |
| 7688 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7689 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7690 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7691 | starting from offset and continuing for len bytes."); |
| 7692 | |
| 7693 | static PyObject * |
| 7694 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7695 | { |
| 7696 | off_t len, offset; |
| 7697 | int res, fd; |
| 7698 | |
| 7699 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7700 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7701 | return NULL; |
| 7702 | |
| 7703 | Py_BEGIN_ALLOW_THREADS |
| 7704 | res = posix_fallocate(fd, offset, len); |
| 7705 | Py_END_ALLOW_THREADS |
| 7706 | if (res != 0) { |
| 7707 | errno = res; |
| 7708 | return posix_error(); |
| 7709 | } |
| 7710 | Py_RETURN_NONE; |
| 7711 | } |
| 7712 | #endif |
| 7713 | |
| 7714 | #ifdef HAVE_POSIX_FADVISE |
| 7715 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7716 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7717 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7718 | the kernel to make optimizations.\n\ |
| 7719 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7720 | offset and continuing for len bytes.\n\ |
| 7721 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7722 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7723 | POSIX_FADV_DONTNEED."); |
| 7724 | |
| 7725 | static PyObject * |
| 7726 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7727 | { |
| 7728 | off_t len, offset; |
| 7729 | int res, fd, advice; |
| 7730 | |
| 7731 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7732 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7733 | return NULL; |
| 7734 | |
| 7735 | Py_BEGIN_ALLOW_THREADS |
| 7736 | res = posix_fadvise(fd, offset, len, advice); |
| 7737 | Py_END_ALLOW_THREADS |
| 7738 | if (res != 0) { |
| 7739 | errno = res; |
| 7740 | return posix_error(); |
| 7741 | } |
| 7742 | Py_RETURN_NONE; |
| 7743 | } |
| 7744 | #endif |
| 7745 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7746 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7747 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7748 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7749 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7750 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7751 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7752 | * get re-set with another call for the same key. */ |
| 7753 | static PyObject *posix_putenv_garbage; |
| 7754 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7755 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7756 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7757 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7758 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7759 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7760 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7761 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7762 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7763 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7764 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7765 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7766 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7767 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7768 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7769 | if (newstr == NULL) { |
| 7770 | PyErr_NoMemory(); |
| 7771 | goto error; |
| 7772 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7773 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 7774 | PyErr_Format(PyExc_ValueError, |
| 7775 | "the environment variable is longer than %u characters", |
| 7776 | _MAX_ENV); |
| 7777 | goto error; |
| 7778 | } |
| 7779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7780 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7781 | if (newenv == NULL) |
| 7782 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7785 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7786 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7787 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7788 | PyObject *os1, *os2; |
| 7789 | char *s1, *s2; |
| 7790 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7791 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7792 | if (!PyArg_ParseTuple(args, |
| 7793 | "O&O&:putenv", |
| 7794 | PyUnicode_FSConverter, &os1, |
| 7795 | PyUnicode_FSConverter, &os2)) |
| 7796 | return NULL; |
| 7797 | s1 = PyBytes_AsString(os1); |
| 7798 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7799 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7800 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7801 | if (newstr == NULL) { |
| 7802 | PyErr_NoMemory(); |
| 7803 | goto error; |
| 7804 | } |
| 7805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7806 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7807 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7808 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7809 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7810 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7811 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7812 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7813 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7814 | * this will cause previous value to be collected. This has to |
| 7815 | * happen after the real putenv() call because the old value |
| 7816 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7817 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7818 | /* really not much we can do; just leak */ |
| 7819 | PyErr_Clear(); |
| 7820 | } |
| 7821 | else { |
| 7822 | Py_DECREF(newstr); |
| 7823 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7824 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7825 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7826 | Py_DECREF(os1); |
| 7827 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7828 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7829 | Py_RETURN_NONE; |
| 7830 | |
| 7831 | error: |
| 7832 | #ifndef MS_WINDOWS |
| 7833 | Py_DECREF(os1); |
| 7834 | Py_DECREF(os2); |
| 7835 | #endif |
| 7836 | Py_XDECREF(newstr); |
| 7837 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7838 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7839 | #endif /* putenv */ |
| 7840 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7841 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7842 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7843 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7844 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7845 | |
| 7846 | static PyObject * |
| 7847 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7848 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7849 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7850 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7851 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7852 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7853 | |
| 7854 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7855 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7856 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7857 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7858 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7859 | #ifdef HAVE_BROKEN_UNSETENV |
| 7860 | unsetenv(PyBytes_AS_STRING(name)); |
| 7861 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7862 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7863 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 7864 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7865 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7866 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7867 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7868 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7869 | /* Remove the key from posix_putenv_garbage; |
| 7870 | * this will cause it to be collected. This has to |
| 7871 | * happen after the real unsetenv() call because the |
| 7872 | * old value was still accessible until then. |
| 7873 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7874 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7875 | /* really not much we can do; just leak */ |
| 7876 | PyErr_Clear(); |
| 7877 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7878 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7879 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7880 | } |
| 7881 | #endif /* unsetenv */ |
| 7882 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7883 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7884 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7885 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7886 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7887 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7888 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7889 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | int code; |
| 7891 | char *message; |
| 7892 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7893 | return NULL; |
| 7894 | message = strerror(code); |
| 7895 | if (message == NULL) { |
| 7896 | PyErr_SetString(PyExc_ValueError, |
| 7897 | "strerror() argument out of range"); |
| 7898 | return NULL; |
| 7899 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 7900 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7901 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7902 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7903 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7904 | #ifdef HAVE_SYS_WAIT_H |
| 7905 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7906 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7907 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7908 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7909 | 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] | 7910 | |
| 7911 | static PyObject * |
| 7912 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7913 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7914 | WAIT_TYPE status; |
| 7915 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7918 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7920 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7921 | } |
| 7922 | #endif /* WCOREDUMP */ |
| 7923 | |
| 7924 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7925 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7926 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7927 | 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] | 7928 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7929 | |
| 7930 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7931 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
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 | WAIT_TYPE status; |
| 7934 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7937 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7938 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7939 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7940 | } |
| 7941 | #endif /* WIFCONTINUED */ |
| 7942 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7943 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7944 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7945 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7946 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7947 | |
| 7948 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7949 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7950 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7951 | WAIT_TYPE status; |
| 7952 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7955 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7956 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7957 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7958 | } |
| 7959 | #endif /* WIFSTOPPED */ |
| 7960 | |
| 7961 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7962 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7963 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7964 | 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] | 7965 | |
| 7966 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7967 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7969 | WAIT_TYPE status; |
| 7970 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7973 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7974 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7975 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7976 | } |
| 7977 | #endif /* WIFSIGNALED */ |
| 7978 | |
| 7979 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7980 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7981 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7982 | 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] | 7983 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7984 | |
| 7985 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7986 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7987 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7988 | WAIT_TYPE status; |
| 7989 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7992 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7994 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7995 | } |
| 7996 | #endif /* WIFEXITED */ |
| 7997 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7998 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7999 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8000 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8001 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8002 | |
| 8003 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8004 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8005 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8006 | WAIT_TYPE status; |
| 8007 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 8010 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8012 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8013 | } |
| 8014 | #endif /* WEXITSTATUS */ |
| 8015 | |
| 8016 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8017 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8018 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 8019 | 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] | 8020 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8021 | |
| 8022 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8023 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8024 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8025 | WAIT_TYPE status; |
| 8026 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8029 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8030 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8031 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8032 | } |
| 8033 | #endif /* WTERMSIG */ |
| 8034 | |
| 8035 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8036 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8037 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8038 | Return the signal that stopped the process that provided\n\ |
| 8039 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8040 | |
| 8041 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8042 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8043 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8044 | WAIT_TYPE status; |
| 8045 | WAIT_STATUS_INT(status) = 0; |
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 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8048 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8049 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8050 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8051 | } |
| 8052 | #endif /* WSTOPSIG */ |
| 8053 | |
| 8054 | #endif /* HAVE_SYS_WAIT_H */ |
| 8055 | |
| 8056 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8057 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8058 | #ifdef _SCO_DS |
| 8059 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8060 | needed definitions in sys/statvfs.h */ |
| 8061 | #define _SVID3 |
| 8062 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8063 | #include <sys/statvfs.h> |
| 8064 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8065 | static PyObject* |
| 8066 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8067 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8068 | if (v == NULL) |
| 8069 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8070 | |
| 8071 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8072 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8073 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8074 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8075 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8076 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8077 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8078 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8079 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8080 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8081 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8082 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8083 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8084 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8085 | PyStructSequence_SET_ITEM(v, 2, |
| 8086 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8087 | PyStructSequence_SET_ITEM(v, 3, |
| 8088 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8089 | PyStructSequence_SET_ITEM(v, 4, |
| 8090 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8091 | PyStructSequence_SET_ITEM(v, 5, |
| 8092 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8093 | PyStructSequence_SET_ITEM(v, 6, |
| 8094 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8095 | PyStructSequence_SET_ITEM(v, 7, |
| 8096 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8097 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8098 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8099 | #endif |
| 8100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8101 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8104 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8105 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8106 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8107 | |
| 8108 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8109 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8110 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8111 | int fd, res; |
| 8112 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8115 | return NULL; |
| 8116 | Py_BEGIN_ALLOW_THREADS |
| 8117 | res = fstatvfs(fd, &st); |
| 8118 | Py_END_ALLOW_THREADS |
| 8119 | if (res != 0) |
| 8120 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8121 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8122 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8123 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8124 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8125 | |
| 8126 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8127 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8128 | #include <sys/statvfs.h> |
| 8129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8130 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8131 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8132 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8133 | |
| 8134 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8135 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8136 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8137 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8138 | int res; |
| 8139 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8140 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8141 | return NULL; |
| 8142 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8143 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8144 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8145 | if (res != 0) { |
| 8146 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8147 | Py_DECREF(path); |
| 8148 | return NULL; |
| 8149 | } |
| 8150 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8152 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8153 | } |
| 8154 | #endif /* HAVE_STATVFS */ |
| 8155 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8156 | #ifdef MS_WINDOWS |
| 8157 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8158 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8159 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8160 | |
| 8161 | static PyObject * |
| 8162 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8163 | { |
| 8164 | BOOL retval; |
| 8165 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8166 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8167 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8168 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8169 | return NULL; |
| 8170 | |
| 8171 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8172 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8173 | Py_END_ALLOW_THREADS |
| 8174 | if (retval == 0) |
| 8175 | return PyErr_SetFromWindowsErr(0); |
| 8176 | |
| 8177 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8178 | } |
| 8179 | #endif |
| 8180 | |
| 8181 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8182 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8183 | * It maps strings representing configuration variable names to |
| 8184 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8185 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8186 | * rarely-used constants. There are three separate tables that use |
| 8187 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8188 | * |
| 8189 | * This code is always included, even if none of the interfaces that |
| 8190 | * need it are included. The #if hackery needed to avoid it would be |
| 8191 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8192 | */ |
| 8193 | struct constdef { |
| 8194 | char *name; |
| 8195 | long value; |
| 8196 | }; |
| 8197 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8198 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8199 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8200 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8201 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8202 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8203 | *valuep = PyLong_AS_LONG(arg); |
| 8204 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8205 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8206 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8207 | /* look up the value in the table using a binary search */ |
| 8208 | size_t lo = 0; |
| 8209 | size_t mid; |
| 8210 | size_t hi = tablesize; |
| 8211 | int cmp; |
| 8212 | const char *confname; |
| 8213 | if (!PyUnicode_Check(arg)) { |
| 8214 | PyErr_SetString(PyExc_TypeError, |
| 8215 | "configuration names must be strings or integers"); |
| 8216 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8217 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8218 | confname = _PyUnicode_AsString(arg); |
| 8219 | if (confname == NULL) |
| 8220 | return 0; |
| 8221 | while (lo < hi) { |
| 8222 | mid = (lo + hi) / 2; |
| 8223 | cmp = strcmp(confname, table[mid].name); |
| 8224 | if (cmp < 0) |
| 8225 | hi = mid; |
| 8226 | else if (cmp > 0) |
| 8227 | lo = mid + 1; |
| 8228 | else { |
| 8229 | *valuep = table[mid].value; |
| 8230 | return 1; |
| 8231 | } |
| 8232 | } |
| 8233 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8234 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8235 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8236 | } |
| 8237 | |
| 8238 | |
| 8239 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8240 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8241 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8242 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8243 | #endif |
| 8244 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8245 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8246 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8247 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8248 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8249 | #endif |
| 8250 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8251 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8252 | #endif |
| 8253 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8255 | #endif |
| 8256 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8257 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8258 | #endif |
| 8259 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8260 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8261 | #endif |
| 8262 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8263 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8264 | #endif |
| 8265 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8266 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8267 | #endif |
| 8268 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8269 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8270 | #endif |
| 8271 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8272 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8273 | #endif |
| 8274 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8275 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8276 | #endif |
| 8277 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8278 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8279 | #endif |
| 8280 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8281 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8282 | #endif |
| 8283 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8284 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8285 | #endif |
| 8286 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8287 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8288 | #endif |
| 8289 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8290 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8291 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8292 | #ifdef _PC_ACL_ENABLED |
| 8293 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8294 | #endif |
| 8295 | #ifdef _PC_MIN_HOLE_SIZE |
| 8296 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8297 | #endif |
| 8298 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8299 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8300 | #endif |
| 8301 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8302 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8303 | #endif |
| 8304 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8305 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8306 | #endif |
| 8307 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8308 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8309 | #endif |
| 8310 | #ifdef _PC_REC_XFER_ALIGN |
| 8311 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8312 | #endif |
| 8313 | #ifdef _PC_SYMLINK_MAX |
| 8314 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8315 | #endif |
| 8316 | #ifdef _PC_XATTR_ENABLED |
| 8317 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8318 | #endif |
| 8319 | #ifdef _PC_XATTR_EXISTS |
| 8320 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8321 | #endif |
| 8322 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8323 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8324 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8325 | }; |
| 8326 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8327 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8328 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8329 | { |
| 8330 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8331 | sizeof(posix_constants_pathconf) |
| 8332 | / sizeof(struct constdef)); |
| 8333 | } |
| 8334 | #endif |
| 8335 | |
| 8336 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8337 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8338 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8339 | 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] | 8340 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8341 | |
| 8342 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8343 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8344 | { |
| 8345 | PyObject *result = NULL; |
| 8346 | int name, fd; |
| 8347 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8348 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8349 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8350 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8351 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8352 | errno = 0; |
| 8353 | limit = fpathconf(fd, name); |
| 8354 | if (limit == -1 && errno != 0) |
| 8355 | posix_error(); |
| 8356 | else |
| 8357 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8358 | } |
| 8359 | return result; |
| 8360 | } |
| 8361 | #endif |
| 8362 | |
| 8363 | |
| 8364 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8365 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8366 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8367 | 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] | 8368 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8369 | |
| 8370 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8371 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8372 | { |
| 8373 | PyObject *result = NULL; |
| 8374 | int name; |
| 8375 | char *path; |
| 8376 | |
| 8377 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8378 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8379 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8381 | errno = 0; |
| 8382 | limit = pathconf(path, name); |
| 8383 | if (limit == -1 && errno != 0) { |
| 8384 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8385 | /* could be a path or name problem */ |
| 8386 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8387 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8388 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8389 | } |
| 8390 | else |
| 8391 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8392 | } |
| 8393 | return result; |
| 8394 | } |
| 8395 | #endif |
| 8396 | |
| 8397 | #ifdef HAVE_CONFSTR |
| 8398 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8399 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8401 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8402 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8404 | #endif |
| 8405 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8407 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8408 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8410 | #endif |
| 8411 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8413 | #endif |
| 8414 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8415 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8416 | #endif |
| 8417 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8419 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8420 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8421 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8422 | #endif |
| 8423 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8425 | #endif |
| 8426 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8428 | #endif |
| 8429 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8431 | #endif |
| 8432 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8433 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8434 | #endif |
| 8435 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8436 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8437 | #endif |
| 8438 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8440 | #endif |
| 8441 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8442 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8443 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8444 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8445 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8446 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8447 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8448 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8449 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8450 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8451 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8452 | #endif |
| 8453 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8454 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8455 | #endif |
| 8456 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8458 | #endif |
| 8459 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8461 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8462 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8463 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8464 | #endif |
| 8465 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8467 | #endif |
| 8468 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8470 | #endif |
| 8471 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8473 | #endif |
| 8474 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8475 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8476 | #endif |
| 8477 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8479 | #endif |
| 8480 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8482 | #endif |
| 8483 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8484 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8485 | #endif |
| 8486 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8487 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8488 | #endif |
| 8489 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8490 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8491 | #endif |
| 8492 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8494 | #endif |
| 8495 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8497 | #endif |
| 8498 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8500 | #endif |
| 8501 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8502 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8503 | #endif |
| 8504 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8505 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8506 | #endif |
| 8507 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8509 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8510 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8511 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8512 | #endif |
| 8513 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8515 | #endif |
| 8516 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8517 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8518 | #endif |
| 8519 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8520 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8521 | #endif |
| 8522 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8523 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8524 | #endif |
| 8525 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8526 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8527 | #endif |
| 8528 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8529 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8530 | #endif |
| 8531 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8532 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8533 | #endif |
| 8534 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8536 | #endif |
| 8537 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8538 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8539 | #endif |
| 8540 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8541 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8545 | #endif |
| 8546 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8547 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8548 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8549 | }; |
| 8550 | |
| 8551 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8552 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8553 | { |
| 8554 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8555 | sizeof(posix_constants_confstr) |
| 8556 | / sizeof(struct constdef)); |
| 8557 | } |
| 8558 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8559 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8560 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8561 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8562 | |
| 8563 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8564 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8565 | { |
| 8566 | PyObject *result = NULL; |
| 8567 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8568 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8569 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8570 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8571 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8572 | return NULL; |
| 8573 | |
| 8574 | errno = 0; |
| 8575 | len = confstr(name, buffer, sizeof(buffer)); |
| 8576 | if (len == 0) { |
| 8577 | if (errno) { |
| 8578 | posix_error(); |
| 8579 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8580 | } |
| 8581 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8582 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8583 | } |
| 8584 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8585 | |
| 8586 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8587 | char *buf = PyMem_Malloc(len); |
| 8588 | if (buf == NULL) |
| 8589 | return PyErr_NoMemory(); |
| 8590 | confstr(name, buf, len); |
| 8591 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8592 | PyMem_Free(buf); |
| 8593 | } |
| 8594 | else |
| 8595 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8596 | return result; |
| 8597 | } |
| 8598 | #endif |
| 8599 | |
| 8600 | |
| 8601 | #ifdef HAVE_SYSCONF |
| 8602 | static struct constdef posix_constants_sysconf[] = { |
| 8603 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8611 | #endif |
| 8612 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8614 | #endif |
| 8615 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8617 | #endif |
| 8618 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8619 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8622 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | #endif |
| 8627 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8629 | #endif |
| 8630 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8632 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8633 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8635 | #endif |
| 8636 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8638 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8639 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8640 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8641 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8642 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8643 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8644 | #endif |
| 8645 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8646 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | #endif |
| 8648 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8649 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8650 | #endif |
| 8651 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8653 | #endif |
| 8654 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8655 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8656 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8657 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8659 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8660 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8662 | #endif |
| 8663 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
| 8672 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8674 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8675 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8677 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8678 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
| 8702 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
| 8708 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
| 8717 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8719 | #endif |
| 8720 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8722 | #endif |
| 8723 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | #endif |
| 8726 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | #endif |
| 8729 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8734 | #endif |
| 8735 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8737 | #endif |
| 8738 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8739 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8740 | #endif |
| 8741 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8742 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | #endif |
| 8744 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8746 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8747 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8749 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8750 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8752 | #endif |
| 8753 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | #endif |
| 8756 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8758 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8759 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8761 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8762 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8764 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8765 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8767 | #endif |
| 8768 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8770 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8771 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | #endif |
| 8774 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #endif |
| 8777 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8778 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8779 | #endif |
| 8780 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8782 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8783 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8785 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8786 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8788 | #endif |
| 8789 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8791 | #endif |
| 8792 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8793 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8794 | #endif |
| 8795 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #endif |
| 8798 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8799 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8800 | #endif |
| 8801 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #endif |
| 8804 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8805 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8806 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8807 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8809 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8810 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8811 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8812 | #endif |
| 8813 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8814 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8815 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8816 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8818 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8819 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8821 | #endif |
| 8822 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8824 | #endif |
| 8825 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8830 | #endif |
| 8831 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8832 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8836 | #endif |
| 8837 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | #endif |
| 8843 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8845 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8846 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8848 | #endif |
| 8849 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8851 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8852 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8853 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8854 | #endif |
| 8855 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8857 | #endif |
| 8858 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8862 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8866 | #endif |
| 8867 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8869 | #endif |
| 8870 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8871 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8872 | #endif |
| 8873 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8875 | #endif |
| 8876 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8877 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8878 | #endif |
| 8879 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8881 | #endif |
| 8882 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | #endif |
| 8885 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #endif |
| 8894 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8899 | #endif |
| 8900 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | #endif |
| 8906 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8908 | #endif |
| 8909 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8911 | #endif |
| 8912 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8914 | #endif |
| 8915 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8916 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8920 | #endif |
| 8921 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8922 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8925 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8929 | #endif |
| 8930 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #endif |
| 8933 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8934 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8935 | #endif |
| 8936 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8937 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8938 | #endif |
| 8939 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8941 | #endif |
| 8942 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8947 | #endif |
| 8948 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8950 | #endif |
| 8951 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8953 | #endif |
| 8954 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8955 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8956 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8957 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8959 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8960 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | #endif |
| 8972 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8973 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #endif |
| 8975 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #endif |
| 8978 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | #endif |
| 8981 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8985 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8986 | #endif |
| 8987 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8992 | #endif |
| 8993 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #endif |
| 8996 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #endif |
| 8999 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | #endif |
| 9002 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9012 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #endif |
| 9017 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9018 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | #endif |
| 9020 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9022 | #endif |
| 9023 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9030 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | #endif |
| 9032 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9033 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9034 | #endif |
| 9035 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9037 | #endif |
| 9038 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9039 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9040 | #endif |
| 9041 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9042 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9043 | #endif |
| 9044 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9046 | #endif |
| 9047 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9048 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9049 | #endif |
| 9050 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9051 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9052 | #endif |
| 9053 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9054 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9055 | #endif |
| 9056 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9057 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9058 | #endif |
| 9059 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9060 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9061 | #endif |
| 9062 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9064 | #endif |
| 9065 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9067 | #endif |
| 9068 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #endif |
| 9071 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9072 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9073 | #endif |
| 9074 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9075 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9076 | #endif |
| 9077 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9078 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9079 | #endif |
| 9080 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9082 | #endif |
| 9083 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9084 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9085 | #endif |
| 9086 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9087 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9088 | #endif |
| 9089 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9090 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9091 | #endif |
| 9092 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9093 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9094 | #endif |
| 9095 | }; |
| 9096 | |
| 9097 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9098 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9099 | { |
| 9100 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9101 | sizeof(posix_constants_sysconf) |
| 9102 | / sizeof(struct constdef)); |
| 9103 | } |
| 9104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9105 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9106 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9107 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9108 | |
| 9109 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9110 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9111 | { |
| 9112 | PyObject *result = NULL; |
| 9113 | int name; |
| 9114 | |
| 9115 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9116 | int value; |
| 9117 | |
| 9118 | errno = 0; |
| 9119 | value = sysconf(name); |
| 9120 | if (value == -1 && errno != 0) |
| 9121 | posix_error(); |
| 9122 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9123 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9124 | } |
| 9125 | return result; |
| 9126 | } |
| 9127 | #endif |
| 9128 | |
| 9129 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9130 | /* This code is used to ensure that the tables of configuration value names |
| 9131 | * are in sorted order as required by conv_confname(), and also to build the |
| 9132 | * the exported dictionaries that are used to publish information about the |
| 9133 | * names available on the host platform. |
| 9134 | * |
| 9135 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9136 | * when used, even for platforms we're not able to test on. It also makes |
| 9137 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9138 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9139 | |
| 9140 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9141 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9142 | { |
| 9143 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9144 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9145 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9146 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9147 | |
| 9148 | return strcmp(c1->name, c2->name); |
| 9149 | } |
| 9150 | |
| 9151 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9152 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9153 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9154 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9155 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9156 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9157 | |
| 9158 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9159 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9160 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9161 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9162 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9163 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9164 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9165 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9166 | Py_XDECREF(o); |
| 9167 | Py_DECREF(d); |
| 9168 | return -1; |
| 9169 | } |
| 9170 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9171 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9172 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9173 | } |
| 9174 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9175 | /* Return -1 on failure, 0 on success. */ |
| 9176 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9177 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9178 | { |
| 9179 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9180 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9181 | sizeof(posix_constants_pathconf) |
| 9182 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9183 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9184 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9185 | #endif |
| 9186 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9187 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9188 | sizeof(posix_constants_confstr) |
| 9189 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9190 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9191 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9192 | #endif |
| 9193 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9194 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9195 | sizeof(posix_constants_sysconf) |
| 9196 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9197 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9198 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9199 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9200 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9201 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9202 | |
| 9203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9204 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9205 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9206 | 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] | 9207 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9208 | |
| 9209 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9210 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9211 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9212 | abort(); |
| 9213 | /*NOTREACHED*/ |
| 9214 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9215 | return NULL; |
| 9216 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9217 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9218 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9219 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9220 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9221 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9222 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9223 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9224 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9225 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9226 | application (if any) its extension is associated.\n\ |
| 9227 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9228 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9229 | \n\ |
| 9230 | startfile returns as soon as the associated application is launched.\n\ |
| 9231 | There is no option to wait for the application to close, and no way\n\ |
| 9232 | to retrieve the application's exit status.\n\ |
| 9233 | \n\ |
| 9234 | The filepath is relative to the current directory. If you want to use\n\ |
| 9235 | 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] | 9236 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9237 | |
| 9238 | static PyObject * |
| 9239 | win32_startfile(PyObject *self, PyObject *args) |
| 9240 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9241 | PyObject *ofilepath; |
| 9242 | char *filepath; |
| 9243 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9244 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9245 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9246 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9247 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9248 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9249 | &unipath, &operation)) { |
| 9250 | PyErr_Clear(); |
| 9251 | goto normal; |
| 9252 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9253 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9255 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9256 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9257 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9258 | PyErr_Clear(); |
| 9259 | operation = NULL; |
| 9260 | goto normal; |
| 9261 | } |
| 9262 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9263 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9264 | wpath = PyUnicode_AsUnicode(unipath); |
| 9265 | if (wpath == NULL) |
| 9266 | goto normal; |
| 9267 | if (uoperation) { |
| 9268 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9269 | if (woperation == NULL) |
| 9270 | goto normal; |
| 9271 | } |
| 9272 | else |
| 9273 | woperation = NULL; |
| 9274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9276 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9277 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | Py_END_ALLOW_THREADS |
| 9279 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9280 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9282 | win32_error_object("startfile", unipath); |
| 9283 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9284 | } |
| 9285 | Py_INCREF(Py_None); |
| 9286 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9287 | |
| 9288 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9289 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9290 | PyUnicode_FSConverter, &ofilepath, |
| 9291 | &operation)) |
| 9292 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9293 | if (win32_warn_bytes_api()) { |
| 9294 | Py_DECREF(ofilepath); |
| 9295 | return NULL; |
| 9296 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9297 | filepath = PyBytes_AsString(ofilepath); |
| 9298 | Py_BEGIN_ALLOW_THREADS |
| 9299 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9300 | NULL, NULL, SW_SHOWNORMAL); |
| 9301 | Py_END_ALLOW_THREADS |
| 9302 | if (rc <= (HINSTANCE)32) { |
| 9303 | PyObject *errval = win32_error("startfile", filepath); |
| 9304 | Py_DECREF(ofilepath); |
| 9305 | return errval; |
| 9306 | } |
| 9307 | Py_DECREF(ofilepath); |
| 9308 | Py_INCREF(Py_None); |
| 9309 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9310 | } |
| 9311 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9312 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9313 | #ifdef HAVE_GETLOADAVG |
| 9314 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9315 | "getloadavg() -> (float, float, float)\n\n\ |
| 9316 | Return the number of processes in the system run queue averaged over\n\ |
| 9317 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9318 | was unobtainable"); |
| 9319 | |
| 9320 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9321 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9322 | { |
| 9323 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9324 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9325 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9326 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9327 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9328 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9329 | } |
| 9330 | #endif |
| 9331 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9332 | #ifdef MS_WINDOWS |
| 9333 | |
| 9334 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9335 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9336 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9337 | |
| 9338 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9339 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9340 | DWORD dwFlags ); |
| 9341 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9342 | BYTE *pbBuffer ); |
| 9343 | |
| 9344 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9345 | /* This handle is never explicitly released. Instead, the operating |
| 9346 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9347 | static HCRYPTPROV hCryptProv = 0; |
| 9348 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9349 | static PyObject* |
| 9350 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9351 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9352 | int howMany; |
| 9353 | PyObject* result; |
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 | /* Read arguments */ |
| 9356 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9357 | return NULL; |
| 9358 | if (howMany < 0) |
| 9359 | return PyErr_Format(PyExc_ValueError, |
| 9360 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9361 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9362 | if (hCryptProv == 0) { |
| 9363 | HINSTANCE hAdvAPI32 = NULL; |
| 9364 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9365 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9366 | /* Obtain handle to the DLL containing CryptoAPI |
| 9367 | This should not fail */ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 9368 | hAdvAPI32 = GetModuleHandleW(L"advapi32.dll"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | if(hAdvAPI32 == NULL) |
| 9370 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9371 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9372 | /* Obtain pointers to the CryptoAPI functions |
| 9373 | This will fail on some early versions of Win95 */ |
| 9374 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9375 | hAdvAPI32, |
| 9376 | "CryptAcquireContextA"); |
| 9377 | if (pCryptAcquireContext == NULL) |
| 9378 | return PyErr_Format(PyExc_NotImplementedError, |
| 9379 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9382 | hAdvAPI32, "CryptGenRandom"); |
| 9383 | if (pCryptGenRandom == NULL) |
| 9384 | return PyErr_Format(PyExc_NotImplementedError, |
| 9385 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9387 | /* Acquire context */ |
| 9388 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9389 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9390 | return win32_error("CryptAcquireContext", NULL); |
| 9391 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9393 | /* Allocate bytes */ |
| 9394 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9395 | if (result != NULL) { |
| 9396 | /* Get random data */ |
| 9397 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9398 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9399 | PyBytes_AS_STRING(result))) { |
| 9400 | Py_DECREF(result); |
| 9401 | return win32_error("CryptGenRandom", NULL); |
| 9402 | } |
| 9403 | } |
| 9404 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9405 | } |
| 9406 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9407 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9408 | PyDoc_STRVAR(device_encoding__doc__, |
| 9409 | "device_encoding(fd) -> str\n\n\ |
| 9410 | Return a string describing the encoding of the device\n\ |
| 9411 | if the output is a terminal; else return None."); |
| 9412 | |
| 9413 | static PyObject * |
| 9414 | device_encoding(PyObject *self, PyObject *args) |
| 9415 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9416 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9417 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9418 | UINT cp; |
| 9419 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9420 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9421 | return NULL; |
| 9422 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9423 | Py_INCREF(Py_None); |
| 9424 | return Py_None; |
| 9425 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9426 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9427 | if (fd == 0) |
| 9428 | cp = GetConsoleCP(); |
| 9429 | else if (fd == 1 || fd == 2) |
| 9430 | cp = GetConsoleOutputCP(); |
| 9431 | else |
| 9432 | cp = 0; |
| 9433 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9434 | has no console */ |
| 9435 | if (cp != 0) |
| 9436 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9437 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9438 | { |
| 9439 | char *codeset = nl_langinfo(CODESET); |
| 9440 | if (codeset != NULL && codeset[0] != 0) |
| 9441 | return PyUnicode_FromString(codeset); |
| 9442 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9443 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9444 | Py_INCREF(Py_None); |
| 9445 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9446 | } |
| 9447 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9448 | #ifdef __VMS |
| 9449 | /* Use openssl random routine */ |
| 9450 | #include <openssl/rand.h> |
| 9451 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9452 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9453 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9454 | |
| 9455 | static PyObject* |
| 9456 | vms_urandom(PyObject *self, PyObject *args) |
| 9457 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9458 | int howMany; |
| 9459 | PyObject* result; |
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 | /* Read arguments */ |
| 9462 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9463 | return NULL; |
| 9464 | if (howMany < 0) |
| 9465 | return PyErr_Format(PyExc_ValueError, |
| 9466 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9468 | /* Allocate bytes */ |
| 9469 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9470 | if (result != NULL) { |
| 9471 | /* Get random data */ |
| 9472 | if (RAND_pseudo_bytes((unsigned char*) |
| 9473 | PyBytes_AS_STRING(result), |
| 9474 | howMany) < 0) { |
| 9475 | Py_DECREF(result); |
| 9476 | return PyErr_Format(PyExc_ValueError, |
| 9477 | "RAND_pseudo_bytes"); |
| 9478 | } |
| 9479 | } |
| 9480 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9481 | } |
| 9482 | #endif |
| 9483 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9484 | #ifdef HAVE_SETRESUID |
| 9485 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9486 | "setresuid(ruid, euid, suid)\n\n\ |
| 9487 | Set the current process's real, effective, and saved user ids."); |
| 9488 | |
| 9489 | static PyObject* |
| 9490 | posix_setresuid (PyObject *self, PyObject *args) |
| 9491 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9492 | /* We assume uid_t is no larger than a long. */ |
| 9493 | long ruid, euid, suid; |
| 9494 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9495 | return NULL; |
| 9496 | if (setresuid(ruid, euid, suid) < 0) |
| 9497 | return posix_error(); |
| 9498 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9499 | } |
| 9500 | #endif |
| 9501 | |
| 9502 | #ifdef HAVE_SETRESGID |
| 9503 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9504 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9505 | Set the current process's real, effective, and saved group ids."); |
| 9506 | |
| 9507 | static PyObject* |
| 9508 | posix_setresgid (PyObject *self, PyObject *args) |
| 9509 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9510 | /* We assume uid_t is no larger than a long. */ |
| 9511 | long rgid, egid, sgid; |
| 9512 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9513 | return NULL; |
| 9514 | if (setresgid(rgid, egid, sgid) < 0) |
| 9515 | return posix_error(); |
| 9516 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9517 | } |
| 9518 | #endif |
| 9519 | |
| 9520 | #ifdef HAVE_GETRESUID |
| 9521 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9522 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9523 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9524 | |
| 9525 | static PyObject* |
| 9526 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9527 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9528 | uid_t ruid, euid, suid; |
| 9529 | long l_ruid, l_euid, l_suid; |
| 9530 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9531 | return posix_error(); |
| 9532 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9533 | l_ruid = ruid; |
| 9534 | l_euid = euid; |
| 9535 | l_suid = suid; |
| 9536 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9537 | } |
| 9538 | #endif |
| 9539 | |
| 9540 | #ifdef HAVE_GETRESGID |
| 9541 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9542 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9543 | 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] | 9544 | |
| 9545 | static PyObject* |
| 9546 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9547 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9548 | uid_t rgid, egid, sgid; |
| 9549 | long l_rgid, l_egid, l_sgid; |
| 9550 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9551 | return posix_error(); |
| 9552 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9553 | l_rgid = rgid; |
| 9554 | l_egid = egid; |
| 9555 | l_sgid = sgid; |
| 9556 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9557 | } |
| 9558 | #endif |
| 9559 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9560 | /* Posix *at family of functions: |
| 9561 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9562 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9563 | unlinkat, utimensat, mkfifoat */ |
| 9564 | |
| 9565 | #ifdef HAVE_FACCESSAT |
| 9566 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9567 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9568 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9569 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9570 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9571 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9572 | is interpreted relative to the current working directory."); |
| 9573 | |
| 9574 | static PyObject * |
| 9575 | posix_faccessat(PyObject *self, PyObject *args) |
| 9576 | { |
| 9577 | PyObject *opath; |
| 9578 | char *path; |
| 9579 | int mode; |
| 9580 | int res; |
| 9581 | int dirfd, flags = 0; |
| 9582 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9583 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9584 | return NULL; |
| 9585 | path = PyBytes_AsString(opath); |
| 9586 | Py_BEGIN_ALLOW_THREADS |
| 9587 | res = faccessat(dirfd, path, mode, flags); |
| 9588 | Py_END_ALLOW_THREADS |
| 9589 | Py_DECREF(opath); |
| 9590 | return PyBool_FromLong(res == 0); |
| 9591 | } |
| 9592 | #endif |
| 9593 | |
| 9594 | #ifdef HAVE_FCHMODAT |
| 9595 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9596 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9597 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9598 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9599 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9600 | is interpreted relative to the current working directory."); |
| 9601 | |
| 9602 | static PyObject * |
| 9603 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9604 | { |
| 9605 | int dirfd, mode, res; |
| 9606 | int flags = 0; |
| 9607 | PyObject *opath; |
| 9608 | char *path; |
| 9609 | |
| 9610 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9611 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9612 | return NULL; |
| 9613 | |
| 9614 | path = PyBytes_AsString(opath); |
| 9615 | |
| 9616 | Py_BEGIN_ALLOW_THREADS |
| 9617 | res = fchmodat(dirfd, path, mode, flags); |
| 9618 | Py_END_ALLOW_THREADS |
| 9619 | Py_DECREF(opath); |
| 9620 | if (res < 0) |
| 9621 | return posix_error(); |
| 9622 | Py_RETURN_NONE; |
| 9623 | } |
| 9624 | #endif /* HAVE_FCHMODAT */ |
| 9625 | |
| 9626 | #ifdef HAVE_FCHOWNAT |
| 9627 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9628 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9629 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9630 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9631 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9632 | is interpreted relative to the current working directory."); |
| 9633 | |
| 9634 | static PyObject * |
| 9635 | posix_fchownat(PyObject *self, PyObject *args) |
| 9636 | { |
| 9637 | PyObject *opath; |
| 9638 | int dirfd, res; |
| 9639 | long uid, gid; |
| 9640 | int flags = 0; |
| 9641 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9642 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9643 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9644 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9645 | return NULL; |
| 9646 | |
| 9647 | path = PyBytes_AsString(opath); |
| 9648 | |
| 9649 | Py_BEGIN_ALLOW_THREADS |
| 9650 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9651 | Py_END_ALLOW_THREADS |
| 9652 | Py_DECREF(opath); |
| 9653 | if (res < 0) |
| 9654 | return posix_error(); |
| 9655 | Py_RETURN_NONE; |
| 9656 | } |
| 9657 | #endif /* HAVE_FCHOWNAT */ |
| 9658 | |
| 9659 | #ifdef HAVE_FSTATAT |
| 9660 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9661 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9662 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9663 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9664 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9665 | is interpreted relative to the current working directory."); |
| 9666 | |
| 9667 | static PyObject * |
| 9668 | posix_fstatat(PyObject *self, PyObject *args) |
| 9669 | { |
| 9670 | PyObject *opath; |
| 9671 | char *path; |
| 9672 | STRUCT_STAT st; |
| 9673 | int dirfd, res, flags = 0; |
| 9674 | |
| 9675 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9676 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9677 | return NULL; |
| 9678 | path = PyBytes_AsString(opath); |
| 9679 | |
| 9680 | Py_BEGIN_ALLOW_THREADS |
| 9681 | res = fstatat(dirfd, path, &st, flags); |
| 9682 | Py_END_ALLOW_THREADS |
| 9683 | Py_DECREF(opath); |
| 9684 | if (res != 0) |
| 9685 | return posix_error(); |
| 9686 | |
| 9687 | return _pystat_fromstructstat(&st); |
| 9688 | } |
| 9689 | #endif |
| 9690 | |
| 9691 | #ifdef HAVE_FUTIMESAT |
| 9692 | PyDoc_STRVAR(posix_futimesat__doc__, |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9693 | "futimesat(dirfd, path[, (atime, mtime)])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9694 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9695 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9696 | is interpreted relative to the current working directory."); |
| 9697 | |
| 9698 | static PyObject * |
| 9699 | posix_futimesat(PyObject *self, PyObject *args) |
| 9700 | { |
| 9701 | PyObject *opath; |
| 9702 | char *path; |
| 9703 | int res, dirfd; |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9704 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9705 | time_t atime, mtime; |
| 9706 | long ausec, musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9707 | |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9708 | if (!PyArg_ParseTuple(args, "iO&|O:futimesat", |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9709 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9710 | return NULL; |
| 9711 | path = PyBytes_AsString(opath); |
| 9712 | if (arg == Py_None) { |
| 9713 | /* optional time values not given */ |
| 9714 | Py_BEGIN_ALLOW_THREADS |
| 9715 | res = futimesat(dirfd, path, NULL); |
| 9716 | Py_END_ALLOW_THREADS |
| 9717 | } |
| 9718 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9719 | PyErr_SetString(PyExc_TypeError, |
| 9720 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9721 | Py_DECREF(opath); |
| 9722 | return NULL; |
| 9723 | } |
| 9724 | else { |
| 9725 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9726 | &atime, &ausec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9727 | Py_DECREF(opath); |
| 9728 | return NULL; |
| 9729 | } |
| 9730 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9731 | &mtime, &musec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9732 | Py_DECREF(opath); |
| 9733 | return NULL; |
| 9734 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9735 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9736 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9737 | { |
| 9738 | #ifdef HAVE_UTIMENSAT |
| 9739 | struct timespec buf[2]; |
| 9740 | buf[0].tv_sec = atime; |
| 9741 | buf[0].tv_nsec = ausec; |
| 9742 | buf[1].tv_sec = mtime; |
| 9743 | buf[1].tv_nsec = musec; |
| 9744 | res = utimensat(dirfd, path, buf, 0); |
| 9745 | #else |
| 9746 | struct timeval buf[2]; |
| 9747 | buf[0].tv_sec = atime; |
| 9748 | buf[0].tv_usec = ausec; |
| 9749 | buf[1].tv_sec = mtime; |
| 9750 | buf[1].tv_usec = musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9751 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9752 | #endif |
| 9753 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9754 | Py_END_ALLOW_THREADS |
| 9755 | } |
| 9756 | Py_DECREF(opath); |
| 9757 | if (res < 0) { |
| 9758 | return posix_error(); |
| 9759 | } |
| 9760 | Py_RETURN_NONE; |
| 9761 | } |
| 9762 | #endif |
| 9763 | |
| 9764 | #ifdef HAVE_LINKAT |
| 9765 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9766 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9767 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9768 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9769 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9770 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9771 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9772 | also applies for dstpath."); |
| 9773 | |
| 9774 | static PyObject * |
| 9775 | posix_linkat(PyObject *self, PyObject *args) |
| 9776 | { |
| 9777 | PyObject *osrc, *odst; |
| 9778 | char *src, *dst; |
| 9779 | int res, srcfd, dstfd; |
| 9780 | int flags = 0; |
| 9781 | |
| 9782 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9783 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9784 | return NULL; |
| 9785 | src = PyBytes_AsString(osrc); |
| 9786 | dst = PyBytes_AsString(odst); |
| 9787 | Py_BEGIN_ALLOW_THREADS |
| 9788 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9789 | Py_END_ALLOW_THREADS |
| 9790 | Py_DECREF(osrc); |
| 9791 | Py_DECREF(odst); |
| 9792 | if (res < 0) |
| 9793 | return posix_error(); |
| 9794 | Py_RETURN_NONE; |
| 9795 | } |
| 9796 | #endif /* HAVE_LINKAT */ |
| 9797 | |
| 9798 | #ifdef HAVE_MKDIRAT |
| 9799 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9800 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9801 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9802 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9803 | is interpreted relative to the current working directory."); |
| 9804 | |
| 9805 | static PyObject * |
| 9806 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9807 | { |
| 9808 | int res, dirfd; |
| 9809 | PyObject *opath; |
| 9810 | char *path; |
| 9811 | int mode = 0777; |
| 9812 | |
| 9813 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9814 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9815 | return NULL; |
| 9816 | path = PyBytes_AsString(opath); |
| 9817 | Py_BEGIN_ALLOW_THREADS |
| 9818 | res = mkdirat(dirfd, path, mode); |
| 9819 | Py_END_ALLOW_THREADS |
| 9820 | Py_DECREF(opath); |
| 9821 | if (res < 0) |
| 9822 | return posix_error(); |
| 9823 | Py_RETURN_NONE; |
| 9824 | } |
| 9825 | #endif |
| 9826 | |
| 9827 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9828 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9829 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9830 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9831 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9832 | is interpreted relative to the current working directory."); |
| 9833 | |
| 9834 | static PyObject * |
| 9835 | posix_mknodat(PyObject *self, PyObject *args) |
| 9836 | { |
| 9837 | PyObject *opath; |
| 9838 | char *filename; |
| 9839 | int mode = 0600; |
| 9840 | int device = 0; |
| 9841 | int res, dirfd; |
| 9842 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9843 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9844 | return NULL; |
| 9845 | filename = PyBytes_AS_STRING(opath); |
| 9846 | Py_BEGIN_ALLOW_THREADS |
| 9847 | res = mknodat(dirfd, filename, mode, device); |
| 9848 | Py_END_ALLOW_THREADS |
| 9849 | Py_DECREF(opath); |
| 9850 | if (res < 0) |
| 9851 | return posix_error(); |
| 9852 | Py_RETURN_NONE; |
| 9853 | } |
| 9854 | #endif |
| 9855 | |
| 9856 | #ifdef HAVE_OPENAT |
| 9857 | PyDoc_STRVAR(posix_openat__doc__, |
| 9858 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9859 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9860 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9861 | is interpreted relative to the current working directory."); |
| 9862 | |
| 9863 | static PyObject * |
| 9864 | posix_openat(PyObject *self, PyObject *args) |
| 9865 | { |
| 9866 | PyObject *ofile; |
| 9867 | char *file; |
| 9868 | int flag, dirfd, fd; |
| 9869 | int mode = 0777; |
| 9870 | |
| 9871 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9872 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9873 | &flag, &mode)) |
| 9874 | return NULL; |
| 9875 | file = PyBytes_AsString(ofile); |
| 9876 | Py_BEGIN_ALLOW_THREADS |
| 9877 | fd = openat(dirfd, file, flag, mode); |
| 9878 | Py_END_ALLOW_THREADS |
| 9879 | Py_DECREF(ofile); |
| 9880 | if (fd < 0) |
| 9881 | return posix_error(); |
| 9882 | return PyLong_FromLong((long)fd); |
| 9883 | } |
| 9884 | #endif |
| 9885 | |
| 9886 | #ifdef HAVE_READLINKAT |
| 9887 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9888 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9889 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9890 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9891 | is interpreted relative to the current working directory."); |
| 9892 | |
| 9893 | static PyObject * |
| 9894 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9895 | { |
| 9896 | PyObject *v, *opath; |
| 9897 | char buf[MAXPATHLEN]; |
| 9898 | char *path; |
| 9899 | int n, dirfd; |
| 9900 | int arg_is_unicode = 0; |
| 9901 | |
| 9902 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9903 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9904 | return NULL; |
| 9905 | path = PyBytes_AsString(opath); |
| 9906 | v = PySequence_GetItem(args, 1); |
| 9907 | if (v == NULL) { |
| 9908 | Py_DECREF(opath); |
| 9909 | return NULL; |
| 9910 | } |
| 9911 | |
| 9912 | if (PyUnicode_Check(v)) { |
| 9913 | arg_is_unicode = 1; |
| 9914 | } |
| 9915 | Py_DECREF(v); |
| 9916 | |
| 9917 | Py_BEGIN_ALLOW_THREADS |
| 9918 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9919 | Py_END_ALLOW_THREADS |
| 9920 | Py_DECREF(opath); |
| 9921 | if (n < 0) |
| 9922 | return posix_error(); |
| 9923 | |
| 9924 | if (arg_is_unicode) |
| 9925 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9926 | else |
| 9927 | return PyBytes_FromStringAndSize(buf, n); |
| 9928 | } |
| 9929 | #endif /* HAVE_READLINKAT */ |
| 9930 | |
| 9931 | #ifdef HAVE_RENAMEAT |
| 9932 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9933 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9934 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9935 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9936 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9937 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9938 | also applies for newpath."); |
| 9939 | |
| 9940 | static PyObject * |
| 9941 | posix_renameat(PyObject *self, PyObject *args) |
| 9942 | { |
| 9943 | int res; |
| 9944 | PyObject *opathold, *opathnew; |
| 9945 | char *opath, *npath; |
| 9946 | int oldfd, newfd; |
| 9947 | |
| 9948 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9949 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9950 | return NULL; |
| 9951 | opath = PyBytes_AsString(opathold); |
| 9952 | npath = PyBytes_AsString(opathnew); |
| 9953 | Py_BEGIN_ALLOW_THREADS |
| 9954 | res = renameat(oldfd, opath, newfd, npath); |
| 9955 | Py_END_ALLOW_THREADS |
| 9956 | Py_DECREF(opathold); |
| 9957 | Py_DECREF(opathnew); |
| 9958 | if (res < 0) |
| 9959 | return posix_error(); |
| 9960 | Py_RETURN_NONE; |
| 9961 | } |
| 9962 | #endif |
| 9963 | |
| 9964 | #if HAVE_SYMLINKAT |
| 9965 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9966 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9967 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9968 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9969 | is interpreted relative to the current working directory."); |
| 9970 | |
| 9971 | static PyObject * |
| 9972 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9973 | { |
| 9974 | int res, dstfd; |
| 9975 | PyObject *osrc, *odst; |
| 9976 | char *src, *dst; |
| 9977 | |
| 9978 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9979 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9980 | return NULL; |
| 9981 | src = PyBytes_AsString(osrc); |
| 9982 | dst = PyBytes_AsString(odst); |
| 9983 | Py_BEGIN_ALLOW_THREADS |
| 9984 | res = symlinkat(src, dstfd, dst); |
| 9985 | Py_END_ALLOW_THREADS |
| 9986 | Py_DECREF(osrc); |
| 9987 | Py_DECREF(odst); |
| 9988 | if (res < 0) |
| 9989 | return posix_error(); |
| 9990 | Py_RETURN_NONE; |
| 9991 | } |
| 9992 | #endif /* HAVE_SYMLINKAT */ |
| 9993 | |
| 9994 | #ifdef HAVE_UNLINKAT |
| 9995 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9996 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9997 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9998 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9999 | specified, unlinkat() behaves like rmdir().\n\ |
| 10000 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10001 | is interpreted relative to the current working directory."); |
| 10002 | |
| 10003 | static PyObject * |
| 10004 | posix_unlinkat(PyObject *self, PyObject *args) |
| 10005 | { |
| 10006 | int dirfd, res, flags = 0; |
| 10007 | PyObject *opath; |
| 10008 | char *path; |
| 10009 | |
| 10010 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 10011 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 10012 | return NULL; |
| 10013 | path = PyBytes_AsString(opath); |
| 10014 | Py_BEGIN_ALLOW_THREADS |
| 10015 | res = unlinkat(dirfd, path, flags); |
| 10016 | Py_END_ALLOW_THREADS |
| 10017 | Py_DECREF(opath); |
| 10018 | if (res < 0) |
| 10019 | return posix_error(); |
| 10020 | Py_RETURN_NONE; |
| 10021 | } |
| 10022 | #endif |
| 10023 | |
| 10024 | #ifdef HAVE_UTIMENSAT |
| 10025 | PyDoc_STRVAR(posix_utimensat__doc__, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10026 | "utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\ |
| 10027 | mtime=(mtime_sec, mtime_nsec), flags=0])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10028 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 10029 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 10030 | relative, it is taken as relative to dirfd.\n\ |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10031 | If atime and mtime are both None, which is the default, set atime and\n\ |
| 10032 | mtime to the current time.\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10033 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 10034 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10035 | is interpreted relative to the current working directory.\n\ |
| 10036 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 10037 | current time.\n\ |
| 10038 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 10039 | |
| 10040 | static PyObject * |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10041 | posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10042 | { |
| 10043 | PyObject *opath; |
| 10044 | char *path; |
| 10045 | int res, dirfd, flags = 0; |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10046 | PyObject *atime = Py_None; |
| 10047 | PyObject *mtime = Py_None; |
| 10048 | |
| 10049 | static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL}; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10050 | |
| 10051 | struct timespec buf[2]; |
| 10052 | |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10053 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10054 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10055 | return NULL; |
| 10056 | path = PyBytes_AsString(opath); |
| 10057 | if (atime == Py_None && mtime == Py_None) { |
| 10058 | /* optional time values not given */ |
| 10059 | Py_BEGIN_ALLOW_THREADS |
| 10060 | res = utimensat(dirfd, path, NULL, flags); |
| 10061 | Py_END_ALLOW_THREADS |
| 10062 | } |
| 10063 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10064 | PyErr_SetString(PyExc_TypeError, |
| 10065 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10066 | Py_DECREF(opath); |
| 10067 | return NULL; |
| 10068 | } |
| 10069 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10070 | PyErr_SetString(PyExc_TypeError, |
| 10071 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10072 | Py_DECREF(opath); |
| 10073 | return NULL; |
| 10074 | } |
| 10075 | else { |
| 10076 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10077 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10078 | Py_DECREF(opath); |
| 10079 | return NULL; |
| 10080 | } |
| 10081 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10082 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10083 | Py_DECREF(opath); |
| 10084 | return NULL; |
| 10085 | } |
| 10086 | Py_BEGIN_ALLOW_THREADS |
| 10087 | res = utimensat(dirfd, path, buf, flags); |
| 10088 | Py_END_ALLOW_THREADS |
| 10089 | } |
| 10090 | Py_DECREF(opath); |
| 10091 | if (res < 0) { |
| 10092 | return posix_error(); |
| 10093 | } |
| 10094 | Py_RETURN_NONE; |
| 10095 | } |
| 10096 | #endif |
| 10097 | |
| 10098 | #ifdef HAVE_MKFIFOAT |
| 10099 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10100 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10101 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10102 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10103 | is interpreted relative to the current working directory."); |
| 10104 | |
| 10105 | static PyObject * |
| 10106 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10107 | { |
| 10108 | PyObject *opath; |
| 10109 | char *filename; |
| 10110 | int mode = 0666; |
| 10111 | int res, dirfd; |
| 10112 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10113 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10114 | return NULL; |
| 10115 | filename = PyBytes_AS_STRING(opath); |
| 10116 | Py_BEGIN_ALLOW_THREADS |
| 10117 | res = mkfifoat(dirfd, filename, mode); |
| 10118 | Py_END_ALLOW_THREADS |
| 10119 | Py_DECREF(opath); |
| 10120 | if (res < 0) |
| 10121 | return posix_error(); |
| 10122 | Py_RETURN_NONE; |
| 10123 | } |
| 10124 | #endif |
| 10125 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10126 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10127 | |
| 10128 | static int |
| 10129 | try_getxattr(const char *path, const char *name, |
| 10130 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10131 | Py_ssize_t buf_size, PyObject **res) |
| 10132 | { |
| 10133 | PyObject *value; |
| 10134 | Py_ssize_t len; |
| 10135 | |
| 10136 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10137 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10138 | if (!value) |
| 10139 | return 0; |
| 10140 | Py_BEGIN_ALLOW_THREADS; |
| 10141 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10142 | Py_END_ALLOW_THREADS; |
| 10143 | if (len < 0) { |
| 10144 | Py_DECREF(value); |
| 10145 | if (errno == ERANGE) { |
| 10146 | value = NULL; |
| 10147 | } |
| 10148 | else { |
| 10149 | posix_error(); |
| 10150 | return 0; |
| 10151 | } |
| 10152 | } |
| 10153 | else if (len != buf_size) { |
| 10154 | /* Can only shrink. */ |
| 10155 | _PyBytes_Resize(&value, len); |
| 10156 | } |
| 10157 | *res = value; |
| 10158 | return 1; |
| 10159 | } |
| 10160 | |
| 10161 | static PyObject * |
| 10162 | getxattr_common(const char *path, PyObject *name_obj, |
| 10163 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10164 | { |
| 10165 | PyObject *value; |
| 10166 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10167 | |
| 10168 | /* Try a small value first. */ |
| 10169 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10170 | return NULL; |
| 10171 | if (value) |
| 10172 | return value; |
| 10173 | /* Now the maximum possible one. */ |
| 10174 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10175 | return NULL; |
| 10176 | assert(value); |
| 10177 | return value; |
| 10178 | } |
| 10179 | |
| 10180 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10181 | "getxattr(path, attr) -> value\n\n\ |
| 10182 | Return the value of extended attribute *name* on *path*."); |
| 10183 | |
| 10184 | static PyObject * |
| 10185 | posix_getxattr(PyObject *self, PyObject *args) |
| 10186 | { |
| 10187 | PyObject *path, *res, *name; |
| 10188 | |
| 10189 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10190 | PyUnicode_FSConverter, &name)) |
| 10191 | return NULL; |
| 10192 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10193 | Py_DECREF(path); |
| 10194 | Py_DECREF(name); |
| 10195 | return res; |
| 10196 | } |
| 10197 | |
| 10198 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10199 | "lgetxattr(path, attr) -> value\n\n\ |
| 10200 | Like getxattr but don't follow symlinks."); |
| 10201 | |
| 10202 | static PyObject * |
| 10203 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10204 | { |
| 10205 | PyObject *path, *res, *name; |
| 10206 | |
| 10207 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10208 | PyUnicode_FSConverter, &name)) |
| 10209 | return NULL; |
| 10210 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10211 | Py_DECREF(path); |
| 10212 | Py_DECREF(name); |
| 10213 | return res; |
| 10214 | } |
| 10215 | |
| 10216 | static ssize_t |
| 10217 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10218 | { |
| 10219 | /* Hack to share code. */ |
| 10220 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10221 | } |
| 10222 | |
| 10223 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10224 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10225 | Like getxattr but operate on a fd instead of a path."); |
| 10226 | |
| 10227 | static PyObject * |
| 10228 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10229 | { |
| 10230 | PyObject *res, *name; |
| 10231 | int fd; |
| 10232 | |
| 10233 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10234 | return NULL; |
| 10235 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10236 | Py_DECREF(name); |
| 10237 | return res; |
| 10238 | } |
| 10239 | |
| 10240 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10241 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10242 | Set extended attribute *attr* on *path* to *value*."); |
| 10243 | |
| 10244 | static PyObject * |
| 10245 | posix_setxattr(PyObject *self, PyObject *args) |
| 10246 | { |
| 10247 | PyObject *path, *name; |
| 10248 | Py_buffer data; |
| 10249 | int flags = 0, err; |
| 10250 | |
| 10251 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10252 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10253 | return NULL; |
| 10254 | Py_BEGIN_ALLOW_THREADS; |
| 10255 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10256 | data.buf, data.len, flags); |
| 10257 | Py_END_ALLOW_THREADS; |
| 10258 | Py_DECREF(path); |
| 10259 | Py_DECREF(name); |
| 10260 | PyBuffer_Release(&data); |
| 10261 | if (err) |
| 10262 | return posix_error(); |
| 10263 | Py_RETURN_NONE; |
| 10264 | } |
| 10265 | |
| 10266 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10267 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10268 | Like setxattr but don't follow symlinks."); |
| 10269 | |
| 10270 | static PyObject * |
| 10271 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10272 | { |
| 10273 | PyObject *path, *name; |
| 10274 | Py_buffer data; |
| 10275 | int flags = 0, err; |
| 10276 | |
| 10277 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10278 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10279 | return NULL; |
| 10280 | Py_BEGIN_ALLOW_THREADS; |
| 10281 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10282 | data.buf, data.len, flags); |
| 10283 | Py_END_ALLOW_THREADS; |
| 10284 | Py_DECREF(path); |
| 10285 | Py_DECREF(name); |
| 10286 | PyBuffer_Release(&data); |
| 10287 | if (err) |
| 10288 | return posix_error(); |
| 10289 | Py_RETURN_NONE; |
| 10290 | } |
| 10291 | |
| 10292 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10293 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10294 | Like setxattr but operates on *fd* instead of a path."); |
| 10295 | |
| 10296 | static PyObject * |
| 10297 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10298 | { |
| 10299 | Py_buffer data; |
| 10300 | const char *name; |
| 10301 | int fd, flags = 0, err; |
| 10302 | |
| 10303 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10304 | &name, &data, &flags)) |
| 10305 | return NULL; |
| 10306 | Py_BEGIN_ALLOW_THREADS; |
| 10307 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10308 | Py_END_ALLOW_THREADS; |
| 10309 | Py_DECREF(name); |
| 10310 | PyBuffer_Release(&data); |
| 10311 | if (err) |
| 10312 | return posix_error(); |
| 10313 | Py_RETURN_NONE; |
| 10314 | } |
| 10315 | |
| 10316 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10317 | "removexattr(path, attr)\n\n\ |
| 10318 | Remove extended attribute *attr* on *path*."); |
| 10319 | |
| 10320 | static PyObject * |
| 10321 | posix_removexattr(PyObject *self, PyObject *args) |
| 10322 | { |
| 10323 | PyObject *path, *name; |
| 10324 | int err; |
| 10325 | |
| 10326 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10327 | PyUnicode_FSConverter, &name)) |
| 10328 | return NULL; |
| 10329 | Py_BEGIN_ALLOW_THREADS; |
| 10330 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10331 | Py_END_ALLOW_THREADS; |
| 10332 | Py_DECREF(path); |
| 10333 | Py_DECREF(name); |
| 10334 | if (err) |
| 10335 | return posix_error(); |
| 10336 | Py_RETURN_NONE; |
| 10337 | } |
| 10338 | |
| 10339 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10340 | "lremovexattr(path, attr)\n\n\ |
| 10341 | Like removexattr but don't follow symlinks."); |
| 10342 | |
| 10343 | static PyObject * |
| 10344 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10345 | { |
| 10346 | PyObject *path, *name; |
| 10347 | int err; |
| 10348 | |
| 10349 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10350 | PyUnicode_FSConverter, &name)) |
| 10351 | return NULL; |
| 10352 | Py_BEGIN_ALLOW_THREADS; |
| 10353 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10354 | Py_END_ALLOW_THREADS; |
| 10355 | Py_DECREF(path); |
| 10356 | Py_DECREF(name); |
| 10357 | if (err) |
| 10358 | return posix_error(); |
| 10359 | Py_RETURN_NONE; |
| 10360 | } |
| 10361 | |
| 10362 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10363 | "fremovexattr(fd, attr)\n\n\ |
| 10364 | Like removexattr but operates on a file descriptor."); |
| 10365 | |
| 10366 | static PyObject * |
| 10367 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10368 | { |
| 10369 | PyObject *name; |
| 10370 | int fd, err; |
| 10371 | |
| 10372 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10373 | PyUnicode_FSConverter, &name)) |
| 10374 | return NULL; |
| 10375 | Py_BEGIN_ALLOW_THREADS; |
| 10376 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10377 | Py_END_ALLOW_THREADS; |
| 10378 | Py_DECREF(name); |
| 10379 | if (err) |
| 10380 | return posix_error(); |
| 10381 | Py_RETURN_NONE; |
| 10382 | } |
| 10383 | |
| 10384 | static Py_ssize_t |
| 10385 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10386 | Py_ssize_t buf_size, char **buf) |
| 10387 | { |
| 10388 | Py_ssize_t len; |
| 10389 | |
| 10390 | *buf = PyMem_MALLOC(buf_size); |
| 10391 | if (!*buf) { |
| 10392 | PyErr_NoMemory(); |
| 10393 | return -1; |
| 10394 | } |
| 10395 | Py_BEGIN_ALLOW_THREADS; |
| 10396 | len = list(path, *buf, buf_size); |
| 10397 | Py_END_ALLOW_THREADS; |
| 10398 | if (len < 0) { |
| 10399 | PyMem_FREE(*buf); |
| 10400 | if (errno != ERANGE) |
| 10401 | posix_error(); |
| 10402 | return -1; |
| 10403 | } |
| 10404 | return len; |
| 10405 | } |
| 10406 | |
| 10407 | static PyObject * |
| 10408 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10409 | { |
| 10410 | PyObject *res, *attr; |
| 10411 | Py_ssize_t len, err, start, i; |
| 10412 | char *buf; |
| 10413 | |
| 10414 | len = try_listxattr(path, list, 256, &buf); |
| 10415 | if (len < 0) { |
| 10416 | if (PyErr_Occurred()) |
| 10417 | return NULL; |
| 10418 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10419 | if (len < 0) |
| 10420 | return NULL; |
| 10421 | } |
| 10422 | res = PyList_New(0); |
| 10423 | if (!res) { |
| 10424 | PyMem_FREE(buf); |
| 10425 | return NULL; |
| 10426 | } |
| 10427 | for (start = i = 0; i < len; i++) { |
| 10428 | if (!buf[i]) { |
| 10429 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10430 | if (!attr) { |
| 10431 | Py_DECREF(res); |
| 10432 | PyMem_FREE(buf); |
| 10433 | return NULL; |
| 10434 | } |
| 10435 | err = PyList_Append(res, attr); |
| 10436 | Py_DECREF(attr); |
| 10437 | if (err) { |
| 10438 | Py_DECREF(res); |
| 10439 | PyMem_FREE(buf); |
| 10440 | return NULL; |
| 10441 | } |
| 10442 | start = i + 1; |
| 10443 | } |
| 10444 | } |
| 10445 | PyMem_FREE(buf); |
| 10446 | return res; |
| 10447 | } |
| 10448 | |
| 10449 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10450 | "listxattr(path)\n\n\ |
| 10451 | Return a list of extended attributes on *path*."); |
| 10452 | |
| 10453 | static PyObject * |
| 10454 | posix_listxattr(PyObject *self, PyObject *args) |
| 10455 | { |
| 10456 | PyObject *path, *res; |
| 10457 | |
| 10458 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10459 | return NULL; |
| 10460 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10461 | Py_DECREF(path); |
| 10462 | return res; |
| 10463 | } |
| 10464 | |
| 10465 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10466 | "llistxattr(path)\n\n\ |
| 10467 | Like listxattr but don't follow symlinks.."); |
| 10468 | |
| 10469 | static PyObject * |
| 10470 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10471 | { |
| 10472 | PyObject *path, *res; |
| 10473 | |
| 10474 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10475 | return NULL; |
| 10476 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10477 | Py_DECREF(path); |
| 10478 | return res; |
| 10479 | } |
| 10480 | |
| 10481 | static ssize_t |
| 10482 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10483 | { |
| 10484 | /* Hack to share code. */ |
| 10485 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10486 | } |
| 10487 | |
| 10488 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10489 | "flistxattr(path)\n\n\ |
| 10490 | Like flistxattr but operates on a file descriptor."); |
| 10491 | |
| 10492 | static PyObject * |
| 10493 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10494 | { |
| 10495 | long fd; |
| 10496 | |
| 10497 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10498 | return NULL; |
| 10499 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10500 | } |
| 10501 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10502 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10503 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10504 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10505 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10506 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10507 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10508 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10509 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10510 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10511 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10512 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10513 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10514 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10515 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10516 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10517 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10518 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10519 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10520 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10522 | #endif /* HAVE_LCHMOD */ |
| 10523 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10524 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10525 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10526 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10527 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10528 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10529 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10530 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10531 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10532 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10533 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10534 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10535 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10536 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10537 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10538 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10540 | METH_NOARGS, posix_getcwd__doc__}, |
| 10541 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10542 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10543 | #endif |
| 10544 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10545 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10546 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10547 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10548 | #ifdef HAVE_FDOPENDIR |
| 10549 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10550 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10551 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10552 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10553 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10554 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10555 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10556 | #ifdef HAVE_GETPRIORITY |
| 10557 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10558 | #endif /* HAVE_GETPRIORITY */ |
| 10559 | #ifdef HAVE_SETPRIORITY |
| 10560 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10561 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10562 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10564 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10565 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10566 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10567 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10568 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10569 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10570 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10572 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10574 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10575 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10576 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10577 | win_symlink__doc__}, |
| 10578 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10579 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10581 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10582 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10583 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10584 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10585 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10586 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10587 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10588 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10589 | #ifdef HAVE_FUTIMES |
| 10590 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10591 | #endif |
| 10592 | #ifdef HAVE_LUTIMES |
| 10593 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10594 | #endif |
| 10595 | #ifdef HAVE_FUTIMENS |
| 10596 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10597 | #endif |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10598 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10599 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10600 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10602 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10603 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10604 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10605 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10606 | #ifdef HAVE_FEXECVE |
| 10607 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10608 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10609 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10610 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10611 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10612 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10614 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10615 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10616 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10617 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10619 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10620 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10621 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10622 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10623 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10624 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10625 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10626 | {"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] | 10627 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10628 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10629 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10630 | #endif |
| 10631 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10632 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10633 | #endif |
| 10634 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10635 | {"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] | 10636 | #endif |
| 10637 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10638 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10639 | #endif |
| 10640 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10641 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10642 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10643 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10644 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10645 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10646 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10647 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10648 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10649 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10651 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10652 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10653 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10654 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10655 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10656 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10657 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10658 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10659 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10660 | #endif /* HAVE_GETEUID */ |
| 10661 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10662 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10663 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10664 | #ifdef HAVE_GETGROUPLIST |
| 10665 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10666 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10667 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10668 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10669 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10670 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10671 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10673 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10674 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10676 | #endif /* HAVE_GETPPID */ |
| 10677 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10679 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10680 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10682 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10683 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10685 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10686 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10688 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10689 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10691 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10692 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10694 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10695 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10696 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10697 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10698 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10699 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10700 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10701 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10702 | #endif /* HAVE_SETEUID */ |
| 10703 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10704 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10705 | #endif /* HAVE_SETEGID */ |
| 10706 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10707 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10708 | #endif /* HAVE_SETREUID */ |
| 10709 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10711 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10712 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10714 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10715 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10716 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10717 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10718 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10719 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10720 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10721 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10722 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10723 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10724 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10726 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10727 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10728 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10729 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10730 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10732 | #endif /* HAVE_WAIT3 */ |
| 10733 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10735 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10736 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10737 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10738 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10739 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10741 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10742 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10744 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10745 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10746 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10747 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10748 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10749 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10750 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10751 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10752 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10753 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10754 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10755 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10756 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10757 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10758 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10759 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10760 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10761 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10762 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10763 | #ifdef HAVE_LOCKF |
| 10764 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10765 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10766 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10767 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10768 | #ifdef HAVE_READV |
| 10769 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10770 | #endif |
| 10771 | #ifdef HAVE_PREAD |
| 10772 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10773 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10775 | #ifdef HAVE_WRITEV |
| 10776 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10777 | #endif |
| 10778 | #ifdef HAVE_PWRITE |
| 10779 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10780 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10781 | #ifdef HAVE_SENDFILE |
| 10782 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10783 | posix_sendfile__doc__}, |
| 10784 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10786 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10787 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10789 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10790 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10791 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10792 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10793 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10795 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10796 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10798 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10799 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10801 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10802 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10803 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10804 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10805 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10806 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10807 | #ifdef HAVE_TRUNCATE |
| 10808 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10809 | #endif |
| 10810 | #ifdef HAVE_POSIX_FALLOCATE |
| 10811 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10812 | #endif |
| 10813 | #ifdef HAVE_POSIX_FADVISE |
| 10814 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10815 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10816 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10818 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10819 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10821 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10822 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10823 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10825 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10826 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10828 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10829 | #ifdef HAVE_SYNC |
| 10830 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10831 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10832 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10834 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10835 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10836 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10837 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10838 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10839 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10840 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10841 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10842 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10843 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10844 | #endif /* WIFSTOPPED */ |
| 10845 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10846 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10847 | #endif /* WIFSIGNALED */ |
| 10848 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10849 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10850 | #endif /* WIFEXITED */ |
| 10851 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10852 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10853 | #endif /* WEXITSTATUS */ |
| 10854 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10855 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10856 | #endif /* WTERMSIG */ |
| 10857 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10859 | #endif /* WSTOPSIG */ |
| 10860 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10861 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10862 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10863 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10864 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10866 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10867 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10868 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10869 | #endif |
| 10870 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10872 | #endif |
| 10873 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10874 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10875 | #endif |
| 10876 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10877 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10878 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10880 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10882 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10883 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10884 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10885 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10886 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10887 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10889 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10890 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10892 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10893 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10895 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10896 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10901 | #endif |
| 10902 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10904 | #endif |
| 10905 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10907 | #endif |
| 10908 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10909 | /* posix *at family of functions */ |
| 10910 | #ifdef HAVE_FACCESSAT |
| 10911 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10912 | #endif |
| 10913 | #ifdef HAVE_FCHMODAT |
| 10914 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10915 | #endif /* HAVE_FCHMODAT */ |
| 10916 | #ifdef HAVE_FCHOWNAT |
| 10917 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10918 | #endif /* HAVE_FCHOWNAT */ |
| 10919 | #ifdef HAVE_FSTATAT |
| 10920 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10921 | #endif |
| 10922 | #ifdef HAVE_FUTIMESAT |
| 10923 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10924 | #endif |
| 10925 | #ifdef HAVE_LINKAT |
| 10926 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10927 | #endif /* HAVE_LINKAT */ |
| 10928 | #ifdef HAVE_MKDIRAT |
| 10929 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10930 | #endif |
| 10931 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10932 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10933 | #endif |
| 10934 | #ifdef HAVE_OPENAT |
| 10935 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10936 | #endif |
| 10937 | #ifdef HAVE_READLINKAT |
| 10938 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10939 | #endif /* HAVE_READLINKAT */ |
| 10940 | #ifdef HAVE_RENAMEAT |
| 10941 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10942 | #endif |
| 10943 | #if HAVE_SYMLINKAT |
| 10944 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10945 | #endif /* HAVE_SYMLINKAT */ |
| 10946 | #ifdef HAVE_UNLINKAT |
| 10947 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10948 | #endif |
| 10949 | #ifdef HAVE_UTIMENSAT |
Jesus Cea | d03a491 | 2011-11-08 17:28:04 +0100 | [diff] [blame] | 10950 | {"utimensat", (PyCFunction)posix_utimensat, |
| 10951 | METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10952 | posix_utimensat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10953 | #endif |
| 10954 | #ifdef HAVE_MKFIFOAT |
| 10955 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10956 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10957 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10958 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10959 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10960 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10961 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10962 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10963 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10964 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10965 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10966 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10967 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10968 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10969 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10970 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10972 | }; |
| 10973 | |
| 10974 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10975 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10976 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10977 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10978 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10979 | } |
| 10980 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10981 | #if defined(PYOS_OS2) |
| 10982 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10983 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10984 | { |
| 10985 | APIRET rc; |
| 10986 | ULONG values[QSV_MAX+1]; |
| 10987 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10988 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10989 | |
| 10990 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10991 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10992 | Py_END_ALLOW_THREADS |
| 10993 | |
| 10994 | if (rc != NO_ERROR) { |
| 10995 | os2_error(rc); |
| 10996 | return -1; |
| 10997 | } |
| 10998 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10999 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 11000 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 11001 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 11002 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 11003 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 11004 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 11005 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11006 | |
| 11007 | switch (values[QSV_VERSION_MINOR]) { |
| 11008 | case 0: ver = "2.00"; break; |
| 11009 | case 10: ver = "2.10"; break; |
| 11010 | case 11: ver = "2.11"; break; |
| 11011 | case 30: ver = "3.00"; break; |
| 11012 | case 40: ver = "4.00"; break; |
| 11013 | case 50: ver = "5.00"; break; |
| 11014 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11015 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 11017 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11018 | ver = &tmp[0]; |
| 11019 | } |
| 11020 | |
| 11021 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11022 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11023 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11024 | |
| 11025 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11026 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11027 | tmp[1] = ':'; |
| 11028 | tmp[2] = '\0'; |
| 11029 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11030 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11031 | } |
| 11032 | #endif |
| 11033 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11034 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11035 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11036 | enable_symlink() |
| 11037 | { |
| 11038 | HANDLE tok; |
| 11039 | TOKEN_PRIVILEGES tok_priv; |
| 11040 | LUID luid; |
| 11041 | int meth_idx = 0; |
| 11042 | |
| 11043 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
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 | |
| 11046 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11047 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11048 | |
| 11049 | tok_priv.PrivilegeCount = 1; |
| 11050 | tok_priv.Privileges[0].Luid = luid; |
| 11051 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11052 | |
| 11053 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11054 | sizeof(TOKEN_PRIVILEGES), |
| 11055 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11056 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11057 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11058 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11059 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11060 | } |
| 11061 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11062 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11063 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11064 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11065 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11066 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11068 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11069 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11071 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11072 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11074 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11075 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11077 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11078 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11080 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11081 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11083 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11084 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11086 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11087 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11089 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11090 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11092 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11093 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11095 | #endif |
| 11096 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11098 | #endif |
| 11099 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11119 | #endif |
| 11120 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11131 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11132 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11137 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11138 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11140 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11141 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11142 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11143 | #endif |
| 11144 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11145 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11146 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11147 | #ifdef PRIO_PROCESS |
| 11148 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11149 | #endif |
| 11150 | #ifdef PRIO_PGRP |
| 11151 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11152 | #endif |
| 11153 | #ifdef PRIO_USER |
| 11154 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11155 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11156 | #ifdef O_CLOEXEC |
| 11157 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11158 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11159 | /* posix - constants for *at functions */ |
| 11160 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11161 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11162 | #endif |
| 11163 | #ifdef AT_EACCESS |
| 11164 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11165 | #endif |
| 11166 | #ifdef AT_FDCWD |
| 11167 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11168 | #endif |
| 11169 | #ifdef AT_REMOVEDIR |
| 11170 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11171 | #endif |
| 11172 | #ifdef AT_SYMLINK_FOLLOW |
| 11173 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11174 | #endif |
| 11175 | #ifdef UTIME_NOW |
| 11176 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11177 | #endif |
| 11178 | #ifdef UTIME_OMIT |
| 11179 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11180 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11181 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11182 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11183 | /* MS Windows */ |
| 11184 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | /* Don't inherit in child processes. */ |
| 11186 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11187 | #endif |
| 11188 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | /* Optimize for short life (keep in memory). */ |
| 11190 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11191 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | /* Automatically delete when last handle is closed. */ |
| 11195 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11196 | #endif |
| 11197 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | /* Optimize for random access. */ |
| 11199 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11200 | #endif |
| 11201 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | /* Optimize for sequential access. */ |
| 11203 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11204 | #endif |
| 11205 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11206 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11207 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | /* Send a SIGIO signal whenever input or output |
| 11209 | becomes available on file descriptor */ |
| 11210 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11211 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11212 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11213 | /* Direct disk access. */ |
| 11214 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11215 | #endif |
| 11216 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | /* Must be a directory. */ |
| 11218 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11219 | #endif |
| 11220 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | /* Do not follow links. */ |
| 11222 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11223 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11224 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | /* Do not update the access time. */ |
| 11226 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11227 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11230 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11231 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11232 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11233 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11235 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11236 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11238 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11239 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11241 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11242 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11243 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11244 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11245 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11246 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11247 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11248 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11250 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11251 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11253 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11254 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11255 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11256 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11257 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11258 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11259 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11260 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11262 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11263 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11265 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11266 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11267 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11268 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11269 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11270 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11271 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11272 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11273 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11274 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11275 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11276 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11277 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11278 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11279 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11280 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11281 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11282 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11283 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11284 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11285 | #endif /* ST_RDONLY */ |
| 11286 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11287 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11288 | #endif /* ST_NOSUID */ |
| 11289 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11290 | /* FreeBSD sendfile() constants */ |
| 11291 | #ifdef SF_NODISKIO |
| 11292 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11293 | #endif |
| 11294 | #ifdef SF_MNOWAIT |
| 11295 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11296 | #endif |
| 11297 | #ifdef SF_SYNC |
| 11298 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11299 | #endif |
| 11300 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11301 | /* constants for posix_fadvise */ |
| 11302 | #ifdef POSIX_FADV_NORMAL |
| 11303 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11304 | #endif |
| 11305 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11306 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11307 | #endif |
| 11308 | #ifdef POSIX_FADV_RANDOM |
| 11309 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11310 | #endif |
| 11311 | #ifdef POSIX_FADV_NOREUSE |
| 11312 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11313 | #endif |
| 11314 | #ifdef POSIX_FADV_WILLNEED |
| 11315 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11316 | #endif |
| 11317 | #ifdef POSIX_FADV_DONTNEED |
| 11318 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11319 | #endif |
| 11320 | |
| 11321 | /* constants for waitid */ |
| 11322 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11323 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11324 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11325 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11326 | #endif |
| 11327 | #ifdef WEXITED |
| 11328 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11329 | #endif |
| 11330 | #ifdef WNOWAIT |
| 11331 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11332 | #endif |
| 11333 | #ifdef WSTOPPED |
| 11334 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11335 | #endif |
| 11336 | #ifdef CLD_EXITED |
| 11337 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11338 | #endif |
| 11339 | #ifdef CLD_DUMPED |
| 11340 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11341 | #endif |
| 11342 | #ifdef CLD_TRAPPED |
| 11343 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11344 | #endif |
| 11345 | #ifdef CLD_CONTINUED |
| 11346 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11347 | #endif |
| 11348 | |
| 11349 | /* constants for lockf */ |
| 11350 | #ifdef F_LOCK |
| 11351 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11352 | #endif |
| 11353 | #ifdef F_TLOCK |
| 11354 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11355 | #endif |
| 11356 | #ifdef F_ULOCK |
| 11357 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11358 | #endif |
| 11359 | #ifdef F_TEST |
| 11360 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11361 | #endif |
| 11362 | |
| 11363 | /* constants for futimens */ |
| 11364 | #ifdef UTIME_NOW |
| 11365 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11366 | #endif |
| 11367 | #ifdef UTIME_OMIT |
| 11368 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11369 | #endif |
| 11370 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11371 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11372 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11374 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11375 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11376 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11377 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11378 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11379 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11380 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11381 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11382 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11383 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11384 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11385 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11386 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11387 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11388 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11389 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11390 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11391 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11392 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11393 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11395 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11396 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11397 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11398 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11399 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11400 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11401 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11402 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11403 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11404 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11405 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11406 | #ifdef SCHED_SPORADIC |
| 11407 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11408 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11409 | #ifdef SCHED_BATCH |
| 11410 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11411 | #endif |
| 11412 | #ifdef SCHED_IDLE |
| 11413 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11414 | #endif |
| 11415 | #ifdef SCHED_RESET_ON_FORK |
| 11416 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11417 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11418 | #ifdef SCHED_SYS |
| 11419 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11420 | #endif |
| 11421 | #ifdef SCHED_IA |
| 11422 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11423 | #endif |
| 11424 | #ifdef SCHED_FSS |
| 11425 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11426 | #endif |
| 11427 | #ifdef SCHED_FX |
| 11428 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11429 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11430 | #endif |
| 11431 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11432 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11433 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11434 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11435 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11436 | #endif |
| 11437 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11438 | #ifdef RTLD_LAZY |
| 11439 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11440 | #endif |
| 11441 | #ifdef RTLD_NOW |
| 11442 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11443 | #endif |
| 11444 | #ifdef RTLD_GLOBAL |
| 11445 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11446 | #endif |
| 11447 | #ifdef RTLD_LOCAL |
| 11448 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11449 | #endif |
| 11450 | #ifdef RTLD_NODELETE |
| 11451 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11452 | #endif |
| 11453 | #ifdef RTLD_NOLOAD |
| 11454 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11455 | #endif |
| 11456 | #ifdef RTLD_DEEPBIND |
| 11457 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11458 | #endif |
| 11459 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11460 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11462 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11464 | } |
| 11465 | |
| 11466 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11467 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11468 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11469 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11470 | |
| 11471 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11472 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11473 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11474 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11475 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11476 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11477 | #define MODNAME "posix" |
| 11478 | #endif |
| 11479 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11480 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | PyModuleDef_HEAD_INIT, |
| 11482 | MODNAME, |
| 11483 | posix__doc__, |
| 11484 | -1, |
| 11485 | posix_methods, |
| 11486 | NULL, |
| 11487 | NULL, |
| 11488 | NULL, |
| 11489 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11490 | }; |
| 11491 | |
| 11492 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11493 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11494 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11495 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11497 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11498 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11499 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11500 | #endif |
| 11501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | m = PyModule_Create(&posixmodule); |
| 11503 | if (m == NULL) |
| 11504 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11505 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | /* Initialize environ dictionary */ |
| 11507 | v = convertenviron(); |
| 11508 | Py_XINCREF(v); |
| 11509 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11510 | return NULL; |
| 11511 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11513 | if (all_ins(m)) |
| 11514 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11515 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11516 | if (setup_confname_tables(m)) |
| 11517 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11518 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11519 | Py_INCREF(PyExc_OSError); |
| 11520 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11521 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11522 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11523 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11524 | return NULL; |
| 11525 | Py_INCREF(&cpu_set_type); |
| 11526 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11527 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11528 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11529 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | if (posix_putenv_garbage == NULL) |
| 11531 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11532 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11533 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11534 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11535 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11536 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11537 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11538 | #endif |
| 11539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11541 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11542 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11543 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11544 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11545 | structseq_new = StatResultType.tp_new; |
| 11546 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11547 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11549 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11550 | #ifdef NEED_TICKS_PER_SECOND |
| 11551 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11552 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11553 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11555 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11556 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11557 | # endif |
| 11558 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11559 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11560 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11561 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11562 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11563 | SchedParamType.tp_new = sched_param_new; |
| 11564 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11565 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11566 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11567 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11568 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11569 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11570 | Py_INCREF((PyObject*) &StatResultType); |
| 11571 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11572 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11573 | PyModule_AddObject(m, "statvfs_result", |
| 11574 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11575 | |
| 11576 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11577 | Py_INCREF(&SchedParamType); |
| 11578 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11579 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11580 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11581 | |
| 11582 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11583 | /* |
| 11584 | * Step 2 of weak-linking support on Mac OS X. |
| 11585 | * |
| 11586 | * The code below removes functions that are not available on the |
| 11587 | * currently active platform. |
| 11588 | * |
| 11589 | * This block allow one to use a python binary that was build on |
| 11590 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11591 | * OSX 10.4. |
| 11592 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11593 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11594 | if (fstatvfs == NULL) { |
| 11595 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11596 | return NULL; |
| 11597 | } |
| 11598 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11599 | #endif /* HAVE_FSTATVFS */ |
| 11600 | |
| 11601 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11602 | if (statvfs == NULL) { |
| 11603 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11604 | return NULL; |
| 11605 | } |
| 11606 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11607 | #endif /* HAVE_STATVFS */ |
| 11608 | |
| 11609 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11610 | if (lchown == NULL) { |
| 11611 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11612 | return NULL; |
| 11613 | } |
| 11614 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11615 | #endif /* HAVE_LCHOWN */ |
| 11616 | |
| 11617 | |
| 11618 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11619 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11620 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11621 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11622 | |
| 11623 | #ifdef __cplusplus |
| 11624 | } |
| 11625 | #endif |