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 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 190 | |
| 191 | |
| 192 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 193 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 194 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 195 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 196 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 197 | (default) */ |
| 198 | extern char *ctermid_r(char *); |
| 199 | #endif |
| 200 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 201 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 203 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 204 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 205 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 206 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 207 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 210 | #endif |
| 211 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 212 | extern int chdir(char *); |
| 213 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 214 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 215 | extern int chdir(const char *); |
| 216 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 217 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 218 | #ifdef __BORLANDC__ |
| 219 | extern int chmod(const char *, int); |
| 220 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 221 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 222 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 223 | /*#ifdef HAVE_FCHMOD |
| 224 | extern int fchmod(int, mode_t); |
| 225 | #endif*/ |
| 226 | /*#ifdef HAVE_LCHMOD |
| 227 | extern int lchmod(const char *, mode_t); |
| 228 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 229 | extern int chown(const char *, uid_t, gid_t); |
| 230 | extern char *getcwd(char *, int); |
| 231 | extern char *strerror(int); |
| 232 | extern int link(const char *, const char *); |
| 233 | extern int rename(const char *, const char *); |
| 234 | extern int stat(const char *, struct stat *); |
| 235 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 237 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 238 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 240 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 241 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 244 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 246 | #ifdef HAVE_UTIME_H |
| 247 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 248 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 250 | #ifdef HAVE_SYS_UTIME_H |
| 251 | #include <sys/utime.h> |
| 252 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 253 | #endif /* HAVE_SYS_UTIME_H */ |
| 254 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #ifdef HAVE_SYS_TIMES_H |
| 256 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 257 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | |
| 259 | #ifdef HAVE_SYS_PARAM_H |
| 260 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 261 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | |
| 263 | #ifdef HAVE_SYS_UTSNAME_H |
| 264 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 265 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 267 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 270 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 271 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 272 | #include <direct.h> |
| 273 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 274 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 277 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #endif |
| 281 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 282 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 286 | #endif |
| 287 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 289 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 290 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | #endif |
| 296 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 297 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 298 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 299 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 300 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 301 | #endif |
| 302 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 303 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 304 | #endif |
| 305 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 306 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 307 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 308 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 309 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 310 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 311 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 312 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 313 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 314 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 315 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 316 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 317 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 319 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 320 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 321 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 323 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 325 | #define MAXPATHLEN PATH_MAX |
| 326 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 327 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 328 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 329 | #endif /* MAXPATHLEN */ |
| 330 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 331 | #ifdef UNION_WAIT |
| 332 | /* Emulate some macros on systems that have a union instead of macros */ |
| 333 | |
| 334 | #ifndef WIFEXITED |
| 335 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 336 | #endif |
| 337 | |
| 338 | #ifndef WEXITSTATUS |
| 339 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 340 | #endif |
| 341 | |
| 342 | #ifndef WTERMSIG |
| 343 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 344 | #endif |
| 345 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | #define WAIT_TYPE union wait |
| 347 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 348 | |
| 349 | #else /* !UNION_WAIT */ |
| 350 | #define WAIT_TYPE int |
| 351 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 352 | #endif /* UNION_WAIT */ |
| 353 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 354 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 355 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 356 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 357 | #define USE_CTERMID_R |
| 358 | #endif |
| 359 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 360 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 361 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 362 | #undef FSTAT |
| 363 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 364 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 365 | # define STAT win32_stat |
| 366 | # define FSTAT win32_fstat |
| 367 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 368 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 369 | # define STAT stat |
| 370 | # define FSTAT fstat |
| 371 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 374 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 375 | #include <sys/mkdev.h> |
| 376 | #else |
| 377 | #if defined(MAJOR_IN_SYSMACROS) |
| 378 | #include <sys/sysmacros.h> |
| 379 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 380 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 381 | #include <sys/mkdev.h> |
| 382 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 383 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 384 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 385 | /* A helper used by a number of POSIX-only functions */ |
| 386 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 387 | static int |
| 388 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 389 | { |
| 390 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 391 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 392 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 393 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 394 | #endif |
| 395 | if (PyErr_Occurred()) |
| 396 | return 0; |
| 397 | return 1; |
| 398 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 399 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 400 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 401 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 402 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 403 | * valid and throw an assertion if it isn't. |
| 404 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 405 | * an assertion can be useful, but it does contradict the POSIX standard |
| 406 | * which for write(2) states: |
| 407 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 408 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 409 | * writing." |
| 410 | * Furthermore, python allows the user to enter any old integer |
| 411 | * as a fd and should merely raise a python exception on error. |
| 412 | * The Microsoft CRT doesn't provide an official way to check for the |
| 413 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 414 | * 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] | 415 | * internal structures involved. |
| 416 | * The structures below must be updated for each version of visual studio |
| 417 | * according to the file internal.h in the CRT source, until MS comes |
| 418 | * up with a less hacky way to do this. |
| 419 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 420 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 421 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 422 | /* The actual size of the structure is determined at runtime. |
| 423 | * Only the first items must be present. |
| 424 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 425 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 426 | intptr_t osfhnd; |
| 427 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 428 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 429 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 430 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 431 | #define IOINFO_L2E 5 |
| 432 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 433 | #define IOINFO_ARRAYS 64 |
| 434 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 435 | #define FOPEN 0x01 |
| 436 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 437 | |
| 438 | /* This function emulates what the windows CRT does to validate file handles */ |
| 439 | int |
| 440 | _PyVerify_fd(int fd) |
| 441 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 442 | const int i1 = fd >> IOINFO_L2E; |
| 443 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 444 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 445 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 446 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 447 | /* Determine the actual size of the ioinfo structure, |
| 448 | * as used by the CRT loaded in memory |
| 449 | */ |
| 450 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 451 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 452 | } |
| 453 | if (sizeof_ioinfo == 0) { |
| 454 | /* This should not happen... */ |
| 455 | goto fail; |
| 456 | } |
| 457 | |
| 458 | /* See that it isn't a special CLEAR fileno */ |
| 459 | if (fd != _NO_CONSOLE_FILENO) { |
| 460 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 461 | * we check pointer validity and other info |
| 462 | */ |
| 463 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 464 | /* finally, check that the file is open */ |
| 465 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 466 | if (info->osfile & FOPEN) { |
| 467 | return 1; |
| 468 | } |
| 469 | } |
| 470 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 471 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 472 | errno = EBADF; |
| 473 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 477 | static int |
| 478 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 479 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 480 | if (!_PyVerify_fd(fd1)) |
| 481 | return 0; |
| 482 | if (fd2 == _NO_CONSOLE_FILENO) |
| 483 | return 0; |
| 484 | if ((unsigned)fd2 < _NHANDLE_) |
| 485 | return 1; |
| 486 | else |
| 487 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 488 | } |
| 489 | #else |
| 490 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 491 | #define _PyVerify_fd_dup2(A, B) (1) |
| 492 | #endif |
| 493 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 494 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 495 | /* The following structure was copied from |
| 496 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 497 | include doesn't seem to be present in the Windows SDK (at least as included |
| 498 | with Visual Studio Express). */ |
| 499 | typedef struct _REPARSE_DATA_BUFFER { |
| 500 | ULONG ReparseTag; |
| 501 | USHORT ReparseDataLength; |
| 502 | USHORT Reserved; |
| 503 | union { |
| 504 | struct { |
| 505 | USHORT SubstituteNameOffset; |
| 506 | USHORT SubstituteNameLength; |
| 507 | USHORT PrintNameOffset; |
| 508 | USHORT PrintNameLength; |
| 509 | ULONG Flags; |
| 510 | WCHAR PathBuffer[1]; |
| 511 | } SymbolicLinkReparseBuffer; |
| 512 | |
| 513 | struct { |
| 514 | USHORT SubstituteNameOffset; |
| 515 | USHORT SubstituteNameLength; |
| 516 | USHORT PrintNameOffset; |
| 517 | USHORT PrintNameLength; |
| 518 | WCHAR PathBuffer[1]; |
| 519 | } MountPointReparseBuffer; |
| 520 | |
| 521 | struct { |
| 522 | UCHAR DataBuffer[1]; |
| 523 | } GenericReparseBuffer; |
| 524 | }; |
| 525 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 526 | |
| 527 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 528 | GenericReparseBuffer) |
| 529 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 530 | |
| 531 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 532 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 533 | { |
| 534 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 535 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 536 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 537 | |
| 538 | if (0 == DeviceIoControl( |
| 539 | reparse_point_handle, |
| 540 | FSCTL_GET_REPARSE_POINT, |
| 541 | NULL, 0, /* in buffer */ |
| 542 | target_buffer, sizeof(target_buffer), |
| 543 | &n_bytes_returned, |
| 544 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 545 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 546 | |
| 547 | if (reparse_tag) |
| 548 | *reparse_tag = rdb->ReparseTag; |
| 549 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 550 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 551 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 552 | |
| 553 | static int |
| 554 | win32_warn_bytes_api() |
| 555 | { |
| 556 | return PyErr_WarnEx(PyExc_DeprecationWarning, |
| 557 | "The Windows bytes API has been deprecated, " |
| 558 | "use Unicode filenames instead", |
| 559 | 1); |
| 560 | } |
| 561 | |
| 562 | static PyObject* |
| 563 | win32_decode_filename(PyObject *obj) |
| 564 | { |
| 565 | PyObject *unicode; |
| 566 | if (PyUnicode_Check(obj)) { |
| 567 | if (PyUnicode_READY(obj)) |
| 568 | return NULL; |
| 569 | Py_INCREF(obj); |
| 570 | return obj; |
| 571 | } |
| 572 | if (!PyUnicode_FSDecoder(obj, &unicode)) |
| 573 | return NULL; |
| 574 | if (win32_warn_bytes_api()) { |
| 575 | Py_DECREF(unicode); |
| 576 | return NULL; |
| 577 | } |
| 578 | return unicode; |
| 579 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 580 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 582 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 583 | #ifdef WITH_NEXT_FRAMEWORK |
| 584 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 585 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 586 | */ |
| 587 | #include <crt_externs.h> |
| 588 | static char **environ; |
| 589 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 590 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 591 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 593 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 594 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 595 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 596 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 597 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 598 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 599 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 600 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 601 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 602 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 603 | APIRET rc; |
| 604 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 605 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 607 | d = PyDict_New(); |
| 608 | if (d == NULL) |
| 609 | return NULL; |
| 610 | #ifdef WITH_NEXT_FRAMEWORK |
| 611 | if (environ == NULL) |
| 612 | environ = *_NSGetEnviron(); |
| 613 | #endif |
| 614 | #ifdef MS_WINDOWS |
| 615 | /* _wenviron must be initialized in this way if the program is started |
| 616 | through main() instead of wmain(). */ |
| 617 | _wgetenv(L""); |
| 618 | if (_wenviron == NULL) |
| 619 | return d; |
| 620 | /* This part ignores errors */ |
| 621 | for (e = _wenviron; *e != NULL; e++) { |
| 622 | PyObject *k; |
| 623 | PyObject *v; |
| 624 | wchar_t *p = wcschr(*e, L'='); |
| 625 | if (p == NULL) |
| 626 | continue; |
| 627 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 628 | if (k == NULL) { |
| 629 | PyErr_Clear(); |
| 630 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 631 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 632 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 633 | if (v == NULL) { |
| 634 | PyErr_Clear(); |
| 635 | Py_DECREF(k); |
| 636 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 637 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 638 | if (PyDict_GetItem(d, k) == NULL) { |
| 639 | if (PyDict_SetItem(d, k, v) != 0) |
| 640 | PyErr_Clear(); |
| 641 | } |
| 642 | Py_DECREF(k); |
| 643 | Py_DECREF(v); |
| 644 | } |
| 645 | #else |
| 646 | if (environ == NULL) |
| 647 | return d; |
| 648 | /* This part ignores errors */ |
| 649 | for (e = environ; *e != NULL; e++) { |
| 650 | PyObject *k; |
| 651 | PyObject *v; |
| 652 | char *p = strchr(*e, '='); |
| 653 | if (p == NULL) |
| 654 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 655 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 656 | if (k == NULL) { |
| 657 | PyErr_Clear(); |
| 658 | continue; |
| 659 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 660 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 661 | if (v == NULL) { |
| 662 | PyErr_Clear(); |
| 663 | Py_DECREF(k); |
| 664 | continue; |
| 665 | } |
| 666 | if (PyDict_GetItem(d, k) == NULL) { |
| 667 | if (PyDict_SetItem(d, k, v) != 0) |
| 668 | PyErr_Clear(); |
| 669 | } |
| 670 | Py_DECREF(k); |
| 671 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 672 | } |
| 673 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 674 | #if defined(PYOS_OS2) |
| 675 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 676 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 677 | PyObject *v = PyBytes_FromString(buffer); |
| 678 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 679 | Py_DECREF(v); |
| 680 | } |
| 681 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 682 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 683 | PyObject *v = PyBytes_FromString(buffer); |
| 684 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 685 | Py_DECREF(v); |
| 686 | } |
| 687 | #endif |
| 688 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 691 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 692 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 693 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 694 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 695 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 696 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 697 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 698 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 699 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 700 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 701 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 704 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 705 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 706 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 707 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 708 | PyObject *name_str, *rc; |
| 709 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 710 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 711 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 712 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 713 | name_str); |
| 714 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 715 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 718 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 719 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 720 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 722 | /* XXX We should pass the function name along in the future. |
| 723 | (winreg.c also wants to pass the function name.) |
| 724 | This would however require an additional param to the |
| 725 | Windows error object, which is non-trivial. |
| 726 | */ |
| 727 | errno = GetLastError(); |
| 728 | if (filename) |
| 729 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 730 | else |
| 731 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 732 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 733 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 734 | static PyObject * |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 735 | win32_error_unicode(char* function, wchar_t* filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 736 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 737 | /* XXX - see win32_error for comments on 'function' */ |
| 738 | errno = GetLastError(); |
| 739 | if (filename) |
| 740 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 741 | else |
| 742 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 745 | static PyObject * |
| 746 | win32_error_object(char* function, PyObject* filename) |
| 747 | { |
| 748 | /* XXX - see win32_error for comments on 'function' */ |
| 749 | errno = GetLastError(); |
| 750 | if (filename) |
| 751 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 752 | PyExc_WindowsError, |
| 753 | errno, |
| 754 | filename); |
| 755 | else |
| 756 | return PyErr_SetFromWindowsErr(errno); |
| 757 | } |
| 758 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 759 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 761 | #if defined(PYOS_OS2) |
| 762 | /********************************************************************** |
| 763 | * Helper Function to Trim and Format OS/2 Messages |
| 764 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 765 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 766 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 767 | { |
| 768 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 769 | |
| 770 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 771 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 772 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 773 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 774 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 775 | } |
| 776 | |
| 777 | /* Add Optional Reason Text */ |
| 778 | if (reason) { |
| 779 | strcat(msgbuf, " : "); |
| 780 | strcat(msgbuf, reason); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /********************************************************************** |
| 785 | * Decode an OS/2 Operating System Error Code |
| 786 | * |
| 787 | * A convenience function to lookup an OS/2 error code and return a |
| 788 | * text message we can use to raise a Python exception. |
| 789 | * |
| 790 | * Notes: |
| 791 | * The messages for errors returned from the OS/2 kernel reside in |
| 792 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 793 | * |
| 794 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 795 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 796 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 797 | { |
| 798 | APIRET rc; |
| 799 | ULONG msglen; |
| 800 | |
| 801 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 802 | Py_BEGIN_ALLOW_THREADS |
| 803 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 804 | errorcode, "oso001.msg", &msglen); |
| 805 | Py_END_ALLOW_THREADS |
| 806 | |
| 807 | if (rc == NO_ERROR) |
| 808 | os2_formatmsg(msgbuf, msglen, reason); |
| 809 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 810 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 811 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 812 | |
| 813 | return msgbuf; |
| 814 | } |
| 815 | |
| 816 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 817 | errors are not in a global variable e.g. 'errno' nor are |
| 818 | they congruent with posix error numbers. */ |
| 819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 820 | static PyObject * |
| 821 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 822 | { |
| 823 | char text[1024]; |
| 824 | PyObject *v; |
| 825 | |
| 826 | os2_strerror(text, sizeof(text), code, ""); |
| 827 | |
| 828 | v = Py_BuildValue("(is)", code, text); |
| 829 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 830 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 831 | Py_DECREF(v); |
| 832 | } |
| 833 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 834 | } |
| 835 | |
| 836 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | |
| 838 | /* POSIX generic methods */ |
| 839 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 840 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 841 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 842 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 843 | int fd; |
| 844 | int res; |
| 845 | fd = PyObject_AsFileDescriptor(fdobj); |
| 846 | if (fd < 0) |
| 847 | return NULL; |
| 848 | if (!_PyVerify_fd(fd)) |
| 849 | return posix_error(); |
| 850 | Py_BEGIN_ALLOW_THREADS |
| 851 | res = (*func)(fd); |
| 852 | Py_END_ALLOW_THREADS |
| 853 | if (res < 0) |
| 854 | return posix_error(); |
| 855 | Py_INCREF(Py_None); |
| 856 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 857 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 858 | |
| 859 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 860 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 861 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 862 | PyObject *opath1 = NULL; |
| 863 | char *path1; |
| 864 | int res; |
| 865 | if (!PyArg_ParseTuple(args, format, |
| 866 | PyUnicode_FSConverter, &opath1)) |
| 867 | return NULL; |
| 868 | path1 = PyBytes_AsString(opath1); |
| 869 | Py_BEGIN_ALLOW_THREADS |
| 870 | res = (*func)(path1); |
| 871 | Py_END_ALLOW_THREADS |
| 872 | if (res < 0) |
| 873 | return posix_error_with_allocated_filename(opath1); |
| 874 | Py_DECREF(opath1); |
| 875 | Py_INCREF(Py_None); |
| 876 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 879 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 880 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 881 | char *format, |
| 882 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 884 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 885 | char *path1, *path2; |
| 886 | int res; |
| 887 | if (!PyArg_ParseTuple(args, format, |
| 888 | PyUnicode_FSConverter, &opath1, |
| 889 | PyUnicode_FSConverter, &opath2)) { |
| 890 | return NULL; |
| 891 | } |
| 892 | path1 = PyBytes_AsString(opath1); |
| 893 | path2 = PyBytes_AsString(opath2); |
| 894 | Py_BEGIN_ALLOW_THREADS |
| 895 | res = (*func)(path1, path2); |
| 896 | Py_END_ALLOW_THREADS |
| 897 | Py_DECREF(opath1); |
| 898 | Py_DECREF(opath2); |
| 899 | if (res != 0) |
| 900 | /* XXX how to report both path1 and path2??? */ |
| 901 | return posix_error(); |
| 902 | Py_INCREF(Py_None); |
| 903 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 906 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 907 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 908 | win32_1str(PyObject* args, char* func, |
| 909 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 910 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 911 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 912 | PyObject *uni; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 913 | const char *ansi; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 914 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 915 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 916 | if (PyArg_ParseTuple(args, wformat, &uni)) |
| 917 | { |
| 918 | wchar_t *wstr = PyUnicode_AsUnicode(uni); |
| 919 | if (wstr == NULL) |
| 920 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 921 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 922 | result = funcW(wstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 923 | Py_END_ALLOW_THREADS |
| 924 | if (!result) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 925 | return win32_error_object(func, uni); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 926 | Py_INCREF(Py_None); |
| 927 | return Py_None; |
| 928 | } |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 929 | PyErr_Clear(); |
| 930 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 931 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 932 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 933 | if (win32_warn_bytes_api()) |
| 934 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 935 | Py_BEGIN_ALLOW_THREADS |
| 936 | result = funcA(ansi); |
| 937 | Py_END_ALLOW_THREADS |
| 938 | if (!result) |
| 939 | return win32_error(func, ansi); |
| 940 | Py_INCREF(Py_None); |
| 941 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 942 | |
| 943 | } |
| 944 | |
| 945 | /* This is a reimplementation of the C library's chdir function, |
| 946 | but one that produces Win32 errors instead of DOS error codes. |
| 947 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 948 | it also needs to set "magic" environment variables indicating |
| 949 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 950 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 951 | win32_chdir(LPCSTR path) |
| 952 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 953 | char new_path[MAX_PATH+1]; |
| 954 | int result; |
| 955 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 957 | if(!SetCurrentDirectoryA(path)) |
| 958 | return FALSE; |
| 959 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 960 | if (!result) |
| 961 | return FALSE; |
| 962 | /* In the ANSI API, there should not be any paths longer |
| 963 | than MAX_PATH. */ |
| 964 | assert(result <= MAX_PATH+1); |
| 965 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 966 | strncmp(new_path, "//", 2) == 0) |
| 967 | /* UNC path, nothing to do. */ |
| 968 | return TRUE; |
| 969 | env[1] = new_path[0]; |
| 970 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* The Unicode version differs from the ANSI version |
| 974 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 975 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 976 | win32_wchdir(LPCWSTR path) |
| 977 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 978 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 979 | int result; |
| 980 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 982 | if(!SetCurrentDirectoryW(path)) |
| 983 | return FALSE; |
| 984 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 985 | if (!result) |
| 986 | return FALSE; |
| 987 | if (result > MAX_PATH+1) { |
| 988 | new_path = malloc(result * sizeof(wchar_t)); |
| 989 | if (!new_path) { |
| 990 | SetLastError(ERROR_OUTOFMEMORY); |
| 991 | return FALSE; |
| 992 | } |
| 993 | result = GetCurrentDirectoryW(result, new_path); |
| 994 | if (!result) { |
| 995 | free(new_path); |
| 996 | return FALSE; |
| 997 | } |
| 998 | } |
| 999 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1000 | wcsncmp(new_path, L"//", 2) == 0) |
| 1001 | /* UNC path, nothing to do. */ |
| 1002 | return TRUE; |
| 1003 | env[1] = new_path[0]; |
| 1004 | result = SetEnvironmentVariableW(env, new_path); |
| 1005 | if (new_path != _new_path) |
| 1006 | free(new_path); |
| 1007 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1008 | } |
| 1009 | #endif |
| 1010 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1011 | #ifdef MS_WINDOWS |
| 1012 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1013 | - time stamps are restricted to second resolution |
| 1014 | - file modification times suffer from forth-and-back conversions between |
| 1015 | UTC and local time |
| 1016 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1017 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1018 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1019 | |
| 1020 | struct win32_stat{ |
| 1021 | int st_dev; |
| 1022 | __int64 st_ino; |
| 1023 | unsigned short st_mode; |
| 1024 | int st_nlink; |
| 1025 | int st_uid; |
| 1026 | int st_gid; |
| 1027 | int st_rdev; |
| 1028 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1029 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1030 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1031 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1032 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1033 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1034 | int st_ctime_nsec; |
| 1035 | }; |
| 1036 | |
| 1037 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1038 | |
| 1039 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1040 | 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] | 1041 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1042 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1043 | /* Cannot simply cast and dereference in_ptr, |
| 1044 | since it might not be aligned properly */ |
| 1045 | __int64 in; |
| 1046 | memcpy(&in, in_ptr, sizeof(in)); |
| 1047 | *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] | 1048 | *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] | 1049 | } |
| 1050 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1051 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1052 | 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] | 1053 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1054 | /* XXX endianness */ |
| 1055 | __int64 out; |
| 1056 | out = time_in + secs_between_epochs; |
| 1057 | out = out * 10000000 + nsec_in / 100; |
| 1058 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1061 | /* Below, we *know* that ugo+r is 0444 */ |
| 1062 | #if _S_IREAD != 0400 |
| 1063 | #error Unsupported C library |
| 1064 | #endif |
| 1065 | static int |
| 1066 | attributes_to_mode(DWORD attr) |
| 1067 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1068 | int m = 0; |
| 1069 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1070 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1071 | else |
| 1072 | m |= _S_IFREG; |
| 1073 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1074 | m |= 0444; |
| 1075 | else |
| 1076 | m |= 0666; |
| 1077 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1081 | 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] | 1082 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1083 | memset(result, 0, sizeof(*result)); |
| 1084 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1085 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1086 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1087 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1088 | 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] | 1089 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1090 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1091 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1092 | /* first clear the S_IFMT bits */ |
| 1093 | result->st_mode ^= (result->st_mode & 0170000); |
| 1094 | /* now set the bits that make this a symlink */ |
| 1095 | result->st_mode |= 0120000; |
| 1096 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1098 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1101 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1102 | 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] | 1103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1104 | HANDLE hFindFile; |
| 1105 | WIN32_FIND_DATAA FileData; |
| 1106 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1107 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1108 | return FALSE; |
| 1109 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1110 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1111 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1112 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1113 | info->ftCreationTime = FileData.ftCreationTime; |
| 1114 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1115 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1116 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1117 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1118 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1119 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1120 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1121 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1125 | 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] | 1126 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1127 | HANDLE hFindFile; |
| 1128 | WIN32_FIND_DATAW FileData; |
| 1129 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1130 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1131 | return FALSE; |
| 1132 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1133 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1134 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1135 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1136 | info->ftCreationTime = FileData.ftCreationTime; |
| 1137 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1138 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1139 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1140 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1141 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1142 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1143 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1144 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1147 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1148 | static int has_GetFinalPathNameByHandle = 0; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1149 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1150 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1151 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1152 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1153 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1154 | HINSTANCE hKernel32; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1155 | DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1156 | DWORD); |
| 1157 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1158 | /* only recheck */ |
| 1159 | if (!has_GetFinalPathNameByHandle) |
| 1160 | { |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1161 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1162 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1163 | "GetFinalPathNameByHandleA"); |
| 1164 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1165 | "GetFinalPathNameByHandleW"); |
| 1166 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1167 | Py_GetFinalPathNameByHandleW; |
| 1168 | } |
| 1169 | return has_GetFinalPathNameByHandle; |
| 1170 | } |
| 1171 | |
| 1172 | static BOOL |
| 1173 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1174 | { |
| 1175 | int buf_size, result_length; |
| 1176 | wchar_t *buf; |
| 1177 | |
| 1178 | /* We have a good handle to the target, use it to determine |
| 1179 | the target path name (then we'll call lstat on it). */ |
| 1180 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1181 | VOLUME_NAME_DOS); |
| 1182 | if(!buf_size) |
| 1183 | return FALSE; |
| 1184 | |
| 1185 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1186 | if (!buf) { |
| 1187 | SetLastError(ERROR_OUTOFMEMORY); |
| 1188 | return FALSE; |
| 1189 | } |
| 1190 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1191 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1192 | buf, buf_size, VOLUME_NAME_DOS); |
| 1193 | |
| 1194 | if(!result_length) { |
| 1195 | free(buf); |
| 1196 | return FALSE; |
| 1197 | } |
| 1198 | |
| 1199 | if(!CloseHandle(hdl)) { |
| 1200 | free(buf); |
| 1201 | return FALSE; |
| 1202 | } |
| 1203 | |
| 1204 | buf[result_length] = 0; |
| 1205 | |
| 1206 | *target_path = buf; |
| 1207 | return TRUE; |
| 1208 | } |
| 1209 | |
| 1210 | static int |
| 1211 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1212 | BOOL traverse); |
| 1213 | static int |
| 1214 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1215 | BOOL traverse) |
| 1216 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1217 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1218 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1219 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1220 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1221 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1222 | const char *dot; |
| 1223 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1224 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1225 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1226 | traverse reparse point. */ |
| 1227 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1230 | hFile = CreateFileA( |
| 1231 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1232 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1233 | 0, /* share mode */ |
| 1234 | NULL, /* security attributes */ |
| 1235 | OPEN_EXISTING, |
| 1236 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1237 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1238 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1239 | the symlink path agin and not the actual final path. */ |
| 1240 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1241 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1242 | NULL); |
| 1243 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1244 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1245 | /* Either the target doesn't exist, or we don't have access to |
| 1246 | get a handle to it. If the former, we need to return an error. |
| 1247 | If the latter, we can use attributes_from_dir. */ |
| 1248 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1249 | return -1; |
| 1250 | /* Could not get attributes on open file. Fall back to |
| 1251 | reading the directory. */ |
| 1252 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1253 | /* Very strange. This should not fail now */ |
| 1254 | return -1; |
| 1255 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1256 | if (traverse) { |
| 1257 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1258 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1259 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1260 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1261 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1262 | } else { |
| 1263 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1264 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1265 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1266 | } |
| 1267 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1268 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1269 | return -1; |
| 1270 | |
| 1271 | /* Close the outer open file handle now that we're about to |
| 1272 | reopen it with different flags. */ |
| 1273 | if (!CloseHandle(hFile)) |
| 1274 | return -1; |
| 1275 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1276 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1277 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1278 | the file without the reparse handling flag set. */ |
| 1279 | hFile2 = CreateFileA( |
| 1280 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1281 | NULL, OPEN_EXISTING, |
| 1282 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1283 | NULL); |
| 1284 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1285 | return -1; |
| 1286 | |
| 1287 | if (!get_target_path(hFile2, &target_path)) |
| 1288 | return -1; |
| 1289 | |
| 1290 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1291 | free(target_path); |
| 1292 | return code; |
| 1293 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1294 | } else |
| 1295 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1296 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1297 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1298 | |
| 1299 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1300 | dot = strrchr(path, '.'); |
| 1301 | if (dot) { |
| 1302 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1303 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1304 | result->st_mode |= 0111; |
| 1305 | } |
| 1306 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1310 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1311 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1312 | { |
| 1313 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1314 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1315 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1316 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1317 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1318 | const wchar_t *dot; |
| 1319 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1320 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1321 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1322 | traverse reparse point. */ |
| 1323 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1326 | hFile = CreateFileW( |
| 1327 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1328 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1329 | 0, /* share mode */ |
| 1330 | NULL, /* security attributes */ |
| 1331 | OPEN_EXISTING, |
| 1332 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1333 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1334 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1335 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1336 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1337 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1338 | NULL); |
| 1339 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1340 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1341 | /* Either the target doesn't exist, or we don't have access to |
| 1342 | get a handle to it. If the former, we need to return an error. |
| 1343 | If the latter, we can use attributes_from_dir. */ |
| 1344 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1345 | return -1; |
| 1346 | /* Could not get attributes on open file. Fall back to |
| 1347 | reading the directory. */ |
| 1348 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1349 | /* Very strange. This should not fail now */ |
| 1350 | return -1; |
| 1351 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1352 | if (traverse) { |
| 1353 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1354 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1355 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1356 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1357 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1358 | } else { |
| 1359 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1360 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1361 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1362 | } |
| 1363 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1364 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1365 | return -1; |
| 1366 | |
| 1367 | /* Close the outer open file handle now that we're about to |
| 1368 | reopen it with different flags. */ |
| 1369 | if (!CloseHandle(hFile)) |
| 1370 | return -1; |
| 1371 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1372 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1373 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1374 | the file without the reparse handling flag set. */ |
| 1375 | hFile2 = CreateFileW( |
| 1376 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1377 | NULL, OPEN_EXISTING, |
| 1378 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1379 | NULL); |
| 1380 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1381 | return -1; |
| 1382 | |
| 1383 | if (!get_target_path(hFile2, &target_path)) |
| 1384 | return -1; |
| 1385 | |
| 1386 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1387 | free(target_path); |
| 1388 | return code; |
| 1389 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1390 | } else |
| 1391 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1392 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1393 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1394 | |
| 1395 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1396 | dot = wcsrchr(path, '.'); |
| 1397 | if (dot) { |
| 1398 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1399 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1400 | result->st_mode |= 0111; |
| 1401 | } |
| 1402 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1406 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1407 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1408 | /* Protocol violation: we explicitly clear errno, instead of |
| 1409 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1410 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1411 | errno = 0; |
| 1412 | return code; |
| 1413 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1414 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1415 | static int |
| 1416 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1417 | { |
| 1418 | /* Protocol violation: we explicitly clear errno, instead of |
| 1419 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1420 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1421 | errno = 0; |
| 1422 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1423 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1424 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1425 | |
| 1426 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1427 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1428 | default does not traverse symlinks and instead returns attributes for |
| 1429 | the symlink. |
| 1430 | |
| 1431 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1432 | win32_stat will first explicitly resolve the symlink target and then will |
| 1433 | call win32_lstat on that result. |
| 1434 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1435 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1436 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1437 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1438 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1439 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1440 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1444 | 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] | 1445 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1446 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | static int |
| 1450 | win32_stat(const char* path, struct win32_stat *result) |
| 1451 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1452 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1455 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1456 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1457 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1458 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | static int |
| 1462 | win32_fstat(int file_number, struct win32_stat *result) |
| 1463 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1464 | BY_HANDLE_FILE_INFORMATION info; |
| 1465 | HANDLE h; |
| 1466 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1467 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1468 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1469 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1470 | /* Protocol violation: we explicitly clear errno, instead of |
| 1471 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1472 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1474 | if (h == INVALID_HANDLE_VALUE) { |
| 1475 | /* This is really a C library error (invalid file handle). |
| 1476 | We set the Win32 error to the closes one matching. */ |
| 1477 | SetLastError(ERROR_INVALID_HANDLE); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1481 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1482 | type = GetFileType(h); |
| 1483 | if (type == FILE_TYPE_UNKNOWN) { |
| 1484 | DWORD error = GetLastError(); |
| 1485 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1486 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1487 | } |
| 1488 | /* else: valid but unknown file */ |
| 1489 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1490 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1491 | if (type != FILE_TYPE_DISK) { |
| 1492 | if (type == FILE_TYPE_CHAR) |
| 1493 | result->st_mode = _S_IFCHR; |
| 1494 | else if (type == FILE_TYPE_PIPE) |
| 1495 | result->st_mode = _S_IFIFO; |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | if (!GetFileInformationByHandle(h, &info)) { |
| 1500 | return -1; |
| 1501 | } |
| 1502 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1503 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1504 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1505 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1506 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | #endif /* MS_WINDOWS */ |
| 1510 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1511 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1512 | "stat_result: Result from stat or lstat.\n\n\ |
| 1513 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1514 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1515 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1516 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1517 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1518 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1519 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1520 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1521 | |
| 1522 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1523 | {"st_mode", "protection bits"}, |
| 1524 | {"st_ino", "inode"}, |
| 1525 | {"st_dev", "device"}, |
| 1526 | {"st_nlink", "number of hard links"}, |
| 1527 | {"st_uid", "user ID of owner"}, |
| 1528 | {"st_gid", "group ID of owner"}, |
| 1529 | {"st_size", "total size, in bytes"}, |
| 1530 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1531 | {NULL, "integer time of last access"}, |
| 1532 | {NULL, "integer time of last modification"}, |
| 1533 | {NULL, "integer time of last change"}, |
| 1534 | {"st_atime", "time of last access"}, |
| 1535 | {"st_mtime", "time of last modification"}, |
| 1536 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1537 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1538 | {"st_blksize", "blocksize for filesystem I/O"}, |
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_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1541 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1542 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1543 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1544 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1545 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1546 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1547 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1548 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1549 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1550 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1551 | #endif |
| 1552 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1553 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1554 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1555 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1556 | }; |
| 1557 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1558 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1559 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1560 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1561 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1562 | #endif |
| 1563 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1564 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1565 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1566 | #else |
| 1567 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1568 | #endif |
| 1569 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1570 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1571 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1572 | #else |
| 1573 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1574 | #endif |
| 1575 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1576 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1577 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1578 | #else |
| 1579 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1580 | #endif |
| 1581 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1582 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1583 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1584 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1585 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1586 | #endif |
| 1587 | |
| 1588 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1589 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1590 | #else |
| 1591 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1592 | #endif |
| 1593 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1594 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1595 | "stat_result", /* name */ |
| 1596 | stat_result__doc__, /* doc */ |
| 1597 | stat_result_fields, |
| 1598 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1599 | }; |
| 1600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1601 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1602 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1603 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1604 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1605 | 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] | 1606 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1607 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1608 | |
| 1609 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1610 | {"f_bsize", }, |
| 1611 | {"f_frsize", }, |
| 1612 | {"f_blocks", }, |
| 1613 | {"f_bfree", }, |
| 1614 | {"f_bavail", }, |
| 1615 | {"f_files", }, |
| 1616 | {"f_ffree", }, |
| 1617 | {"f_favail", }, |
| 1618 | {"f_flag", }, |
| 1619 | {"f_namemax",}, |
| 1620 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1621 | }; |
| 1622 | |
| 1623 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1624 | "statvfs_result", /* name */ |
| 1625 | statvfs_result__doc__, /* doc */ |
| 1626 | statvfs_result_fields, |
| 1627 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1628 | }; |
| 1629 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1630 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1631 | PyDoc_STRVAR(waitid_result__doc__, |
| 1632 | "waitid_result: Result from waitid.\n\n\ |
| 1633 | This object may be accessed either as a tuple of\n\ |
| 1634 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1635 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1636 | \n\ |
| 1637 | See os.waitid for more information."); |
| 1638 | |
| 1639 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1640 | {"si_pid", }, |
| 1641 | {"si_uid", }, |
| 1642 | {"si_signo", }, |
| 1643 | {"si_status", }, |
| 1644 | {"si_code", }, |
| 1645 | {0} |
| 1646 | }; |
| 1647 | |
| 1648 | static PyStructSequence_Desc waitid_result_desc = { |
| 1649 | "waitid_result", /* name */ |
| 1650 | waitid_result__doc__, /* doc */ |
| 1651 | waitid_result_fields, |
| 1652 | 5 |
| 1653 | }; |
| 1654 | static PyTypeObject WaitidResultType; |
| 1655 | #endif |
| 1656 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1657 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1658 | static PyTypeObject StatResultType; |
| 1659 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1660 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1661 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1662 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1663 | static newfunc structseq_new; |
| 1664 | |
| 1665 | static PyObject * |
| 1666 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1667 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1668 | PyStructSequence *result; |
| 1669 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1670 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1671 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1672 | if (!result) |
| 1673 | return NULL; |
| 1674 | /* If we have been initialized from a tuple, |
| 1675 | st_?time might be set to None. Initialize it |
| 1676 | from the int slots. */ |
| 1677 | for (i = 7; i <= 9; i++) { |
| 1678 | if (result->ob_item[i+3] == Py_None) { |
| 1679 | Py_DECREF(Py_None); |
| 1680 | Py_INCREF(result->ob_item[i]); |
| 1681 | result->ob_item[i+3] = result->ob_item[i]; |
| 1682 | } |
| 1683 | } |
| 1684 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1690 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1691 | |
| 1692 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1693 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1694 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1695 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1696 | future calls return ints. \n\ |
| 1697 | If newval is omitted, return the current setting.\n"); |
| 1698 | |
| 1699 | static PyObject* |
| 1700 | stat_float_times(PyObject* self, PyObject *args) |
| 1701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1702 | int newval = -1; |
| 1703 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1704 | return NULL; |
| 1705 | if (newval == -1) |
| 1706 | /* Return old value */ |
| 1707 | return PyBool_FromLong(_stat_float_times); |
| 1708 | _stat_float_times = newval; |
| 1709 | Py_INCREF(Py_None); |
| 1710 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1711 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1712 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1713 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1714 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1715 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1716 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1717 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1718 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1719 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1720 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1721 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | if (!ival) |
| 1723 | return; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1724 | if (_stat_float_times) { |
| 1725 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1726 | } else { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1727 | fval = ival; |
| 1728 | Py_INCREF(fval); |
| 1729 | } |
| 1730 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1731 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1734 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1735 | (used by posix_stat() and posix_fstat()) */ |
| 1736 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1737 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1738 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1739 | unsigned long ansec, mnsec, cnsec; |
| 1740 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1741 | if (v == NULL) |
| 1742 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1743 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1744 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1745 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1746 | PyStructSequence_SET_ITEM(v, 1, |
| 1747 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1748 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1749 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1750 | #endif |
| 1751 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | PyStructSequence_SET_ITEM(v, 2, |
| 1753 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1754 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1755 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1756 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1757 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1758 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1759 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1760 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1761 | PyStructSequence_SET_ITEM(v, 6, |
| 1762 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1763 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1765 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1766 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1767 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1768 | ansec = st->st_atim.tv_nsec; |
| 1769 | mnsec = st->st_mtim.tv_nsec; |
| 1770 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1771 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1772 | ansec = st->st_atimespec.tv_nsec; |
| 1773 | mnsec = st->st_mtimespec.tv_nsec; |
| 1774 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1775 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1776 | ansec = st->st_atime_nsec; |
| 1777 | mnsec = st->st_mtime_nsec; |
| 1778 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1779 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1781 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1782 | fill_time(v, 7, st->st_atime, ansec); |
| 1783 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1784 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1785 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1786 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1787 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1788 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1789 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1791 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1792 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1793 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1794 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1795 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1796 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1797 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1798 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1799 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1800 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1801 | #endif |
| 1802 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1803 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1804 | PyObject *val; |
| 1805 | unsigned long bsec,bnsec; |
| 1806 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1807 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1808 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1809 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1810 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1811 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1812 | if (_stat_float_times) { |
| 1813 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1814 | } else { |
| 1815 | val = PyLong_FromLong((long)bsec); |
| 1816 | } |
| 1817 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1818 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1819 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1820 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1821 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1822 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1823 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1824 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1825 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1826 | if (PyErr_Occurred()) { |
| 1827 | Py_DECREF(v); |
| 1828 | return NULL; |
| 1829 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1830 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1831 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1834 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1835 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1836 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1837 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1838 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1839 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1841 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1842 | char *wformat, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1843 | int (*wstatfunc)(const wchar_t *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1844 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1845 | STRUCT_STAT st; |
| 1846 | PyObject *opath; |
| 1847 | char *path; |
| 1848 | int res; |
| 1849 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1850 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1851 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1852 | PyObject *po; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1853 | if (PyArg_ParseTuple(args, wformat, &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1854 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 1855 | if (wpath == NULL) |
| 1856 | return NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1857 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1858 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1859 | res = wstatfunc(wpath, &st); |
| 1860 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1861 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1862 | if (res != 0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1863 | return win32_error_object("stat", po); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1864 | return _pystat_fromstructstat(&st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1865 | } |
| 1866 | /* Drop the argument parsing error as narrow strings |
| 1867 | are also valid. */ |
| 1868 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1869 | #endif |
| 1870 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1871 | if (!PyArg_ParseTuple(args, format, |
| 1872 | PyUnicode_FSConverter, &opath)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1873 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1874 | #ifdef MS_WINDOWS |
| 1875 | if (win32_warn_bytes_api()) { |
| 1876 | Py_DECREF(opath); |
| 1877 | return NULL; |
| 1878 | } |
| 1879 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1880 | path = PyBytes_AsString(opath); |
| 1881 | Py_BEGIN_ALLOW_THREADS |
| 1882 | res = (*statfunc)(path, &st); |
| 1883 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1884 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1885 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1886 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1887 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1888 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1889 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1890 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1891 | } |
| 1892 | else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 1893 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1894 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1895 | Py_DECREF(opath); |
| 1896 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1899 | /* POSIX methods */ |
| 1900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1901 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1902 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1903 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1904 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1905 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1906 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1907 | 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] | 1908 | |
| 1909 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1910 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1911 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1912 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1913 | int mode; |
| 1914 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1915 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1916 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1917 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1918 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1919 | wchar_t* wpath = PyUnicode_AsUnicode(po); |
| 1920 | if (wpath == NULL) |
| 1921 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1922 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1923 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | Py_END_ALLOW_THREADS |
| 1925 | goto finish; |
| 1926 | } |
| 1927 | /* Drop the argument parsing error as narrow strings |
| 1928 | are also valid. */ |
| 1929 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1930 | if (!PyArg_ParseTuple(args, "yi:access", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1932 | if (win32_warn_bytes_api()) |
| 1933 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1934 | Py_BEGIN_ALLOW_THREADS |
| 1935 | attr = GetFileAttributesA(path); |
| 1936 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1937 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | if (attr == 0xFFFFFFFF) |
| 1939 | /* File does not exist, or cannot read attributes */ |
| 1940 | return PyBool_FromLong(0); |
| 1941 | /* Access is possible if either write access wasn't requested, or |
| 1942 | the file isn't read-only, or if it's a directory, as there are |
| 1943 | no read-only directories on Windows. */ |
| 1944 | return PyBool_FromLong(!(mode & 2) |
| 1945 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1946 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1947 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1948 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1949 | int res; |
| 1950 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1951 | PyUnicode_FSConverter, &opath, &mode)) |
| 1952 | return NULL; |
| 1953 | path = PyBytes_AsString(opath); |
| 1954 | Py_BEGIN_ALLOW_THREADS |
| 1955 | res = access(path, mode); |
| 1956 | Py_END_ALLOW_THREADS |
| 1957 | Py_DECREF(opath); |
| 1958 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1959 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1962 | #ifndef F_OK |
| 1963 | #define F_OK 0 |
| 1964 | #endif |
| 1965 | #ifndef R_OK |
| 1966 | #define R_OK 4 |
| 1967 | #endif |
| 1968 | #ifndef W_OK |
| 1969 | #define W_OK 2 |
| 1970 | #endif |
| 1971 | #ifndef X_OK |
| 1972 | #define X_OK 1 |
| 1973 | #endif |
| 1974 | |
| 1975 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1977 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1978 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1979 | |
| 1980 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1981 | posix_ttyname(PyObject *self, PyObject *args) |
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 | int id; |
| 1984 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1986 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1987 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1988 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1989 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1990 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1991 | if (id == 0) { |
| 1992 | ret = ttyname(); |
| 1993 | } |
| 1994 | else { |
| 1995 | ret = NULL; |
| 1996 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1997 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1998 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1999 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2000 | if (ret == NULL) |
| 2001 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2002 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2003 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2004 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2005 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2006 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2007 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2008 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2009 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2010 | |
| 2011 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 2012 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2013 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2014 | char *ret; |
| 2015 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2016 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2017 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2018 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2019 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2020 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2021 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | if (ret == NULL) |
| 2023 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2024 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2025 | } |
| 2026 | #endif |
| 2027 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2028 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2029 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2030 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2031 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2032 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2033 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2034 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2035 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2036 | 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] | 2037 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2038 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 2039 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2040 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2041 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2042 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2043 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2046 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2047 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2048 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2049 | 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] | 2050 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2051 | |
| 2052 | static PyObject * |
| 2053 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2054 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2056 | } |
| 2057 | #endif /* HAVE_FCHDIR */ |
| 2058 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2059 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2060 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2061 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2062 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2063 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2064 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2065 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2066 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2067 | PyObject *opath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2068 | const char *path = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | int i; |
| 2070 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2071 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2072 | DWORD attr; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2073 | PyObject *po; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2074 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2075 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 2076 | if (wpath == NULL) |
| 2077 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2078 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2079 | attr = GetFileAttributesW(wpath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2080 | if (attr != 0xFFFFFFFF) { |
| 2081 | if (i & _S_IWRITE) |
| 2082 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2083 | else |
| 2084 | attr |= FILE_ATTRIBUTE_READONLY; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2085 | res = SetFileAttributesW(wpath, attr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2086 | } |
| 2087 | else |
| 2088 | res = 0; |
| 2089 | Py_END_ALLOW_THREADS |
| 2090 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2091 | return win32_error_object("chmod", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2092 | Py_INCREF(Py_None); |
| 2093 | return Py_None; |
| 2094 | } |
| 2095 | /* Drop the argument parsing error as narrow strings |
| 2096 | are also valid. */ |
| 2097 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2098 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2099 | if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2100 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2101 | if (win32_warn_bytes_api()) |
| 2102 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2103 | Py_BEGIN_ALLOW_THREADS |
| 2104 | attr = GetFileAttributesA(path); |
| 2105 | if (attr != 0xFFFFFFFF) { |
| 2106 | if (i & _S_IWRITE) |
| 2107 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2108 | else |
| 2109 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2110 | res = SetFileAttributesA(path, attr); |
| 2111 | } |
| 2112 | else |
| 2113 | res = 0; |
| 2114 | Py_END_ALLOW_THREADS |
| 2115 | if (!res) { |
| 2116 | win32_error("chmod", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2117 | return NULL; |
| 2118 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2119 | Py_INCREF(Py_None); |
| 2120 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2121 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2122 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2123 | &opath, &i)) |
| 2124 | return NULL; |
| 2125 | path = PyBytes_AsString(opath); |
| 2126 | Py_BEGIN_ALLOW_THREADS |
| 2127 | res = chmod(path, i); |
| 2128 | Py_END_ALLOW_THREADS |
| 2129 | if (res < 0) |
| 2130 | return posix_error_with_allocated_filename(opath); |
| 2131 | Py_DECREF(opath); |
| 2132 | Py_INCREF(Py_None); |
| 2133 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2134 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2137 | #ifdef HAVE_FCHMOD |
| 2138 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2139 | "fchmod(fd, mode)\n\n\ |
| 2140 | Change the access permissions of the file given by file\n\ |
| 2141 | descriptor fd."); |
| 2142 | |
| 2143 | static PyObject * |
| 2144 | posix_fchmod(PyObject *self, PyObject *args) |
| 2145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2146 | int fd, mode, res; |
| 2147 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2148 | return NULL; |
| 2149 | Py_BEGIN_ALLOW_THREADS |
| 2150 | res = fchmod(fd, mode); |
| 2151 | Py_END_ALLOW_THREADS |
| 2152 | if (res < 0) |
| 2153 | return posix_error(); |
| 2154 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2155 | } |
| 2156 | #endif /* HAVE_FCHMOD */ |
| 2157 | |
| 2158 | #ifdef HAVE_LCHMOD |
| 2159 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2160 | "lchmod(path, mode)\n\n\ |
| 2161 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2162 | affects the link itself rather than the target."); |
| 2163 | |
| 2164 | static PyObject * |
| 2165 | posix_lchmod(PyObject *self, PyObject *args) |
| 2166 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2167 | PyObject *opath; |
| 2168 | char *path; |
| 2169 | int i; |
| 2170 | int res; |
| 2171 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2172 | &opath, &i)) |
| 2173 | return NULL; |
| 2174 | path = PyBytes_AsString(opath); |
| 2175 | Py_BEGIN_ALLOW_THREADS |
| 2176 | res = lchmod(path, i); |
| 2177 | Py_END_ALLOW_THREADS |
| 2178 | if (res < 0) |
| 2179 | return posix_error_with_allocated_filename(opath); |
| 2180 | Py_DECREF(opath); |
| 2181 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2182 | } |
| 2183 | #endif /* HAVE_LCHMOD */ |
| 2184 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2185 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2186 | #ifdef HAVE_CHFLAGS |
| 2187 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2188 | "chflags(path, flags)\n\n\ |
| 2189 | Set file flags."); |
| 2190 | |
| 2191 | static PyObject * |
| 2192 | posix_chflags(PyObject *self, PyObject *args) |
| 2193 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2194 | PyObject *opath; |
| 2195 | char *path; |
| 2196 | unsigned long flags; |
| 2197 | int res; |
| 2198 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2199 | PyUnicode_FSConverter, &opath, &flags)) |
| 2200 | return NULL; |
| 2201 | path = PyBytes_AsString(opath); |
| 2202 | Py_BEGIN_ALLOW_THREADS |
| 2203 | res = chflags(path, flags); |
| 2204 | Py_END_ALLOW_THREADS |
| 2205 | if (res < 0) |
| 2206 | return posix_error_with_allocated_filename(opath); |
| 2207 | Py_DECREF(opath); |
| 2208 | Py_INCREF(Py_None); |
| 2209 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2210 | } |
| 2211 | #endif /* HAVE_CHFLAGS */ |
| 2212 | |
| 2213 | #ifdef HAVE_LCHFLAGS |
| 2214 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2215 | "lchflags(path, flags)\n\n\ |
| 2216 | Set file flags.\n\ |
| 2217 | This function will not follow symbolic links."); |
| 2218 | |
| 2219 | static PyObject * |
| 2220 | posix_lchflags(PyObject *self, PyObject *args) |
| 2221 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2222 | PyObject *opath; |
| 2223 | char *path; |
| 2224 | unsigned long flags; |
| 2225 | int res; |
| 2226 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2227 | PyUnicode_FSConverter, &opath, &flags)) |
| 2228 | return NULL; |
| 2229 | path = PyBytes_AsString(opath); |
| 2230 | Py_BEGIN_ALLOW_THREADS |
| 2231 | res = lchflags(path, flags); |
| 2232 | Py_END_ALLOW_THREADS |
| 2233 | if (res < 0) |
| 2234 | return posix_error_with_allocated_filename(opath); |
| 2235 | Py_DECREF(opath); |
| 2236 | Py_INCREF(Py_None); |
| 2237 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2238 | } |
| 2239 | #endif /* HAVE_LCHFLAGS */ |
| 2240 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2241 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2242 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2243 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2244 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2245 | |
| 2246 | static PyObject * |
| 2247 | posix_chroot(PyObject *self, PyObject *args) |
| 2248 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2249 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2250 | } |
| 2251 | #endif |
| 2252 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2253 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2254 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2255 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2256 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2257 | |
| 2258 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2259 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2260 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2261 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2262 | } |
| 2263 | #endif /* HAVE_FSYNC */ |
| 2264 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2265 | #ifdef HAVE_SYNC |
| 2266 | PyDoc_STRVAR(posix_sync__doc__, |
| 2267 | "sync()\n\n\ |
| 2268 | Force write of everything to disk."); |
| 2269 | |
| 2270 | static PyObject * |
| 2271 | posix_sync(PyObject *self, PyObject *noargs) |
| 2272 | { |
| 2273 | Py_BEGIN_ALLOW_THREADS |
| 2274 | sync(); |
| 2275 | Py_END_ALLOW_THREADS |
| 2276 | Py_RETURN_NONE; |
| 2277 | } |
| 2278 | #endif |
| 2279 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2280 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2281 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2282 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2283 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2284 | #endif |
| 2285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2287 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2288 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2289 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2290 | |
| 2291 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2292 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2293 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2294 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2295 | } |
| 2296 | #endif /* HAVE_FDATASYNC */ |
| 2297 | |
| 2298 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2299 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2300 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2301 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2302 | 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] | 2303 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2304 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2305 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2307 | PyObject *opath; |
| 2308 | char *path; |
| 2309 | long uid, gid; |
| 2310 | int res; |
| 2311 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2312 | PyUnicode_FSConverter, &opath, |
| 2313 | &uid, &gid)) |
| 2314 | return NULL; |
| 2315 | path = PyBytes_AsString(opath); |
| 2316 | Py_BEGIN_ALLOW_THREADS |
| 2317 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2318 | Py_END_ALLOW_THREADS |
| 2319 | if (res < 0) |
| 2320 | return posix_error_with_allocated_filename(opath); |
| 2321 | Py_DECREF(opath); |
| 2322 | Py_INCREF(Py_None); |
| 2323 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2324 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2325 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2326 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2327 | #ifdef HAVE_FCHOWN |
| 2328 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2329 | "fchown(fd, uid, gid)\n\n\ |
| 2330 | Change the owner and group id of the file given by file descriptor\n\ |
| 2331 | fd to the numeric uid and gid."); |
| 2332 | |
| 2333 | static PyObject * |
| 2334 | posix_fchown(PyObject *self, PyObject *args) |
| 2335 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2336 | int fd; |
| 2337 | long uid, gid; |
| 2338 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2339 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2340 | return NULL; |
| 2341 | Py_BEGIN_ALLOW_THREADS |
| 2342 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2343 | Py_END_ALLOW_THREADS |
| 2344 | if (res < 0) |
| 2345 | return posix_error(); |
| 2346 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2347 | } |
| 2348 | #endif /* HAVE_FCHOWN */ |
| 2349 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2350 | #ifdef HAVE_LCHOWN |
| 2351 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2352 | "lchown(path, uid, gid)\n\n\ |
| 2353 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2354 | This function will not follow symbolic links."); |
| 2355 | |
| 2356 | static PyObject * |
| 2357 | posix_lchown(PyObject *self, PyObject *args) |
| 2358 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2359 | PyObject *opath; |
| 2360 | char *path; |
| 2361 | long uid, gid; |
| 2362 | int res; |
| 2363 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2364 | PyUnicode_FSConverter, &opath, |
| 2365 | &uid, &gid)) |
| 2366 | return NULL; |
| 2367 | path = PyBytes_AsString(opath); |
| 2368 | Py_BEGIN_ALLOW_THREADS |
| 2369 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2370 | Py_END_ALLOW_THREADS |
| 2371 | if (res < 0) |
| 2372 | return posix_error_with_allocated_filename(opath); |
| 2373 | Py_DECREF(opath); |
| 2374 | Py_INCREF(Py_None); |
| 2375 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2376 | } |
| 2377 | #endif /* HAVE_LCHOWN */ |
| 2378 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2379 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2380 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2381 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2382 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2383 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2384 | char buf[1026]; |
| 2385 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2386 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2387 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2388 | if (!use_bytes) { |
| 2389 | wchar_t wbuf[1026]; |
| 2390 | wchar_t *wbuf2 = wbuf; |
| 2391 | PyObject *resobj; |
| 2392 | DWORD len; |
| 2393 | Py_BEGIN_ALLOW_THREADS |
| 2394 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2395 | /* If the buffer is large enough, len does not include the |
| 2396 | terminating \0. If the buffer is too small, len includes |
| 2397 | the space needed for the terminator. */ |
| 2398 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2399 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2400 | if (wbuf2) |
| 2401 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2402 | } |
| 2403 | Py_END_ALLOW_THREADS |
| 2404 | if (!wbuf2) { |
| 2405 | PyErr_NoMemory(); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | if (!len) { |
| 2409 | if (wbuf2 != wbuf) free(wbuf2); |
| 2410 | return win32_error("getcwdu", NULL); |
| 2411 | } |
| 2412 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2413 | if (wbuf2 != wbuf) free(wbuf2); |
| 2414 | return resobj; |
| 2415 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2416 | |
| 2417 | if (win32_warn_bytes_api()) |
| 2418 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2419 | #endif |
| 2420 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2421 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2422 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2423 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2424 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2425 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2426 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2427 | Py_END_ALLOW_THREADS |
| 2428 | if (res == NULL) |
| 2429 | return posix_error(); |
| 2430 | if (use_bytes) |
| 2431 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2432 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2433 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2434 | |
| 2435 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2436 | "getcwd() -> path\n\n\ |
| 2437 | Return a unicode string representing the current working directory."); |
| 2438 | |
| 2439 | static PyObject * |
| 2440 | posix_getcwd_unicode(PyObject *self) |
| 2441 | { |
| 2442 | return posix_getcwd(0); |
| 2443 | } |
| 2444 | |
| 2445 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2446 | "getcwdb() -> path\n\n\ |
| 2447 | Return a bytes string representing the current working directory."); |
| 2448 | |
| 2449 | static PyObject * |
| 2450 | posix_getcwd_bytes(PyObject *self) |
| 2451 | { |
| 2452 | return posix_getcwd(1); |
| 2453 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2454 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2455 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2456 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2457 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2458 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2459 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2460 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2461 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2462 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2463 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2464 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2465 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2466 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2467 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2468 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2469 | #ifdef MS_WINDOWS |
| 2470 | PyDoc_STRVAR(win32_link__doc__, |
| 2471 | "link(src, dst)\n\n\ |
| 2472 | Create a hard link to a file."); |
| 2473 | |
| 2474 | static PyObject * |
| 2475 | win32_link(PyObject *self, PyObject *args) |
| 2476 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2477 | PyObject *src, *dst; |
| 2478 | BOOL ok; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2479 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2480 | if (PyArg_ParseTuple(args, "UU:link", &src, &dst)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2481 | { |
| 2482 | wchar_t *wsrc, *wdst; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2483 | |
| 2484 | wsrc = PyUnicode_AsUnicode(src); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2485 | if (wsrc == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2486 | goto error; |
| 2487 | wdst = PyUnicode_AsUnicode(dst); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2488 | if (wdst == NULL) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2489 | goto error; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2490 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2491 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2492 | ok = CreateHardLinkW(wdst, wsrc, NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2493 | Py_END_ALLOW_THREADS |
| 2494 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2495 | if (!ok) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2496 | return win32_error("link", NULL); |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2497 | Py_RETURN_NONE; |
| 2498 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2499 | else { |
| 2500 | PyErr_Clear(); |
| 2501 | if (!PyArg_ParseTuple(args, "O&O&:link", |
| 2502 | PyUnicode_FSConverter, &src, |
| 2503 | PyUnicode_FSConverter, &dst)) |
| 2504 | return NULL; |
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 | if (win32_warn_bytes_api()) |
| 2507 | goto error; |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2508 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2509 | Py_BEGIN_ALLOW_THREADS |
| 2510 | ok = CreateHardLinkA(PyBytes_AS_STRING(dst), |
| 2511 | PyBytes_AS_STRING(src), |
| 2512 | NULL); |
| 2513 | Py_END_ALLOW_THREADS |
| 2514 | |
| 2515 | Py_XDECREF(src); |
| 2516 | Py_XDECREF(dst); |
| 2517 | |
| 2518 | if (!ok) |
| 2519 | return win32_error("link", NULL); |
| 2520 | Py_RETURN_NONE; |
| 2521 | |
| 2522 | error: |
| 2523 | Py_XDECREF(src); |
| 2524 | Py_XDECREF(dst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2525 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2526 | } |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2527 | } |
| 2528 | #endif /* MS_WINDOWS */ |
| 2529 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2531 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2532 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2533 | Return a list containing the names of the entries in the directory.\n\ |
| 2534 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2535 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2536 | \n\ |
| 2537 | 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] | 2538 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2539 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2540 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2541 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2543 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2544 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2545 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2546 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2547 | PyObject *d, *v; |
| 2548 | HANDLE hFindFile; |
| 2549 | BOOL result; |
| 2550 | WIN32_FIND_DATA FileData; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2551 | const char *path; |
| 2552 | Py_ssize_t pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2553 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2554 | char *bufptr = namebuf; |
| 2555 | 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] | 2556 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2557 | PyObject *po = NULL; |
| 2558 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2559 | WIN32_FIND_DATAW wFileData; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2560 | wchar_t *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2561 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2562 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2563 | po_wchars = L"."; |
| 2564 | len = 1; |
| 2565 | } else { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 2566 | po_wchars = PyUnicode_AsUnicodeAndSize(po, &len); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2567 | if (po_wchars == NULL) |
| 2568 | return NULL; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2569 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2570 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2571 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2572 | if (!wnamebuf) { |
| 2573 | PyErr_NoMemory(); |
| 2574 | return NULL; |
| 2575 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2576 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2577 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2578 | wchar_t wch = wnamebuf[len-1]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2579 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2580 | wnamebuf[len++] = L'\\'; |
| 2581 | wcscpy(wnamebuf + len, L"*.*"); |
| 2582 | } |
| 2583 | if ((d = PyList_New(0)) == NULL) { |
| 2584 | free(wnamebuf); |
| 2585 | return NULL; |
| 2586 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2587 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2588 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2589 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2590 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2591 | int error = GetLastError(); |
| 2592 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2593 | free(wnamebuf); |
| 2594 | return d; |
| 2595 | } |
| 2596 | Py_DECREF(d); |
| 2597 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2598 | free(wnamebuf); |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | do { |
| 2602 | /* Skip over . and .. */ |
| 2603 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2604 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2605 | v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2606 | if (v == NULL) { |
| 2607 | Py_DECREF(d); |
| 2608 | d = NULL; |
| 2609 | break; |
| 2610 | } |
| 2611 | if (PyList_Append(d, v) != 0) { |
| 2612 | Py_DECREF(v); |
| 2613 | Py_DECREF(d); |
| 2614 | d = NULL; |
| 2615 | break; |
| 2616 | } |
| 2617 | Py_DECREF(v); |
| 2618 | } |
| 2619 | Py_BEGIN_ALLOW_THREADS |
| 2620 | result = FindNextFileW(hFindFile, &wFileData); |
| 2621 | Py_END_ALLOW_THREADS |
| 2622 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2623 | it got to the end of the directory. */ |
| 2624 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2625 | Py_DECREF(d); |
| 2626 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2627 | FindClose(hFindFile); |
| 2628 | free(wnamebuf); |
| 2629 | return NULL; |
| 2630 | } |
| 2631 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2632 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2633 | if (FindClose(hFindFile) == FALSE) { |
| 2634 | Py_DECREF(d); |
| 2635 | win32_error_unicode("FindClose", wnamebuf); |
| 2636 | free(wnamebuf); |
| 2637 | return NULL; |
| 2638 | } |
| 2639 | free(wnamebuf); |
| 2640 | return d; |
| 2641 | } |
| 2642 | /* Drop the argument parsing error as narrow strings |
| 2643 | are also valid. */ |
| 2644 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2645 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2646 | if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2647 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2648 | if (win32_warn_bytes_api()) |
| 2649 | return NULL; |
| 2650 | if (pathlen+1 > MAX_PATH) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2651 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2652 | return NULL; |
| 2653 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2654 | strcpy(namebuf, path); |
| 2655 | len = pathlen; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2656 | if (len > 0) { |
| 2657 | char ch = namebuf[len-1]; |
| 2658 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2659 | namebuf[len++] = '/'; |
| 2660 | strcpy(namebuf + len, "*.*"); |
| 2661 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2662 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2663 | if ((d = PyList_New(0)) == NULL) |
| 2664 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2665 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2666 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2667 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2668 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2669 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2670 | int error = GetLastError(); |
| 2671 | if (error == ERROR_FILE_NOT_FOUND) |
| 2672 | return d; |
| 2673 | Py_DECREF(d); |
| 2674 | return win32_error("FindFirstFile", namebuf); |
| 2675 | } |
| 2676 | do { |
| 2677 | /* Skip over . and .. */ |
| 2678 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2679 | strcmp(FileData.cFileName, "..") != 0) { |
| 2680 | v = PyBytes_FromString(FileData.cFileName); |
| 2681 | if (v == NULL) { |
| 2682 | Py_DECREF(d); |
| 2683 | d = NULL; |
| 2684 | break; |
| 2685 | } |
| 2686 | if (PyList_Append(d, v) != 0) { |
| 2687 | Py_DECREF(v); |
| 2688 | Py_DECREF(d); |
| 2689 | d = NULL; |
| 2690 | break; |
| 2691 | } |
| 2692 | Py_DECREF(v); |
| 2693 | } |
| 2694 | Py_BEGIN_ALLOW_THREADS |
| 2695 | result = FindNextFile(hFindFile, &FileData); |
| 2696 | Py_END_ALLOW_THREADS |
| 2697 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2698 | it got to the end of the directory. */ |
| 2699 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2700 | Py_DECREF(d); |
| 2701 | win32_error("FindNextFile", namebuf); |
| 2702 | FindClose(hFindFile); |
| 2703 | return NULL; |
| 2704 | } |
| 2705 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2706 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2707 | if (FindClose(hFindFile) == FALSE) { |
| 2708 | Py_DECREF(d); |
| 2709 | return win32_error("FindClose", namebuf); |
| 2710 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2711 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2712 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2713 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2714 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2715 | |
| 2716 | #ifndef MAX_PATH |
| 2717 | #define MAX_PATH CCHMAXPATH |
| 2718 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2719 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2720 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2721 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2722 | PyObject *d, *v; |
| 2723 | char namebuf[MAX_PATH+5]; |
| 2724 | HDIR hdir = 1; |
| 2725 | ULONG srchcnt = 1; |
| 2726 | FILEFINDBUF3 ep; |
| 2727 | APIRET rc; |
| 2728 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2729 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2730 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2731 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2732 | name = PyBytes_AsString(oname); |
| 2733 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2734 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2735 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2736 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2737 | return NULL; |
| 2738 | } |
| 2739 | strcpy(namebuf, name); |
| 2740 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2741 | if (*pt == ALTSEP) |
| 2742 | *pt = SEP; |
| 2743 | if (namebuf[len-1] != SEP) |
| 2744 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2745 | strcpy(namebuf + len, "*.*"); |
| 2746 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2747 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2748 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2749 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2750 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2751 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2752 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2753 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2754 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2755 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2756 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2757 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2758 | |
| 2759 | if (rc != NO_ERROR) { |
| 2760 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2761 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2764 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2765 | do { |
| 2766 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2767 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2768 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2769 | |
| 2770 | strcpy(namebuf, ep.achName); |
| 2771 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2772 | /* Leave Case of Name Alone -- In Native Form */ |
| 2773 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2774 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2775 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2776 | if (v == NULL) { |
| 2777 | Py_DECREF(d); |
| 2778 | d = NULL; |
| 2779 | break; |
| 2780 | } |
| 2781 | if (PyList_Append(d, v) != 0) { |
| 2782 | Py_DECREF(v); |
| 2783 | Py_DECREF(d); |
| 2784 | d = NULL; |
| 2785 | break; |
| 2786 | } |
| 2787 | Py_DECREF(v); |
| 2788 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2789 | } |
| 2790 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2791 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2792 | return d; |
| 2793 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2794 | PyObject *oname; |
| 2795 | char *name; |
| 2796 | PyObject *d, *v; |
| 2797 | DIR *dirp; |
| 2798 | struct dirent *ep; |
| 2799 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2802 | /* v is never read, so it does not need to be initialized yet. */ |
| 2803 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2804 | arg_is_unicode = 0; |
| 2805 | PyErr_Clear(); |
| 2806 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2807 | oname = NULL; |
| 2808 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2809 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2810 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2811 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2812 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2813 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2814 | Py_BEGIN_ALLOW_THREADS |
| 2815 | dirp = opendir(name); |
| 2816 | Py_END_ALLOW_THREADS |
| 2817 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2818 | return posix_error_with_allocated_filename(oname); |
| 2819 | } |
| 2820 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2821 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2822 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2823 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2824 | Py_DECREF(oname); |
| 2825 | return NULL; |
| 2826 | } |
| 2827 | for (;;) { |
| 2828 | errno = 0; |
| 2829 | Py_BEGIN_ALLOW_THREADS |
| 2830 | ep = readdir(dirp); |
| 2831 | Py_END_ALLOW_THREADS |
| 2832 | if (ep == NULL) { |
| 2833 | if (errno == 0) { |
| 2834 | break; |
| 2835 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2836 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2837 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2838 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2839 | Py_DECREF(d); |
| 2840 | return posix_error_with_allocated_filename(oname); |
| 2841 | } |
| 2842 | } |
| 2843 | if (ep->d_name[0] == '.' && |
| 2844 | (NAMLEN(ep) == 1 || |
| 2845 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2846 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2847 | if (arg_is_unicode) |
| 2848 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2849 | else |
| 2850 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2851 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2852 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2855 | if (PyList_Append(d, v) != 0) { |
| 2856 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2857 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2858 | break; |
| 2859 | } |
| 2860 | Py_DECREF(v); |
| 2861 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2862 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2863 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2864 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2865 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2866 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2867 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2868 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2869 | #endif /* which OS */ |
| 2870 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2871 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2872 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2873 | PyDoc_STRVAR(posix_flistdir__doc__, |
| 2874 | "flistdir(fd) -> list_of_strings\n\n\ |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2875 | Like listdir(), but uses a file descriptor instead."); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2876 | |
| 2877 | static PyObject * |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2878 | posix_flistdir(PyObject *self, PyObject *args) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2879 | { |
| 2880 | PyObject *d, *v; |
| 2881 | DIR *dirp; |
| 2882 | struct dirent *ep; |
| 2883 | int fd; |
| 2884 | |
| 2885 | errno = 0; |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 2886 | if (!PyArg_ParseTuple(args, "i:flistdir", &fd)) |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2887 | return NULL; |
Charles-François Natali | 76961fa | 2012-01-10 20:25:09 +0100 | [diff] [blame] | 2888 | /* closedir() closes the FD, so we duplicate it */ |
| 2889 | fd = dup(fd); |
| 2890 | if (fd < 0) |
| 2891 | return posix_error(); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2892 | Py_BEGIN_ALLOW_THREADS |
| 2893 | dirp = fdopendir(fd); |
| 2894 | Py_END_ALLOW_THREADS |
| 2895 | if (dirp == NULL) { |
| 2896 | close(fd); |
| 2897 | return posix_error(); |
| 2898 | } |
| 2899 | if ((d = PyList_New(0)) == NULL) { |
| 2900 | Py_BEGIN_ALLOW_THREADS |
| 2901 | closedir(dirp); |
| 2902 | Py_END_ALLOW_THREADS |
| 2903 | return NULL; |
| 2904 | } |
| 2905 | for (;;) { |
| 2906 | errno = 0; |
| 2907 | Py_BEGIN_ALLOW_THREADS |
| 2908 | ep = readdir(dirp); |
| 2909 | Py_END_ALLOW_THREADS |
| 2910 | if (ep == NULL) { |
| 2911 | if (errno == 0) { |
| 2912 | break; |
| 2913 | } else { |
| 2914 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2915 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2916 | closedir(dirp); |
| 2917 | Py_END_ALLOW_THREADS |
| 2918 | Py_DECREF(d); |
| 2919 | return posix_error(); |
| 2920 | } |
| 2921 | } |
| 2922 | if (ep->d_name[0] == '.' && |
| 2923 | (NAMLEN(ep) == 1 || |
| 2924 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2925 | continue; |
| 2926 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2927 | if (v == NULL) { |
| 2928 | Py_CLEAR(d); |
| 2929 | break; |
| 2930 | } |
| 2931 | if (PyList_Append(d, v) != 0) { |
| 2932 | Py_DECREF(v); |
| 2933 | Py_CLEAR(d); |
| 2934 | break; |
| 2935 | } |
| 2936 | Py_DECREF(v); |
| 2937 | } |
| 2938 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | f2840a8 | 2012-01-08 20:30:47 +0100 | [diff] [blame] | 2939 | rewinddir(dirp); |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2940 | closedir(dirp); |
| 2941 | Py_END_ALLOW_THREADS |
| 2942 | |
| 2943 | return d; |
| 2944 | } |
| 2945 | #endif |
| 2946 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2947 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2948 | /* A helper function for abspath on win32 */ |
| 2949 | static PyObject * |
| 2950 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2951 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2952 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2953 | char outbuf[MAX_PATH*2]; |
| 2954 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2955 | PyObject *po; |
| 2956 | |
| 2957 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 2958 | { |
| 2959 | wchar_t *wpath; |
| 2960 | wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2961 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2962 | DWORD result; |
| 2963 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2964 | |
| 2965 | wpath = PyUnicode_AsUnicode(po); |
| 2966 | if (wpath == NULL) |
| 2967 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2968 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2969 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2970 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2971 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2972 | woutbufp = malloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2973 | if (!woutbufp) |
| 2974 | return PyErr_NoMemory(); |
| 2975 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2976 | } |
| 2977 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 2978 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2979 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2980 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2981 | if (woutbufp != woutbuf) |
| 2982 | free(woutbufp); |
| 2983 | return v; |
| 2984 | } |
| 2985 | /* Drop the argument parsing error as narrow strings |
| 2986 | are also valid. */ |
| 2987 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 2988 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2989 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 2990 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2991 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2992 | if (win32_warn_bytes_api()) |
| 2993 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2994 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2995 | outbuf, &temp)) { |
| 2996 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2997 | return NULL; |
| 2998 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2999 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 3000 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 3001 | Py_FileSystemDefaultEncoding, NULL); |
| 3002 | } |
| 3003 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3004 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3005 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3006 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3007 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3008 | /* A helper function for samepath on windows */ |
| 3009 | static PyObject * |
| 3010 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 3011 | { |
| 3012 | HANDLE hFile; |
| 3013 | int buf_size; |
| 3014 | wchar_t *target_path; |
| 3015 | int result_length; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3016 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3017 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3018 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3019 | if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3020 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3021 | path = PyUnicode_AsUnicode(po); |
| 3022 | if (path == NULL) |
| 3023 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3024 | |
| 3025 | if(!check_GetFinalPathNameByHandle()) { |
| 3026 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 3027 | NotImplementedError. */ |
| 3028 | return PyErr_Format(PyExc_NotImplementedError, |
| 3029 | "GetFinalPathNameByHandle not available on this platform"); |
| 3030 | } |
| 3031 | |
| 3032 | hFile = CreateFileW( |
| 3033 | path, |
| 3034 | 0, /* desired access */ |
| 3035 | 0, /* share mode */ |
| 3036 | NULL, /* security attributes */ |
| 3037 | OPEN_EXISTING, |
| 3038 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3039 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3040 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3041 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3042 | if(hFile == INVALID_HANDLE_VALUE) |
| 3043 | return win32_error_object("CreateFileW", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3044 | |
| 3045 | /* We have a good handle to the target, use it to determine the |
| 3046 | target path name. */ |
| 3047 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 3048 | |
| 3049 | if(!buf_size) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3050 | return win32_error_object("GetFinalPathNameByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3051 | |
| 3052 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 3053 | if(!target_path) |
| 3054 | return PyErr_NoMemory(); |
| 3055 | |
| 3056 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 3057 | buf_size, VOLUME_NAME_DOS); |
| 3058 | if(!result_length) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3059 | return win32_error_object("GetFinalPathNamyByHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3060 | |
| 3061 | if(!CloseHandle(hFile)) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3062 | return win32_error_object("CloseHandle", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3063 | |
| 3064 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3065 | result = PyUnicode_FromWideChar(target_path, result_length); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3066 | free(target_path); |
| 3067 | return result; |
| 3068 | |
| 3069 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3070 | |
| 3071 | static PyObject * |
| 3072 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3073 | { |
| 3074 | HANDLE hFile; |
| 3075 | BY_HANDLE_FILE_INFORMATION info; |
| 3076 | int fd; |
| 3077 | |
| 3078 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3079 | return NULL; |
| 3080 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3081 | if (!_PyVerify_fd(fd)) |
| 3082 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3083 | |
| 3084 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3085 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3086 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3087 | |
| 3088 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3089 | return win32_error("_getfileinformation", NULL); |
| 3090 | |
| 3091 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3092 | info.nFileIndexHigh, |
| 3093 | info.nFileIndexLow); |
| 3094 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3095 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3096 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3097 | "Return true if the pathname refers to an existing directory."); |
| 3098 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3099 | static PyObject * |
| 3100 | posix__isdir(PyObject *self, PyObject *args) |
| 3101 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3102 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3103 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3104 | DWORD attributes; |
| 3105 | |
| 3106 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3107 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3108 | if (wpath == NULL) |
| 3109 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3110 | |
| 3111 | attributes = GetFileAttributesW(wpath); |
| 3112 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3113 | Py_RETURN_FALSE; |
| 3114 | goto check; |
| 3115 | } |
| 3116 | /* Drop the argument parsing error as narrow strings |
| 3117 | are also valid. */ |
| 3118 | PyErr_Clear(); |
| 3119 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3120 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3121 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3122 | if (win32_warn_bytes_api()) |
| 3123 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3124 | attributes = GetFileAttributesA(path); |
| 3125 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3126 | Py_RETURN_FALSE; |
| 3127 | |
| 3128 | check: |
| 3129 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3130 | Py_RETURN_TRUE; |
| 3131 | else |
| 3132 | Py_RETURN_FALSE; |
| 3133 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3134 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3136 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3137 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3138 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3139 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3140 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3141 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3142 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3143 | int res; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3144 | const char *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3145 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3146 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3147 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3148 | PyObject *po; |
| 3149 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) |
| 3150 | { |
| 3151 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 3152 | if (wpath == NULL) |
| 3153 | return NULL; |
| 3154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3155 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3156 | res = CreateDirectoryW(wpath, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3157 | Py_END_ALLOW_THREADS |
| 3158 | if (!res) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3159 | return win32_error_object("mkdir", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3160 | Py_INCREF(Py_None); |
| 3161 | return Py_None; |
| 3162 | } |
| 3163 | /* Drop the argument parsing error as narrow strings |
| 3164 | are also valid. */ |
| 3165 | PyErr_Clear(); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3166 | if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3167 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3168 | if (win32_warn_bytes_api()) |
| 3169 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3170 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3171 | res = CreateDirectoryA(path, NULL); |
| 3172 | Py_END_ALLOW_THREADS |
| 3173 | if (!res) { |
| 3174 | win32_error("mkdir", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3175 | return NULL; |
| 3176 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3177 | Py_INCREF(Py_None); |
| 3178 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3179 | #else |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3180 | PyObject *opath; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3183 | PyUnicode_FSConverter, &opath, &mode)) |
| 3184 | return NULL; |
| 3185 | path = PyBytes_AsString(opath); |
| 3186 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3187 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3188 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3189 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3190 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3191 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3192 | Py_END_ALLOW_THREADS |
| 3193 | if (res < 0) |
| 3194 | return posix_error_with_allocated_filename(opath); |
| 3195 | Py_DECREF(opath); |
| 3196 | Py_INCREF(Py_None); |
| 3197 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3198 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3201 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3202 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3203 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3204 | #include <sys/resource.h> |
| 3205 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3206 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3207 | |
| 3208 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3209 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3210 | "nice(inc) -> new_priority\n\n\ |
| 3211 | 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] | 3212 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3213 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3214 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3215 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3216 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3217 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3218 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3219 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3220 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3221 | /* There are two flavours of 'nice': one that returns the new |
| 3222 | priority (as required by almost all standards out there) and the |
| 3223 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3224 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3225 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3226 | If we are of the nice family that returns the new priority, we |
| 3227 | need to clear errno before the call, and check if errno is filled |
| 3228 | before calling posix_error() on a returnvalue of -1, because the |
| 3229 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3230 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3231 | errno = 0; |
| 3232 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3233 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3234 | if (value == 0) |
| 3235 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3236 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3237 | if (value == -1 && errno != 0) |
| 3238 | /* either nice() or getpriority() returned an error */ |
| 3239 | return posix_error(); |
| 3240 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3241 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3242 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3243 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3244 | |
| 3245 | #ifdef HAVE_GETPRIORITY |
| 3246 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3247 | "getpriority(which, who) -> current_priority\n\n\ |
| 3248 | Get program scheduling priority."); |
| 3249 | |
| 3250 | static PyObject * |
| 3251 | posix_getpriority(PyObject *self, PyObject *args) |
| 3252 | { |
| 3253 | int which, who, retval; |
| 3254 | |
| 3255 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3256 | return NULL; |
| 3257 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3258 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3259 | if (errno != 0) |
| 3260 | return posix_error(); |
| 3261 | return PyLong_FromLong((long)retval); |
| 3262 | } |
| 3263 | #endif /* HAVE_GETPRIORITY */ |
| 3264 | |
| 3265 | |
| 3266 | #ifdef HAVE_SETPRIORITY |
| 3267 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3268 | "setpriority(which, who, prio) -> None\n\n\ |
| 3269 | Set program scheduling priority."); |
| 3270 | |
| 3271 | static PyObject * |
| 3272 | posix_setpriority(PyObject *self, PyObject *args) |
| 3273 | { |
| 3274 | int which, who, prio, retval; |
| 3275 | |
| 3276 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3277 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3278 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3279 | if (retval == -1) |
| 3280 | return posix_error(); |
| 3281 | Py_RETURN_NONE; |
| 3282 | } |
| 3283 | #endif /* HAVE_SETPRIORITY */ |
| 3284 | |
| 3285 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3286 | static PyObject * |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3287 | internal_rename(PyObject *self, PyObject *args, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3288 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3289 | #ifdef MS_WINDOWS |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3290 | PyObject *src, *dst; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3291 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3292 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
| 3293 | if (PyArg_ParseTuple(args, |
| 3294 | is_replace ? "UU:replace" : "UU:rename", |
| 3295 | &src, &dst)) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3296 | { |
| 3297 | wchar_t *wsrc, *wdst; |
| 3298 | |
| 3299 | wsrc = PyUnicode_AsUnicode(src); |
| 3300 | if (wsrc == NULL) |
| 3301 | return NULL; |
| 3302 | wdst = PyUnicode_AsUnicode(dst); |
| 3303 | if (wdst == NULL) |
| 3304 | return NULL; |
| 3305 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3306 | result = MoveFileExW(wsrc, wdst, flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3307 | Py_END_ALLOW_THREADS |
| 3308 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3309 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3310 | Py_INCREF(Py_None); |
| 3311 | return Py_None; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3312 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3313 | else { |
| 3314 | PyErr_Clear(); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3315 | if (!PyArg_ParseTuple(args, |
| 3316 | is_replace ? "O&O&:replace" : "O&O&:rename", |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3317 | PyUnicode_FSConverter, &src, |
| 3318 | PyUnicode_FSConverter, &dst)) |
| 3319 | return NULL; |
| 3320 | |
| 3321 | if (win32_warn_bytes_api()) |
| 3322 | goto error; |
| 3323 | |
| 3324 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3325 | result = MoveFileExA(PyBytes_AS_STRING(src), |
| 3326 | PyBytes_AS_STRING(dst), flags); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3327 | Py_END_ALLOW_THREADS |
| 3328 | |
| 3329 | Py_XDECREF(src); |
| 3330 | Py_XDECREF(dst); |
| 3331 | |
| 3332 | if (!result) |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3333 | return win32_error(is_replace ? "replace" : "rename", NULL); |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3334 | Py_INCREF(Py_None); |
| 3335 | return Py_None; |
| 3336 | |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3337 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3338 | Py_XDECREF(src); |
| 3339 | Py_XDECREF(dst); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3341 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3342 | #else |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3343 | return posix_2str(args, |
| 3344 | is_replace ? "O&O&:replace" : "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3345 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 3348 | PyDoc_STRVAR(posix_rename__doc__, |
| 3349 | "rename(old, new)\n\n\ |
| 3350 | Rename a file or directory."); |
| 3351 | |
| 3352 | static PyObject * |
| 3353 | posix_rename(PyObject *self, PyObject *args) |
| 3354 | { |
| 3355 | return internal_rename(self, args, 0); |
| 3356 | } |
| 3357 | |
| 3358 | PyDoc_STRVAR(posix_replace__doc__, |
| 3359 | "replace(old, new)\n\n\ |
| 3360 | Rename a file or directory, overwriting the destination."); |
| 3361 | |
| 3362 | static PyObject * |
| 3363 | posix_replace(PyObject *self, PyObject *args) |
| 3364 | { |
| 3365 | return internal_rename(self, args, 1); |
| 3366 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3368 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3369 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3370 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3371 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3372 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3373 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3374 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3375 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3376 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3377 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3378 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3379 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3383 | PyDoc_STRVAR(posix_stat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 3384 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3385 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3386 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3387 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 3388 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3389 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3390 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 3391 | 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] | 3392 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 3393 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3394 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3397 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3398 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3399 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3400 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3401 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3402 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3403 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3404 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3405 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3406 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3407 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3408 | wchar_t *command; |
| 3409 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3410 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | Py_BEGIN_ALLOW_THREADS |
| 3413 | sts = _wsystem(command); |
| 3414 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3415 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3416 | PyObject *command_obj; |
| 3417 | char *command; |
| 3418 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3419 | PyUnicode_FSConverter, &command_obj)) |
| 3420 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3421 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3422 | command = PyBytes_AsString(command_obj); |
| 3423 | Py_BEGIN_ALLOW_THREADS |
| 3424 | sts = system(command); |
| 3425 | Py_END_ALLOW_THREADS |
| 3426 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3427 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3428 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3429 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3430 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3431 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3433 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3434 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3435 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3436 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3437 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3438 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3439 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3440 | int i; |
| 3441 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3442 | return NULL; |
| 3443 | i = (int)umask(i); |
| 3444 | if (i < 0) |
| 3445 | return posix_error(); |
| 3446 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3449 | #ifdef MS_WINDOWS |
| 3450 | |
| 3451 | /* override the default DeleteFileW behavior so that directory |
| 3452 | symlinks can be removed with this function, the same as with |
| 3453 | Unix symlinks */ |
| 3454 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3455 | { |
| 3456 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3457 | WIN32_FIND_DATAW find_data; |
| 3458 | HANDLE find_data_handle; |
| 3459 | int is_directory = 0; |
| 3460 | int is_link = 0; |
| 3461 | |
| 3462 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3463 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3464 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3465 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3466 | it is a symlink */ |
| 3467 | if(is_directory && |
| 3468 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3469 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3470 | |
| 3471 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3472 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3473 | FindClose(find_data_handle); |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | if (is_directory && is_link) |
| 3479 | return RemoveDirectoryW(lpFileName); |
| 3480 | |
| 3481 | return DeleteFileW(lpFileName); |
| 3482 | } |
| 3483 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3485 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3486 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3487 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3488 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3489 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3490 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3491 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3492 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3493 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3494 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3495 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3496 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3497 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3498 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3499 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3500 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3501 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3504 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3505 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3506 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3507 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3508 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3509 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3510 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3511 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3513 | struct utsname u; |
| 3514 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3515 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3516 | Py_BEGIN_ALLOW_THREADS |
| 3517 | res = uname(&u); |
| 3518 | Py_END_ALLOW_THREADS |
| 3519 | if (res < 0) |
| 3520 | return posix_error(); |
| 3521 | return Py_BuildValue("(sssss)", |
| 3522 | u.sysname, |
| 3523 | u.nodename, |
| 3524 | u.release, |
| 3525 | u.version, |
| 3526 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3527 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3528 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3529 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3530 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3531 | static int |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3532 | extract_time(PyObject *t, time_t* sec, long* nsec) |
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)) { |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3536 | double d = PyFloat_AsDouble(t); |
| 3537 | double mod; |
| 3538 | *sec = (time_t)d; |
| 3539 | mod = fmod(d, 1.0); |
| 3540 | mod *= 1e9; |
| 3541 | *nsec = (long)mod; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3542 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3543 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3544 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3545 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3546 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3548 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3549 | if (intval == -1 && PyErr_Occurred()) |
| 3550 | return -1; |
| 3551 | *sec = intval; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3552 | *nsec = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3554 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3556 | PyDoc_STRVAR(posix_utime__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3557 | "utime(path[, (atime, mtime)])\n\ |
| 3558 | Set the access and modified time of the file to the given values.\n\ |
| 3559 | If no second argument is used, set the access and modified times to\n\ |
| 3560 | the current time."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3561 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3562 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3563 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3564 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3565 | #ifdef MS_WINDOWS |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3566 | PyObject *arg = Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3567 | PyObject *obwpath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3568 | wchar_t *wpath = NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3569 | const char *apath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3571 | time_t atimesec, mtimesec; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3572 | long ansec, mnsec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3573 | FILETIME atime, mtime; |
| 3574 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3575 | |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 3576 | if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3577 | wpath = PyUnicode_AsUnicode(obwpath); |
| 3578 | if (wpath == NULL) |
| 3579 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3580 | Py_BEGIN_ALLOW_THREADS |
| 3581 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3582 | NULL, OPEN_EXISTING, |
| 3583 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3584 | Py_END_ALLOW_THREADS |
| 3585 | if (hFile == INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3586 | return win32_error_object("utime", obwpath); |
| 3587 | } |
| 3588 | else { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3589 | /* Drop the argument parsing error as narrow strings |
| 3590 | are also valid. */ |
| 3591 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3592 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3593 | if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg)) |
| 3594 | return NULL; |
| 3595 | if (win32_warn_bytes_api()) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3596 | return NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3597 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3598 | Py_BEGIN_ALLOW_THREADS |
| 3599 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3600 | NULL, OPEN_EXISTING, |
| 3601 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3602 | Py_END_ALLOW_THREADS |
| 3603 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3604 | win32_error("utime", apath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3605 | return NULL; |
| 3606 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3609 | if (arg == Py_None) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3610 | SYSTEMTIME now; |
| 3611 | GetSystemTime(&now); |
| 3612 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3613 | !SystemTimeToFileTime(&now, &atime)) { |
| 3614 | win32_error("utime", NULL); |
| 3615 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3616 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3617 | } |
| 3618 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3619 | PyErr_SetString(PyExc_TypeError, |
| 3620 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3621 | goto done; |
| 3622 | } |
| 3623 | else { |
| 3624 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3625 | &atimesec, &ansec) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3626 | goto done; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3627 | time_t_to_FILE_TIME(atimesec, ansec, &atime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3629 | &mtimesec, &mnsec) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | goto done; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3631 | time_t_to_FILE_TIME(mtimesec, mnsec, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3632 | } |
| 3633 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3634 | /* Avoid putting the file name into the error here, |
| 3635 | as that may confuse the user into believing that |
| 3636 | something is wrong with the file, when it also |
| 3637 | could be the time stamp that gives a problem. */ |
| 3638 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3639 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3640 | } |
| 3641 | Py_INCREF(Py_None); |
| 3642 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3643 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3644 | CloseHandle(hFile); |
| 3645 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3646 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3647 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3648 | PyObject *opath; |
| 3649 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3650 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3651 | long ansec, mnsec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3652 | int res; |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3653 | PyObject* arg = Py_None; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3654 | |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 3655 | if (!PyArg_ParseTuple(args, "O&|O:utime", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3656 | PyUnicode_FSConverter, &opath, &arg)) |
| 3657 | return NULL; |
| 3658 | path = PyBytes_AsString(opath); |
Brian Curtin | b0d5b5d | 2011-11-07 10:51:18 -0600 | [diff] [blame] | 3659 | if (arg == Py_None) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3660 | /* optional time values not given */ |
| 3661 | Py_BEGIN_ALLOW_THREADS |
| 3662 | res = utime(path, NULL); |
| 3663 | Py_END_ALLOW_THREADS |
| 3664 | } |
| 3665 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3666 | PyErr_SetString(PyExc_TypeError, |
| 3667 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3668 | Py_DECREF(opath); |
| 3669 | return NULL; |
| 3670 | } |
| 3671 | else { |
| 3672 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3673 | &atime, &ansec) == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3674 | Py_DECREF(opath); |
| 3675 | return NULL; |
| 3676 | } |
| 3677 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3678 | &mtime, &mnsec) == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3679 | Py_DECREF(opath); |
| 3680 | return NULL; |
| 3681 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3682 | |
| 3683 | Py_BEGIN_ALLOW_THREADS |
| 3684 | { |
| 3685 | #ifdef HAVE_UTIMENSAT |
| 3686 | struct timespec buf[2]; |
| 3687 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3688 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3689 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3690 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3691 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3692 | #elif defined(HAVE_UTIMES) |
| 3693 | struct timeval buf[2]; |
| 3694 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3695 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3696 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3697 | buf[1].tv_usec = mnsec / 1000; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3698 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3699 | #elif defined(HAVE_UTIME_H) |
| 3700 | /* XXX should define struct utimbuf instead, above */ |
| 3701 | struct utimbuf buf; |
| 3702 | buf.actime = atime; |
| 3703 | buf.modtime = mtime; |
| 3704 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3705 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3706 | time_t buf[2]; |
| 3707 | buf[0] = atime; |
| 3708 | buf[1] = mtime; |
| 3709 | res = utime(path, buf); |
| 3710 | #endif |
| 3711 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3712 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3713 | } |
| 3714 | if (res < 0) { |
| 3715 | return posix_error_with_allocated_filename(opath); |
| 3716 | } |
| 3717 | Py_DECREF(opath); |
| 3718 | Py_INCREF(Py_None); |
| 3719 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3720 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3721 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3724 | #ifdef HAVE_FUTIMES |
| 3725 | PyDoc_STRVAR(posix_futimes__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3726 | "futimes(fd[, (atime, mtime)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3727 | 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] | 3728 | 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] | 3729 | access and modified times to the current time."); |
| 3730 | |
| 3731 | static PyObject * |
| 3732 | posix_futimes(PyObject *self, PyObject *args) |
| 3733 | { |
| 3734 | int res, fd; |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3735 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3736 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3737 | long ansec, mnsec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3738 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3739 | if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3740 | return NULL; |
| 3741 | |
| 3742 | if (arg == Py_None) { |
| 3743 | /* optional time values not given */ |
| 3744 | Py_BEGIN_ALLOW_THREADS |
| 3745 | res = futimes(fd, NULL); |
| 3746 | Py_END_ALLOW_THREADS |
| 3747 | } |
| 3748 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3749 | PyErr_SetString(PyExc_TypeError, |
| 3750 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3751 | return NULL; |
| 3752 | } |
| 3753 | else { |
| 3754 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3755 | &atime, &ansec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3756 | return NULL; |
| 3757 | } |
| 3758 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3759 | &mtime, &mnsec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3760 | return NULL; |
| 3761 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3762 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3763 | { |
| 3764 | #ifdef HAVE_FUTIMENS |
| 3765 | struct timespec buf[2]; |
| 3766 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3767 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3768 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3769 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3770 | res = futimens(fd, buf); |
| 3771 | #else |
| 3772 | struct timeval buf[2]; |
| 3773 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3774 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3775 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3776 | buf[1].tv_usec = mnsec / 1000; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3777 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3778 | #endif |
| 3779 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3780 | Py_END_ALLOW_THREADS |
| 3781 | } |
| 3782 | if (res < 0) |
| 3783 | return posix_error(); |
| 3784 | Py_RETURN_NONE; |
| 3785 | } |
| 3786 | #endif |
| 3787 | |
| 3788 | #ifdef HAVE_LUTIMES |
| 3789 | PyDoc_STRVAR(posix_lutimes__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3790 | "lutimes(path[, (atime, mtime)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3791 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3792 | |
| 3793 | static PyObject * |
| 3794 | posix_lutimes(PyObject *self, PyObject *args) |
| 3795 | { |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3796 | PyObject *opath; |
| 3797 | PyObject *arg = Py_None; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3798 | const char *path; |
| 3799 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3800 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3801 | long ansec, mnsec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3802 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3803 | if (!PyArg_ParseTuple(args, "O&|O:lutimes", |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3804 | PyUnicode_FSConverter, &opath, &arg)) |
| 3805 | return NULL; |
| 3806 | path = PyBytes_AsString(opath); |
| 3807 | if (arg == Py_None) { |
| 3808 | /* optional time values not given */ |
| 3809 | Py_BEGIN_ALLOW_THREADS |
| 3810 | res = lutimes(path, NULL); |
| 3811 | Py_END_ALLOW_THREADS |
| 3812 | } |
| 3813 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3814 | PyErr_SetString(PyExc_TypeError, |
| 3815 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3816 | Py_DECREF(opath); |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | else { |
| 3820 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3821 | &atime, &ansec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3822 | Py_DECREF(opath); |
| 3823 | return NULL; |
| 3824 | } |
| 3825 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3826 | &mtime, &mnsec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3827 | Py_DECREF(opath); |
| 3828 | return NULL; |
| 3829 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3830 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3831 | { |
| 3832 | #ifdef HAVE_UTIMENSAT |
| 3833 | struct timespec buf[2]; |
| 3834 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3835 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3836 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3837 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3838 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3839 | #else |
| 3840 | struct timeval buf[2]; |
| 3841 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3842 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3843 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 3844 | buf[1].tv_usec = mnsec / 1000; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3845 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3846 | #endif |
| 3847 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3848 | Py_END_ALLOW_THREADS |
| 3849 | } |
| 3850 | Py_DECREF(opath); |
| 3851 | if (res < 0) |
| 3852 | return posix_error(); |
| 3853 | Py_RETURN_NONE; |
| 3854 | } |
| 3855 | #endif |
| 3856 | |
| 3857 | #ifdef HAVE_FUTIMENS |
| 3858 | PyDoc_STRVAR(posix_futimens__doc__, |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3859 | "futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3860 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3861 | nanosecond precision.\n\ |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3862 | 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] | 3863 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3864 | current time.\n\ |
| 3865 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3866 | |
| 3867 | static PyObject * |
| 3868 | posix_futimens(PyObject *self, PyObject *args) |
| 3869 | { |
| 3870 | int res, fd; |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3871 | PyObject *atime = Py_None; |
| 3872 | PyObject *mtime = Py_None; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3873 | struct timespec buf[2]; |
| 3874 | |
Brian Curtin | c1b65d1 | 2011-11-07 14:18:54 -0600 | [diff] [blame] | 3875 | if (!PyArg_ParseTuple(args, "i|OO:futimens", |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3876 | &fd, &atime, &mtime)) |
| 3877 | return NULL; |
| 3878 | if (atime == Py_None && mtime == Py_None) { |
| 3879 | /* optional time values not given */ |
| 3880 | Py_BEGIN_ALLOW_THREADS |
| 3881 | res = futimens(fd, NULL); |
| 3882 | Py_END_ALLOW_THREADS |
| 3883 | } |
| 3884 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3885 | PyErr_SetString(PyExc_TypeError, |
| 3886 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3887 | return NULL; |
| 3888 | } |
| 3889 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3890 | PyErr_SetString(PyExc_TypeError, |
| 3891 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3892 | return NULL; |
| 3893 | } |
| 3894 | else { |
| 3895 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3896 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3897 | return NULL; |
| 3898 | } |
| 3899 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3900 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3901 | return NULL; |
| 3902 | } |
| 3903 | Py_BEGIN_ALLOW_THREADS |
| 3904 | res = futimens(fd, buf); |
| 3905 | Py_END_ALLOW_THREADS |
| 3906 | } |
| 3907 | if (res < 0) |
| 3908 | return posix_error(); |
| 3909 | Py_RETURN_NONE; |
| 3910 | } |
| 3911 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3912 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3913 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3915 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3916 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3917 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3918 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3919 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3920 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3922 | int sts; |
| 3923 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3924 | return NULL; |
| 3925 | _exit(sts); |
| 3926 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3929 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3930 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3931 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3932 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3933 | Py_ssize_t i; |
| 3934 | for (i = 0; i < count; i++) |
| 3935 | PyMem_Free(array[i]); |
| 3936 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3937 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3938 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3939 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3940 | int fsconvert_strdup(PyObject *o, char**out) |
| 3941 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3942 | PyObject *bytes; |
| 3943 | Py_ssize_t size; |
| 3944 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3945 | return 0; |
| 3946 | size = PyBytes_GET_SIZE(bytes); |
| 3947 | *out = PyMem_Malloc(size+1); |
| 3948 | if (!*out) |
| 3949 | return 0; |
| 3950 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3951 | Py_DECREF(bytes); |
| 3952 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3953 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3954 | #endif |
| 3955 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3956 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3957 | static char** |
| 3958 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3959 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3960 | char **envlist; |
| 3961 | Py_ssize_t i, pos, envc; |
| 3962 | PyObject *keys=NULL, *vals=NULL; |
| 3963 | PyObject *key, *val, *key2, *val2; |
| 3964 | char *p, *k, *v; |
| 3965 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3967 | i = PyMapping_Size(env); |
| 3968 | if (i < 0) |
| 3969 | return NULL; |
| 3970 | envlist = PyMem_NEW(char *, i + 1); |
| 3971 | if (envlist == NULL) { |
| 3972 | PyErr_NoMemory(); |
| 3973 | return NULL; |
| 3974 | } |
| 3975 | envc = 0; |
| 3976 | keys = PyMapping_Keys(env); |
| 3977 | vals = PyMapping_Values(env); |
| 3978 | if (!keys || !vals) |
| 3979 | goto error; |
| 3980 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3981 | PyErr_Format(PyExc_TypeError, |
| 3982 | "env.keys() or env.values() is not a list"); |
| 3983 | goto error; |
| 3984 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3985 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3986 | for (pos = 0; pos < i; pos++) { |
| 3987 | key = PyList_GetItem(keys, pos); |
| 3988 | val = PyList_GetItem(vals, pos); |
| 3989 | if (!key || !val) |
| 3990 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3992 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3993 | goto error; |
| 3994 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3995 | Py_DECREF(key2); |
| 3996 | goto error; |
| 3997 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3998 | |
| 3999 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4000 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 4001 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4002 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4003 | k = PyBytes_AsString(key2); |
| 4004 | v = PyBytes_AsString(val2); |
| 4005 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4007 | p = PyMem_NEW(char, len); |
| 4008 | if (p == NULL) { |
| 4009 | PyErr_NoMemory(); |
| 4010 | Py_DECREF(key2); |
| 4011 | Py_DECREF(val2); |
| 4012 | goto error; |
| 4013 | } |
| 4014 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 4015 | envlist[envc++] = p; |
| 4016 | Py_DECREF(key2); |
| 4017 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4018 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4019 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4020 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4021 | } |
| 4022 | Py_DECREF(vals); |
| 4023 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4024 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4025 | envlist[envc] = 0; |
| 4026 | *envc_ptr = envc; |
| 4027 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4028 | |
| 4029 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4030 | Py_XDECREF(keys); |
| 4031 | Py_XDECREF(vals); |
| 4032 | while (--envc >= 0) |
| 4033 | PyMem_DEL(envlist[envc]); |
| 4034 | PyMem_DEL(envlist); |
| 4035 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 4036 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4037 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4038 | static char** |
| 4039 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 4040 | { |
| 4041 | int i; |
| 4042 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 4043 | if (argvlist == NULL) { |
| 4044 | PyErr_NoMemory(); |
| 4045 | return NULL; |
| 4046 | } |
| 4047 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4048 | PyObject* item = PySequence_ITEM(argv, i); |
| 4049 | if (item == NULL) |
| 4050 | goto fail; |
| 4051 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 4052 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4053 | goto fail; |
| 4054 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4055 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4056 | } |
| 4057 | argvlist[*argc] = NULL; |
| 4058 | return argvlist; |
| 4059 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 4060 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4061 | free_string_array(argvlist, *argc); |
| 4062 | return NULL; |
| 4063 | } |
| 4064 | #endif |
| 4065 | |
| 4066 | #ifdef HAVE_EXECV |
| 4067 | PyDoc_STRVAR(posix_execv__doc__, |
| 4068 | "execv(path, args)\n\n\ |
| 4069 | Execute an executable path with arguments, replacing current process.\n\ |
| 4070 | \n\ |
| 4071 | path: path of executable file\n\ |
| 4072 | args: tuple or list of strings"); |
| 4073 | |
| 4074 | static PyObject * |
| 4075 | posix_execv(PyObject *self, PyObject *args) |
| 4076 | { |
| 4077 | PyObject *opath; |
| 4078 | char *path; |
| 4079 | PyObject *argv; |
| 4080 | char **argvlist; |
| 4081 | Py_ssize_t argc; |
| 4082 | |
| 4083 | /* execv has two arguments: (path, argv), where |
| 4084 | argv is a list or tuple of strings. */ |
| 4085 | |
| 4086 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4087 | PyUnicode_FSConverter, |
| 4088 | &opath, &argv)) |
| 4089 | return NULL; |
| 4090 | path = PyBytes_AsString(opath); |
| 4091 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4092 | PyErr_SetString(PyExc_TypeError, |
| 4093 | "execv() arg 2 must be a tuple or list"); |
| 4094 | Py_DECREF(opath); |
| 4095 | return NULL; |
| 4096 | } |
| 4097 | argc = PySequence_Size(argv); |
| 4098 | if (argc < 1) { |
| 4099 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4100 | Py_DECREF(opath); |
| 4101 | return NULL; |
| 4102 | } |
| 4103 | |
| 4104 | argvlist = parse_arglist(argv, &argc); |
| 4105 | if (argvlist == NULL) { |
| 4106 | Py_DECREF(opath); |
| 4107 | return NULL; |
| 4108 | } |
| 4109 | |
| 4110 | execv(path, argvlist); |
| 4111 | |
| 4112 | /* If we get here it's definitely an error */ |
| 4113 | |
| 4114 | free_string_array(argvlist, argc); |
| 4115 | Py_DECREF(opath); |
| 4116 | return posix_error(); |
| 4117 | } |
| 4118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4119 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4120 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4121 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4122 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4123 | path: path of executable file\n\ |
| 4124 | args: tuple or list of arguments\n\ |
| 4125 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4127 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4128 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4129 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4130 | PyObject *opath; |
| 4131 | char *path; |
| 4132 | PyObject *argv, *env; |
| 4133 | char **argvlist; |
| 4134 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4135 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4136 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4137 | /* execve has three arguments: (path, argv, env), where |
| 4138 | argv is a list or tuple of strings and env is a dictionary |
| 4139 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4140 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4141 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4142 | PyUnicode_FSConverter, |
| 4143 | &opath, &argv, &env)) |
| 4144 | return NULL; |
| 4145 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4146 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4147 | PyErr_SetString(PyExc_TypeError, |
| 4148 | "execve() arg 2 must be a tuple or list"); |
| 4149 | goto fail_0; |
| 4150 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4151 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4152 | if (!PyMapping_Check(env)) { |
| 4153 | PyErr_SetString(PyExc_TypeError, |
| 4154 | "execve() arg 3 must be a mapping object"); |
| 4155 | goto fail_0; |
| 4156 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4157 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4158 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4159 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4160 | goto fail_0; |
| 4161 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4162 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4163 | envlist = parse_envlist(env, &envc); |
| 4164 | if (envlist == NULL) |
| 4165 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4166 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4167 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4169 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4171 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4172 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4173 | while (--envc >= 0) |
| 4174 | PyMem_DEL(envlist[envc]); |
| 4175 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4176 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4177 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4178 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4179 | Py_DECREF(opath); |
| 4180 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4181 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4182 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4183 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4184 | #ifdef HAVE_FEXECVE |
| 4185 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4186 | "fexecve(fd, args, env)\n\n\ |
| 4187 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4188 | environment, replacing the current process.\n\ |
| 4189 | \n\ |
| 4190 | fd: file descriptor of executable\n\ |
| 4191 | args: tuple or list of arguments\n\ |
| 4192 | env: dictionary of strings mapping to strings"); |
| 4193 | |
| 4194 | static PyObject * |
| 4195 | posix_fexecve(PyObject *self, PyObject *args) |
| 4196 | { |
| 4197 | int fd; |
| 4198 | PyObject *argv, *env; |
| 4199 | char **argvlist; |
| 4200 | char **envlist; |
| 4201 | Py_ssize_t argc, envc; |
| 4202 | |
| 4203 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4204 | &fd, &argv, &env)) |
| 4205 | return NULL; |
| 4206 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4207 | PyErr_SetString(PyExc_TypeError, |
| 4208 | "fexecve() arg 2 must be a tuple or list"); |
| 4209 | return NULL; |
| 4210 | } |
| 4211 | argc = PySequence_Size(argv); |
| 4212 | if (!PyMapping_Check(env)) { |
| 4213 | PyErr_SetString(PyExc_TypeError, |
| 4214 | "fexecve() arg 3 must be a mapping object"); |
| 4215 | return NULL; |
| 4216 | } |
| 4217 | |
| 4218 | argvlist = parse_arglist(argv, &argc); |
| 4219 | if (argvlist == NULL) |
| 4220 | return NULL; |
| 4221 | |
| 4222 | envlist = parse_envlist(env, &envc); |
| 4223 | if (envlist == NULL) |
| 4224 | goto fail; |
| 4225 | |
| 4226 | fexecve(fd, argvlist, envlist); |
| 4227 | |
| 4228 | /* If we get here it's definitely an error */ |
| 4229 | |
| 4230 | (void) posix_error(); |
| 4231 | |
| 4232 | while (--envc >= 0) |
| 4233 | PyMem_DEL(envlist[envc]); |
| 4234 | PyMem_DEL(envlist); |
| 4235 | fail: |
| 4236 | free_string_array(argvlist, argc); |
| 4237 | return NULL; |
| 4238 | } |
| 4239 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4240 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4241 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4242 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4243 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4244 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4245 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4246 | mode: mode of process creation\n\ |
| 4247 | path: path of executable file\n\ |
| 4248 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4249 | |
| 4250 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4251 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4252 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4253 | PyObject *opath; |
| 4254 | char *path; |
| 4255 | PyObject *argv; |
| 4256 | char **argvlist; |
| 4257 | int mode, i; |
| 4258 | Py_ssize_t argc; |
| 4259 | Py_intptr_t spawnval; |
| 4260 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4261 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4262 | /* spawnv has three arguments: (mode, path, argv), where |
| 4263 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4264 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4265 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4266 | PyUnicode_FSConverter, |
| 4267 | &opath, &argv)) |
| 4268 | return NULL; |
| 4269 | path = PyBytes_AsString(opath); |
| 4270 | if (PyList_Check(argv)) { |
| 4271 | argc = PyList_Size(argv); |
| 4272 | getitem = PyList_GetItem; |
| 4273 | } |
| 4274 | else if (PyTuple_Check(argv)) { |
| 4275 | argc = PyTuple_Size(argv); |
| 4276 | getitem = PyTuple_GetItem; |
| 4277 | } |
| 4278 | else { |
| 4279 | PyErr_SetString(PyExc_TypeError, |
| 4280 | "spawnv() arg 2 must be a tuple or list"); |
| 4281 | Py_DECREF(opath); |
| 4282 | return NULL; |
| 4283 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4284 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4285 | argvlist = PyMem_NEW(char *, argc+1); |
| 4286 | if (argvlist == NULL) { |
| 4287 | Py_DECREF(opath); |
| 4288 | return PyErr_NoMemory(); |
| 4289 | } |
| 4290 | for (i = 0; i < argc; i++) { |
| 4291 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4292 | &argvlist[i])) { |
| 4293 | free_string_array(argvlist, i); |
| 4294 | PyErr_SetString( |
| 4295 | PyExc_TypeError, |
| 4296 | "spawnv() arg 2 must contain only strings"); |
| 4297 | Py_DECREF(opath); |
| 4298 | return NULL; |
| 4299 | } |
| 4300 | } |
| 4301 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4302 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4303 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4304 | Py_BEGIN_ALLOW_THREADS |
| 4305 | spawnval = spawnv(mode, path, argvlist); |
| 4306 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4307 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4308 | if (mode == _OLD_P_OVERLAY) |
| 4309 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4310 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4311 | Py_BEGIN_ALLOW_THREADS |
| 4312 | spawnval = _spawnv(mode, path, argvlist); |
| 4313 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4314 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4316 | free_string_array(argvlist, argc); |
| 4317 | Py_DECREF(opath); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4319 | if (spawnval == -1) |
| 4320 | return posix_error(); |
| 4321 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4322 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4323 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4324 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4325 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4326 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4330 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4331 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4332 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4333 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4334 | mode: mode of process creation\n\ |
| 4335 | path: path of executable file\n\ |
| 4336 | args: tuple or list of arguments\n\ |
| 4337 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4338 | |
| 4339 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4340 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4341 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4342 | PyObject *opath; |
| 4343 | char *path; |
| 4344 | PyObject *argv, *env; |
| 4345 | char **argvlist; |
| 4346 | char **envlist; |
| 4347 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4348 | int mode; |
| 4349 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4350 | Py_intptr_t spawnval; |
| 4351 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4352 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4353 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4354 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4355 | argv is a list or tuple of strings and env is a dictionary |
| 4356 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4358 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4359 | PyUnicode_FSConverter, |
| 4360 | &opath, &argv, &env)) |
| 4361 | return NULL; |
| 4362 | path = PyBytes_AsString(opath); |
| 4363 | if (PyList_Check(argv)) { |
| 4364 | argc = PyList_Size(argv); |
| 4365 | getitem = PyList_GetItem; |
| 4366 | } |
| 4367 | else if (PyTuple_Check(argv)) { |
| 4368 | argc = PyTuple_Size(argv); |
| 4369 | getitem = PyTuple_GetItem; |
| 4370 | } |
| 4371 | else { |
| 4372 | PyErr_SetString(PyExc_TypeError, |
| 4373 | "spawnve() arg 2 must be a tuple or list"); |
| 4374 | goto fail_0; |
| 4375 | } |
| 4376 | if (!PyMapping_Check(env)) { |
| 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "spawnve() arg 3 must be a mapping object"); |
| 4379 | goto fail_0; |
| 4380 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4382 | argvlist = PyMem_NEW(char *, argc+1); |
| 4383 | if (argvlist == NULL) { |
| 4384 | PyErr_NoMemory(); |
| 4385 | goto fail_0; |
| 4386 | } |
| 4387 | for (i = 0; i < argc; i++) { |
| 4388 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4389 | &argvlist[i])) |
| 4390 | { |
| 4391 | lastarg = i; |
| 4392 | goto fail_1; |
| 4393 | } |
| 4394 | } |
| 4395 | lastarg = argc; |
| 4396 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4397 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4398 | envlist = parse_envlist(env, &envc); |
| 4399 | if (envlist == NULL) |
| 4400 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4401 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4402 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4403 | Py_BEGIN_ALLOW_THREADS |
| 4404 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4405 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4406 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4407 | if (mode == _OLD_P_OVERLAY) |
| 4408 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4410 | Py_BEGIN_ALLOW_THREADS |
| 4411 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4412 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4413 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4415 | if (spawnval == -1) |
| 4416 | (void) posix_error(); |
| 4417 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4418 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4419 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4420 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4421 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4422 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4423 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4424 | while (--envc >= 0) |
| 4425 | PyMem_DEL(envlist[envc]); |
| 4426 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4427 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4428 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4429 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4430 | Py_DECREF(opath); |
| 4431 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4432 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4433 | |
| 4434 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4435 | #if defined(PYOS_OS2) |
| 4436 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4437 | "spawnvp(mode, file, args)\n\n\ |
| 4438 | Execute the program 'file' in a new process, using the environment\n\ |
| 4439 | search path to find the file.\n\ |
| 4440 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4441 | mode: mode of process creation\n\ |
| 4442 | file: executable file name\n\ |
| 4443 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4444 | |
| 4445 | static PyObject * |
| 4446 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4447 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4448 | PyObject *opath; |
| 4449 | char *path; |
| 4450 | PyObject *argv; |
| 4451 | char **argvlist; |
| 4452 | int mode, i, argc; |
| 4453 | Py_intptr_t spawnval; |
| 4454 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4455 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4456 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4457 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4458 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4459 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4460 | PyUnicode_FSConverter, |
| 4461 | &opath, &argv)) |
| 4462 | return NULL; |
| 4463 | path = PyBytes_AsString(opath); |
| 4464 | if (PyList_Check(argv)) { |
| 4465 | argc = PyList_Size(argv); |
| 4466 | getitem = PyList_GetItem; |
| 4467 | } |
| 4468 | else if (PyTuple_Check(argv)) { |
| 4469 | argc = PyTuple_Size(argv); |
| 4470 | getitem = PyTuple_GetItem; |
| 4471 | } |
| 4472 | else { |
| 4473 | PyErr_SetString(PyExc_TypeError, |
| 4474 | "spawnvp() arg 2 must be a tuple or list"); |
| 4475 | Py_DECREF(opath); |
| 4476 | return NULL; |
| 4477 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4479 | argvlist = PyMem_NEW(char *, argc+1); |
| 4480 | if (argvlist == NULL) { |
| 4481 | Py_DECREF(opath); |
| 4482 | return PyErr_NoMemory(); |
| 4483 | } |
| 4484 | for (i = 0; i < argc; i++) { |
| 4485 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4486 | &argvlist[i])) { |
| 4487 | free_string_array(argvlist, i); |
| 4488 | PyErr_SetString( |
| 4489 | PyExc_TypeError, |
| 4490 | "spawnvp() arg 2 must contain only strings"); |
| 4491 | Py_DECREF(opath); |
| 4492 | return NULL; |
| 4493 | } |
| 4494 | } |
| 4495 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4496 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4497 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4498 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4499 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4500 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4501 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4502 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4503 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4504 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4505 | free_string_array(argvlist, argc); |
| 4506 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4508 | if (spawnval == -1) |
| 4509 | return posix_error(); |
| 4510 | else |
| 4511 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | |
| 4515 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4516 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4517 | Execute the program 'file' in a new process, using the environment\n\ |
| 4518 | search path to find the file.\n\ |
| 4519 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4520 | mode: mode of process creation\n\ |
| 4521 | file: executable file name\n\ |
| 4522 | args: tuple or list of arguments\n\ |
| 4523 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4524 | |
| 4525 | static PyObject * |
| 4526 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4527 | { |
Ross Lagerwall | dcfde5a | 2011-11-04 07:09:14 +0200 | [diff] [blame] | 4528 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4529 | char *path; |
| 4530 | PyObject *argv, *env; |
| 4531 | char **argvlist; |
| 4532 | char **envlist; |
| 4533 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4534 | int mode; |
| 4535 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4536 | Py_intptr_t spawnval; |
| 4537 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4538 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4539 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4540 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4541 | argv is a list or tuple of strings and env is a dictionary |
| 4542 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4543 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4544 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4545 | PyUnicode_FSConverter, |
| 4546 | &opath, &argv, &env)) |
| 4547 | return NULL; |
| 4548 | path = PyBytes_AsString(opath); |
| 4549 | if (PyList_Check(argv)) { |
| 4550 | argc = PyList_Size(argv); |
| 4551 | getitem = PyList_GetItem; |
| 4552 | } |
| 4553 | else if (PyTuple_Check(argv)) { |
| 4554 | argc = PyTuple_Size(argv); |
| 4555 | getitem = PyTuple_GetItem; |
| 4556 | } |
| 4557 | else { |
| 4558 | PyErr_SetString(PyExc_TypeError, |
| 4559 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4560 | goto fail_0; |
| 4561 | } |
| 4562 | if (!PyMapping_Check(env)) { |
| 4563 | PyErr_SetString(PyExc_TypeError, |
| 4564 | "spawnvpe() arg 3 must be a mapping object"); |
| 4565 | goto fail_0; |
| 4566 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4567 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4568 | argvlist = PyMem_NEW(char *, argc+1); |
| 4569 | if (argvlist == NULL) { |
| 4570 | PyErr_NoMemory(); |
| 4571 | goto fail_0; |
| 4572 | } |
| 4573 | for (i = 0; i < argc; i++) { |
| 4574 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4575 | &argvlist[i])) |
| 4576 | { |
| 4577 | lastarg = i; |
| 4578 | goto fail_1; |
| 4579 | } |
| 4580 | } |
| 4581 | lastarg = argc; |
| 4582 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4583 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4584 | envlist = parse_envlist(env, &envc); |
| 4585 | if (envlist == NULL) |
| 4586 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4588 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4589 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4590 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4591 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4592 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4593 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4594 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4596 | if (spawnval == -1) |
| 4597 | (void) posix_error(); |
| 4598 | else |
| 4599 | res = Py_BuildValue("l", (long) spawnval); |
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 | while (--envc >= 0) |
| 4602 | PyMem_DEL(envlist[envc]); |
| 4603 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4604 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4605 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4606 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4607 | Py_DECREF(opath); |
| 4608 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4609 | } |
| 4610 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4611 | #endif /* HAVE_SPAWNV */ |
| 4612 | |
| 4613 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4614 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4615 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4616 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4617 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4618 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4619 | 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] | 4620 | |
| 4621 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4622 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4623 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4624 | pid_t pid; |
| 4625 | int result = 0; |
| 4626 | _PyImport_AcquireLock(); |
| 4627 | pid = fork1(); |
| 4628 | if (pid == 0) { |
| 4629 | /* child: this clobbers and resets the import lock. */ |
| 4630 | PyOS_AfterFork(); |
| 4631 | } else { |
| 4632 | /* parent: release the import lock. */ |
| 4633 | result = _PyImport_ReleaseLock(); |
| 4634 | } |
| 4635 | if (pid == -1) |
| 4636 | return posix_error(); |
| 4637 | if (result < 0) { |
| 4638 | /* Don't clobber the OSError if the fork failed. */ |
| 4639 | PyErr_SetString(PyExc_RuntimeError, |
| 4640 | "not holding the import lock"); |
| 4641 | return NULL; |
| 4642 | } |
| 4643 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4644 | } |
| 4645 | #endif |
| 4646 | |
| 4647 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4648 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4649 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4650 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4651 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4652 | 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] | 4653 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4654 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4655 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4656 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4657 | pid_t pid; |
| 4658 | int result = 0; |
| 4659 | _PyImport_AcquireLock(); |
| 4660 | pid = fork(); |
| 4661 | if (pid == 0) { |
| 4662 | /* child: this clobbers and resets the import lock. */ |
| 4663 | PyOS_AfterFork(); |
| 4664 | } else { |
| 4665 | /* parent: release the import lock. */ |
| 4666 | result = _PyImport_ReleaseLock(); |
| 4667 | } |
| 4668 | if (pid == -1) |
| 4669 | return posix_error(); |
| 4670 | if (result < 0) { |
| 4671 | /* Don't clobber the OSError if the fork failed. */ |
| 4672 | PyErr_SetString(PyExc_RuntimeError, |
| 4673 | "not holding the import lock"); |
| 4674 | return NULL; |
| 4675 | } |
| 4676 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4677 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4678 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4679 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4680 | #ifdef HAVE_SCHED_H |
| 4681 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4682 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4683 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4684 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4685 | "sched_get_priority_max(policy)\n\n\ |
| 4686 | Get the maximum scheduling priority for *policy*."); |
| 4687 | |
| 4688 | static PyObject * |
| 4689 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4690 | { |
| 4691 | int policy, max; |
| 4692 | |
| 4693 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4694 | return NULL; |
| 4695 | max = sched_get_priority_max(policy); |
| 4696 | if (max < 0) |
| 4697 | return posix_error(); |
| 4698 | return PyLong_FromLong(max); |
| 4699 | } |
| 4700 | |
| 4701 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4702 | "sched_get_priority_min(policy)\n\n\ |
| 4703 | Get the minimum scheduling priority for *policy*."); |
| 4704 | |
| 4705 | static PyObject * |
| 4706 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4707 | { |
| 4708 | int policy, min; |
| 4709 | |
| 4710 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4711 | return NULL; |
| 4712 | min = sched_get_priority_min(policy); |
| 4713 | if (min < 0) |
| 4714 | return posix_error(); |
| 4715 | return PyLong_FromLong(min); |
| 4716 | } |
| 4717 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4718 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4719 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4720 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4721 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4722 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4723 | "sched_getscheduler(pid)\n\n\ |
| 4724 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4725 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4726 | |
| 4727 | static PyObject * |
| 4728 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4729 | { |
| 4730 | pid_t pid; |
| 4731 | int policy; |
| 4732 | |
| 4733 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4734 | return NULL; |
| 4735 | policy = sched_getscheduler(pid); |
| 4736 | if (policy < 0) |
| 4737 | return posix_error(); |
| 4738 | return PyLong_FromLong(policy); |
| 4739 | } |
| 4740 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4741 | #endif |
| 4742 | |
| 4743 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4744 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4745 | static PyObject * |
| 4746 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4747 | { |
| 4748 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4749 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4750 | |
| 4751 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4752 | return NULL; |
| 4753 | res = PyStructSequence_New(type); |
| 4754 | if (!res) |
| 4755 | return NULL; |
| 4756 | Py_INCREF(priority); |
| 4757 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4758 | return res; |
| 4759 | } |
| 4760 | |
| 4761 | PyDoc_STRVAR(sched_param__doc__, |
| 4762 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4763 | Current has only one field: sched_priority"); |
| 4764 | |
| 4765 | static PyStructSequence_Field sched_param_fields[] = { |
| 4766 | {"sched_priority", "the scheduling priority"}, |
| 4767 | {0} |
| 4768 | }; |
| 4769 | |
| 4770 | static PyStructSequence_Desc sched_param_desc = { |
| 4771 | "sched_param", /* name */ |
| 4772 | sched_param__doc__, /* doc */ |
| 4773 | sched_param_fields, |
| 4774 | 1 |
| 4775 | }; |
| 4776 | |
| 4777 | static int |
| 4778 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4779 | { |
| 4780 | long priority; |
| 4781 | |
| 4782 | if (Py_TYPE(param) != &SchedParamType) { |
| 4783 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4784 | return 0; |
| 4785 | } |
| 4786 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4787 | if (priority == -1 && PyErr_Occurred()) |
| 4788 | return 0; |
| 4789 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4790 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4791 | return 0; |
| 4792 | } |
| 4793 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4794 | return 1; |
| 4795 | } |
| 4796 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4797 | #endif |
| 4798 | |
| 4799 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4800 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4801 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4802 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4803 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4804 | If *pid* is 0, the calling process is changed.\n\ |
| 4805 | *param* is an instance of sched_param."); |
| 4806 | |
| 4807 | static PyObject * |
| 4808 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4809 | { |
| 4810 | pid_t pid; |
| 4811 | int policy; |
| 4812 | struct sched_param param; |
| 4813 | |
| 4814 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4815 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4816 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4817 | |
| 4818 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4819 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4820 | ** scheduling policy under Solaris/Illumos, and others. |
| 4821 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4822 | */ |
| 4823 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4824 | return posix_error(); |
| 4825 | Py_RETURN_NONE; |
| 4826 | } |
| 4827 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4828 | #endif |
| 4829 | |
| 4830 | #ifdef HAVE_SCHED_SETPARAM |
| 4831 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4832 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4833 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4834 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4835 | sched_param class. A PID of 0 means the calling process."); |
| 4836 | |
| 4837 | static PyObject * |
| 4838 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4839 | { |
| 4840 | pid_t pid; |
| 4841 | struct sched_param param; |
| 4842 | PyObject *res, *priority; |
| 4843 | |
| 4844 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4845 | return NULL; |
| 4846 | if (sched_getparam(pid, ¶m)) |
| 4847 | return posix_error(); |
| 4848 | res = PyStructSequence_New(&SchedParamType); |
| 4849 | if (!res) |
| 4850 | return NULL; |
| 4851 | priority = PyLong_FromLong(param.sched_priority); |
| 4852 | if (!priority) { |
| 4853 | Py_DECREF(res); |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4857 | return res; |
| 4858 | } |
| 4859 | |
| 4860 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4861 | "sched_setparam(pid, param)\n\n\ |
| 4862 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4863 | A PID of 0 means the calling process."); |
| 4864 | |
| 4865 | static PyObject * |
| 4866 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4867 | { |
| 4868 | pid_t pid; |
| 4869 | struct sched_param param; |
| 4870 | |
| 4871 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4872 | &pid, &convert_sched_param, ¶m)) |
| 4873 | return NULL; |
| 4874 | if (sched_setparam(pid, ¶m)) |
| 4875 | return posix_error(); |
| 4876 | Py_RETURN_NONE; |
| 4877 | } |
| 4878 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4879 | #endif |
| 4880 | |
| 4881 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4882 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4883 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4884 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4885 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4886 | |
| 4887 | static PyObject * |
| 4888 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4889 | { |
| 4890 | pid_t pid; |
| 4891 | struct timespec interval; |
| 4892 | |
| 4893 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4894 | return NULL; |
| 4895 | if (sched_rr_get_interval(pid, &interval)) |
| 4896 | return posix_error(); |
| 4897 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4898 | } |
| 4899 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4900 | #endif |
| 4901 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4902 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4903 | "sched_yield()\n\n\ |
| 4904 | Voluntarily relinquish the CPU."); |
| 4905 | |
| 4906 | static PyObject * |
| 4907 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4908 | { |
| 4909 | if (sched_yield()) |
| 4910 | return posix_error(); |
| 4911 | Py_RETURN_NONE; |
| 4912 | } |
| 4913 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4914 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4915 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4916 | typedef struct { |
| 4917 | PyObject_HEAD; |
| 4918 | Py_ssize_t size; |
| 4919 | int ncpus; |
| 4920 | cpu_set_t *set; |
| 4921 | } Py_cpu_set; |
| 4922 | |
| 4923 | static PyTypeObject cpu_set_type; |
| 4924 | |
| 4925 | static void |
| 4926 | cpu_set_dealloc(Py_cpu_set *set) |
| 4927 | { |
| 4928 | assert(set->set); |
| 4929 | CPU_FREE(set->set); |
| 4930 | Py_TYPE(set)->tp_free(set); |
| 4931 | } |
| 4932 | |
| 4933 | static Py_cpu_set * |
| 4934 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4935 | { |
| 4936 | Py_cpu_set *set; |
| 4937 | |
| 4938 | if (size < 0) { |
| 4939 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4940 | return NULL; |
| 4941 | } |
| 4942 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4943 | if (!set) |
| 4944 | return NULL; |
| 4945 | set->ncpus = size; |
| 4946 | set->size = CPU_ALLOC_SIZE(size); |
| 4947 | set->set = CPU_ALLOC(size); |
| 4948 | if (!set->set) { |
| 4949 | type->tp_free(set); |
| 4950 | PyErr_NoMemory(); |
| 4951 | return NULL; |
| 4952 | } |
| 4953 | CPU_ZERO_S(set->size, set->set); |
| 4954 | return set; |
| 4955 | } |
| 4956 | |
| 4957 | static PyObject * |
| 4958 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4959 | { |
| 4960 | int size; |
| 4961 | |
| 4962 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4963 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4964 | return NULL; |
| 4965 | return (PyObject *)make_new_cpu_set(type, size); |
| 4966 | } |
| 4967 | |
| 4968 | static PyObject * |
| 4969 | cpu_set_repr(Py_cpu_set *set) |
| 4970 | { |
| 4971 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4972 | } |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4973 | |
| 4974 | static Py_ssize_t |
| 4975 | cpu_set_len(Py_cpu_set *set) |
| 4976 | { |
| 4977 | return set->ncpus; |
| 4978 | } |
| 4979 | |
| 4980 | static int |
| 4981 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4982 | { |
| 4983 | int cpu; |
| 4984 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4985 | return -1; |
| 4986 | if (cpu < 0) { |
| 4987 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4988 | return -1; |
| 4989 | } |
| 4990 | if (cpu >= set->ncpus) { |
| 4991 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4992 | return -1; |
| 4993 | } |
| 4994 | return cpu; |
| 4995 | } |
| 4996 | |
| 4997 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4998 | "cpu_set.set(i)\n\n\ |
| 4999 | Add CPU *i* to the set."); |
| 5000 | |
| 5001 | static PyObject * |
| 5002 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 5003 | { |
| 5004 | int cpu = _get_cpu(set, "i|set", args); |
| 5005 | if (cpu == -1) |
| 5006 | return NULL; |
| 5007 | CPU_SET_S(cpu, set->size, set->set); |
| 5008 | Py_RETURN_NONE; |
| 5009 | } |
| 5010 | |
| 5011 | PyDoc_STRVAR(cpu_set_count_doc, |
| 5012 | "cpu_set.count() -> int\n\n\ |
| 5013 | Return the number of CPUs active in the set."); |
| 5014 | |
| 5015 | static PyObject * |
| 5016 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 5017 | { |
| 5018 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 5019 | } |
| 5020 | |
| 5021 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 5022 | "cpu_set.clear(i)\n\n\ |
| 5023 | Remove CPU *i* from the set."); |
| 5024 | |
| 5025 | static PyObject * |
| 5026 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 5027 | { |
| 5028 | int cpu = _get_cpu(set, "i|clear", args); |
| 5029 | if (cpu == -1) |
| 5030 | return NULL; |
| 5031 | CPU_CLR_S(cpu, set->size, set->set); |
| 5032 | Py_RETURN_NONE; |
| 5033 | } |
| 5034 | |
| 5035 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 5036 | "cpu_set.isset(i) -> bool\n\n\ |
| 5037 | Test if CPU *i* is in the set."); |
| 5038 | |
| 5039 | static PyObject * |
| 5040 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 5041 | { |
| 5042 | int cpu = _get_cpu(set, "i|isset", args); |
| 5043 | if (cpu == -1) |
| 5044 | return NULL; |
| 5045 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 5046 | Py_RETURN_TRUE; |
| 5047 | Py_RETURN_FALSE; |
| 5048 | } |
| 5049 | |
| 5050 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 5051 | "cpu_set.zero()\n\n\ |
| 5052 | Clear the cpu_set."); |
| 5053 | |
| 5054 | static PyObject * |
| 5055 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 5056 | { |
| 5057 | CPU_ZERO_S(set->size, set->set); |
| 5058 | Py_RETURN_NONE; |
| 5059 | } |
| 5060 | |
| 5061 | static PyObject * |
| 5062 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 5063 | { |
| 5064 | int eq; |
| 5065 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5066 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5067 | Py_RETURN_NOTIMPLEMENTED; |
| 5068 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5069 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5070 | if ((op == Py_EQ) ? eq : !eq) |
| 5071 | Py_RETURN_TRUE; |
| 5072 | else |
| 5073 | Py_RETURN_FALSE; |
| 5074 | } |
| 5075 | |
| 5076 | #define CPU_SET_BINOP(name, op) \ |
| 5077 | static PyObject * \ |
| 5078 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5079 | if (res) { \ |
| 5080 | Py_INCREF(res); \ |
| 5081 | } \ |
| 5082 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5083 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5084 | if (!res) \ |
| 5085 | return NULL; \ |
| 5086 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5087 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5088 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5089 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5090 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5091 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5092 | op(res->size, res->set, left->set, right->set); \ |
| 5093 | return (PyObject *)res; \ |
| 5094 | } \ |
| 5095 | static PyObject * \ |
| 5096 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5097 | return do_cpu_set_##name(left, right, NULL); \ |
| 5098 | } \ |
| 5099 | static PyObject * \ |
| 5100 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5101 | return do_cpu_set_##name(left, right, left); \ |
| 5102 | } \ |
| 5103 | |
| 5104 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5105 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5106 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5107 | #undef CPU_SET_BINOP |
| 5108 | |
| 5109 | PyDoc_STRVAR(cpu_set_doc, |
| 5110 | "cpu_set(size)\n\n\ |
| 5111 | Create an empty mask of CPUs."); |
| 5112 | |
| 5113 | static PyNumberMethods cpu_set_as_number = { |
| 5114 | 0, /*nb_add*/ |
| 5115 | 0, /*nb_subtract*/ |
| 5116 | 0, /*nb_multiply*/ |
| 5117 | 0, /*nb_remainder*/ |
| 5118 | 0, /*nb_divmod*/ |
| 5119 | 0, /*nb_power*/ |
| 5120 | 0, /*nb_negative*/ |
| 5121 | 0, /*nb_positive*/ |
| 5122 | 0, /*nb_absolute*/ |
| 5123 | 0, /*nb_bool*/ |
| 5124 | 0, /*nb_invert*/ |
| 5125 | 0, /*nb_lshift*/ |
| 5126 | 0, /*nb_rshift*/ |
| 5127 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5128 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5129 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5130 | 0, /*nb_int*/ |
| 5131 | 0, /*nb_reserved*/ |
| 5132 | 0, /*nb_float*/ |
| 5133 | 0, /*nb_inplace_add*/ |
| 5134 | 0, /*nb_inplace_subtract*/ |
| 5135 | 0, /*nb_inplace_multiply*/ |
| 5136 | 0, /*nb_inplace_remainder*/ |
| 5137 | 0, /*nb_inplace_power*/ |
| 5138 | 0, /*nb_inplace_lshift*/ |
| 5139 | 0, /*nb_inplace_rshift*/ |
| 5140 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5141 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5142 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5143 | }; |
| 5144 | |
| 5145 | static PySequenceMethods cpu_set_as_sequence = { |
| 5146 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5147 | }; |
| 5148 | |
| 5149 | static PyMethodDef cpu_set_methods[] = { |
| 5150 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5151 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5152 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5153 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5154 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5155 | {NULL, NULL} /* sentinel */ |
| 5156 | }; |
| 5157 | |
| 5158 | static PyTypeObject cpu_set_type = { |
| 5159 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5160 | "posix.cpu_set", /* tp_name */ |
| 5161 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5162 | 0, /* tp_itemsize */ |
| 5163 | /* methods */ |
| 5164 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5165 | 0, /* tp_print */ |
| 5166 | 0, /* tp_getattr */ |
| 5167 | 0, /* tp_setattr */ |
| 5168 | 0, /* tp_reserved */ |
| 5169 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5170 | &cpu_set_as_number, /* tp_as_number */ |
| 5171 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5172 | 0, /* tp_as_mapping */ |
| 5173 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5174 | 0, /* tp_call */ |
| 5175 | 0, /* tp_str */ |
| 5176 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5177 | 0, /* tp_setattro */ |
| 5178 | 0, /* tp_as_buffer */ |
| 5179 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5180 | cpu_set_doc, /* tp_doc */ |
| 5181 | 0, /* tp_traverse */ |
| 5182 | 0, /* tp_clear */ |
| 5183 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5184 | 0, /* tp_weaklistoffset */ |
| 5185 | 0, /* tp_iter */ |
| 5186 | 0, /* tp_iternext */ |
| 5187 | cpu_set_methods, /* tp_methods */ |
| 5188 | 0, /* tp_members */ |
| 5189 | 0, /* tp_getset */ |
| 5190 | 0, /* tp_base */ |
| 5191 | 0, /* tp_dict */ |
| 5192 | 0, /* tp_descr_get */ |
| 5193 | 0, /* tp_descr_set */ |
| 5194 | 0, /* tp_dictoffset */ |
| 5195 | 0, /* tp_init */ |
| 5196 | PyType_GenericAlloc, /* tp_alloc */ |
| 5197 | cpu_set_new, /* tp_new */ |
| 5198 | PyObject_Del, /* tp_free */ |
| 5199 | }; |
| 5200 | |
| 5201 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5202 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5203 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5204 | |
| 5205 | static PyObject * |
| 5206 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5207 | { |
| 5208 | pid_t pid; |
| 5209 | Py_cpu_set *cpu_set; |
| 5210 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5211 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5212 | &pid, &cpu_set_type, &cpu_set)) |
| 5213 | return NULL; |
| 5214 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5215 | return posix_error(); |
| 5216 | Py_RETURN_NONE; |
| 5217 | } |
| 5218 | |
| 5219 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5220 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5221 | Return the affinity of the process with PID *pid*.\n\ |
| 5222 | The returned cpu_set will be of size *ncpus*."); |
| 5223 | |
| 5224 | static PyObject * |
| 5225 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5226 | { |
| 5227 | pid_t pid; |
| 5228 | int ncpus; |
| 5229 | Py_cpu_set *res; |
| 5230 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5231 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5232 | &pid, &ncpus)) |
| 5233 | return NULL; |
| 5234 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5235 | if (!res) |
| 5236 | return NULL; |
| 5237 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5238 | Py_DECREF(res); |
| 5239 | return posix_error(); |
| 5240 | } |
| 5241 | return (PyObject *)res; |
| 5242 | } |
| 5243 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5244 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5245 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5246 | #endif /* HAVE_SCHED_H */ |
| 5247 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5248 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5249 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5250 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5251 | #define DEV_PTY_FILE "/dev/ptc" |
| 5252 | #define HAVE_DEV_PTMX |
| 5253 | #else |
| 5254 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5255 | #endif |
| 5256 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5257 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5258 | #ifdef HAVE_PTY_H |
| 5259 | #include <pty.h> |
| 5260 | #else |
| 5261 | #ifdef HAVE_LIBUTIL_H |
| 5262 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5263 | #else |
| 5264 | #ifdef HAVE_UTIL_H |
| 5265 | #include <util.h> |
| 5266 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5267 | #endif /* HAVE_LIBUTIL_H */ |
| 5268 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5269 | #ifdef HAVE_STROPTS_H |
| 5270 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5271 | #endif |
| 5272 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5273 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5274 | #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] | 5275 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5276 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5277 | 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] | 5278 | |
| 5279 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5280 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5281 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5282 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5283 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5284 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5285 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5286 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5287 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5288 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5289 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5290 | #endif |
| 5291 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5292 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5293 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5294 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5295 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5296 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5297 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5298 | if (slave_name == NULL) |
| 5299 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5301 | slave_fd = open(slave_name, O_RDWR); |
| 5302 | if (slave_fd < 0) |
| 5303 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5304 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5305 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5306 | if (master_fd < 0) |
| 5307 | return posix_error(); |
| 5308 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5309 | /* change permission of slave */ |
| 5310 | if (grantpt(master_fd) < 0) { |
| 5311 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5312 | return posix_error(); |
| 5313 | } |
| 5314 | /* unlock slave */ |
| 5315 | if (unlockpt(master_fd) < 0) { |
| 5316 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5317 | return posix_error(); |
| 5318 | } |
| 5319 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5320 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5321 | if (slave_name == NULL) |
| 5322 | return posix_error(); |
| 5323 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5324 | if (slave_fd < 0) |
| 5325 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5326 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5327 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5328 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5329 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5330 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5331 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5332 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5333 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5335 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5336 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5337 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5338 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5339 | |
| 5340 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5341 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5342 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5343 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5344 | 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] | 5345 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5346 | |
| 5347 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5348 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5350 | int master_fd = -1, result = 0; |
| 5351 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5352 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5353 | _PyImport_AcquireLock(); |
| 5354 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5355 | if (pid == 0) { |
| 5356 | /* child: this clobbers and resets the import lock. */ |
| 5357 | PyOS_AfterFork(); |
| 5358 | } else { |
| 5359 | /* parent: release the import lock. */ |
| 5360 | result = _PyImport_ReleaseLock(); |
| 5361 | } |
| 5362 | if (pid == -1) |
| 5363 | return posix_error(); |
| 5364 | if (result < 0) { |
| 5365 | /* Don't clobber the OSError if the fork failed. */ |
| 5366 | PyErr_SetString(PyExc_RuntimeError, |
| 5367 | "not holding the import lock"); |
| 5368 | return NULL; |
| 5369 | } |
| 5370 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5371 | } |
| 5372 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5373 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5374 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5375 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5376 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5377 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5378 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5379 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5380 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5381 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5382 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5383 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5384 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5385 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5386 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5387 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5388 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5390 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5391 | Return the current process's effective user 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_geteuid(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)geteuid()); |
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_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5402 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5403 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5404 | Return the current process's group 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_getgid(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)getgid()); |
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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5414 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5415 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5416 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5417 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5418 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5419 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5420 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5421 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5424 | #ifdef HAVE_GETGROUPLIST |
| 5425 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5426 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5427 | Returns a list of groups to which a user belongs.\n\n\ |
| 5428 | user: username to lookup\n\ |
| 5429 | group: base group id of the user"); |
| 5430 | |
| 5431 | static PyObject * |
| 5432 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5433 | { |
| 5434 | #ifdef NGROUPS_MAX |
| 5435 | #define MAX_GROUPS NGROUPS_MAX |
| 5436 | #else |
| 5437 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5438 | #define MAX_GROUPS 64 |
| 5439 | #endif |
| 5440 | |
| 5441 | const char *user; |
| 5442 | int i, ngroups; |
| 5443 | PyObject *list; |
| 5444 | #ifdef __APPLE__ |
| 5445 | int *groups, basegid; |
| 5446 | #else |
| 5447 | gid_t *groups, basegid; |
| 5448 | #endif |
| 5449 | ngroups = MAX_GROUPS; |
| 5450 | |
| 5451 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5452 | return NULL; |
| 5453 | |
| 5454 | #ifdef __APPLE__ |
| 5455 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5456 | #else |
| 5457 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5458 | #endif |
| 5459 | if (groups == NULL) |
| 5460 | return PyErr_NoMemory(); |
| 5461 | |
| 5462 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5463 | PyMem_Del(groups); |
| 5464 | return posix_error(); |
| 5465 | } |
| 5466 | |
| 5467 | list = PyList_New(ngroups); |
| 5468 | if (list == NULL) { |
| 5469 | PyMem_Del(groups); |
| 5470 | return NULL; |
| 5471 | } |
| 5472 | |
| 5473 | for (i = 0; i < ngroups; i++) { |
| 5474 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5475 | if (o == NULL) { |
| 5476 | Py_DECREF(list); |
| 5477 | PyMem_Del(groups); |
| 5478 | return NULL; |
| 5479 | } |
| 5480 | PyList_SET_ITEM(list, i, o); |
| 5481 | } |
| 5482 | |
| 5483 | PyMem_Del(groups); |
| 5484 | |
| 5485 | return list; |
| 5486 | } |
| 5487 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5488 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5489 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5490 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5491 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5492 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5493 | |
| 5494 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5495 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5496 | { |
| 5497 | PyObject *result = NULL; |
| 5498 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5499 | #ifdef NGROUPS_MAX |
| 5500 | #define MAX_GROUPS NGROUPS_MAX |
| 5501 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5502 | /* 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] | 5503 | #define MAX_GROUPS 64 |
| 5504 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5505 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5506 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5507 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5508 | * This is a helper variable to store the intermediate result when |
| 5509 | * that happens. |
| 5510 | * |
| 5511 | * To keep the code readable the OSX behaviour is unconditional, |
| 5512 | * according to the POSIX spec this should be safe on all unix-y |
| 5513 | * systems. |
| 5514 | */ |
| 5515 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5516 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5518 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5519 | if (n < 0) { |
| 5520 | if (errno == EINVAL) { |
| 5521 | n = getgroups(0, NULL); |
| 5522 | if (n == -1) { |
| 5523 | return posix_error(); |
| 5524 | } |
| 5525 | if (n == 0) { |
| 5526 | /* Avoid malloc(0) */ |
| 5527 | alt_grouplist = grouplist; |
| 5528 | } else { |
| 5529 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5530 | if (alt_grouplist == NULL) { |
| 5531 | errno = EINVAL; |
| 5532 | return posix_error(); |
| 5533 | } |
| 5534 | n = getgroups(n, alt_grouplist); |
| 5535 | if (n == -1) { |
| 5536 | PyMem_Free(alt_grouplist); |
| 5537 | return posix_error(); |
| 5538 | } |
| 5539 | } |
| 5540 | } else { |
| 5541 | return posix_error(); |
| 5542 | } |
| 5543 | } |
| 5544 | result = PyList_New(n); |
| 5545 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5546 | int i; |
| 5547 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5548 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5549 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5550 | Py_DECREF(result); |
| 5551 | result = NULL; |
| 5552 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5553 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5554 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5555 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | if (alt_grouplist != grouplist) { |
| 5559 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5560 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5561 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5562 | return result; |
| 5563 | } |
| 5564 | #endif |
| 5565 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5566 | #ifdef HAVE_INITGROUPS |
| 5567 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5568 | "initgroups(username, gid) -> None\n\n\ |
| 5569 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5570 | the groups of which the specified username is a member, plus the specified\n\ |
| 5571 | group id."); |
| 5572 | |
| 5573 | static PyObject * |
| 5574 | posix_initgroups(PyObject *self, PyObject *args) |
| 5575 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5576 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5577 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5578 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5579 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5580 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5581 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5582 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5583 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5584 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5585 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5586 | res = initgroups(username, (gid_t) gid); |
| 5587 | Py_DECREF(oname); |
| 5588 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5589 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5590 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5591 | Py_INCREF(Py_None); |
| 5592 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5593 | } |
| 5594 | #endif |
| 5595 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5596 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5597 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5598 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5599 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5600 | |
| 5601 | static PyObject * |
| 5602 | posix_getpgid(PyObject *self, PyObject *args) |
| 5603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5604 | pid_t pid, pgid; |
| 5605 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5606 | return NULL; |
| 5607 | pgid = getpgid(pid); |
| 5608 | if (pgid < 0) |
| 5609 | return posix_error(); |
| 5610 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5611 | } |
| 5612 | #endif /* HAVE_GETPGID */ |
| 5613 | |
| 5614 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5615 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5616 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5617 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5618 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5619 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5620 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5621 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5622 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5623 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5624 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5625 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5626 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5627 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5628 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5629 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5630 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5631 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5632 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5633 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5634 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5635 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5636 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5637 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5638 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5639 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5640 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5641 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5642 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5643 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5644 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5645 | return posix_error(); |
| 5646 | Py_INCREF(Py_None); |
| 5647 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5650 | #endif /* HAVE_SETPGRP */ |
| 5651 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5652 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5653 | |
| 5654 | #ifdef MS_WINDOWS |
| 5655 | #include <tlhelp32.h> |
| 5656 | |
| 5657 | static PyObject* |
| 5658 | win32_getppid() |
| 5659 | { |
| 5660 | HANDLE snapshot; |
| 5661 | pid_t mypid; |
| 5662 | PyObject* result = NULL; |
| 5663 | BOOL have_record; |
| 5664 | PROCESSENTRY32 pe; |
| 5665 | |
| 5666 | mypid = getpid(); /* This function never fails */ |
| 5667 | |
| 5668 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5669 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5670 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5671 | |
| 5672 | pe.dwSize = sizeof(pe); |
| 5673 | have_record = Process32First(snapshot, &pe); |
| 5674 | while (have_record) { |
| 5675 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5676 | /* We could cache the ulong value in a static variable. */ |
| 5677 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5678 | break; |
| 5679 | } |
| 5680 | |
| 5681 | have_record = Process32Next(snapshot, &pe); |
| 5682 | } |
| 5683 | |
| 5684 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5685 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5686 | * error anyway, so let's raise it. */ |
| 5687 | if (!result) |
| 5688 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5689 | |
| 5690 | CloseHandle(snapshot); |
| 5691 | |
| 5692 | return result; |
| 5693 | } |
| 5694 | #endif /*MS_WINDOWS*/ |
| 5695 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5696 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5697 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5698 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5699 | Windows machines will still return its id; others systems will return the id\n\ |
| 5700 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5702 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5703 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5704 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5705 | #ifdef MS_WINDOWS |
| 5706 | return win32_getppid(); |
| 5707 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5709 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5710 | } |
| 5711 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5712 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5713 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5714 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5715 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5716 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5717 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5718 | |
| 5719 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5720 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5722 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5723 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5724 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 5725 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5726 | |
| 5727 | if (GetUserNameW(user_name, &num_chars)) { |
| 5728 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5729 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5730 | } |
| 5731 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5732 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5733 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5734 | char *name; |
| 5735 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5736 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5737 | errno = 0; |
| 5738 | name = getlogin(); |
| 5739 | if (name == NULL) { |
| 5740 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5741 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5742 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5743 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5744 | } |
| 5745 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5746 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5747 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5748 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5749 | return result; |
| 5750 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5751 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5752 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5753 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5754 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5755 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5756 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5757 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5758 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5759 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5760 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5761 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5762 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5763 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5764 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5765 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5766 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5767 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5768 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5769 | Kill a process with a signal."); |
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 * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5772 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5773 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5774 | pid_t pid; |
| 5775 | int sig; |
| 5776 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5777 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5778 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5779 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5780 | APIRET rc; |
| 5781 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5782 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5783 | |
| 5784 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5785 | APIRET rc; |
| 5786 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5787 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5788 | |
| 5789 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5790 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5791 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5792 | if (kill(pid, sig) == -1) |
| 5793 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5794 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5795 | Py_INCREF(Py_None); |
| 5796 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5797 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5798 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5799 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5800 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5801 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5802 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5803 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5804 | |
| 5805 | static PyObject * |
| 5806 | posix_killpg(PyObject *self, PyObject *args) |
| 5807 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5808 | int sig; |
| 5809 | pid_t pgid; |
| 5810 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5811 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5812 | take the same type. Moreover, pid_t is always at least as wide as |
| 5813 | int (else compilation of this module fails), which is safe. */ |
| 5814 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5815 | return NULL; |
| 5816 | if (killpg(pgid, sig) == -1) |
| 5817 | return posix_error(); |
| 5818 | Py_INCREF(Py_None); |
| 5819 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5820 | } |
| 5821 | #endif |
| 5822 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5823 | #ifdef MS_WINDOWS |
| 5824 | PyDoc_STRVAR(win32_kill__doc__, |
| 5825 | "kill(pid, sig)\n\n\ |
| 5826 | Kill a process with a signal."); |
| 5827 | |
| 5828 | static PyObject * |
| 5829 | win32_kill(PyObject *self, PyObject *args) |
| 5830 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5831 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5832 | DWORD pid, sig, err; |
| 5833 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5834 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5835 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5836 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5838 | /* Console processes which share a common console can be sent CTRL+C or |
| 5839 | CTRL+BREAK events, provided they handle said events. */ |
| 5840 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5841 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5842 | err = GetLastError(); |
| 5843 | PyErr_SetFromWindowsErr(err); |
| 5844 | } |
| 5845 | else |
| 5846 | Py_RETURN_NONE; |
| 5847 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5849 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5850 | attempt to open and terminate the process. */ |
| 5851 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5852 | if (handle == NULL) { |
| 5853 | err = GetLastError(); |
| 5854 | return PyErr_SetFromWindowsErr(err); |
| 5855 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5857 | if (TerminateProcess(handle, sig) == 0) { |
| 5858 | err = GetLastError(); |
| 5859 | result = PyErr_SetFromWindowsErr(err); |
| 5860 | } else { |
| 5861 | Py_INCREF(Py_None); |
| 5862 | result = Py_None; |
| 5863 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5865 | CloseHandle(handle); |
| 5866 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5867 | } |
| 5868 | #endif /* MS_WINDOWS */ |
| 5869 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5870 | #ifdef HAVE_PLOCK |
| 5871 | |
| 5872 | #ifdef HAVE_SYS_LOCK_H |
| 5873 | #include <sys/lock.h> |
| 5874 | #endif |
| 5875 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5876 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5877 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5878 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5879 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5880 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5881 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5882 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5883 | int op; |
| 5884 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5885 | return NULL; |
| 5886 | if (plock(op) == -1) |
| 5887 | return posix_error(); |
| 5888 | Py_INCREF(Py_None); |
| 5889 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5890 | } |
| 5891 | #endif |
| 5892 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5893 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5894 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5895 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5896 | Set the current process's user id."); |
| 5897 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5898 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5899 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5900 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5901 | long uid_arg; |
| 5902 | uid_t uid; |
| 5903 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5904 | return NULL; |
| 5905 | uid = uid_arg; |
| 5906 | if (uid != uid_arg) { |
| 5907 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5908 | return NULL; |
| 5909 | } |
| 5910 | if (setuid(uid) < 0) |
| 5911 | return posix_error(); |
| 5912 | Py_INCREF(Py_None); |
| 5913 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5914 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5915 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5916 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5917 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5918 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5919 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5920 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5921 | Set the current process's effective user id."); |
| 5922 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5923 | static PyObject * |
| 5924 | posix_seteuid (PyObject *self, PyObject *args) |
| 5925 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5926 | long euid_arg; |
| 5927 | uid_t euid; |
| 5928 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5929 | return NULL; |
| 5930 | euid = euid_arg; |
| 5931 | if (euid != euid_arg) { |
| 5932 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5933 | return NULL; |
| 5934 | } |
| 5935 | if (seteuid(euid) < 0) { |
| 5936 | return posix_error(); |
| 5937 | } else { |
| 5938 | Py_INCREF(Py_None); |
| 5939 | return Py_None; |
| 5940 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5941 | } |
| 5942 | #endif /* HAVE_SETEUID */ |
| 5943 | |
| 5944 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5945 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5946 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5947 | Set the current process's effective group id."); |
| 5948 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5949 | static PyObject * |
| 5950 | posix_setegid (PyObject *self, PyObject *args) |
| 5951 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5952 | long egid_arg; |
| 5953 | gid_t egid; |
| 5954 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5955 | return NULL; |
| 5956 | egid = egid_arg; |
| 5957 | if (egid != egid_arg) { |
| 5958 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5959 | return NULL; |
| 5960 | } |
| 5961 | if (setegid(egid) < 0) { |
| 5962 | return posix_error(); |
| 5963 | } else { |
| 5964 | Py_INCREF(Py_None); |
| 5965 | return Py_None; |
| 5966 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5967 | } |
| 5968 | #endif /* HAVE_SETEGID */ |
| 5969 | |
| 5970 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5971 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5972 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5973 | Set the current process's real and effective user ids."); |
| 5974 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5975 | static PyObject * |
| 5976 | posix_setreuid (PyObject *self, PyObject *args) |
| 5977 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5978 | long ruid_arg, euid_arg; |
| 5979 | uid_t ruid, euid; |
| 5980 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5981 | return NULL; |
| 5982 | if (ruid_arg == -1) |
| 5983 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5984 | else |
| 5985 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5986 | if (euid_arg == -1) |
| 5987 | euid = (uid_t)-1; |
| 5988 | else |
| 5989 | euid = euid_arg; |
| 5990 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5991 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5992 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5993 | return NULL; |
| 5994 | } |
| 5995 | if (setreuid(ruid, euid) < 0) { |
| 5996 | return posix_error(); |
| 5997 | } else { |
| 5998 | Py_INCREF(Py_None); |
| 5999 | return Py_None; |
| 6000 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6001 | } |
| 6002 | #endif /* HAVE_SETREUID */ |
| 6003 | |
| 6004 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6005 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 6006 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6007 | Set the current process's real and effective group ids."); |
| 6008 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6009 | static PyObject * |
| 6010 | posix_setregid (PyObject *self, PyObject *args) |
| 6011 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6012 | long rgid_arg, egid_arg; |
| 6013 | gid_t rgid, egid; |
| 6014 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 6015 | return NULL; |
| 6016 | if (rgid_arg == -1) |
| 6017 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 6018 | else |
| 6019 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 6020 | if (egid_arg == -1) |
| 6021 | egid = (gid_t)-1; |
| 6022 | else |
| 6023 | egid = egid_arg; |
| 6024 | if ((egid_arg != -1 && egid != egid_arg) || |
| 6025 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 6026 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6027 | return NULL; |
| 6028 | } |
| 6029 | if (setregid(rgid, egid) < 0) { |
| 6030 | return posix_error(); |
| 6031 | } else { |
| 6032 | Py_INCREF(Py_None); |
| 6033 | return Py_None; |
| 6034 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 6035 | } |
| 6036 | #endif /* HAVE_SETREGID */ |
| 6037 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6038 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6039 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6040 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6041 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6042 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6043 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6044 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6045 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | long gid_arg; |
| 6047 | gid_t gid; |
| 6048 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 6049 | return NULL; |
| 6050 | gid = gid_arg; |
| 6051 | if (gid != gid_arg) { |
| 6052 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 6053 | return NULL; |
| 6054 | } |
| 6055 | if (setgid(gid) < 0) |
| 6056 | return posix_error(); |
| 6057 | Py_INCREF(Py_None); |
| 6058 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6059 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6060 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 6061 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6062 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6064 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6065 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6066 | |
| 6067 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6068 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6069 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6070 | int i, len; |
| 6071 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6073 | if (!PySequence_Check(groups)) { |
| 6074 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6075 | return NULL; |
| 6076 | } |
| 6077 | len = PySequence_Size(groups); |
| 6078 | if (len > MAX_GROUPS) { |
| 6079 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6080 | return NULL; |
| 6081 | } |
| 6082 | for(i = 0; i < len; i++) { |
| 6083 | PyObject *elem; |
| 6084 | elem = PySequence_GetItem(groups, i); |
| 6085 | if (!elem) |
| 6086 | return NULL; |
| 6087 | if (!PyLong_Check(elem)) { |
| 6088 | PyErr_SetString(PyExc_TypeError, |
| 6089 | "groups must be integers"); |
| 6090 | Py_DECREF(elem); |
| 6091 | return NULL; |
| 6092 | } else { |
| 6093 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6094 | if (PyErr_Occurred()) { |
| 6095 | PyErr_SetString(PyExc_TypeError, |
| 6096 | "group id too big"); |
| 6097 | Py_DECREF(elem); |
| 6098 | return NULL; |
| 6099 | } |
| 6100 | grouplist[i] = x; |
| 6101 | /* read back the value to see if it fitted in gid_t */ |
| 6102 | if (grouplist[i] != x) { |
| 6103 | PyErr_SetString(PyExc_TypeError, |
| 6104 | "group id too big"); |
| 6105 | Py_DECREF(elem); |
| 6106 | return NULL; |
| 6107 | } |
| 6108 | } |
| 6109 | Py_DECREF(elem); |
| 6110 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6111 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | if (setgroups(len, grouplist) < 0) |
| 6113 | return posix_error(); |
| 6114 | Py_INCREF(Py_None); |
| 6115 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6116 | } |
| 6117 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6118 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6119 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6120 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6121 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6122 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6123 | PyObject *result; |
| 6124 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6125 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6126 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6127 | if (pid == -1) |
| 6128 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6129 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6130 | if (struct_rusage == NULL) { |
| 6131 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6132 | if (m == NULL) |
| 6133 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6134 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6135 | Py_DECREF(m); |
| 6136 | if (struct_rusage == NULL) |
| 6137 | return NULL; |
| 6138 | } |
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 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6141 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6142 | if (!result) |
| 6143 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6144 | |
| 6145 | #ifndef doubletime |
| 6146 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6147 | #endif |
| 6148 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6149 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6150 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6151 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6152 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6153 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6155 | SET_INT(result, 2, ru->ru_maxrss); |
| 6156 | SET_INT(result, 3, ru->ru_ixrss); |
| 6157 | SET_INT(result, 4, ru->ru_idrss); |
| 6158 | SET_INT(result, 5, ru->ru_isrss); |
| 6159 | SET_INT(result, 6, ru->ru_minflt); |
| 6160 | SET_INT(result, 7, ru->ru_majflt); |
| 6161 | SET_INT(result, 8, ru->ru_nswap); |
| 6162 | SET_INT(result, 9, ru->ru_inblock); |
| 6163 | SET_INT(result, 10, ru->ru_oublock); |
| 6164 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6165 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6166 | SET_INT(result, 13, ru->ru_nsignals); |
| 6167 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6168 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6169 | #undef SET_INT |
| 6170 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6171 | if (PyErr_Occurred()) { |
| 6172 | Py_DECREF(result); |
| 6173 | return NULL; |
| 6174 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6175 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6176 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6177 | } |
| 6178 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6179 | |
| 6180 | #ifdef HAVE_WAIT3 |
| 6181 | PyDoc_STRVAR(posix_wait3__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6182 | "wait3(options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6183 | Wait for completion of a child process."); |
| 6184 | |
| 6185 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6186 | posix_wait3(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6187 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6188 | pid_t pid; |
| 6189 | int options; |
| 6190 | struct rusage ru; |
| 6191 | WAIT_TYPE status; |
| 6192 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6193 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6194 | if (!PyArg_ParseTuple(args, "i:wait3", &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6195 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6196 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6197 | Py_BEGIN_ALLOW_THREADS |
| 6198 | pid = wait3(&status, options, &ru); |
| 6199 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6200 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6201 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6202 | } |
| 6203 | #endif /* HAVE_WAIT3 */ |
| 6204 | |
| 6205 | #ifdef HAVE_WAIT4 |
| 6206 | PyDoc_STRVAR(posix_wait4__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6207 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6208 | Wait for completion of a given child process."); |
| 6209 | |
| 6210 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6211 | posix_wait4(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6212 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6213 | pid_t pid; |
| 6214 | int options; |
| 6215 | struct rusage ru; |
| 6216 | WAIT_TYPE status; |
| 6217 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6218 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6219 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6220 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6221 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6222 | Py_BEGIN_ALLOW_THREADS |
| 6223 | pid = wait4(pid, &status, options, &ru); |
| 6224 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6225 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6226 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6227 | } |
| 6228 | #endif /* HAVE_WAIT4 */ |
| 6229 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6230 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6231 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6232 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6233 | Wait for the completion of one or more child processes.\n\n\ |
| 6234 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6235 | id specifies the pid to wait on.\n\ |
| 6236 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6237 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6238 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6239 | no children in a waitable state."); |
| 6240 | |
| 6241 | static PyObject * |
| 6242 | posix_waitid(PyObject *self, PyObject *args) |
| 6243 | { |
| 6244 | PyObject *result; |
| 6245 | idtype_t idtype; |
| 6246 | id_t id; |
| 6247 | int options, res; |
| 6248 | siginfo_t si; |
| 6249 | si.si_pid = 0; |
| 6250 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6251 | return NULL; |
| 6252 | Py_BEGIN_ALLOW_THREADS |
| 6253 | res = waitid(idtype, id, &si, options); |
| 6254 | Py_END_ALLOW_THREADS |
| 6255 | if (res == -1) |
| 6256 | return posix_error(); |
| 6257 | |
| 6258 | if (si.si_pid == 0) |
| 6259 | Py_RETURN_NONE; |
| 6260 | |
| 6261 | result = PyStructSequence_New(&WaitidResultType); |
| 6262 | if (!result) |
| 6263 | return NULL; |
| 6264 | |
| 6265 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6266 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6267 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6268 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6269 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6270 | if (PyErr_Occurred()) { |
| 6271 | Py_DECREF(result); |
| 6272 | return NULL; |
| 6273 | } |
| 6274 | |
| 6275 | return result; |
| 6276 | } |
| 6277 | #endif |
| 6278 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6279 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6280 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6281 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6282 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6285 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6286 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6287 | pid_t pid; |
| 6288 | int options; |
| 6289 | WAIT_TYPE status; |
| 6290 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6292 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6293 | return NULL; |
| 6294 | Py_BEGIN_ALLOW_THREADS |
| 6295 | pid = waitpid(pid, &status, options); |
| 6296 | Py_END_ALLOW_THREADS |
| 6297 | if (pid == -1) |
| 6298 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6299 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6300 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6303 | #elif defined(HAVE_CWAIT) |
| 6304 | |
| 6305 | /* 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] | 6306 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6307 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6309 | |
| 6310 | static PyObject * |
| 6311 | posix_waitpid(PyObject *self, PyObject *args) |
| 6312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | Py_intptr_t pid; |
| 6314 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6316 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6317 | return NULL; |
| 6318 | Py_BEGIN_ALLOW_THREADS |
| 6319 | pid = _cwait(&status, pid, options); |
| 6320 | Py_END_ALLOW_THREADS |
| 6321 | if (pid == -1) |
| 6322 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6324 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6325 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6326 | } |
| 6327 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6328 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6329 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6330 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6331 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6332 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6334 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6335 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6336 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6337 | pid_t pid; |
| 6338 | WAIT_TYPE status; |
| 6339 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6341 | Py_BEGIN_ALLOW_THREADS |
| 6342 | pid = wait(&status); |
| 6343 | Py_END_ALLOW_THREADS |
| 6344 | if (pid == -1) |
| 6345 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6346 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6347 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6348 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6349 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6350 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6352 | PyDoc_STRVAR(posix_lstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6353 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6354 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6355 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6356 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6357 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6358 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6359 | #ifdef HAVE_LSTAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6360 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6361 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6362 | #ifdef MS_WINDOWS |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6363 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6364 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6365 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 6366 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6367 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6368 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6371 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6372 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6373 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6374 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6375 | 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] | 6376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6377 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6378 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6379 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6380 | PyObject* v; |
| 6381 | char buf[MAXPATHLEN]; |
| 6382 | PyObject *opath; |
| 6383 | char *path; |
| 6384 | int n; |
| 6385 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6387 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6388 | PyUnicode_FSConverter, &opath)) |
| 6389 | return NULL; |
| 6390 | path = PyBytes_AsString(opath); |
| 6391 | v = PySequence_GetItem(args, 0); |
| 6392 | if (v == NULL) { |
| 6393 | Py_DECREF(opath); |
| 6394 | return NULL; |
| 6395 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6396 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6397 | if (PyUnicode_Check(v)) { |
| 6398 | arg_is_unicode = 1; |
| 6399 | } |
| 6400 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6402 | Py_BEGIN_ALLOW_THREADS |
| 6403 | n = readlink(path, buf, (int) sizeof buf); |
| 6404 | Py_END_ALLOW_THREADS |
| 6405 | if (n < 0) |
| 6406 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6408 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6409 | if (arg_is_unicode) |
| 6410 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6411 | else |
| 6412 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6413 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6414 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6415 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6416 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6417 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6418 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6419 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6420 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6421 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6422 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6423 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6424 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6425 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6426 | } |
| 6427 | #endif /* HAVE_SYMLINK */ |
| 6428 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6429 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6430 | |
| 6431 | PyDoc_STRVAR(win_readlink__doc__, |
| 6432 | "readlink(path) -> path\n\n\ |
| 6433 | Return a string representing the path to which the symbolic link points."); |
| 6434 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6435 | /* Windows readlink implementation */ |
| 6436 | static PyObject * |
| 6437 | win_readlink(PyObject *self, PyObject *args) |
| 6438 | { |
| 6439 | wchar_t *path; |
| 6440 | DWORD n_bytes_returned; |
| 6441 | DWORD io_result; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6442 | PyObject *po, *result; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6443 | HANDLE reparse_point_handle; |
| 6444 | |
| 6445 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6446 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6447 | wchar_t *print_name; |
| 6448 | |
| 6449 | if (!PyArg_ParseTuple(args, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6450 | "U:readlink", |
| 6451 | &po)) |
| 6452 | return NULL; |
| 6453 | path = PyUnicode_AsUnicode(po); |
| 6454 | if (path == NULL) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6455 | return NULL; |
| 6456 | |
| 6457 | /* First get a handle to the reparse point */ |
| 6458 | Py_BEGIN_ALLOW_THREADS |
| 6459 | reparse_point_handle = CreateFileW( |
| 6460 | path, |
| 6461 | 0, |
| 6462 | 0, |
| 6463 | 0, |
| 6464 | OPEN_EXISTING, |
| 6465 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6466 | 0); |
| 6467 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6468 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6469 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6470 | return win32_error_object("readlink", po); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6471 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6472 | Py_BEGIN_ALLOW_THREADS |
| 6473 | /* New call DeviceIoControl to read the reparse point */ |
| 6474 | io_result = DeviceIoControl( |
| 6475 | reparse_point_handle, |
| 6476 | FSCTL_GET_REPARSE_POINT, |
| 6477 | 0, 0, /* in buffer */ |
| 6478 | target_buffer, sizeof(target_buffer), |
| 6479 | &n_bytes_returned, |
| 6480 | 0 /* we're not using OVERLAPPED_IO */ |
| 6481 | ); |
| 6482 | CloseHandle(reparse_point_handle); |
| 6483 | Py_END_ALLOW_THREADS |
| 6484 | |
| 6485 | if (io_result==0) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6486 | return win32_error_object("readlink", po); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6487 | |
| 6488 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6489 | { |
| 6490 | PyErr_SetString(PyExc_ValueError, |
| 6491 | "not a symbolic link"); |
| 6492 | return NULL; |
| 6493 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6494 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6495 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6496 | |
| 6497 | result = PyUnicode_FromWideChar(print_name, |
| 6498 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6499 | return result; |
| 6500 | } |
| 6501 | |
| 6502 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6503 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6504 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6505 | |
| 6506 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6507 | static int has_CreateSymbolicLinkW = 0; |
| 6508 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6509 | static int |
| 6510 | check_CreateSymbolicLinkW() |
| 6511 | { |
| 6512 | HINSTANCE hKernel32; |
| 6513 | /* only recheck */ |
| 6514 | if (has_CreateSymbolicLinkW) |
| 6515 | return has_CreateSymbolicLinkW; |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 6516 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6517 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6518 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6519 | if (Py_CreateSymbolicLinkW) |
| 6520 | has_CreateSymbolicLinkW = 1; |
| 6521 | return has_CreateSymbolicLinkW; |
| 6522 | } |
| 6523 | |
| 6524 | PyDoc_STRVAR(win_symlink__doc__, |
| 6525 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6526 | Create a symbolic link pointing to src named dst.\n\ |
| 6527 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6528 | a directory.\n\ |
| 6529 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6530 | NotImplementedError otherwise."); |
| 6531 | |
| 6532 | static PyObject * |
| 6533 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6534 | { |
| 6535 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6536 | PyObject *osrc, *odest; |
| 6537 | PyObject *usrc = NULL, *udest = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6538 | wchar_t *wsrc, *wdest; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6539 | int target_is_directory = 0; |
| 6540 | DWORD res; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6541 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6542 | if (!check_CreateSymbolicLinkW()) |
| 6543 | { |
| 6544 | /* raise NotImplementedError */ |
| 6545 | return PyErr_Format(PyExc_NotImplementedError, |
| 6546 | "CreateSymbolicLinkW not found"); |
| 6547 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6548 | if (!PyArg_ParseTupleAndKeywords( |
| 6549 | args, kwargs, "OO|i:symlink", kwlist, |
| 6550 | &osrc, &odest, &target_is_directory)) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6551 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6552 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6553 | usrc = win32_decode_filename(osrc); |
| 6554 | if (!usrc) |
| 6555 | return NULL; |
| 6556 | udest = win32_decode_filename(odest); |
| 6557 | if (!udest) |
| 6558 | goto error; |
| 6559 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6560 | if (win32_can_symlink == 0) |
| 6561 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6562 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6563 | wsrc = PyUnicode_AsUnicode(usrc); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6564 | if (wsrc == NULL) |
| 6565 | goto error; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6566 | wdest = PyUnicode_AsUnicode(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6567 | if (wsrc == NULL) |
| 6568 | goto error; |
| 6569 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6570 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6571 | res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6572 | Py_END_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6573 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6574 | Py_DECREF(usrc); |
| 6575 | Py_DECREF(udest); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6576 | if (!res) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6577 | return win32_error_object("symlink", osrc); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6578 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6579 | Py_INCREF(Py_None); |
| 6580 | return Py_None; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6581 | |
| 6582 | error: |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6583 | Py_XDECREF(usrc); |
| 6584 | Py_XDECREF(udest); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6585 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6586 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6587 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6588 | |
| 6589 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6590 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6591 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6592 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6593 | { |
| 6594 | ULONG value = 0; |
| 6595 | |
| 6596 | Py_BEGIN_ALLOW_THREADS |
| 6597 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6598 | Py_END_ALLOW_THREADS |
| 6599 | |
| 6600 | return value; |
| 6601 | } |
| 6602 | |
| 6603 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6604 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6605 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6606 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6607 | return Py_BuildValue("ddddd", |
| 6608 | (double)0 /* t.tms_utime / HZ */, |
| 6609 | (double)0 /* t.tms_stime / HZ */, |
| 6610 | (double)0 /* t.tms_cutime / HZ */, |
| 6611 | (double)0 /* t.tms_cstime / HZ */, |
| 6612 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6613 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6614 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6615 | #define NEED_TICKS_PER_SECOND |
| 6616 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6617 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6618 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6619 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6620 | struct tms t; |
| 6621 | clock_t c; |
| 6622 | errno = 0; |
| 6623 | c = times(&t); |
| 6624 | if (c == (clock_t) -1) |
| 6625 | return posix_error(); |
| 6626 | return Py_BuildValue("ddddd", |
| 6627 | (double)t.tms_utime / ticks_per_second, |
| 6628 | (double)t.tms_stime / ticks_per_second, |
| 6629 | (double)t.tms_cutime / ticks_per_second, |
| 6630 | (double)t.tms_cstime / ticks_per_second, |
| 6631 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6632 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6633 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6634 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6635 | |
| 6636 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6637 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6638 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6639 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6640 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6641 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6642 | FILETIME create, exit, kernel, user; |
| 6643 | HANDLE hProc; |
| 6644 | hProc = GetCurrentProcess(); |
| 6645 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6646 | /* The fields of a FILETIME structure are the hi and lo part |
| 6647 | of a 64-bit value expressed in 100 nanosecond units. |
| 6648 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6649 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6650 | */ |
| 6651 | return Py_BuildValue( |
| 6652 | "ddddd", |
| 6653 | (double)(user.dwHighDateTime*429.4967296 + |
| 6654 | user.dwLowDateTime*1e-7), |
| 6655 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6656 | kernel.dwLowDateTime*1e-7), |
| 6657 | (double)0, |
| 6658 | (double)0, |
| 6659 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6660 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6661 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6662 | |
| 6663 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6664 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6665 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6667 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6668 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6669 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6670 | #ifdef HAVE_GETSID |
| 6671 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6672 | "getsid(pid) -> sid\n\n\ |
| 6673 | Call the system call getsid()."); |
| 6674 | |
| 6675 | static PyObject * |
| 6676 | posix_getsid(PyObject *self, PyObject *args) |
| 6677 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6678 | pid_t pid; |
| 6679 | int sid; |
| 6680 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6681 | return NULL; |
| 6682 | sid = getsid(pid); |
| 6683 | if (sid < 0) |
| 6684 | return posix_error(); |
| 6685 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6686 | } |
| 6687 | #endif /* HAVE_GETSID */ |
| 6688 | |
| 6689 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6690 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6691 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6692 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6693 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6694 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6695 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6696 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6697 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6698 | if (setsid() < 0) |
| 6699 | return posix_error(); |
| 6700 | Py_INCREF(Py_None); |
| 6701 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6702 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6703 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6704 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6705 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6706 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6707 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6708 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6709 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6710 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6711 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6712 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6713 | pid_t pid; |
| 6714 | int pgrp; |
| 6715 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6716 | return NULL; |
| 6717 | if (setpgid(pid, pgrp) < 0) |
| 6718 | return posix_error(); |
| 6719 | Py_INCREF(Py_None); |
| 6720 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6721 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6722 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6723 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6724 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6725 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6726 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6727 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6728 | 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] | 6729 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6730 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6731 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6732 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6733 | int fd; |
| 6734 | pid_t pgid; |
| 6735 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6736 | return NULL; |
| 6737 | pgid = tcgetpgrp(fd); |
| 6738 | if (pgid < 0) |
| 6739 | return posix_error(); |
| 6740 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6741 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6742 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6743 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6744 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6745 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6746 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6747 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6748 | 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] | 6749 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6750 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6751 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6753 | int fd; |
| 6754 | pid_t pgid; |
| 6755 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6756 | return NULL; |
| 6757 | if (tcsetpgrp(fd, pgid) < 0) |
| 6758 | return posix_error(); |
| 6759 | Py_INCREF(Py_None); |
| 6760 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6761 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6762 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6763 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6764 | /* Functions acting on file descriptors */ |
| 6765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6766 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6767 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6768 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6769 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6770 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6771 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | PyObject *ofile; |
| 6774 | char *file; |
| 6775 | int flag; |
| 6776 | int mode = 0777; |
| 6777 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6778 | |
| 6779 | #ifdef MS_WINDOWS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6780 | PyObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6781 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6782 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 6783 | if (wpath == NULL) |
| 6784 | return NULL; |
| 6785 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6786 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 6787 | fd = _wopen(wpath, flag, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | Py_END_ALLOW_THREADS |
| 6789 | if (fd < 0) |
| 6790 | return posix_error(); |
| 6791 | return PyLong_FromLong((long)fd); |
| 6792 | } |
| 6793 | /* Drop the argument parsing error as narrow strings |
| 6794 | are also valid. */ |
| 6795 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6796 | #endif |
| 6797 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6798 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6799 | PyUnicode_FSConverter, &ofile, |
| 6800 | &flag, &mode)) |
| 6801 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6802 | #ifdef MS_WINDOWS |
| 6803 | if (win32_warn_bytes_api()) { |
| 6804 | Py_DECREF(ofile); |
| 6805 | return NULL; |
| 6806 | } |
| 6807 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6808 | file = PyBytes_AsString(ofile); |
| 6809 | Py_BEGIN_ALLOW_THREADS |
| 6810 | fd = open(file, flag, mode); |
| 6811 | Py_END_ALLOW_THREADS |
| 6812 | if (fd < 0) |
| 6813 | return posix_error_with_allocated_filename(ofile); |
| 6814 | Py_DECREF(ofile); |
| 6815 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6819 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6820 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6821 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6822 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6823 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6824 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6825 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | int fd, res; |
| 6827 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6828 | return NULL; |
| 6829 | if (!_PyVerify_fd(fd)) |
| 6830 | return posix_error(); |
| 6831 | Py_BEGIN_ALLOW_THREADS |
| 6832 | res = close(fd); |
| 6833 | Py_END_ALLOW_THREADS |
| 6834 | if (res < 0) |
| 6835 | return posix_error(); |
| 6836 | Py_INCREF(Py_None); |
| 6837 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6841 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6842 | "closerange(fd_low, fd_high)\n\n\ |
| 6843 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6844 | |
| 6845 | static PyObject * |
| 6846 | posix_closerange(PyObject *self, PyObject *args) |
| 6847 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6848 | int fd_from, fd_to, i; |
| 6849 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6850 | return NULL; |
| 6851 | Py_BEGIN_ALLOW_THREADS |
| 6852 | for (i = fd_from; i < fd_to; i++) |
| 6853 | if (_PyVerify_fd(i)) |
| 6854 | close(i); |
| 6855 | Py_END_ALLOW_THREADS |
| 6856 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6860 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6861 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6862 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6863 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6865 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6867 | int fd; |
| 6868 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6869 | return NULL; |
| 6870 | if (!_PyVerify_fd(fd)) |
| 6871 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6873 | if (fd < 0) |
| 6874 | return posix_error(); |
| 6875 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6879 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6880 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6881 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6882 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6883 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6884 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6885 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | int fd, fd2, res; |
| 6887 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6888 | return NULL; |
| 6889 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6890 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6891 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6892 | if (res < 0) |
| 6893 | return posix_error(); |
| 6894 | Py_INCREF(Py_None); |
| 6895 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6898 | #ifdef HAVE_LOCKF |
| 6899 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6900 | "lockf(fd, cmd, len)\n\n\ |
| 6901 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6902 | fd is an open file descriptor.\n\ |
| 6903 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6904 | F_TEST.\n\ |
| 6905 | len specifies the section of the file to lock."); |
| 6906 | |
| 6907 | static PyObject * |
| 6908 | posix_lockf(PyObject *self, PyObject *args) |
| 6909 | { |
| 6910 | int fd, cmd, res; |
| 6911 | off_t len; |
| 6912 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6913 | &fd, &cmd, _parse_off_t, &len)) |
| 6914 | return NULL; |
| 6915 | |
| 6916 | Py_BEGIN_ALLOW_THREADS |
| 6917 | res = lockf(fd, cmd, len); |
| 6918 | Py_END_ALLOW_THREADS |
| 6919 | |
| 6920 | if (res < 0) |
| 6921 | return posix_error(); |
| 6922 | |
| 6923 | Py_RETURN_NONE; |
| 6924 | } |
| 6925 | #endif |
| 6926 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6928 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6929 | "lseek(fd, pos, how) -> newpos\n\n\ |
Victor Stinner | e83f899 | 2011-12-17 23:15:09 +0100 | [diff] [blame] | 6930 | Set the current position of a file descriptor.\n\ |
| 6931 | Return the new cursor position in bytes, starting from the beginning."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6932 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6933 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6934 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6935 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6936 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6937 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6938 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6939 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6940 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6941 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6942 | PyObject *posobj; |
| 6943 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6945 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6946 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6947 | switch (how) { |
| 6948 | case 0: how = SEEK_SET; break; |
| 6949 | case 1: how = SEEK_CUR; break; |
| 6950 | case 2: how = SEEK_END; break; |
| 6951 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6952 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6953 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6954 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6955 | pos = PyLong_AsLong(posobj); |
| 6956 | #else |
| 6957 | pos = PyLong_AsLongLong(posobj); |
| 6958 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6959 | if (PyErr_Occurred()) |
| 6960 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6961 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6962 | if (!_PyVerify_fd(fd)) |
| 6963 | return posix_error(); |
| 6964 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6965 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6966 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6967 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6968 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6969 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6970 | Py_END_ALLOW_THREADS |
| 6971 | if (res < 0) |
| 6972 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6973 | |
| 6974 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6975 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6976 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6977 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6978 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6979 | } |
| 6980 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6981 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6982 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6983 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6984 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6985 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6986 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6987 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6988 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | int fd, size; |
| 6990 | Py_ssize_t n; |
| 6991 | PyObject *buffer; |
| 6992 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6993 | return NULL; |
| 6994 | if (size < 0) { |
| 6995 | errno = EINVAL; |
| 6996 | return posix_error(); |
| 6997 | } |
| 6998 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6999 | if (buffer == NULL) |
| 7000 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7001 | if (!_PyVerify_fd(fd)) { |
| 7002 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7003 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 7004 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7005 | Py_BEGIN_ALLOW_THREADS |
| 7006 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 7007 | Py_END_ALLOW_THREADS |
| 7008 | if (n < 0) { |
| 7009 | Py_DECREF(buffer); |
| 7010 | return posix_error(); |
| 7011 | } |
| 7012 | if (n != size) |
| 7013 | _PyBytes_Resize(&buffer, n); |
| 7014 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7015 | } |
| 7016 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7017 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 7018 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7019 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7020 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 7021 | { |
| 7022 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7023 | Py_ssize_t blen, total = 0; |
| 7024 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7025 | *iov = PyMem_New(struct iovec, cnt); |
| 7026 | if (*iov == NULL) { |
| 7027 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7028 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7029 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7030 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7031 | *buf = PyMem_New(Py_buffer, cnt); |
| 7032 | if (*buf == NULL) { |
| 7033 | PyMem_Del(*iov); |
| 7034 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7035 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7039 | PyObject *item = PySequence_GetItem(seq, i); |
| 7040 | if (item == NULL) |
| 7041 | goto fail; |
| 7042 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 7043 | Py_DECREF(item); |
| 7044 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7045 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7046 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7047 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7048 | blen = (*buf)[i].len; |
| 7049 | (*iov)[i].iov_len = blen; |
| 7050 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7051 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7052 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 7053 | |
| 7054 | fail: |
| 7055 | PyMem_Del(*iov); |
| 7056 | for (j = 0; j < i; j++) { |
| 7057 | PyBuffer_Release(&(*buf)[j]); |
| 7058 | } |
| 7059 | PyMem_Del(*buf); |
| 7060 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | static void |
| 7064 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 7065 | { |
| 7066 | int i; |
| 7067 | PyMem_Del(iov); |
| 7068 | for (i = 0; i < cnt; i++) { |
| 7069 | PyBuffer_Release(&buf[i]); |
| 7070 | } |
| 7071 | PyMem_Del(buf); |
| 7072 | } |
| 7073 | #endif |
| 7074 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7075 | #ifdef HAVE_READV |
| 7076 | PyDoc_STRVAR(posix_readv__doc__, |
| 7077 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7078 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7079 | is an arbitrary sequence of writable buffers.\n\ |
| 7080 | Returns the total number of bytes read."); |
| 7081 | |
| 7082 | static PyObject * |
| 7083 | posix_readv(PyObject *self, PyObject *args) |
| 7084 | { |
| 7085 | int fd, cnt; |
| 7086 | Py_ssize_t n; |
| 7087 | PyObject *seq; |
| 7088 | struct iovec *iov; |
| 7089 | Py_buffer *buf; |
| 7090 | |
| 7091 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7092 | return NULL; |
| 7093 | if (!PySequence_Check(seq)) { |
| 7094 | PyErr_SetString(PyExc_TypeError, |
| 7095 | "readv() arg 2 must be a sequence"); |
| 7096 | return NULL; |
| 7097 | } |
| 7098 | cnt = PySequence_Size(seq); |
| 7099 | |
| 7100 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7101 | return NULL; |
| 7102 | |
| 7103 | Py_BEGIN_ALLOW_THREADS |
| 7104 | n = readv(fd, iov, cnt); |
| 7105 | Py_END_ALLOW_THREADS |
| 7106 | |
| 7107 | iov_cleanup(iov, buf, cnt); |
| 7108 | return PyLong_FromSsize_t(n); |
| 7109 | } |
| 7110 | #endif |
| 7111 | |
| 7112 | #ifdef HAVE_PREAD |
| 7113 | PyDoc_STRVAR(posix_pread__doc__, |
| 7114 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7115 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7116 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7117 | |
| 7118 | static PyObject * |
| 7119 | posix_pread(PyObject *self, PyObject *args) |
| 7120 | { |
| 7121 | int fd, size; |
| 7122 | off_t offset; |
| 7123 | Py_ssize_t n; |
| 7124 | PyObject *buffer; |
| 7125 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7126 | return NULL; |
| 7127 | |
| 7128 | if (size < 0) { |
| 7129 | errno = EINVAL; |
| 7130 | return posix_error(); |
| 7131 | } |
| 7132 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7133 | if (buffer == NULL) |
| 7134 | return NULL; |
| 7135 | if (!_PyVerify_fd(fd)) { |
| 7136 | Py_DECREF(buffer); |
| 7137 | return posix_error(); |
| 7138 | } |
| 7139 | Py_BEGIN_ALLOW_THREADS |
| 7140 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7141 | Py_END_ALLOW_THREADS |
| 7142 | if (n < 0) { |
| 7143 | Py_DECREF(buffer); |
| 7144 | return posix_error(); |
| 7145 | } |
| 7146 | if (n != size) |
| 7147 | _PyBytes_Resize(&buffer, n); |
| 7148 | return buffer; |
| 7149 | } |
| 7150 | #endif |
| 7151 | |
| 7152 | PyDoc_STRVAR(posix_write__doc__, |
| 7153 | "write(fd, string) -> byteswritten\n\n\ |
| 7154 | Write a string to a file descriptor."); |
| 7155 | |
| 7156 | static PyObject * |
| 7157 | posix_write(PyObject *self, PyObject *args) |
| 7158 | { |
| 7159 | Py_buffer pbuf; |
| 7160 | int fd; |
| 7161 | Py_ssize_t size, len; |
| 7162 | |
| 7163 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7164 | return NULL; |
| 7165 | if (!_PyVerify_fd(fd)) { |
| 7166 | PyBuffer_Release(&pbuf); |
| 7167 | return posix_error(); |
| 7168 | } |
| 7169 | len = pbuf.len; |
| 7170 | Py_BEGIN_ALLOW_THREADS |
| 7171 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7172 | if (len > INT_MAX) |
| 7173 | len = INT_MAX; |
| 7174 | size = write(fd, pbuf.buf, (int)len); |
| 7175 | #else |
| 7176 | size = write(fd, pbuf.buf, len); |
| 7177 | #endif |
| 7178 | Py_END_ALLOW_THREADS |
| 7179 | PyBuffer_Release(&pbuf); |
| 7180 | if (size < 0) |
| 7181 | return posix_error(); |
| 7182 | return PyLong_FromSsize_t(size); |
| 7183 | } |
| 7184 | |
| 7185 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7186 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7187 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7188 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7189 | -> byteswritten\n\ |
| 7190 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7191 | |
| 7192 | static PyObject * |
| 7193 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7194 | { |
| 7195 | int in, out; |
| 7196 | Py_ssize_t ret; |
| 7197 | off_t offset; |
| 7198 | |
| 7199 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7200 | #ifndef __APPLE__ |
| 7201 | Py_ssize_t len; |
| 7202 | #endif |
| 7203 | PyObject *headers = NULL, *trailers = NULL; |
| 7204 | Py_buffer *hbuf, *tbuf; |
| 7205 | off_t sbytes; |
| 7206 | struct sf_hdtr sf; |
| 7207 | int flags = 0; |
| 7208 | sf.headers = NULL; |
| 7209 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7210 | static char *keywords[] = {"out", "in", |
| 7211 | "offset", "count", |
| 7212 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7213 | |
| 7214 | #ifdef __APPLE__ |
| 7215 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7216 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7217 | #else |
| 7218 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7219 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7220 | #endif |
| 7221 | &headers, &trailers, &flags)) |
| 7222 | return NULL; |
| 7223 | if (headers != NULL) { |
| 7224 | if (!PySequence_Check(headers)) { |
| 7225 | PyErr_SetString(PyExc_TypeError, |
| 7226 | "sendfile() headers must be a sequence or None"); |
| 7227 | return NULL; |
| 7228 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7229 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7230 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7231 | if (sf.hdr_cnt > 0 && |
| 7232 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7233 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7234 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7235 | #ifdef __APPLE__ |
| 7236 | sbytes += i; |
| 7237 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7238 | } |
| 7239 | } |
| 7240 | if (trailers != NULL) { |
| 7241 | if (!PySequence_Check(trailers)) { |
| 7242 | PyErr_SetString(PyExc_TypeError, |
| 7243 | "sendfile() trailers must be a sequence or None"); |
| 7244 | return NULL; |
| 7245 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7246 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7247 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7248 | if (sf.trl_cnt > 0 && |
| 7249 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7250 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7251 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7252 | #ifdef __APPLE__ |
| 7253 | sbytes += i; |
| 7254 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7255 | } |
| 7256 | } |
| 7257 | |
| 7258 | Py_BEGIN_ALLOW_THREADS |
| 7259 | #ifdef __APPLE__ |
| 7260 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7261 | #else |
| 7262 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7263 | #endif |
| 7264 | Py_END_ALLOW_THREADS |
| 7265 | |
| 7266 | if (sf.headers != NULL) |
| 7267 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7268 | if (sf.trailers != NULL) |
| 7269 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7270 | |
| 7271 | if (ret < 0) { |
| 7272 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7273 | if (sbytes != 0) { |
| 7274 | // some data has been sent |
| 7275 | goto done; |
| 7276 | } |
| 7277 | else { |
| 7278 | // no data has been sent; upper application is supposed |
| 7279 | // to retry on EAGAIN or EBUSY |
| 7280 | return posix_error(); |
| 7281 | } |
| 7282 | } |
| 7283 | return posix_error(); |
| 7284 | } |
| 7285 | goto done; |
| 7286 | |
| 7287 | done: |
| 7288 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7289 | return Py_BuildValue("l", sbytes); |
| 7290 | #else |
| 7291 | return Py_BuildValue("L", sbytes); |
| 7292 | #endif |
| 7293 | |
| 7294 | #else |
| 7295 | Py_ssize_t count; |
| 7296 | PyObject *offobj; |
| 7297 | static char *keywords[] = {"out", "in", |
| 7298 | "offset", "count", NULL}; |
| 7299 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7300 | keywords, &out, &in, &offobj, &count)) |
| 7301 | return NULL; |
| 7302 | #ifdef linux |
| 7303 | if (offobj == Py_None) { |
| 7304 | Py_BEGIN_ALLOW_THREADS |
| 7305 | ret = sendfile(out, in, NULL, count); |
| 7306 | Py_END_ALLOW_THREADS |
| 7307 | if (ret < 0) |
| 7308 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7309 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7310 | } |
| 7311 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7312 | if (!_parse_off_t(offobj, &offset)) |
| 7313 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7314 | Py_BEGIN_ALLOW_THREADS |
| 7315 | ret = sendfile(out, in, &offset, count); |
| 7316 | Py_END_ALLOW_THREADS |
| 7317 | if (ret < 0) |
| 7318 | return posix_error(); |
| 7319 | return Py_BuildValue("n", ret); |
| 7320 | #endif |
| 7321 | } |
| 7322 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7323 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7324 | PyDoc_STRVAR(posix_fstat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 7325 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7327 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7328 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 7329 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7330 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7331 | int fd; |
| 7332 | STRUCT_STAT st; |
| 7333 | int res; |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 7334 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7335 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7336 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7337 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7338 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7339 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | if (!_PyVerify_fd(fd)) |
| 7341 | return posix_error(); |
| 7342 | Py_BEGIN_ALLOW_THREADS |
| 7343 | res = FSTAT(fd, &st); |
| 7344 | Py_END_ALLOW_THREADS |
| 7345 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7346 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7347 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7348 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7349 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7350 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7351 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7352 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 7353 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7354 | } |
| 7355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7356 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7357 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7358 | 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] | 7359 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7360 | |
| 7361 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7362 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7364 | int fd; |
| 7365 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7366 | return NULL; |
| 7367 | if (!_PyVerify_fd(fd)) |
| 7368 | return PyBool_FromLong(0); |
| 7369 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7370 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7371 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7372 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7373 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7374 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7375 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7376 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7377 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7378 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7379 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7380 | #if defined(PYOS_OS2) |
| 7381 | HFILE read, write; |
| 7382 | APIRET rc; |
| 7383 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7384 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7385 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7386 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7387 | |
| 7388 | return Py_BuildValue("(ii)", read, write); |
| 7389 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7390 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7391 | int fds[2]; |
| 7392 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7393 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7394 | if (res != 0) |
| 7395 | return posix_error(); |
| 7396 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7397 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7398 | HANDLE read, write; |
| 7399 | int read_fd, write_fd; |
| 7400 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7401 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7402 | if (!ok) |
| 7403 | return win32_error("CreatePipe", NULL); |
| 7404 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7405 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7406 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7407 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7408 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7409 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7410 | #endif /* HAVE_PIPE */ |
| 7411 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7412 | #ifdef HAVE_PIPE2 |
| 7413 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7414 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7415 | Create a pipe with flags set atomically.\n\ |
| 7416 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7417 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7418 | "); |
| 7419 | |
| 7420 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7421 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7422 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7423 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7424 | int fds[2]; |
| 7425 | int res; |
| 7426 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7427 | flags = PyLong_AsLong(arg); |
| 7428 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7429 | return NULL; |
| 7430 | |
| 7431 | res = pipe2(fds, flags); |
| 7432 | if (res != 0) |
| 7433 | return posix_error(); |
| 7434 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7435 | } |
| 7436 | #endif /* HAVE_PIPE2 */ |
| 7437 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7438 | #ifdef HAVE_WRITEV |
| 7439 | PyDoc_STRVAR(posix_writev__doc__, |
| 7440 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7441 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7442 | arbitrary sequence of buffers.\n\ |
| 7443 | Returns the total bytes written."); |
| 7444 | |
| 7445 | static PyObject * |
| 7446 | posix_writev(PyObject *self, PyObject *args) |
| 7447 | { |
| 7448 | int fd, cnt; |
| 7449 | Py_ssize_t res; |
| 7450 | PyObject *seq; |
| 7451 | struct iovec *iov; |
| 7452 | Py_buffer *buf; |
| 7453 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7454 | return NULL; |
| 7455 | if (!PySequence_Check(seq)) { |
| 7456 | PyErr_SetString(PyExc_TypeError, |
| 7457 | "writev() arg 2 must be a sequence"); |
| 7458 | return NULL; |
| 7459 | } |
| 7460 | cnt = PySequence_Size(seq); |
| 7461 | |
| 7462 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7463 | return NULL; |
| 7464 | } |
| 7465 | |
| 7466 | Py_BEGIN_ALLOW_THREADS |
| 7467 | res = writev(fd, iov, cnt); |
| 7468 | Py_END_ALLOW_THREADS |
| 7469 | |
| 7470 | iov_cleanup(iov, buf, cnt); |
| 7471 | return PyLong_FromSsize_t(res); |
| 7472 | } |
| 7473 | #endif |
| 7474 | |
| 7475 | #ifdef HAVE_PWRITE |
| 7476 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7477 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7478 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7479 | offset unchanged."); |
| 7480 | |
| 7481 | static PyObject * |
| 7482 | posix_pwrite(PyObject *self, PyObject *args) |
| 7483 | { |
| 7484 | Py_buffer pbuf; |
| 7485 | int fd; |
| 7486 | off_t offset; |
| 7487 | Py_ssize_t size; |
| 7488 | |
| 7489 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7490 | return NULL; |
| 7491 | |
| 7492 | if (!_PyVerify_fd(fd)) { |
| 7493 | PyBuffer_Release(&pbuf); |
| 7494 | return posix_error(); |
| 7495 | } |
| 7496 | Py_BEGIN_ALLOW_THREADS |
| 7497 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7498 | Py_END_ALLOW_THREADS |
| 7499 | PyBuffer_Release(&pbuf); |
| 7500 | if (size < 0) |
| 7501 | return posix_error(); |
| 7502 | return PyLong_FromSsize_t(size); |
| 7503 | } |
| 7504 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7505 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7506 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7507 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7508 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7509 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7510 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7511 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7512 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7513 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7514 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7515 | char *filename; |
| 7516 | int mode = 0666; |
| 7517 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7518 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7519 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7520 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7521 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7522 | Py_BEGIN_ALLOW_THREADS |
| 7523 | res = mkfifo(filename, mode); |
| 7524 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7525 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | if (res < 0) |
| 7527 | return posix_error(); |
| 7528 | Py_INCREF(Py_None); |
| 7529 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7530 | } |
| 7531 | #endif |
| 7532 | |
| 7533 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7534 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7535 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7536 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7537 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7538 | named filename. mode specifies both the permissions to use and the\n\ |
| 7539 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7540 | 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] | 7541 | device defines the newly created device special file (probably using\n\ |
| 7542 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7543 | |
| 7544 | |
| 7545 | static PyObject * |
| 7546 | posix_mknod(PyObject *self, PyObject *args) |
| 7547 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7548 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | char *filename; |
| 7550 | int mode = 0600; |
| 7551 | int device = 0; |
| 7552 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7553 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7554 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7555 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7556 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7557 | Py_BEGIN_ALLOW_THREADS |
| 7558 | res = mknod(filename, mode, device); |
| 7559 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7560 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7561 | if (res < 0) |
| 7562 | return posix_error(); |
| 7563 | Py_INCREF(Py_None); |
| 7564 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7565 | } |
| 7566 | #endif |
| 7567 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7568 | #ifdef HAVE_DEVICE_MACROS |
| 7569 | PyDoc_STRVAR(posix_major__doc__, |
| 7570 | "major(device) -> major number\n\ |
| 7571 | Extracts a device major number from a raw device number."); |
| 7572 | |
| 7573 | static PyObject * |
| 7574 | posix_major(PyObject *self, PyObject *args) |
| 7575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7576 | int device; |
| 7577 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7578 | return NULL; |
| 7579 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
| 7582 | PyDoc_STRVAR(posix_minor__doc__, |
| 7583 | "minor(device) -> minor number\n\ |
| 7584 | Extracts a device minor number from a raw device number."); |
| 7585 | |
| 7586 | static PyObject * |
| 7587 | posix_minor(PyObject *self, PyObject *args) |
| 7588 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7589 | int device; |
| 7590 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7591 | return NULL; |
| 7592 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7593 | } |
| 7594 | |
| 7595 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7596 | "makedev(major, minor) -> device number\n\ |
| 7597 | Composes a raw device number from the major and minor device numbers."); |
| 7598 | |
| 7599 | static PyObject * |
| 7600 | posix_makedev(PyObject *self, PyObject *args) |
| 7601 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7602 | int major, minor; |
| 7603 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7604 | return NULL; |
| 7605 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7606 | } |
| 7607 | #endif /* device macros */ |
| 7608 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7609 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7610 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7611 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7612 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7613 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7614 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7615 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7616 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7618 | int fd; |
| 7619 | off_t length; |
| 7620 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7621 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7622 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7623 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7624 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7625 | Py_BEGIN_ALLOW_THREADS |
| 7626 | res = ftruncate(fd, length); |
| 7627 | Py_END_ALLOW_THREADS |
| 7628 | if (res < 0) |
| 7629 | return posix_error(); |
| 7630 | Py_INCREF(Py_None); |
| 7631 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7632 | } |
| 7633 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7634 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7635 | #ifdef HAVE_TRUNCATE |
| 7636 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7637 | "truncate(path, length)\n\n\ |
| 7638 | Truncate the file given by path to length bytes."); |
| 7639 | |
| 7640 | static PyObject * |
| 7641 | posix_truncate(PyObject *self, PyObject *args) |
| 7642 | { |
| 7643 | PyObject *opath; |
| 7644 | const char *path; |
| 7645 | off_t length; |
| 7646 | int res; |
| 7647 | |
| 7648 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7649 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7650 | return NULL; |
| 7651 | path = PyBytes_AsString(opath); |
| 7652 | |
| 7653 | Py_BEGIN_ALLOW_THREADS |
| 7654 | res = truncate(path, length); |
| 7655 | Py_END_ALLOW_THREADS |
| 7656 | Py_DECREF(opath); |
| 7657 | if (res < 0) |
| 7658 | return posix_error(); |
| 7659 | Py_RETURN_NONE; |
| 7660 | } |
| 7661 | #endif |
| 7662 | |
| 7663 | #ifdef HAVE_POSIX_FALLOCATE |
| 7664 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7665 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7666 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7667 | starting from offset and continuing for len bytes."); |
| 7668 | |
| 7669 | static PyObject * |
| 7670 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7671 | { |
| 7672 | off_t len, offset; |
| 7673 | int res, fd; |
| 7674 | |
| 7675 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7676 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7677 | return NULL; |
| 7678 | |
| 7679 | Py_BEGIN_ALLOW_THREADS |
| 7680 | res = posix_fallocate(fd, offset, len); |
| 7681 | Py_END_ALLOW_THREADS |
| 7682 | if (res != 0) { |
| 7683 | errno = res; |
| 7684 | return posix_error(); |
| 7685 | } |
| 7686 | Py_RETURN_NONE; |
| 7687 | } |
| 7688 | #endif |
| 7689 | |
| 7690 | #ifdef HAVE_POSIX_FADVISE |
| 7691 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7692 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7693 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7694 | the kernel to make optimizations.\n\ |
| 7695 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7696 | offset and continuing for len bytes.\n\ |
| 7697 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7698 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7699 | POSIX_FADV_DONTNEED."); |
| 7700 | |
| 7701 | static PyObject * |
| 7702 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7703 | { |
| 7704 | off_t len, offset; |
| 7705 | int res, fd, advice; |
| 7706 | |
| 7707 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7708 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7709 | return NULL; |
| 7710 | |
| 7711 | Py_BEGIN_ALLOW_THREADS |
| 7712 | res = posix_fadvise(fd, offset, len, advice); |
| 7713 | Py_END_ALLOW_THREADS |
| 7714 | if (res != 0) { |
| 7715 | errno = res; |
| 7716 | return posix_error(); |
| 7717 | } |
| 7718 | Py_RETURN_NONE; |
| 7719 | } |
| 7720 | #endif |
| 7721 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7722 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7723 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7724 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7725 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7726 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7727 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7728 | * get re-set with another call for the same key. */ |
| 7729 | static PyObject *posix_putenv_garbage; |
| 7730 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7731 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7732 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7733 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7734 | PyObject *newstr = NULL; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7735 | #ifdef MS_WINDOWS |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7736 | PyObject *os1, *os2; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7737 | wchar_t *newenv; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7739 | if (!PyArg_ParseTuple(args, |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7740 | "UU:putenv", |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7741 | &os1, &os2)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7742 | return NULL; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7743 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7744 | newstr = PyUnicode_FromFormat("%U=%U", os1, os2); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7745 | if (newstr == NULL) { |
| 7746 | PyErr_NoMemory(); |
| 7747 | goto error; |
| 7748 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7749 | if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) { |
| 7750 | PyErr_Format(PyExc_ValueError, |
| 7751 | "the environment variable is longer than %u characters", |
| 7752 | _MAX_ENV); |
| 7753 | goto error; |
| 7754 | } |
| 7755 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7756 | newenv = PyUnicode_AsUnicode(newstr); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 7757 | if (newenv == NULL) |
| 7758 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7759 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7760 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7761 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7762 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7763 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7764 | PyObject *os1, *os2; |
| 7765 | char *s1, *s2; |
| 7766 | char *newenv; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7767 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7768 | if (!PyArg_ParseTuple(args, |
| 7769 | "O&O&:putenv", |
| 7770 | PyUnicode_FSConverter, &os1, |
| 7771 | PyUnicode_FSConverter, &os2)) |
| 7772 | return NULL; |
| 7773 | s1 = PyBytes_AsString(os1); |
| 7774 | s2 = PyBytes_AsString(os2); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7775 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7776 | newstr = PyBytes_FromFormat("%s=%s", s1, s2); |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 7777 | if (newstr == NULL) { |
| 7778 | PyErr_NoMemory(); |
| 7779 | goto error; |
| 7780 | } |
| 7781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7782 | newenv = PyBytes_AS_STRING(newstr); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | if (putenv(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 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7788 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7789 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7790 | * this will cause previous value to be collected. This has to |
| 7791 | * happen after the real putenv() call because the old value |
| 7792 | * was still accessible until then. */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7793 | if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7794 | /* really not much we can do; just leak */ |
| 7795 | PyErr_Clear(); |
| 7796 | } |
| 7797 | else { |
| 7798 | Py_DECREF(newstr); |
| 7799 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7800 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7801 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7802 | Py_DECREF(os1); |
| 7803 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7804 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7805 | Py_RETURN_NONE; |
| 7806 | |
| 7807 | error: |
| 7808 | #ifndef MS_WINDOWS |
| 7809 | Py_DECREF(os1); |
| 7810 | Py_DECREF(os2); |
| 7811 | #endif |
| 7812 | Py_XDECREF(newstr); |
| 7813 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7814 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7815 | #endif /* putenv */ |
| 7816 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7817 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7818 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7819 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7820 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7821 | |
| 7822 | static PyObject * |
| 7823 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7824 | { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7825 | PyObject *name; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7826 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7827 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7828 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7829 | |
| 7830 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7831 | |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7832 | PyUnicode_FSConverter, &name)) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7833 | return NULL; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7834 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7835 | #ifdef HAVE_BROKEN_UNSETENV |
| 7836 | unsetenv(PyBytes_AS_STRING(name)); |
| 7837 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7838 | err = unsetenv(PyBytes_AS_STRING(name)); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7839 | if (err) { |
Benjamin Peterson | e8eb0e8 | 2011-11-22 23:14:47 -0600 | [diff] [blame] | 7840 | Py_DECREF(name); |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 7841 | return posix_error(); |
Benjamin Peterson | 4bb867d | 2011-11-22 23:12:49 -0600 | [diff] [blame] | 7842 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 7843 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7845 | /* Remove the key from posix_putenv_garbage; |
| 7846 | * this will cause it to be collected. This has to |
| 7847 | * happen after the real unsetenv() call because the |
| 7848 | * old value was still accessible until then. |
| 7849 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7850 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7851 | /* really not much we can do; just leak */ |
| 7852 | PyErr_Clear(); |
| 7853 | } |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 7854 | Py_DECREF(name); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7855 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7856 | } |
| 7857 | #endif /* unsetenv */ |
| 7858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7859 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7860 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7861 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7862 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7863 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7864 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7865 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7866 | int code; |
| 7867 | char *message; |
| 7868 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7869 | return NULL; |
| 7870 | message = strerror(code); |
| 7871 | if (message == NULL) { |
| 7872 | PyErr_SetString(PyExc_ValueError, |
| 7873 | "strerror() argument out of range"); |
| 7874 | return NULL; |
| 7875 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 7876 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7877 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7878 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7879 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7880 | #ifdef HAVE_SYS_WAIT_H |
| 7881 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7882 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7883 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7884 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7885 | 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] | 7886 | |
| 7887 | static PyObject * |
| 7888 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7889 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7890 | WAIT_TYPE status; |
| 7891 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7893 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7894 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7895 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7897 | } |
| 7898 | #endif /* WCOREDUMP */ |
| 7899 | |
| 7900 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7901 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7902 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7903 | 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] | 7904 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7905 | |
| 7906 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7907 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7908 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7909 | WAIT_TYPE status; |
| 7910 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7912 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7913 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7914 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7915 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7916 | } |
| 7917 | #endif /* WIFCONTINUED */ |
| 7918 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7919 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7920 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7921 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7922 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7923 | |
| 7924 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7925 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7926 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7927 | WAIT_TYPE status; |
| 7928 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7929 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7930 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7931 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7933 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7934 | } |
| 7935 | #endif /* WIFSTOPPED */ |
| 7936 | |
| 7937 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7938 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7939 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7940 | 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] | 7941 | |
| 7942 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7943 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7944 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7945 | WAIT_TYPE status; |
| 7946 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7947 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7948 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7949 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7950 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7951 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7952 | } |
| 7953 | #endif /* WIFSIGNALED */ |
| 7954 | |
| 7955 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7956 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7957 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7958 | 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] | 7959 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7960 | |
| 7961 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7962 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7963 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7964 | WAIT_TYPE status; |
| 7965 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7967 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7968 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7969 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7970 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7971 | } |
| 7972 | #endif /* WIFEXITED */ |
| 7973 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7974 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7975 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7976 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7977 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7978 | |
| 7979 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7980 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7981 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | WAIT_TYPE status; |
| 7983 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7984 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7986 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7987 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7988 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7989 | } |
| 7990 | #endif /* WEXITSTATUS */ |
| 7991 | |
| 7992 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7993 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7994 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7995 | 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] | 7996 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7997 | |
| 7998 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7999 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8000 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8001 | WAIT_TYPE status; |
| 8002 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8004 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 8005 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8008 | } |
| 8009 | #endif /* WTERMSIG */ |
| 8010 | |
| 8011 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8012 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8013 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8014 | Return the signal that stopped the process that provided\n\ |
| 8015 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8016 | |
| 8017 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8018 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8019 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8020 | WAIT_TYPE status; |
| 8021 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8022 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8023 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 8024 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8025 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8026 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 8027 | } |
| 8028 | #endif /* WSTOPSIG */ |
| 8029 | |
| 8030 | #endif /* HAVE_SYS_WAIT_H */ |
| 8031 | |
| 8032 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8033 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 8034 | #ifdef _SCO_DS |
| 8035 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 8036 | needed definitions in sys/statvfs.h */ |
| 8037 | #define _SVID3 |
| 8038 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8039 | #include <sys/statvfs.h> |
| 8040 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8041 | static PyObject* |
| 8042 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8043 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8044 | if (v == NULL) |
| 8045 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8046 | |
| 8047 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8048 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8049 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8050 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8051 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8052 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8053 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8054 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8055 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8056 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8057 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8058 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8059 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8060 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8061 | PyStructSequence_SET_ITEM(v, 2, |
| 8062 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8063 | PyStructSequence_SET_ITEM(v, 3, |
| 8064 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8065 | PyStructSequence_SET_ITEM(v, 4, |
| 8066 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8067 | PyStructSequence_SET_ITEM(v, 5, |
| 8068 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8069 | PyStructSequence_SET_ITEM(v, 6, |
| 8070 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8071 | PyStructSequence_SET_ITEM(v, 7, |
| 8072 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8073 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8074 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8075 | #endif |
| 8076 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8077 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8078 | } |
| 8079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8080 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8081 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8082 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8083 | |
| 8084 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8085 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8086 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8087 | int fd, res; |
| 8088 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8090 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8091 | return NULL; |
| 8092 | Py_BEGIN_ALLOW_THREADS |
| 8093 | res = fstatvfs(fd, &st); |
| 8094 | Py_END_ALLOW_THREADS |
| 8095 | if (res != 0) |
| 8096 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8098 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8099 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8100 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8101 | |
| 8102 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8103 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8104 | #include <sys/statvfs.h> |
| 8105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8106 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8107 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8108 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8109 | |
| 8110 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8111 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8112 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8113 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8114 | int res; |
| 8115 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8116 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8117 | return NULL; |
| 8118 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8119 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8120 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8121 | if (res != 0) { |
| 8122 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8123 | Py_DECREF(path); |
| 8124 | return NULL; |
| 8125 | } |
| 8126 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8127 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8128 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8129 | } |
| 8130 | #endif /* HAVE_STATVFS */ |
| 8131 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8132 | #ifdef MS_WINDOWS |
| 8133 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8134 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8135 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8136 | |
| 8137 | static PyObject * |
| 8138 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8139 | { |
| 8140 | BOOL retval; |
| 8141 | ULARGE_INTEGER _, total, free; |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8142 | const wchar_t *path; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8143 | |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8144 | if (! PyArg_ParseTuple(args, "u", &path)) |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8145 | return NULL; |
| 8146 | |
| 8147 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 8148 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8149 | Py_END_ALLOW_THREADS |
| 8150 | if (retval == 0) |
| 8151 | return PyErr_SetFromWindowsErr(0); |
| 8152 | |
| 8153 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8154 | } |
| 8155 | #endif |
| 8156 | |
| 8157 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8158 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8159 | * It maps strings representing configuration variable names to |
| 8160 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8161 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8162 | * rarely-used constants. There are three separate tables that use |
| 8163 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8164 | * |
| 8165 | * This code is always included, even if none of the interfaces that |
| 8166 | * need it are included. The #if hackery needed to avoid it would be |
| 8167 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8168 | */ |
| 8169 | struct constdef { |
| 8170 | char *name; |
| 8171 | long value; |
| 8172 | }; |
| 8173 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8174 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8175 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8176 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8177 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8178 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8179 | *valuep = PyLong_AS_LONG(arg); |
| 8180 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8181 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8182 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8183 | /* look up the value in the table using a binary search */ |
| 8184 | size_t lo = 0; |
| 8185 | size_t mid; |
| 8186 | size_t hi = tablesize; |
| 8187 | int cmp; |
| 8188 | const char *confname; |
| 8189 | if (!PyUnicode_Check(arg)) { |
| 8190 | PyErr_SetString(PyExc_TypeError, |
| 8191 | "configuration names must be strings or integers"); |
| 8192 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8193 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8194 | confname = _PyUnicode_AsString(arg); |
| 8195 | if (confname == NULL) |
| 8196 | return 0; |
| 8197 | while (lo < hi) { |
| 8198 | mid = (lo + hi) / 2; |
| 8199 | cmp = strcmp(confname, table[mid].name); |
| 8200 | if (cmp < 0) |
| 8201 | hi = mid; |
| 8202 | else if (cmp > 0) |
| 8203 | lo = mid + 1; |
| 8204 | else { |
| 8205 | *valuep = table[mid].value; |
| 8206 | return 1; |
| 8207 | } |
| 8208 | } |
| 8209 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8210 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8211 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8212 | } |
| 8213 | |
| 8214 | |
| 8215 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8216 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8217 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8218 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8219 | #endif |
| 8220 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8221 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8222 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8223 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8224 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8225 | #endif |
| 8226 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8227 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8228 | #endif |
| 8229 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8230 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8231 | #endif |
| 8232 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8233 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8234 | #endif |
| 8235 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8236 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8237 | #endif |
| 8238 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8239 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8240 | #endif |
| 8241 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8242 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8243 | #endif |
| 8244 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8245 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8246 | #endif |
| 8247 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8248 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8249 | #endif |
| 8250 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8251 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8252 | #endif |
| 8253 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8254 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8255 | #endif |
| 8256 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8257 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8258 | #endif |
| 8259 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8260 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8261 | #endif |
| 8262 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8263 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8264 | #endif |
| 8265 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8266 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8267 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8268 | #ifdef _PC_ACL_ENABLED |
| 8269 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8270 | #endif |
| 8271 | #ifdef _PC_MIN_HOLE_SIZE |
| 8272 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8273 | #endif |
| 8274 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8275 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8276 | #endif |
| 8277 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8278 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8279 | #endif |
| 8280 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8281 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8282 | #endif |
| 8283 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8284 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8285 | #endif |
| 8286 | #ifdef _PC_REC_XFER_ALIGN |
| 8287 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8288 | #endif |
| 8289 | #ifdef _PC_SYMLINK_MAX |
| 8290 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8291 | #endif |
| 8292 | #ifdef _PC_XATTR_ENABLED |
| 8293 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8294 | #endif |
| 8295 | #ifdef _PC_XATTR_EXISTS |
| 8296 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8297 | #endif |
| 8298 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8299 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8300 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8301 | }; |
| 8302 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8303 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8304 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8305 | { |
| 8306 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8307 | sizeof(posix_constants_pathconf) |
| 8308 | / sizeof(struct constdef)); |
| 8309 | } |
| 8310 | #endif |
| 8311 | |
| 8312 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8313 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8314 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8315 | 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] | 8316 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8317 | |
| 8318 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8319 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8320 | { |
| 8321 | PyObject *result = NULL; |
| 8322 | int name, fd; |
| 8323 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8324 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8325 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8326 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8327 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8328 | errno = 0; |
| 8329 | limit = fpathconf(fd, name); |
| 8330 | if (limit == -1 && errno != 0) |
| 8331 | posix_error(); |
| 8332 | else |
| 8333 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8334 | } |
| 8335 | return result; |
| 8336 | } |
| 8337 | #endif |
| 8338 | |
| 8339 | |
| 8340 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8341 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8342 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8343 | 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] | 8344 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | |
| 8346 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8347 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | { |
| 8349 | PyObject *result = NULL; |
| 8350 | int name; |
| 8351 | char *path; |
| 8352 | |
| 8353 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8354 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8355 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8357 | errno = 0; |
| 8358 | limit = pathconf(path, name); |
| 8359 | if (limit == -1 && errno != 0) { |
| 8360 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8361 | /* could be a path or name problem */ |
| 8362 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8363 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8364 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8365 | } |
| 8366 | else |
| 8367 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8368 | } |
| 8369 | return result; |
| 8370 | } |
| 8371 | #endif |
| 8372 | |
| 8373 | #ifdef HAVE_CONFSTR |
| 8374 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8375 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8376 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8377 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8378 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8379 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8380 | #endif |
| 8381 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8382 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8383 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8384 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8385 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8386 | #endif |
| 8387 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8388 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8389 | #endif |
| 8390 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8391 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8392 | #endif |
| 8393 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8394 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8395 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8396 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8397 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8398 | #endif |
| 8399 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8401 | #endif |
| 8402 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8404 | #endif |
| 8405 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8407 | #endif |
| 8408 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8410 | #endif |
| 8411 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8413 | #endif |
| 8414 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8415 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8416 | #endif |
| 8417 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8419 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8420 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8421 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8422 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8423 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8425 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8426 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8428 | #endif |
| 8429 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8431 | #endif |
| 8432 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8433 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8434 | #endif |
| 8435 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8436 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8437 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8438 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8440 | #endif |
| 8441 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8442 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8443 | #endif |
| 8444 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8445 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8446 | #endif |
| 8447 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8448 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8449 | #endif |
| 8450 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8451 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8452 | #endif |
| 8453 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8454 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8455 | #endif |
| 8456 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8458 | #endif |
| 8459 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8461 | #endif |
| 8462 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8463 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8464 | #endif |
| 8465 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8467 | #endif |
| 8468 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8470 | #endif |
| 8471 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8473 | #endif |
| 8474 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8475 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8476 | #endif |
| 8477 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8479 | #endif |
| 8480 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8482 | #endif |
| 8483 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8484 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8485 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8486 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8487 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8488 | #endif |
| 8489 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8490 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8491 | #endif |
| 8492 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8494 | #endif |
| 8495 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8496 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8497 | #endif |
| 8498 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8500 | #endif |
| 8501 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8502 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8503 | #endif |
| 8504 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8505 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8506 | #endif |
| 8507 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8508 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8509 | #endif |
| 8510 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8511 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8512 | #endif |
| 8513 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8515 | #endif |
| 8516 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8517 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8518 | #endif |
| 8519 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8520 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8521 | #endif |
| 8522 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8523 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8524 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8525 | }; |
| 8526 | |
| 8527 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8528 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8529 | { |
| 8530 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8531 | sizeof(posix_constants_confstr) |
| 8532 | / sizeof(struct constdef)); |
| 8533 | } |
| 8534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8535 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8536 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8537 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8538 | |
| 8539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8540 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8541 | { |
| 8542 | PyObject *result = NULL; |
| 8543 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8544 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8545 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8546 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8547 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8548 | return NULL; |
| 8549 | |
| 8550 | errno = 0; |
| 8551 | len = confstr(name, buffer, sizeof(buffer)); |
| 8552 | if (len == 0) { |
| 8553 | if (errno) { |
| 8554 | posix_error(); |
| 8555 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8556 | } |
| 8557 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8558 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8559 | } |
| 8560 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8561 | |
| 8562 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8563 | char *buf = PyMem_Malloc(len); |
| 8564 | if (buf == NULL) |
| 8565 | return PyErr_NoMemory(); |
| 8566 | confstr(name, buf, len); |
| 8567 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8568 | PyMem_Free(buf); |
| 8569 | } |
| 8570 | else |
| 8571 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8572 | return result; |
| 8573 | } |
| 8574 | #endif |
| 8575 | |
| 8576 | |
| 8577 | #ifdef HAVE_SYSCONF |
| 8578 | static struct constdef posix_constants_sysconf[] = { |
| 8579 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8581 | #endif |
| 8582 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8584 | #endif |
| 8585 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8586 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8587 | #endif |
| 8588 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8589 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8590 | #endif |
| 8591 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8593 | #endif |
| 8594 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8595 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8596 | #endif |
| 8597 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8599 | #endif |
| 8600 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8602 | #endif |
| 8603 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8609 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8611 | #endif |
| 8612 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8614 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8615 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8617 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8618 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8619 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8622 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | #endif |
| 8627 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8629 | #endif |
| 8630 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
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_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8635 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8636 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8638 | #endif |
| 8639 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8640 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8641 | #endif |
| 8642 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8643 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8644 | #endif |
| 8645 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8646 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | #endif |
| 8648 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8649 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8650 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8651 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8653 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8654 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8655 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8656 | #endif |
| 8657 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8659 | #endif |
| 8660 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8662 | #endif |
| 8663 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
| 8672 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8674 | #endif |
| 8675 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
| 8684 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8686 | #endif |
| 8687 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
| 8696 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8698 | #endif |
| 8699 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
| 8702 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8707 | #endif |
| 8708 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
| 8717 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8719 | #endif |
| 8720 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8722 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8723 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8725 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8726 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | #endif |
| 8729 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8734 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8735 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8737 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8738 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8739 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8740 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8741 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8742 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8743 | #endif |
| 8744 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8746 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8747 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #endif |
| 8750 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8752 | #endif |
| 8753 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8755 | #endif |
| 8756 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
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_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"SC_MAC", _SC_MAC}, |
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_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8764 | #endif |
| 8765 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8767 | #endif |
| 8768 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8770 | #endif |
| 8771 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | #endif |
| 8774 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #endif |
| 8777 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8778 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8779 | #endif |
| 8780 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
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_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
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_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8788 | #endif |
| 8789 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8791 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8792 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8793 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8794 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8795 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #endif |
| 8798 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8799 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8800 | #endif |
| 8801 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #endif |
| 8804 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8805 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8806 | #endif |
| 8807 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8809 | #endif |
| 8810 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8811 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8812 | #endif |
| 8813 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8814 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8815 | #endif |
| 8816 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8818 | #endif |
| 8819 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8821 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8822 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8824 | #endif |
| 8825 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8827 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8828 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8830 | #endif |
| 8831 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8832 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8836 | #endif |
| 8837 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | #endif |
| 8843 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8845 | #endif |
| 8846 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8848 | #endif |
| 8849 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8851 | #endif |
| 8852 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8853 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8854 | #endif |
| 8855 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8857 | #endif |
| 8858 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8862 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8866 | #endif |
| 8867 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8869 | #endif |
| 8870 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8871 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8872 | #endif |
| 8873 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8875 | #endif |
| 8876 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8877 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8878 | #endif |
| 8879 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8881 | #endif |
| 8882 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | #endif |
| 8885 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #endif |
| 8894 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8899 | #endif |
| 8900 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | #endif |
| 8906 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8908 | #endif |
| 8909 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8911 | #endif |
| 8912 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8914 | #endif |
| 8915 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8916 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8920 | #endif |
| 8921 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8922 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8925 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8929 | #endif |
| 8930 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8933 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8934 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8935 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8936 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8937 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8938 | #endif |
| 8939 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8941 | #endif |
| 8942 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8947 | #endif |
| 8948 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8950 | #endif |
| 8951 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8953 | #endif |
| 8954 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8955 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8956 | #endif |
| 8957 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8959 | #endif |
| 8960 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | #endif |
| 8972 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8973 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #endif |
| 8975 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #endif |
| 8978 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | #endif |
| 8981 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8985 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8986 | #endif |
| 8987 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8992 | #endif |
| 8993 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #endif |
| 8996 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #endif |
| 8999 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | #endif |
| 9002 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9012 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #endif |
| 9017 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9018 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | #endif |
| 9020 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9022 | #endif |
| 9023 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9030 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | #endif |
| 9032 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9033 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9034 | #endif |
| 9035 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9036 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9037 | #endif |
| 9038 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9039 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9040 | #endif |
| 9041 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9042 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9043 | #endif |
| 9044 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9046 | #endif |
| 9047 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9048 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9049 | #endif |
| 9050 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9051 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9052 | #endif |
| 9053 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9054 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9055 | #endif |
| 9056 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9057 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9058 | #endif |
| 9059 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9060 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9061 | #endif |
| 9062 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9063 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9064 | #endif |
| 9065 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9066 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9067 | #endif |
| 9068 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9069 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9070 | #endif |
| 9071 | }; |
| 9072 | |
| 9073 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9074 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9075 | { |
| 9076 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9077 | sizeof(posix_constants_sysconf) |
| 9078 | / sizeof(struct constdef)); |
| 9079 | } |
| 9080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9081 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9082 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9083 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9084 | |
| 9085 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9086 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9087 | { |
| 9088 | PyObject *result = NULL; |
| 9089 | int name; |
| 9090 | |
| 9091 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9092 | int value; |
| 9093 | |
| 9094 | errno = 0; |
| 9095 | value = sysconf(name); |
| 9096 | if (value == -1 && errno != 0) |
| 9097 | posix_error(); |
| 9098 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9099 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9100 | } |
| 9101 | return result; |
| 9102 | } |
| 9103 | #endif |
| 9104 | |
| 9105 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9106 | /* This code is used to ensure that the tables of configuration value names |
| 9107 | * are in sorted order as required by conv_confname(), and also to build the |
| 9108 | * the exported dictionaries that are used to publish information about the |
| 9109 | * names available on the host platform. |
| 9110 | * |
| 9111 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9112 | * when used, even for platforms we're not able to test on. It also makes |
| 9113 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9114 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9115 | |
| 9116 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9117 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9118 | { |
| 9119 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9120 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9121 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9123 | |
| 9124 | return strcmp(c1->name, c2->name); |
| 9125 | } |
| 9126 | |
| 9127 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9128 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9129 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9130 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9131 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9132 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9133 | |
| 9134 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9135 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9136 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9137 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9138 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9139 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9140 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9141 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9142 | Py_XDECREF(o); |
| 9143 | Py_DECREF(d); |
| 9144 | return -1; |
| 9145 | } |
| 9146 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9147 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9148 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9149 | } |
| 9150 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9151 | /* Return -1 on failure, 0 on success. */ |
| 9152 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9153 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9154 | { |
| 9155 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9156 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9157 | sizeof(posix_constants_pathconf) |
| 9158 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9159 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9160 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9161 | #endif |
| 9162 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9163 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9164 | sizeof(posix_constants_confstr) |
| 9165 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9166 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9167 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9168 | #endif |
| 9169 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9170 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9171 | sizeof(posix_constants_sysconf) |
| 9172 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9173 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9174 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9175 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9176 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9177 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9178 | |
| 9179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9180 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9181 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9182 | 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] | 9183 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9184 | |
| 9185 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9186 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9187 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9188 | abort(); |
| 9189 | /*NOTREACHED*/ |
| 9190 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9191 | return NULL; |
| 9192 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9193 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9194 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9195 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9196 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9197 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9198 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9199 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9200 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9201 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9202 | application (if any) its extension is associated.\n\ |
| 9203 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9204 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9205 | \n\ |
| 9206 | startfile returns as soon as the associated application is launched.\n\ |
| 9207 | There is no option to wait for the application to close, and no way\n\ |
| 9208 | to retrieve the application's exit status.\n\ |
| 9209 | \n\ |
| 9210 | The filepath is relative to the current directory. If you want to use\n\ |
| 9211 | 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] | 9212 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9213 | |
| 9214 | static PyObject * |
| 9215 | win32_startfile(PyObject *self, PyObject *args) |
| 9216 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9217 | PyObject *ofilepath; |
| 9218 | char *filepath; |
| 9219 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9220 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9221 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9222 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9223 | PyObject *unipath, *uoperation = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9224 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9225 | &unipath, &operation)) { |
| 9226 | PyErr_Clear(); |
| 9227 | goto normal; |
| 9228 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9231 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9232 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9233 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9234 | PyErr_Clear(); |
| 9235 | operation = NULL; |
| 9236 | goto normal; |
| 9237 | } |
| 9238 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9239 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9240 | wpath = PyUnicode_AsUnicode(unipath); |
| 9241 | if (wpath == NULL) |
| 9242 | goto normal; |
| 9243 | if (uoperation) { |
| 9244 | woperation = PyUnicode_AsUnicode(uoperation); |
| 9245 | if (woperation == NULL) |
| 9246 | goto normal; |
| 9247 | } |
| 9248 | else |
| 9249 | woperation = NULL; |
| 9250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9252 | rc = ShellExecuteW((HWND)0, woperation, wpath, |
| 9253 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9254 | Py_END_ALLOW_THREADS |
| 9255 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9256 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9257 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 9258 | win32_error_object("startfile", unipath); |
| 9259 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9260 | } |
| 9261 | Py_INCREF(Py_None); |
| 9262 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9263 | |
| 9264 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9265 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9266 | PyUnicode_FSConverter, &ofilepath, |
| 9267 | &operation)) |
| 9268 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 9269 | if (win32_warn_bytes_api()) { |
| 9270 | Py_DECREF(ofilepath); |
| 9271 | return NULL; |
| 9272 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9273 | filepath = PyBytes_AsString(ofilepath); |
| 9274 | Py_BEGIN_ALLOW_THREADS |
| 9275 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9276 | NULL, NULL, SW_SHOWNORMAL); |
| 9277 | Py_END_ALLOW_THREADS |
| 9278 | if (rc <= (HINSTANCE)32) { |
| 9279 | PyObject *errval = win32_error("startfile", filepath); |
| 9280 | Py_DECREF(ofilepath); |
| 9281 | return errval; |
| 9282 | } |
| 9283 | Py_DECREF(ofilepath); |
| 9284 | Py_INCREF(Py_None); |
| 9285 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9286 | } |
| 9287 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9288 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9289 | #ifdef HAVE_GETLOADAVG |
| 9290 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9291 | "getloadavg() -> (float, float, float)\n\n\ |
| 9292 | Return the number of processes in the system run queue averaged over\n\ |
| 9293 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9294 | was unobtainable"); |
| 9295 | |
| 9296 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9297 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9298 | { |
| 9299 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9300 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9301 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9302 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9303 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9304 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9305 | } |
| 9306 | #endif |
| 9307 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9308 | #ifdef MS_WINDOWS |
| 9309 | |
| 9310 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9311 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9312 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9313 | |
| 9314 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9315 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9316 | DWORD dwFlags ); |
| 9317 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9318 | BYTE *pbBuffer ); |
| 9319 | |
| 9320 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9321 | /* This handle is never explicitly released. Instead, the operating |
| 9322 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9323 | static HCRYPTPROV hCryptProv = 0; |
| 9324 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9325 | static PyObject* |
| 9326 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9327 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9328 | int howMany; |
| 9329 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9330 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9331 | /* Read arguments */ |
| 9332 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9333 | return NULL; |
| 9334 | if (howMany < 0) |
| 9335 | return PyErr_Format(PyExc_ValueError, |
| 9336 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9337 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9338 | if (hCryptProv == 0) { |
| 9339 | HINSTANCE hAdvAPI32 = NULL; |
| 9340 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9342 | /* Obtain handle to the DLL containing CryptoAPI |
| 9343 | This should not fail */ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 9344 | hAdvAPI32 = GetModuleHandleW(L"advapi32.dll"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | if(hAdvAPI32 == NULL) |
| 9346 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9347 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9348 | /* Obtain pointers to the CryptoAPI functions |
| 9349 | This will fail on some early versions of Win95 */ |
| 9350 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9351 | hAdvAPI32, |
| 9352 | "CryptAcquireContextA"); |
| 9353 | if (pCryptAcquireContext == NULL) |
| 9354 | return PyErr_Format(PyExc_NotImplementedError, |
| 9355 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9356 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9357 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9358 | hAdvAPI32, "CryptGenRandom"); |
| 9359 | if (pCryptGenRandom == NULL) |
| 9360 | return PyErr_Format(PyExc_NotImplementedError, |
| 9361 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9363 | /* Acquire context */ |
| 9364 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9365 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9366 | return win32_error("CryptAcquireContext", NULL); |
| 9367 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | /* Allocate bytes */ |
| 9370 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9371 | if (result != NULL) { |
| 9372 | /* Get random data */ |
| 9373 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9374 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9375 | PyBytes_AS_STRING(result))) { |
| 9376 | Py_DECREF(result); |
| 9377 | return win32_error("CryptGenRandom", NULL); |
| 9378 | } |
| 9379 | } |
| 9380 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9381 | } |
| 9382 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9383 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9384 | PyDoc_STRVAR(device_encoding__doc__, |
| 9385 | "device_encoding(fd) -> str\n\n\ |
| 9386 | Return a string describing the encoding of the device\n\ |
| 9387 | if the output is a terminal; else return None."); |
| 9388 | |
| 9389 | static PyObject * |
| 9390 | device_encoding(PyObject *self, PyObject *args) |
| 9391 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9392 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9393 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9394 | UINT cp; |
| 9395 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9396 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9397 | return NULL; |
| 9398 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9399 | Py_INCREF(Py_None); |
| 9400 | return Py_None; |
| 9401 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9402 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9403 | if (fd == 0) |
| 9404 | cp = GetConsoleCP(); |
| 9405 | else if (fd == 1 || fd == 2) |
| 9406 | cp = GetConsoleOutputCP(); |
| 9407 | else |
| 9408 | cp = 0; |
| 9409 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9410 | has no console */ |
| 9411 | if (cp != 0) |
| 9412 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9413 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9414 | { |
| 9415 | char *codeset = nl_langinfo(CODESET); |
| 9416 | if (codeset != NULL && codeset[0] != 0) |
| 9417 | return PyUnicode_FromString(codeset); |
| 9418 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9419 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9420 | Py_INCREF(Py_None); |
| 9421 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9422 | } |
| 9423 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9424 | #ifdef __VMS |
| 9425 | /* Use openssl random routine */ |
| 9426 | #include <openssl/rand.h> |
| 9427 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9428 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9429 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9430 | |
| 9431 | static PyObject* |
| 9432 | vms_urandom(PyObject *self, PyObject *args) |
| 9433 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9434 | int howMany; |
| 9435 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9437 | /* Read arguments */ |
| 9438 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9439 | return NULL; |
| 9440 | if (howMany < 0) |
| 9441 | return PyErr_Format(PyExc_ValueError, |
| 9442 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9444 | /* Allocate bytes */ |
| 9445 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9446 | if (result != NULL) { |
| 9447 | /* Get random data */ |
| 9448 | if (RAND_pseudo_bytes((unsigned char*) |
| 9449 | PyBytes_AS_STRING(result), |
| 9450 | howMany) < 0) { |
| 9451 | Py_DECREF(result); |
| 9452 | return PyErr_Format(PyExc_ValueError, |
| 9453 | "RAND_pseudo_bytes"); |
| 9454 | } |
| 9455 | } |
| 9456 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9457 | } |
| 9458 | #endif |
| 9459 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9460 | #ifdef HAVE_SETRESUID |
| 9461 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9462 | "setresuid(ruid, euid, suid)\n\n\ |
| 9463 | Set the current process's real, effective, and saved user ids."); |
| 9464 | |
| 9465 | static PyObject* |
| 9466 | posix_setresuid (PyObject *self, PyObject *args) |
| 9467 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9468 | /* We assume uid_t is no larger than a long. */ |
| 9469 | long ruid, euid, suid; |
| 9470 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9471 | return NULL; |
| 9472 | if (setresuid(ruid, euid, suid) < 0) |
| 9473 | return posix_error(); |
| 9474 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9475 | } |
| 9476 | #endif |
| 9477 | |
| 9478 | #ifdef HAVE_SETRESGID |
| 9479 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9480 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9481 | Set the current process's real, effective, and saved group ids."); |
| 9482 | |
| 9483 | static PyObject* |
| 9484 | posix_setresgid (PyObject *self, PyObject *args) |
| 9485 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9486 | /* We assume uid_t is no larger than a long. */ |
| 9487 | long rgid, egid, sgid; |
| 9488 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9489 | return NULL; |
| 9490 | if (setresgid(rgid, egid, sgid) < 0) |
| 9491 | return posix_error(); |
| 9492 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9493 | } |
| 9494 | #endif |
| 9495 | |
| 9496 | #ifdef HAVE_GETRESUID |
| 9497 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9498 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9499 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9500 | |
| 9501 | static PyObject* |
| 9502 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9503 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9504 | uid_t ruid, euid, suid; |
| 9505 | long l_ruid, l_euid, l_suid; |
| 9506 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9507 | return posix_error(); |
| 9508 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9509 | l_ruid = ruid; |
| 9510 | l_euid = euid; |
| 9511 | l_suid = suid; |
| 9512 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9513 | } |
| 9514 | #endif |
| 9515 | |
| 9516 | #ifdef HAVE_GETRESGID |
| 9517 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9518 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9519 | 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] | 9520 | |
| 9521 | static PyObject* |
| 9522 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9523 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9524 | uid_t rgid, egid, sgid; |
| 9525 | long l_rgid, l_egid, l_sgid; |
| 9526 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9527 | return posix_error(); |
| 9528 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9529 | l_rgid = rgid; |
| 9530 | l_egid = egid; |
| 9531 | l_sgid = sgid; |
| 9532 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9533 | } |
| 9534 | #endif |
| 9535 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9536 | /* Posix *at family of functions: |
| 9537 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9538 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9539 | unlinkat, utimensat, mkfifoat */ |
| 9540 | |
| 9541 | #ifdef HAVE_FACCESSAT |
| 9542 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9543 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9544 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9545 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9546 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9547 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9548 | is interpreted relative to the current working directory."); |
| 9549 | |
| 9550 | static PyObject * |
| 9551 | posix_faccessat(PyObject *self, PyObject *args) |
| 9552 | { |
| 9553 | PyObject *opath; |
| 9554 | char *path; |
| 9555 | int mode; |
| 9556 | int res; |
| 9557 | int dirfd, flags = 0; |
| 9558 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9559 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9560 | return NULL; |
| 9561 | path = PyBytes_AsString(opath); |
| 9562 | Py_BEGIN_ALLOW_THREADS |
| 9563 | res = faccessat(dirfd, path, mode, flags); |
| 9564 | Py_END_ALLOW_THREADS |
| 9565 | Py_DECREF(opath); |
| 9566 | return PyBool_FromLong(res == 0); |
| 9567 | } |
| 9568 | #endif |
| 9569 | |
| 9570 | #ifdef HAVE_FCHMODAT |
| 9571 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9572 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9573 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9574 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9575 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9576 | is interpreted relative to the current working directory."); |
| 9577 | |
| 9578 | static PyObject * |
| 9579 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9580 | { |
| 9581 | int dirfd, mode, res; |
| 9582 | int flags = 0; |
| 9583 | PyObject *opath; |
| 9584 | char *path; |
| 9585 | |
| 9586 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9587 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9588 | return NULL; |
| 9589 | |
| 9590 | path = PyBytes_AsString(opath); |
| 9591 | |
| 9592 | Py_BEGIN_ALLOW_THREADS |
| 9593 | res = fchmodat(dirfd, path, mode, flags); |
| 9594 | Py_END_ALLOW_THREADS |
| 9595 | Py_DECREF(opath); |
| 9596 | if (res < 0) |
| 9597 | return posix_error(); |
| 9598 | Py_RETURN_NONE; |
| 9599 | } |
| 9600 | #endif /* HAVE_FCHMODAT */ |
| 9601 | |
| 9602 | #ifdef HAVE_FCHOWNAT |
| 9603 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9604 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9605 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9606 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9607 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9608 | is interpreted relative to the current working directory."); |
| 9609 | |
| 9610 | static PyObject * |
| 9611 | posix_fchownat(PyObject *self, PyObject *args) |
| 9612 | { |
| 9613 | PyObject *opath; |
| 9614 | int dirfd, res; |
| 9615 | long uid, gid; |
| 9616 | int flags = 0; |
| 9617 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9618 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9619 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9620 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9621 | return NULL; |
| 9622 | |
| 9623 | path = PyBytes_AsString(opath); |
| 9624 | |
| 9625 | Py_BEGIN_ALLOW_THREADS |
| 9626 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9627 | Py_END_ALLOW_THREADS |
| 9628 | Py_DECREF(opath); |
| 9629 | if (res < 0) |
| 9630 | return posix_error(); |
| 9631 | Py_RETURN_NONE; |
| 9632 | } |
| 9633 | #endif /* HAVE_FCHOWNAT */ |
| 9634 | |
| 9635 | #ifdef HAVE_FSTATAT |
| 9636 | PyDoc_STRVAR(posix_fstatat__doc__, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 9637 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9638 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9639 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9640 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9641 | is interpreted relative to the current working directory."); |
| 9642 | |
| 9643 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 9644 | posix_fstatat(PyObject *self, PyObject *args) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9645 | { |
| 9646 | PyObject *opath; |
| 9647 | char *path; |
| 9648 | STRUCT_STAT st; |
| 9649 | int dirfd, res, flags = 0; |
| 9650 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 9651 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9652 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9653 | return NULL; |
| 9654 | path = PyBytes_AsString(opath); |
| 9655 | |
| 9656 | Py_BEGIN_ALLOW_THREADS |
| 9657 | res = fstatat(dirfd, path, &st, flags); |
| 9658 | Py_END_ALLOW_THREADS |
| 9659 | Py_DECREF(opath); |
| 9660 | if (res != 0) |
| 9661 | return posix_error(); |
| 9662 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 9663 | return _pystat_fromstructstat(&st); |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9664 | } |
| 9665 | #endif |
| 9666 | |
| 9667 | #ifdef HAVE_FUTIMESAT |
| 9668 | PyDoc_STRVAR(posix_futimesat__doc__, |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9669 | "futimesat(dirfd, path[, (atime, mtime)])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9670 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9671 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9672 | is interpreted relative to the current working directory."); |
| 9673 | |
| 9674 | static PyObject * |
| 9675 | posix_futimesat(PyObject *self, PyObject *args) |
| 9676 | { |
| 9677 | PyObject *opath; |
| 9678 | char *path; |
| 9679 | int res, dirfd; |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9680 | PyObject* arg = Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9681 | time_t atime, mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9682 | long ansec, mnsec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9683 | |
Brian Curtin | 7ef53ef | 2011-11-07 14:38:24 -0600 | [diff] [blame] | 9684 | if (!PyArg_ParseTuple(args, "iO&|O:futimesat", |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9685 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9686 | return NULL; |
| 9687 | path = PyBytes_AsString(opath); |
| 9688 | if (arg == Py_None) { |
| 9689 | /* optional time values not given */ |
| 9690 | Py_BEGIN_ALLOW_THREADS |
| 9691 | res = futimesat(dirfd, path, NULL); |
| 9692 | Py_END_ALLOW_THREADS |
| 9693 | } |
| 9694 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9695 | PyErr_SetString(PyExc_TypeError, |
| 9696 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9697 | Py_DECREF(opath); |
| 9698 | return NULL; |
| 9699 | } |
| 9700 | else { |
| 9701 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9702 | &atime, &ansec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9703 | Py_DECREF(opath); |
| 9704 | return NULL; |
| 9705 | } |
| 9706 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9707 | &mtime, &mnsec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9708 | Py_DECREF(opath); |
| 9709 | return NULL; |
| 9710 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9711 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9712 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9713 | { |
| 9714 | #ifdef HAVE_UTIMENSAT |
| 9715 | struct timespec buf[2]; |
| 9716 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9717 | buf[0].tv_nsec = ansec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9718 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9719 | buf[1].tv_nsec = mnsec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9720 | res = utimensat(dirfd, path, buf, 0); |
| 9721 | #else |
| 9722 | struct timeval buf[2]; |
| 9723 | buf[0].tv_sec = atime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9724 | buf[0].tv_usec = ansec / 1000; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9725 | buf[1].tv_sec = mtime; |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 9726 | buf[1].tv_usec = mnsec / 1000; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9727 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9728 | #endif |
| 9729 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9730 | Py_END_ALLOW_THREADS |
| 9731 | } |
| 9732 | Py_DECREF(opath); |
| 9733 | if (res < 0) { |
| 9734 | return posix_error(); |
| 9735 | } |
| 9736 | Py_RETURN_NONE; |
| 9737 | } |
| 9738 | #endif |
| 9739 | |
| 9740 | #ifdef HAVE_LINKAT |
| 9741 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9742 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9743 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9744 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9745 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9746 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9747 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9748 | also applies for dstpath."); |
| 9749 | |
| 9750 | static PyObject * |
| 9751 | posix_linkat(PyObject *self, PyObject *args) |
| 9752 | { |
| 9753 | PyObject *osrc, *odst; |
| 9754 | char *src, *dst; |
| 9755 | int res, srcfd, dstfd; |
| 9756 | int flags = 0; |
| 9757 | |
| 9758 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9759 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9760 | return NULL; |
| 9761 | src = PyBytes_AsString(osrc); |
| 9762 | dst = PyBytes_AsString(odst); |
| 9763 | Py_BEGIN_ALLOW_THREADS |
| 9764 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9765 | Py_END_ALLOW_THREADS |
| 9766 | Py_DECREF(osrc); |
| 9767 | Py_DECREF(odst); |
| 9768 | if (res < 0) |
| 9769 | return posix_error(); |
| 9770 | Py_RETURN_NONE; |
| 9771 | } |
| 9772 | #endif /* HAVE_LINKAT */ |
| 9773 | |
| 9774 | #ifdef HAVE_MKDIRAT |
| 9775 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9776 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9777 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9778 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9779 | is interpreted relative to the current working directory."); |
| 9780 | |
| 9781 | static PyObject * |
| 9782 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9783 | { |
| 9784 | int res, dirfd; |
| 9785 | PyObject *opath; |
| 9786 | char *path; |
| 9787 | int mode = 0777; |
| 9788 | |
| 9789 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9790 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9791 | return NULL; |
| 9792 | path = PyBytes_AsString(opath); |
| 9793 | Py_BEGIN_ALLOW_THREADS |
| 9794 | res = mkdirat(dirfd, path, mode); |
| 9795 | Py_END_ALLOW_THREADS |
| 9796 | Py_DECREF(opath); |
| 9797 | if (res < 0) |
| 9798 | return posix_error(); |
| 9799 | Py_RETURN_NONE; |
| 9800 | } |
| 9801 | #endif |
| 9802 | |
| 9803 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9804 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9805 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9806 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9807 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9808 | is interpreted relative to the current working directory."); |
| 9809 | |
| 9810 | static PyObject * |
| 9811 | posix_mknodat(PyObject *self, PyObject *args) |
| 9812 | { |
| 9813 | PyObject *opath; |
| 9814 | char *filename; |
| 9815 | int mode = 0600; |
| 9816 | int device = 0; |
| 9817 | int res, dirfd; |
| 9818 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9819 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9820 | return NULL; |
| 9821 | filename = PyBytes_AS_STRING(opath); |
| 9822 | Py_BEGIN_ALLOW_THREADS |
| 9823 | res = mknodat(dirfd, filename, mode, device); |
| 9824 | Py_END_ALLOW_THREADS |
| 9825 | Py_DECREF(opath); |
| 9826 | if (res < 0) |
| 9827 | return posix_error(); |
| 9828 | Py_RETURN_NONE; |
| 9829 | } |
| 9830 | #endif |
| 9831 | |
| 9832 | #ifdef HAVE_OPENAT |
| 9833 | PyDoc_STRVAR(posix_openat__doc__, |
| 9834 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9835 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9836 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9837 | is interpreted relative to the current working directory."); |
| 9838 | |
| 9839 | static PyObject * |
| 9840 | posix_openat(PyObject *self, PyObject *args) |
| 9841 | { |
| 9842 | PyObject *ofile; |
| 9843 | char *file; |
| 9844 | int flag, dirfd, fd; |
| 9845 | int mode = 0777; |
| 9846 | |
| 9847 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9848 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9849 | &flag, &mode)) |
| 9850 | return NULL; |
| 9851 | file = PyBytes_AsString(ofile); |
| 9852 | Py_BEGIN_ALLOW_THREADS |
| 9853 | fd = openat(dirfd, file, flag, mode); |
| 9854 | Py_END_ALLOW_THREADS |
| 9855 | Py_DECREF(ofile); |
| 9856 | if (fd < 0) |
| 9857 | return posix_error(); |
| 9858 | return PyLong_FromLong((long)fd); |
| 9859 | } |
| 9860 | #endif |
| 9861 | |
| 9862 | #ifdef HAVE_READLINKAT |
| 9863 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9864 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9865 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9866 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9867 | is interpreted relative to the current working directory."); |
| 9868 | |
| 9869 | static PyObject * |
| 9870 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9871 | { |
| 9872 | PyObject *v, *opath; |
| 9873 | char buf[MAXPATHLEN]; |
| 9874 | char *path; |
| 9875 | int n, dirfd; |
| 9876 | int arg_is_unicode = 0; |
| 9877 | |
| 9878 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9879 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9880 | return NULL; |
| 9881 | path = PyBytes_AsString(opath); |
| 9882 | v = PySequence_GetItem(args, 1); |
| 9883 | if (v == NULL) { |
| 9884 | Py_DECREF(opath); |
| 9885 | return NULL; |
| 9886 | } |
| 9887 | |
| 9888 | if (PyUnicode_Check(v)) { |
| 9889 | arg_is_unicode = 1; |
| 9890 | } |
| 9891 | Py_DECREF(v); |
| 9892 | |
| 9893 | Py_BEGIN_ALLOW_THREADS |
| 9894 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9895 | Py_END_ALLOW_THREADS |
| 9896 | Py_DECREF(opath); |
| 9897 | if (n < 0) |
| 9898 | return posix_error(); |
| 9899 | |
| 9900 | if (arg_is_unicode) |
| 9901 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9902 | else |
| 9903 | return PyBytes_FromStringAndSize(buf, n); |
| 9904 | } |
| 9905 | #endif /* HAVE_READLINKAT */ |
| 9906 | |
| 9907 | #ifdef HAVE_RENAMEAT |
| 9908 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9909 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9910 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9911 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9912 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9913 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9914 | also applies for newpath."); |
| 9915 | |
| 9916 | static PyObject * |
| 9917 | posix_renameat(PyObject *self, PyObject *args) |
| 9918 | { |
| 9919 | int res; |
| 9920 | PyObject *opathold, *opathnew; |
| 9921 | char *opath, *npath; |
| 9922 | int oldfd, newfd; |
| 9923 | |
| 9924 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9925 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9926 | return NULL; |
| 9927 | opath = PyBytes_AsString(opathold); |
| 9928 | npath = PyBytes_AsString(opathnew); |
| 9929 | Py_BEGIN_ALLOW_THREADS |
| 9930 | res = renameat(oldfd, opath, newfd, npath); |
| 9931 | Py_END_ALLOW_THREADS |
| 9932 | Py_DECREF(opathold); |
| 9933 | Py_DECREF(opathnew); |
| 9934 | if (res < 0) |
| 9935 | return posix_error(); |
| 9936 | Py_RETURN_NONE; |
| 9937 | } |
| 9938 | #endif |
| 9939 | |
| 9940 | #if HAVE_SYMLINKAT |
| 9941 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9942 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9943 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9944 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9945 | is interpreted relative to the current working directory."); |
| 9946 | |
| 9947 | static PyObject * |
| 9948 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9949 | { |
| 9950 | int res, dstfd; |
| 9951 | PyObject *osrc, *odst; |
| 9952 | char *src, *dst; |
| 9953 | |
| 9954 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9955 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9956 | return NULL; |
| 9957 | src = PyBytes_AsString(osrc); |
| 9958 | dst = PyBytes_AsString(odst); |
| 9959 | Py_BEGIN_ALLOW_THREADS |
| 9960 | res = symlinkat(src, dstfd, dst); |
| 9961 | Py_END_ALLOW_THREADS |
| 9962 | Py_DECREF(osrc); |
| 9963 | Py_DECREF(odst); |
| 9964 | if (res < 0) |
| 9965 | return posix_error(); |
| 9966 | Py_RETURN_NONE; |
| 9967 | } |
| 9968 | #endif /* HAVE_SYMLINKAT */ |
| 9969 | |
| 9970 | #ifdef HAVE_UNLINKAT |
| 9971 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9972 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9973 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9974 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9975 | specified, unlinkat() behaves like rmdir().\n\ |
| 9976 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9977 | is interpreted relative to the current working directory."); |
| 9978 | |
| 9979 | static PyObject * |
| 9980 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9981 | { |
| 9982 | int dirfd, res, flags = 0; |
| 9983 | PyObject *opath; |
| 9984 | char *path; |
| 9985 | |
| 9986 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9987 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9988 | return NULL; |
| 9989 | path = PyBytes_AsString(opath); |
| 9990 | Py_BEGIN_ALLOW_THREADS |
| 9991 | res = unlinkat(dirfd, path, flags); |
| 9992 | Py_END_ALLOW_THREADS |
| 9993 | Py_DECREF(opath); |
| 9994 | if (res < 0) |
| 9995 | return posix_error(); |
| 9996 | Py_RETURN_NONE; |
| 9997 | } |
| 9998 | #endif |
| 9999 | |
| 10000 | #ifdef HAVE_UTIMENSAT |
| 10001 | PyDoc_STRVAR(posix_utimensat__doc__, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10002 | "utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\ |
| 10003 | mtime=(mtime_sec, mtime_nsec), flags=0])\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10004 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 10005 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 10006 | relative, it is taken as relative to dirfd.\n\ |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10007 | If atime and mtime are both None, which is the default, set atime and\n\ |
| 10008 | mtime to the current time.\n\ |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10009 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 10010 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10011 | is interpreted relative to the current working directory.\n\ |
| 10012 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 10013 | current time.\n\ |
| 10014 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 10015 | |
| 10016 | static PyObject * |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10017 | posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10018 | { |
| 10019 | PyObject *opath; |
| 10020 | char *path; |
| 10021 | int res, dirfd, flags = 0; |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10022 | PyObject *atime = Py_None; |
| 10023 | PyObject *mtime = Py_None; |
| 10024 | |
| 10025 | static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL}; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10026 | |
| 10027 | struct timespec buf[2]; |
| 10028 | |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10029 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10030 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 10031 | return NULL; |
| 10032 | path = PyBytes_AsString(opath); |
| 10033 | if (atime == Py_None && mtime == Py_None) { |
| 10034 | /* optional time values not given */ |
| 10035 | Py_BEGIN_ALLOW_THREADS |
| 10036 | res = utimensat(dirfd, path, NULL, flags); |
| 10037 | Py_END_ALLOW_THREADS |
| 10038 | } |
| 10039 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 10040 | PyErr_SetString(PyExc_TypeError, |
| 10041 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 10042 | Py_DECREF(opath); |
| 10043 | return NULL; |
| 10044 | } |
| 10045 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 10046 | PyErr_SetString(PyExc_TypeError, |
| 10047 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 10048 | Py_DECREF(opath); |
| 10049 | return NULL; |
| 10050 | } |
| 10051 | else { |
| 10052 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 10053 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 10054 | Py_DECREF(opath); |
| 10055 | return NULL; |
| 10056 | } |
| 10057 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10058 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10059 | Py_DECREF(opath); |
| 10060 | return NULL; |
| 10061 | } |
| 10062 | Py_BEGIN_ALLOW_THREADS |
| 10063 | res = utimensat(dirfd, path, buf, flags); |
| 10064 | Py_END_ALLOW_THREADS |
| 10065 | } |
| 10066 | Py_DECREF(opath); |
| 10067 | if (res < 0) { |
| 10068 | return posix_error(); |
| 10069 | } |
| 10070 | Py_RETURN_NONE; |
| 10071 | } |
| 10072 | #endif |
| 10073 | |
| 10074 | #ifdef HAVE_MKFIFOAT |
| 10075 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10076 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10077 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10078 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10079 | is interpreted relative to the current working directory."); |
| 10080 | |
| 10081 | static PyObject * |
| 10082 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10083 | { |
| 10084 | PyObject *opath; |
| 10085 | char *filename; |
| 10086 | int mode = 0666; |
| 10087 | int res, dirfd; |
| 10088 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10089 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10090 | return NULL; |
| 10091 | filename = PyBytes_AS_STRING(opath); |
| 10092 | Py_BEGIN_ALLOW_THREADS |
| 10093 | res = mkfifoat(dirfd, filename, mode); |
| 10094 | Py_END_ALLOW_THREADS |
| 10095 | Py_DECREF(opath); |
| 10096 | if (res < 0) |
| 10097 | return posix_error(); |
| 10098 | Py_RETURN_NONE; |
| 10099 | } |
| 10100 | #endif |
| 10101 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10102 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10103 | |
| 10104 | static int |
| 10105 | try_getxattr(const char *path, const char *name, |
| 10106 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10107 | Py_ssize_t buf_size, PyObject **res) |
| 10108 | { |
| 10109 | PyObject *value; |
| 10110 | Py_ssize_t len; |
| 10111 | |
| 10112 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10113 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10114 | if (!value) |
| 10115 | return 0; |
| 10116 | Py_BEGIN_ALLOW_THREADS; |
| 10117 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10118 | Py_END_ALLOW_THREADS; |
| 10119 | if (len < 0) { |
| 10120 | Py_DECREF(value); |
| 10121 | if (errno == ERANGE) { |
| 10122 | value = NULL; |
| 10123 | } |
| 10124 | else { |
| 10125 | posix_error(); |
| 10126 | return 0; |
| 10127 | } |
| 10128 | } |
| 10129 | else if (len != buf_size) { |
| 10130 | /* Can only shrink. */ |
| 10131 | _PyBytes_Resize(&value, len); |
| 10132 | } |
| 10133 | *res = value; |
| 10134 | return 1; |
| 10135 | } |
| 10136 | |
| 10137 | static PyObject * |
| 10138 | getxattr_common(const char *path, PyObject *name_obj, |
| 10139 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10140 | { |
| 10141 | PyObject *value; |
| 10142 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10143 | |
| 10144 | /* Try a small value first. */ |
| 10145 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10146 | return NULL; |
| 10147 | if (value) |
| 10148 | return value; |
| 10149 | /* Now the maximum possible one. */ |
| 10150 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10151 | return NULL; |
| 10152 | assert(value); |
| 10153 | return value; |
| 10154 | } |
| 10155 | |
| 10156 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10157 | "getxattr(path, attr) -> value\n\n\ |
| 10158 | Return the value of extended attribute *name* on *path*."); |
| 10159 | |
| 10160 | static PyObject * |
| 10161 | posix_getxattr(PyObject *self, PyObject *args) |
| 10162 | { |
| 10163 | PyObject *path, *res, *name; |
| 10164 | |
| 10165 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10166 | PyUnicode_FSConverter, &name)) |
| 10167 | return NULL; |
| 10168 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10169 | Py_DECREF(path); |
| 10170 | Py_DECREF(name); |
| 10171 | return res; |
| 10172 | } |
| 10173 | |
| 10174 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10175 | "lgetxattr(path, attr) -> value\n\n\ |
| 10176 | Like getxattr but don't follow symlinks."); |
| 10177 | |
| 10178 | static PyObject * |
| 10179 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10180 | { |
| 10181 | PyObject *path, *res, *name; |
| 10182 | |
| 10183 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10184 | PyUnicode_FSConverter, &name)) |
| 10185 | return NULL; |
| 10186 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10187 | Py_DECREF(path); |
| 10188 | Py_DECREF(name); |
| 10189 | return res; |
| 10190 | } |
| 10191 | |
| 10192 | static ssize_t |
| 10193 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10194 | { |
| 10195 | /* Hack to share code. */ |
| 10196 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10197 | } |
| 10198 | |
| 10199 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10200 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10201 | Like getxattr but operate on a fd instead of a path."); |
| 10202 | |
| 10203 | static PyObject * |
| 10204 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10205 | { |
| 10206 | PyObject *res, *name; |
| 10207 | int fd; |
| 10208 | |
| 10209 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10210 | return NULL; |
| 10211 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10212 | Py_DECREF(name); |
| 10213 | return res; |
| 10214 | } |
| 10215 | |
| 10216 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10217 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10218 | Set extended attribute *attr* on *path* to *value*."); |
| 10219 | |
| 10220 | static PyObject * |
| 10221 | posix_setxattr(PyObject *self, PyObject *args) |
| 10222 | { |
| 10223 | PyObject *path, *name; |
| 10224 | Py_buffer data; |
| 10225 | int flags = 0, err; |
| 10226 | |
| 10227 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10228 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10229 | return NULL; |
| 10230 | Py_BEGIN_ALLOW_THREADS; |
| 10231 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10232 | data.buf, data.len, flags); |
| 10233 | Py_END_ALLOW_THREADS; |
| 10234 | Py_DECREF(path); |
| 10235 | Py_DECREF(name); |
| 10236 | PyBuffer_Release(&data); |
| 10237 | if (err) |
| 10238 | return posix_error(); |
| 10239 | Py_RETURN_NONE; |
| 10240 | } |
| 10241 | |
| 10242 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10243 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10244 | Like setxattr but don't follow symlinks."); |
| 10245 | |
| 10246 | static PyObject * |
| 10247 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10248 | { |
| 10249 | PyObject *path, *name; |
| 10250 | Py_buffer data; |
| 10251 | int flags = 0, err; |
| 10252 | |
| 10253 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10254 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10255 | return NULL; |
| 10256 | Py_BEGIN_ALLOW_THREADS; |
| 10257 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10258 | data.buf, data.len, flags); |
| 10259 | Py_END_ALLOW_THREADS; |
| 10260 | Py_DECREF(path); |
| 10261 | Py_DECREF(name); |
| 10262 | PyBuffer_Release(&data); |
| 10263 | if (err) |
| 10264 | return posix_error(); |
| 10265 | Py_RETURN_NONE; |
| 10266 | } |
| 10267 | |
| 10268 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10269 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10270 | Like setxattr but operates on *fd* instead of a path."); |
| 10271 | |
| 10272 | static PyObject * |
| 10273 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10274 | { |
| 10275 | Py_buffer data; |
| 10276 | const char *name; |
| 10277 | int fd, flags = 0, err; |
| 10278 | |
| 10279 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10280 | &name, &data, &flags)) |
| 10281 | return NULL; |
| 10282 | Py_BEGIN_ALLOW_THREADS; |
| 10283 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10284 | Py_END_ALLOW_THREADS; |
| 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_removexattr__doc__, |
| 10293 | "removexattr(path, attr)\n\n\ |
| 10294 | Remove extended attribute *attr* on *path*."); |
| 10295 | |
| 10296 | static PyObject * |
| 10297 | posix_removexattr(PyObject *self, PyObject *args) |
| 10298 | { |
| 10299 | PyObject *path, *name; |
| 10300 | int err; |
| 10301 | |
| 10302 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10303 | PyUnicode_FSConverter, &name)) |
| 10304 | return NULL; |
| 10305 | Py_BEGIN_ALLOW_THREADS; |
| 10306 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10307 | Py_END_ALLOW_THREADS; |
| 10308 | Py_DECREF(path); |
| 10309 | Py_DECREF(name); |
| 10310 | if (err) |
| 10311 | return posix_error(); |
| 10312 | Py_RETURN_NONE; |
| 10313 | } |
| 10314 | |
| 10315 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10316 | "lremovexattr(path, attr)\n\n\ |
| 10317 | Like removexattr but don't follow symlinks."); |
| 10318 | |
| 10319 | static PyObject * |
| 10320 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10321 | { |
| 10322 | PyObject *path, *name; |
| 10323 | int err; |
| 10324 | |
| 10325 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10326 | PyUnicode_FSConverter, &name)) |
| 10327 | return NULL; |
| 10328 | Py_BEGIN_ALLOW_THREADS; |
| 10329 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10330 | Py_END_ALLOW_THREADS; |
| 10331 | Py_DECREF(path); |
| 10332 | Py_DECREF(name); |
| 10333 | if (err) |
| 10334 | return posix_error(); |
| 10335 | Py_RETURN_NONE; |
| 10336 | } |
| 10337 | |
| 10338 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10339 | "fremovexattr(fd, attr)\n\n\ |
| 10340 | Like removexattr but operates on a file descriptor."); |
| 10341 | |
| 10342 | static PyObject * |
| 10343 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10344 | { |
| 10345 | PyObject *name; |
| 10346 | int fd, err; |
| 10347 | |
| 10348 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10349 | PyUnicode_FSConverter, &name)) |
| 10350 | return NULL; |
| 10351 | Py_BEGIN_ALLOW_THREADS; |
| 10352 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10353 | Py_END_ALLOW_THREADS; |
| 10354 | Py_DECREF(name); |
| 10355 | if (err) |
| 10356 | return posix_error(); |
| 10357 | Py_RETURN_NONE; |
| 10358 | } |
| 10359 | |
| 10360 | static Py_ssize_t |
| 10361 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10362 | Py_ssize_t buf_size, char **buf) |
| 10363 | { |
| 10364 | Py_ssize_t len; |
| 10365 | |
| 10366 | *buf = PyMem_MALLOC(buf_size); |
| 10367 | if (!*buf) { |
| 10368 | PyErr_NoMemory(); |
| 10369 | return -1; |
| 10370 | } |
| 10371 | Py_BEGIN_ALLOW_THREADS; |
| 10372 | len = list(path, *buf, buf_size); |
| 10373 | Py_END_ALLOW_THREADS; |
| 10374 | if (len < 0) { |
| 10375 | PyMem_FREE(*buf); |
| 10376 | if (errno != ERANGE) |
| 10377 | posix_error(); |
| 10378 | return -1; |
| 10379 | } |
| 10380 | return len; |
| 10381 | } |
| 10382 | |
| 10383 | static PyObject * |
| 10384 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10385 | { |
| 10386 | PyObject *res, *attr; |
| 10387 | Py_ssize_t len, err, start, i; |
| 10388 | char *buf; |
| 10389 | |
| 10390 | len = try_listxattr(path, list, 256, &buf); |
| 10391 | if (len < 0) { |
| 10392 | if (PyErr_Occurred()) |
| 10393 | return NULL; |
| 10394 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10395 | if (len < 0) |
| 10396 | return NULL; |
| 10397 | } |
| 10398 | res = PyList_New(0); |
| 10399 | if (!res) { |
| 10400 | PyMem_FREE(buf); |
| 10401 | return NULL; |
| 10402 | } |
| 10403 | for (start = i = 0; i < len; i++) { |
| 10404 | if (!buf[i]) { |
| 10405 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10406 | if (!attr) { |
| 10407 | Py_DECREF(res); |
| 10408 | PyMem_FREE(buf); |
| 10409 | return NULL; |
| 10410 | } |
| 10411 | err = PyList_Append(res, attr); |
| 10412 | Py_DECREF(attr); |
| 10413 | if (err) { |
| 10414 | Py_DECREF(res); |
| 10415 | PyMem_FREE(buf); |
| 10416 | return NULL; |
| 10417 | } |
| 10418 | start = i + 1; |
| 10419 | } |
| 10420 | } |
| 10421 | PyMem_FREE(buf); |
| 10422 | return res; |
| 10423 | } |
| 10424 | |
| 10425 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10426 | "listxattr(path)\n\n\ |
| 10427 | Return a list of extended attributes on *path*."); |
| 10428 | |
| 10429 | static PyObject * |
| 10430 | posix_listxattr(PyObject *self, PyObject *args) |
| 10431 | { |
| 10432 | PyObject *path, *res; |
| 10433 | |
| 10434 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10435 | return NULL; |
| 10436 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10437 | Py_DECREF(path); |
| 10438 | return res; |
| 10439 | } |
| 10440 | |
| 10441 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10442 | "llistxattr(path)\n\n\ |
| 10443 | Like listxattr but don't follow symlinks.."); |
| 10444 | |
| 10445 | static PyObject * |
| 10446 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10447 | { |
| 10448 | PyObject *path, *res; |
| 10449 | |
| 10450 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10451 | return NULL; |
| 10452 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10453 | Py_DECREF(path); |
| 10454 | return res; |
| 10455 | } |
| 10456 | |
| 10457 | static ssize_t |
| 10458 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10459 | { |
| 10460 | /* Hack to share code. */ |
| 10461 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10462 | } |
| 10463 | |
| 10464 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10465 | "flistxattr(path)\n\n\ |
| 10466 | Like flistxattr but operates on a file descriptor."); |
| 10467 | |
| 10468 | static PyObject * |
| 10469 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10470 | { |
| 10471 | long fd; |
| 10472 | |
| 10473 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10474 | return NULL; |
| 10475 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10476 | } |
| 10477 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10478 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10479 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10480 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10481 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10482 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10484 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10485 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10486 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10487 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10488 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10489 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10490 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10491 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10492 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10493 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10494 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10495 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10496 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10497 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10498 | #endif /* HAVE_LCHMOD */ |
| 10499 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10500 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10501 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10502 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10503 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10504 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10505 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10506 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10507 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10508 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10509 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10510 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10511 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10513 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10514 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10515 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10516 | METH_NOARGS, posix_getcwd__doc__}, |
| 10517 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10518 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10519 | #endif |
| 10520 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10522 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10524 | #ifdef HAVE_FDOPENDIR |
Charles-François Natali | 7794090 | 2012-02-06 19:54:48 +0100 | [diff] [blame] | 10525 | {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10526 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10527 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10528 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10529 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10530 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 10531 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10532 | #ifdef HAVE_GETPRIORITY |
| 10533 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10534 | #endif /* HAVE_GETPRIORITY */ |
| 10535 | #ifdef HAVE_SETPRIORITY |
| 10536 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10537 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10538 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10540 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10541 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10542 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10543 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10544 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 10545 | {"replace", posix_replace, METH_VARARGS, posix_replace__doc__}, |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10546 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10547 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10549 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10550 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 10551 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10552 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10553 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10554 | win_symlink__doc__}, |
| 10555 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 10556 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10557 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10558 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10559 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10560 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10561 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10562 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10563 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10564 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10565 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10566 | #ifdef HAVE_FUTIMES |
| 10567 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10568 | #endif |
| 10569 | #ifdef HAVE_LUTIMES |
| 10570 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10571 | #endif |
| 10572 | #ifdef HAVE_FUTIMENS |
| 10573 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10574 | #endif |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 10575 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10577 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10578 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10579 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10580 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10581 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10582 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10583 | #ifdef HAVE_FEXECVE |
| 10584 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10585 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10586 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10587 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10588 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10589 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10590 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10591 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10592 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10593 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10594 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10596 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10597 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10598 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10599 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10600 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10601 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10602 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10603 | {"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] | 10604 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10605 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10606 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10607 | #endif |
| 10608 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10609 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10610 | #endif |
| 10611 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10612 | {"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] | 10613 | #endif |
| 10614 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10615 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10616 | #endif |
| 10617 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10618 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10619 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10620 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10621 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10622 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10623 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10624 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10625 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10626 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10628 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10629 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10630 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10631 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 10632 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 10634 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10635 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10636 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 10637 | #endif /* HAVE_GETEUID */ |
| 10638 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10639 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10640 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10641 | #ifdef HAVE_GETGROUPLIST |
| 10642 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10643 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10644 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10646 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10647 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10648 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10649 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10650 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10651 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10652 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10653 | #endif /* HAVE_GETPPID */ |
| 10654 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10655 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10656 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10657 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10658 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10659 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10660 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10661 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10662 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10663 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10664 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10665 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10666 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10667 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10668 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10669 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10670 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10671 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10672 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10673 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10674 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10676 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10677 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10679 | #endif /* HAVE_SETEUID */ |
| 10680 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10682 | #endif /* HAVE_SETEGID */ |
| 10683 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10685 | #endif /* HAVE_SETREUID */ |
| 10686 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10688 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10689 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10691 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10692 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10694 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10695 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10696 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10697 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10698 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10699 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10700 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10701 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10703 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10704 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10706 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10707 | #ifdef HAVE_WAIT3 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10708 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10709 | #endif /* HAVE_WAIT3 */ |
| 10710 | #ifdef HAVE_WAIT4 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10711 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10712 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10713 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10714 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10715 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10716 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10718 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10719 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10721 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10722 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10724 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10725 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10726 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10727 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10728 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10729 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10730 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10731 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10732 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10733 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10734 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10735 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10736 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10737 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10738 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10739 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10740 | #ifdef HAVE_LOCKF |
| 10741 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10742 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10743 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10744 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10745 | #ifdef HAVE_READV |
| 10746 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10747 | #endif |
| 10748 | #ifdef HAVE_PREAD |
| 10749 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10750 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10752 | #ifdef HAVE_WRITEV |
| 10753 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10754 | #endif |
| 10755 | #ifdef HAVE_PWRITE |
| 10756 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10757 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10758 | #ifdef HAVE_SENDFILE |
| 10759 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10760 | posix_sendfile__doc__}, |
| 10761 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10762 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10764 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10765 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10766 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10767 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10768 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10769 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10770 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10771 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10772 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10773 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10774 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10775 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10776 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10777 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10778 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10779 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10780 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10781 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10783 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10784 | #ifdef HAVE_TRUNCATE |
| 10785 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10786 | #endif |
| 10787 | #ifdef HAVE_POSIX_FALLOCATE |
| 10788 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10789 | #endif |
| 10790 | #ifdef HAVE_POSIX_FADVISE |
| 10791 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10792 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10793 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10795 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10796 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10798 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10800 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10801 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10802 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10803 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10804 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10805 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10806 | #ifdef HAVE_SYNC |
| 10807 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10808 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10809 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10810 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10811 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10812 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10813 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10815 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10816 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10818 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10819 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10821 | #endif /* WIFSTOPPED */ |
| 10822 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10824 | #endif /* WIFSIGNALED */ |
| 10825 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10826 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10827 | #endif /* WIFEXITED */ |
| 10828 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10829 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10830 | #endif /* WEXITSTATUS */ |
| 10831 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10832 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10833 | #endif /* WTERMSIG */ |
| 10834 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10835 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10836 | #endif /* WSTOPSIG */ |
| 10837 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10838 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10840 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10841 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10843 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10844 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10845 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10846 | #endif |
| 10847 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10848 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | #endif |
| 10850 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10851 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10852 | #endif |
| 10853 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10855 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10857 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10859 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10860 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10861 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10862 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10863 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10864 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10866 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10867 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10868 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10869 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10870 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10872 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10873 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10874 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10875 | #endif |
| 10876 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10877 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10878 | #endif |
| 10879 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10880 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10881 | #endif |
| 10882 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10883 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10884 | #endif |
| 10885 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10886 | /* posix *at family of functions */ |
| 10887 | #ifdef HAVE_FACCESSAT |
| 10888 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10889 | #endif |
| 10890 | #ifdef HAVE_FCHMODAT |
| 10891 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10892 | #endif /* HAVE_FCHMODAT */ |
| 10893 | #ifdef HAVE_FCHOWNAT |
| 10894 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10895 | #endif /* HAVE_FCHOWNAT */ |
| 10896 | #ifdef HAVE_FSTATAT |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame^] | 10897 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef HAVE_FUTIMESAT |
| 10900 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10901 | #endif |
| 10902 | #ifdef HAVE_LINKAT |
| 10903 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10904 | #endif /* HAVE_LINKAT */ |
| 10905 | #ifdef HAVE_MKDIRAT |
| 10906 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10907 | #endif |
| 10908 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10909 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10910 | #endif |
| 10911 | #ifdef HAVE_OPENAT |
| 10912 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10913 | #endif |
| 10914 | #ifdef HAVE_READLINKAT |
| 10915 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10916 | #endif /* HAVE_READLINKAT */ |
| 10917 | #ifdef HAVE_RENAMEAT |
| 10918 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10919 | #endif |
| 10920 | #if HAVE_SYMLINKAT |
| 10921 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10922 | #endif /* HAVE_SYMLINKAT */ |
| 10923 | #ifdef HAVE_UNLINKAT |
| 10924 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10925 | #endif |
| 10926 | #ifdef HAVE_UTIMENSAT |
Jesus Cea | d03a491 | 2011-11-08 17:28:04 +0100 | [diff] [blame] | 10927 | {"utimensat", (PyCFunction)posix_utimensat, |
| 10928 | METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 569b494 | 2011-11-07 16:09:20 -0600 | [diff] [blame] | 10929 | posix_utimensat__doc__}, |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10930 | #endif |
| 10931 | #ifdef HAVE_MKFIFOAT |
| 10932 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10933 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10934 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10935 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10936 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10937 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10938 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10939 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10940 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10941 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10942 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10943 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10944 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10945 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10946 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10947 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10949 | }; |
| 10950 | |
| 10951 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10952 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10953 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10954 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10955 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10956 | } |
| 10957 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10958 | #if defined(PYOS_OS2) |
| 10959 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10960 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10961 | { |
| 10962 | APIRET rc; |
| 10963 | ULONG values[QSV_MAX+1]; |
| 10964 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10965 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10966 | |
| 10967 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10968 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10969 | Py_END_ALLOW_THREADS |
| 10970 | |
| 10971 | if (rc != NO_ERROR) { |
| 10972 | os2_error(rc); |
| 10973 | return -1; |
| 10974 | } |
| 10975 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10976 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10977 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10978 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10979 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10980 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10981 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10982 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10983 | |
| 10984 | switch (values[QSV_VERSION_MINOR]) { |
| 10985 | case 0: ver = "2.00"; break; |
| 10986 | case 10: ver = "2.10"; break; |
| 10987 | case 11: ver = "2.11"; break; |
| 10988 | case 30: ver = "3.00"; break; |
| 10989 | case 40: ver = "4.00"; break; |
| 10990 | case 50: ver = "5.00"; break; |
| 10991 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10992 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10993 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10994 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10995 | ver = &tmp[0]; |
| 10996 | } |
| 10997 | |
| 10998 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10999 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11000 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11001 | |
| 11002 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 11003 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 11004 | tmp[1] = ':'; |
| 11005 | tmp[2] = '\0'; |
| 11006 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11007 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11008 | } |
| 11009 | #endif |
| 11010 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11011 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11012 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11013 | enable_symlink() |
| 11014 | { |
| 11015 | HANDLE tok; |
| 11016 | TOKEN_PRIVILEGES tok_priv; |
| 11017 | LUID luid; |
| 11018 | int meth_idx = 0; |
| 11019 | |
| 11020 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11021 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11022 | |
| 11023 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11024 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11025 | |
| 11026 | tok_priv.PrivilegeCount = 1; |
| 11027 | tok_priv.Privileges[0].Luid = luid; |
| 11028 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 11029 | |
| 11030 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 11031 | sizeof(TOKEN_PRIVILEGES), |
| 11032 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11033 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11034 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11035 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 11036 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11037 | } |
| 11038 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 11039 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11040 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11041 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11042 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11043 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11044 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11045 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11046 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11047 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11048 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11049 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11050 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11051 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11052 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11053 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11054 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11055 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11057 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11058 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11060 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11061 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11063 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11064 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11066 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11067 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11069 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11070 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11072 | #endif |
| 11073 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11075 | #endif |
| 11076 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11078 | #endif |
| 11079 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11081 | #endif |
| 11082 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11084 | #endif |
| 11085 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11087 | #endif |
| 11088 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11090 | #endif |
| 11091 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11099 | #endif |
| 11100 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11108 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11109 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11114 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11115 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11117 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11118 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11123 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11124 | #ifdef PRIO_PROCESS |
| 11125 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11126 | #endif |
| 11127 | #ifdef PRIO_PGRP |
| 11128 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11129 | #endif |
| 11130 | #ifdef PRIO_USER |
| 11131 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11132 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11133 | #ifdef O_CLOEXEC |
| 11134 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11135 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11136 | /* posix - constants for *at functions */ |
| 11137 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11138 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11139 | #endif |
| 11140 | #ifdef AT_EACCESS |
| 11141 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11142 | #endif |
| 11143 | #ifdef AT_FDCWD |
| 11144 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11145 | #endif |
| 11146 | #ifdef AT_REMOVEDIR |
| 11147 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11148 | #endif |
| 11149 | #ifdef AT_SYMLINK_FOLLOW |
| 11150 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11151 | #endif |
| 11152 | #ifdef UTIME_NOW |
| 11153 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11154 | #endif |
| 11155 | #ifdef UTIME_OMIT |
| 11156 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11157 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11158 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11159 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11160 | /* MS Windows */ |
| 11161 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11162 | /* Don't inherit in child processes. */ |
| 11163 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11164 | #endif |
| 11165 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11166 | /* Optimize for short life (keep in memory). */ |
| 11167 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11168 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11169 | #endif |
| 11170 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | /* Automatically delete when last handle is closed. */ |
| 11172 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11173 | #endif |
| 11174 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11175 | /* Optimize for random access. */ |
| 11176 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11177 | #endif |
| 11178 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | /* Optimize for sequential access. */ |
| 11180 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11181 | #endif |
| 11182 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11183 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11184 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | /* Send a SIGIO signal whenever input or output |
| 11186 | becomes available on file descriptor */ |
| 11187 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11188 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11189 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | /* Direct disk access. */ |
| 11191 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | /* Must be a directory. */ |
| 11195 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11196 | #endif |
| 11197 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | /* Do not follow links. */ |
| 11199 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11200 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11201 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | /* Do not update the access time. */ |
| 11203 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11204 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11205 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11207 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11209 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11210 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11212 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11213 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11215 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11216 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11218 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11219 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11220 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11221 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11222 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11224 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11225 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11227 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11228 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11230 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11231 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11233 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11234 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11236 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11237 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11239 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11240 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11242 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11243 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11245 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11246 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11248 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11249 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11251 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11252 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11254 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11255 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11257 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11258 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11259 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11260 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11261 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11262 | #endif /* ST_RDONLY */ |
| 11263 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11264 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11265 | #endif /* ST_NOSUID */ |
| 11266 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11267 | /* FreeBSD sendfile() constants */ |
| 11268 | #ifdef SF_NODISKIO |
| 11269 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11270 | #endif |
| 11271 | #ifdef SF_MNOWAIT |
| 11272 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11273 | #endif |
| 11274 | #ifdef SF_SYNC |
| 11275 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11276 | #endif |
| 11277 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11278 | /* constants for posix_fadvise */ |
| 11279 | #ifdef POSIX_FADV_NORMAL |
| 11280 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11281 | #endif |
| 11282 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11283 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11284 | #endif |
| 11285 | #ifdef POSIX_FADV_RANDOM |
| 11286 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11287 | #endif |
| 11288 | #ifdef POSIX_FADV_NOREUSE |
| 11289 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11290 | #endif |
| 11291 | #ifdef POSIX_FADV_WILLNEED |
| 11292 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11293 | #endif |
| 11294 | #ifdef POSIX_FADV_DONTNEED |
| 11295 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11296 | #endif |
| 11297 | |
| 11298 | /* constants for waitid */ |
| 11299 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11300 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11301 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11302 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11303 | #endif |
| 11304 | #ifdef WEXITED |
| 11305 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11306 | #endif |
| 11307 | #ifdef WNOWAIT |
| 11308 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11309 | #endif |
| 11310 | #ifdef WSTOPPED |
| 11311 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11312 | #endif |
| 11313 | #ifdef CLD_EXITED |
| 11314 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11315 | #endif |
| 11316 | #ifdef CLD_DUMPED |
| 11317 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11318 | #endif |
| 11319 | #ifdef CLD_TRAPPED |
| 11320 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11321 | #endif |
| 11322 | #ifdef CLD_CONTINUED |
| 11323 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11324 | #endif |
| 11325 | |
| 11326 | /* constants for lockf */ |
| 11327 | #ifdef F_LOCK |
| 11328 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11329 | #endif |
| 11330 | #ifdef F_TLOCK |
| 11331 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11332 | #endif |
| 11333 | #ifdef F_ULOCK |
| 11334 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11335 | #endif |
| 11336 | #ifdef F_TEST |
| 11337 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11338 | #endif |
| 11339 | |
| 11340 | /* constants for futimens */ |
| 11341 | #ifdef UTIME_NOW |
| 11342 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11343 | #endif |
| 11344 | #ifdef UTIME_OMIT |
| 11345 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11346 | #endif |
| 11347 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11348 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11349 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11351 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11352 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11353 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11354 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11355 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11356 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11357 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11358 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11359 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11360 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11361 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11362 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11363 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11364 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11365 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11366 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11367 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11368 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11369 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11370 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11372 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11373 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11374 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11375 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11376 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11377 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11378 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11379 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11380 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11381 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11382 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11383 | #ifdef SCHED_SPORADIC |
| 11384 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11385 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11386 | #ifdef SCHED_BATCH |
| 11387 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11388 | #endif |
| 11389 | #ifdef SCHED_IDLE |
| 11390 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11391 | #endif |
| 11392 | #ifdef SCHED_RESET_ON_FORK |
| 11393 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11394 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11395 | #ifdef SCHED_SYS |
| 11396 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11397 | #endif |
| 11398 | #ifdef SCHED_IA |
| 11399 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11400 | #endif |
| 11401 | #ifdef SCHED_FSS |
| 11402 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11403 | #endif |
| 11404 | #ifdef SCHED_FX |
| 11405 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11406 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11407 | #endif |
| 11408 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11409 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11410 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11411 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11412 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11413 | #endif |
| 11414 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 11415 | #ifdef RTLD_LAZY |
| 11416 | if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1; |
| 11417 | #endif |
| 11418 | #ifdef RTLD_NOW |
| 11419 | if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1; |
| 11420 | #endif |
| 11421 | #ifdef RTLD_GLOBAL |
| 11422 | if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1; |
| 11423 | #endif |
| 11424 | #ifdef RTLD_LOCAL |
| 11425 | if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1; |
| 11426 | #endif |
| 11427 | #ifdef RTLD_NODELETE |
| 11428 | if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1; |
| 11429 | #endif |
| 11430 | #ifdef RTLD_NOLOAD |
| 11431 | if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1; |
| 11432 | #endif |
| 11433 | #ifdef RTLD_DEEPBIND |
| 11434 | if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1; |
| 11435 | #endif |
| 11436 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11437 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11438 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11439 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11440 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11441 | } |
| 11442 | |
| 11443 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11444 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11445 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11446 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11447 | |
| 11448 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11449 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11450 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11451 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11452 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11453 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11454 | #define MODNAME "posix" |
| 11455 | #endif |
| 11456 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11457 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | PyModuleDef_HEAD_INIT, |
| 11459 | MODNAME, |
| 11460 | posix__doc__, |
| 11461 | -1, |
| 11462 | posix_methods, |
| 11463 | NULL, |
| 11464 | NULL, |
| 11465 | NULL, |
| 11466 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11467 | }; |
| 11468 | |
| 11469 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11470 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11471 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11474 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11475 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11476 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11477 | #endif |
| 11478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | m = PyModule_Create(&posixmodule); |
| 11480 | if (m == NULL) |
| 11481 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11482 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11483 | /* Initialize environ dictionary */ |
| 11484 | v = convertenviron(); |
| 11485 | Py_XINCREF(v); |
| 11486 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11487 | return NULL; |
| 11488 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | if (all_ins(m)) |
| 11491 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11492 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | if (setup_confname_tables(m)) |
| 11494 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11495 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | Py_INCREF(PyExc_OSError); |
| 11497 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11498 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11499 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11500 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11501 | return NULL; |
| 11502 | Py_INCREF(&cpu_set_type); |
| 11503 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11504 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11505 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11506 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11507 | if (posix_putenv_garbage == NULL) |
| 11508 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11509 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11510 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11512 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11513 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11514 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11515 | #endif |
| 11516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11517 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11518 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11519 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11520 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11521 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11522 | structseq_new = StatResultType.tp_new; |
| 11523 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11525 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11526 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11527 | #ifdef NEED_TICKS_PER_SECOND |
| 11528 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11529 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11530 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11531 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11532 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11534 | # endif |
| 11535 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11536 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11537 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11538 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11539 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11540 | SchedParamType.tp_new = sched_param_new; |
| 11541 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11542 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11543 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11544 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11545 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11546 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | Py_INCREF((PyObject*) &StatResultType); |
| 11548 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11549 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11550 | PyModule_AddObject(m, "statvfs_result", |
| 11551 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11552 | |
| 11553 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11554 | Py_INCREF(&SchedParamType); |
| 11555 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11556 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11558 | |
| 11559 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | /* |
| 11561 | * Step 2 of weak-linking support on Mac OS X. |
| 11562 | * |
| 11563 | * The code below removes functions that are not available on the |
| 11564 | * currently active platform. |
| 11565 | * |
| 11566 | * This block allow one to use a python binary that was build on |
| 11567 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11568 | * OSX 10.4. |
| 11569 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11570 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11571 | if (fstatvfs == NULL) { |
| 11572 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11573 | return NULL; |
| 11574 | } |
| 11575 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11576 | #endif /* HAVE_FSTATVFS */ |
| 11577 | |
| 11578 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11579 | if (statvfs == NULL) { |
| 11580 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11581 | return NULL; |
| 11582 | } |
| 11583 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11584 | #endif /* HAVE_STATVFS */ |
| 11585 | |
| 11586 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11587 | if (lchown == NULL) { |
| 11588 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11589 | return NULL; |
| 11590 | } |
| 11591 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11592 | #endif /* HAVE_LCHOWN */ |
| 11593 | |
| 11594 | |
| 11595 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11596 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11597 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11598 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11599 | |
| 11600 | #ifdef __cplusplus |
| 11601 | } |
| 11602 | #endif |