Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the |
| 5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few |
| 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler |
| 10 | independent macro PYOS_OS2 should be defined. On OS/2 the default |
| 11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used |
| 12 | as the compiler specific macro for the EMX port of gcc to OS/2. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | #ifdef __APPLE__ |
| 15 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 16 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 18 | * at the end of this file for more information. |
| 19 | */ |
| 20 | # pragma weak lchown |
| 21 | # pragma weak statvfs |
| 22 | # pragma weak fstatvfs |
| 23 | |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 26 | #define PY_SSIZE_T_CLEAN |
| 27 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 30 | #if defined(__VMS) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 31 | # error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 32 | # include <unixio.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 33 | #endif /* defined(__VMS) */ |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 39 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 40 | "This module provides access to operating system functionality that is\n\ |
| 41 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 42 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 43 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 46 | #if defined(PYOS_OS2) |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 47 | #error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 48 | #define INCL_DOS |
| 49 | #define INCL_DOSERRORS |
| 50 | #define INCL_DOSPROCESS |
| 51 | #define INCL_NOPMAPI |
| 52 | #include <os2.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 53 | #if defined(PYCC_GCC) |
| 54 | #include <ctype.h> |
| 55 | #include <io.h> |
| 56 | #include <stdio.h> |
| 57 | #include <process.h> |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 58 | #endif |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 59 | #include "osdefs.h" |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 62 | #ifdef HAVE_SYS_UIO_H |
| 63 | #include <sys/uio.h> |
| 64 | #endif |
| 65 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #endif /* HAVE_SYS_TYPES_H */ |
| 69 | |
| 70 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 75 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 79 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #ifdef HAVE_FCNTL_H |
| 83 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 84 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #endif |
| 89 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 90 | #ifdef HAVE_SYSEXITS_H |
| 91 | #include <sysexits.h> |
| 92 | #endif /* HAVE_SYSEXITS_H */ |
| 93 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 94 | #ifdef HAVE_SYS_LOADAVG_H |
| 95 | #include <sys/loadavg.h> |
| 96 | #endif |
| 97 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 98 | #ifdef HAVE_LANGINFO_H |
| 99 | #include <langinfo.h> |
| 100 | #endif |
| 101 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 102 | #ifdef HAVE_SYS_SENDFILE_H |
| 103 | #include <sys/sendfile.h> |
| 104 | #endif |
| 105 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 106 | #ifdef HAVE_SCHED_H |
| 107 | #include <sched.h> |
| 108 | #endif |
| 109 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 110 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) |
| 111 | #define USE_XATTRS |
| 112 | #endif |
| 113 | |
| 114 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 115 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 116 | #endif |
| 117 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 118 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 119 | #ifdef HAVE_SYS_SOCKET_H |
| 120 | #include <sys/socket.h> |
| 121 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 124 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 125 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 126 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 127 | #include <process.h> |
| 128 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 129 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 130 | #define HAVE_GETCWD 1 |
| 131 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 132 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 133 | #if defined(__OS2__) |
| 134 | #define HAVE_EXECV 1 |
| 135 | #define HAVE_WAIT 1 |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 136 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 137 | #include <process.h> |
| 138 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 139 | #ifdef __BORLANDC__ /* Borland compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 140 | #define HAVE_EXECV 1 |
| 141 | #define HAVE_GETCWD 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 142 | #define HAVE_OPENDIR 1 |
| 143 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 144 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 145 | #define HAVE_WAIT 1 |
| 146 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 147 | #ifdef _MSC_VER /* Microsoft compiler */ |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 148 | #define HAVE_GETCWD 1 |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 149 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 150 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 151 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 152 | #define HAVE_EXECV 1 |
| 153 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 154 | #define HAVE_SYSTEM 1 |
| 155 | #define HAVE_CWAIT 1 |
| 156 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 157 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 158 | #else |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 159 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) |
| 160 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 161 | #else /* all other compilers */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 162 | /* Unix functions that the configure script doesn't check for */ |
| 163 | #define HAVE_EXECV 1 |
| 164 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 165 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 166 | #define HAVE_FORK1 1 |
| 167 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 168 | #define HAVE_GETCWD 1 |
| 169 | #define HAVE_GETEGID 1 |
| 170 | #define HAVE_GETEUID 1 |
| 171 | #define HAVE_GETGID 1 |
| 172 | #define HAVE_GETPPID 1 |
| 173 | #define HAVE_GETUID 1 |
| 174 | #define HAVE_KILL 1 |
| 175 | #define HAVE_OPENDIR 1 |
| 176 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 177 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 179 | #define HAVE_TTYNAME 1 |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 180 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | #endif /* _MSC_VER */ |
| 182 | #endif /* __BORLANDC__ */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 183 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 184 | #endif /* ! __IBMC__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 186 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 187 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 188 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 189 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 190 | (default) */ |
| 191 | extern char *ctermid_r(char *); |
| 192 | #endif |
| 193 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 194 | #ifndef HAVE_UNISTD_H |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 195 | #if defined(PYCC_VACPP) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 196 | extern int mkdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 197 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 198 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 199 | extern int mkdir(const char *); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 200 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 201 | extern int mkdir(const char *, mode_t); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 202 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 203 | #endif |
| 204 | #if defined(__IBMC__) || defined(__IBMCPP__) |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 205 | extern int chdir(char *); |
| 206 | extern int rmdir(char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 208 | extern int chdir(const char *); |
| 209 | extern int rmdir(const char *); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 210 | #endif |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 211 | #ifdef __BORLANDC__ |
| 212 | extern int chmod(const char *, int); |
| 213 | #else |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 214 | extern int chmod(const char *, mode_t); |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 215 | #endif |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 216 | /*#ifdef HAVE_FCHMOD |
| 217 | extern int fchmod(int, mode_t); |
| 218 | #endif*/ |
| 219 | /*#ifdef HAVE_LCHMOD |
| 220 | extern int lchmod(const char *, mode_t); |
| 221 | #endif*/ |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 222 | extern int chown(const char *, uid_t, gid_t); |
| 223 | extern char *getcwd(char *, int); |
| 224 | extern char *strerror(int); |
| 225 | extern int link(const char *, const char *); |
| 226 | extern int rename(const char *, const char *); |
| 227 | extern int stat(const char *, struct stat *); |
| 228 | extern int unlink(const char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | #ifdef HAVE_SYMLINK |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 230 | extern int symlink(const char *, const char *); |
Guido van Rossum | a38a503 | 1995-02-17 15:11:36 +0000 | [diff] [blame] | 231 | #endif /* HAVE_SYMLINK */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #ifdef HAVE_LSTAT |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 233 | extern int lstat(const char *, struct stat *); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 234 | #endif /* HAVE_LSTAT */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | #endif /* !HAVE_UNISTD_H */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #ifdef HAVE_UTIME_H |
| 240 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 241 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 243 | #ifdef HAVE_SYS_UTIME_H |
| 244 | #include <sys/utime.h> |
| 245 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 246 | #endif /* HAVE_SYS_UTIME_H */ |
| 247 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | #ifdef HAVE_SYS_TIMES_H |
| 249 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 250 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef HAVE_SYS_PARAM_H |
| 253 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
| 256 | #ifdef HAVE_SYS_UTSNAME_H |
| 257 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 258 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 260 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 262 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 263 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 264 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 265 | #include <direct.h> |
| 266 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 267 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 269 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 270 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #endif |
| 274 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #endif |
| 277 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 279 | #endif |
| 280 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 282 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 284 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | #endif |
| 286 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 287 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | #endif |
| 289 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 292 | #ifndef VOLUME_NAME_DOS |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 293 | #define VOLUME_NAME_DOS 0x0 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #ifndef VOLUME_NAME_NT |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 296 | #define VOLUME_NAME_NT 0x2 |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 299 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 300 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 301 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 302 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 303 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 304 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 305 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 306 | #ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */ |
| 307 | #define HAVE_SYMLINK |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 308 | static int win32_can_symlink = 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 309 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 310 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 312 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 313 | #include <io.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 314 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 317 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 318 | #define MAXPATHLEN PATH_MAX |
| 319 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 320 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 322 | #endif /* MAXPATHLEN */ |
| 323 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 324 | #ifdef UNION_WAIT |
| 325 | /* Emulate some macros on systems that have a union instead of macros */ |
| 326 | |
| 327 | #ifndef WIFEXITED |
| 328 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 329 | #endif |
| 330 | |
| 331 | #ifndef WEXITSTATUS |
| 332 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 333 | #endif |
| 334 | |
| 335 | #ifndef WTERMSIG |
| 336 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 337 | #endif |
| 338 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 339 | #define WAIT_TYPE union wait |
| 340 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 341 | |
| 342 | #else /* !UNION_WAIT */ |
| 343 | #define WAIT_TYPE int |
| 344 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 345 | #endif /* UNION_WAIT */ |
| 346 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 347 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 348 | prototype for it, at least on Solaris -- maybe others as well?). */ |
| 349 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) |
| 350 | #define USE_CTERMID_R |
| 351 | #endif |
| 352 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 353 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 354 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 355 | #undef FSTAT |
| 356 | #undef STRUCT_STAT |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 357 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 358 | # define STAT win32_stat |
| 359 | # define FSTAT win32_fstat |
| 360 | # define STRUCT_STAT struct win32_stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 362 | # define STAT stat |
| 363 | # define FSTAT fstat |
| 364 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 367 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 368 | #include <sys/mkdev.h> |
| 369 | #else |
| 370 | #if defined(MAJOR_IN_SYSMACROS) |
| 371 | #include <sys/sysmacros.h> |
| 372 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 373 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 374 | #include <sys/mkdev.h> |
| 375 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 376 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 377 | |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 378 | /* A helper used by a number of POSIX-only functions */ |
| 379 | #ifndef MS_WINDOWS |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 380 | static int |
| 381 | _parse_off_t(PyObject* arg, void* addr) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 382 | { |
| 383 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 384 | *((off_t*)addr) = PyLong_AsLong(arg); |
| 385 | #else |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 386 | *((off_t*)addr) = PyLong_AsLongLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 387 | #endif |
| 388 | if (PyErr_Occurred()) |
| 389 | return 0; |
| 390 | return 1; |
| 391 | } |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 392 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 393 | |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 394 | #if defined _MSC_VER && _MSC_VER >= 1400 |
| 395 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 396 | * valid and throw an assertion if it isn't. |
| 397 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 398 | * an assertion can be useful, but it does contradict the POSIX standard |
| 399 | * which for write(2) states: |
| 400 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 401 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 402 | * writing." |
| 403 | * Furthermore, python allows the user to enter any old integer |
| 404 | * as a fd and should merely raise a python exception on error. |
| 405 | * The Microsoft CRT doesn't provide an official way to check for the |
| 406 | * validity of a file descriptor, but we can emulate its internal behaviour |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 407 | * by using the exported __pinfo data member and knowledge of the |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 408 | * internal structures involved. |
| 409 | * The structures below must be updated for each version of visual studio |
| 410 | * according to the file internal.h in the CRT source, until MS comes |
| 411 | * up with a less hacky way to do this. |
| 412 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 413 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 414 | */ |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 415 | /* The actual size of the structure is determined at runtime. |
| 416 | * Only the first items must be present. |
| 417 | */ |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 418 | typedef struct { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 419 | intptr_t osfhnd; |
| 420 | char osfile; |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 421 | } my_ioinfo; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 422 | |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 423 | extern __declspec(dllimport) char * __pioinfo[]; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 424 | #define IOINFO_L2E 5 |
| 425 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 426 | #define IOINFO_ARRAYS 64 |
| 427 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 428 | #define FOPEN 0x01 |
| 429 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 430 | |
| 431 | /* This function emulates what the windows CRT does to validate file handles */ |
| 432 | int |
| 433 | _PyVerify_fd(int fd) |
| 434 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 435 | const int i1 = fd >> IOINFO_L2E; |
| 436 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 438 | static size_t sizeof_ioinfo = 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 439 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 440 | /* Determine the actual size of the ioinfo structure, |
| 441 | * as used by the CRT loaded in memory |
| 442 | */ |
| 443 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 444 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 445 | } |
| 446 | if (sizeof_ioinfo == 0) { |
| 447 | /* This should not happen... */ |
| 448 | goto fail; |
| 449 | } |
| 450 | |
| 451 | /* See that it isn't a special CLEAR fileno */ |
| 452 | if (fd != _NO_CONSOLE_FILENO) { |
| 453 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 454 | * we check pointer validity and other info |
| 455 | */ |
| 456 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 457 | /* finally, check that the file is open */ |
| 458 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 459 | if (info->osfile & FOPEN) { |
| 460 | return 1; |
| 461 | } |
| 462 | } |
| 463 | } |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 464 | fail: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 465 | errno = EBADF; |
| 466 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* the special case of checking dup2. The target fd must be in a sensible range */ |
| 470 | static int |
| 471 | _PyVerify_fd_dup2(int fd1, int fd2) |
| 472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 473 | if (!_PyVerify_fd(fd1)) |
| 474 | return 0; |
| 475 | if (fd2 == _NO_CONSOLE_FILENO) |
| 476 | return 0; |
| 477 | if ((unsigned)fd2 < _NHANDLE_) |
| 478 | return 1; |
| 479 | else |
| 480 | return 0; |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 481 | } |
| 482 | #else |
| 483 | /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ |
| 484 | #define _PyVerify_fd_dup2(A, B) (1) |
| 485 | #endif |
| 486 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 487 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 488 | /* The following structure was copied from |
| 489 | http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required |
| 490 | include doesn't seem to be present in the Windows SDK (at least as included |
| 491 | with Visual Studio Express). */ |
| 492 | typedef struct _REPARSE_DATA_BUFFER { |
| 493 | ULONG ReparseTag; |
| 494 | USHORT ReparseDataLength; |
| 495 | USHORT Reserved; |
| 496 | union { |
| 497 | struct { |
| 498 | USHORT SubstituteNameOffset; |
| 499 | USHORT SubstituteNameLength; |
| 500 | USHORT PrintNameOffset; |
| 501 | USHORT PrintNameLength; |
| 502 | ULONG Flags; |
| 503 | WCHAR PathBuffer[1]; |
| 504 | } SymbolicLinkReparseBuffer; |
| 505 | |
| 506 | struct { |
| 507 | USHORT SubstituteNameOffset; |
| 508 | USHORT SubstituteNameLength; |
| 509 | USHORT PrintNameOffset; |
| 510 | USHORT PrintNameLength; |
| 511 | WCHAR PathBuffer[1]; |
| 512 | } MountPointReparseBuffer; |
| 513 | |
| 514 | struct { |
| 515 | UCHAR DataBuffer[1]; |
| 516 | } GenericReparseBuffer; |
| 517 | }; |
| 518 | } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 519 | |
| 520 | #define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ |
| 521 | GenericReparseBuffer) |
| 522 | #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) |
| 523 | |
| 524 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 525 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 526 | { |
| 527 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 528 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 529 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 530 | |
| 531 | if (0 == DeviceIoControl( |
| 532 | reparse_point_handle, |
| 533 | FSCTL_GET_REPARSE_POINT, |
| 534 | NULL, 0, /* in buffer */ |
| 535 | target_buffer, sizeof(target_buffer), |
| 536 | &n_bytes_returned, |
| 537 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 538 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 539 | |
| 540 | if (reparse_tag) |
| 541 | *reparse_tag = rdb->ReparseTag; |
| 542 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 543 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 544 | } |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 545 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | /* Return a dictionary corresponding to the POSIX environment table */ |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 548 | #ifdef WITH_NEXT_FRAMEWORK |
| 549 | /* On Darwin/MacOSX a shared library or framework has no access to |
| 550 | ** environ directly, we must obtain it with _NSGetEnviron(). |
| 551 | */ |
| 552 | #include <crt_externs.h> |
| 553 | static char **environ; |
| 554 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 556 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 558 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 559 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 561 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 562 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 563 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 564 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 565 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 566 | #endif |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 567 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 568 | APIRET rc; |
| 569 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ |
| 570 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 571 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 572 | d = PyDict_New(); |
| 573 | if (d == NULL) |
| 574 | return NULL; |
| 575 | #ifdef WITH_NEXT_FRAMEWORK |
| 576 | if (environ == NULL) |
| 577 | environ = *_NSGetEnviron(); |
| 578 | #endif |
| 579 | #ifdef MS_WINDOWS |
| 580 | /* _wenviron must be initialized in this way if the program is started |
| 581 | through main() instead of wmain(). */ |
| 582 | _wgetenv(L""); |
| 583 | if (_wenviron == NULL) |
| 584 | return d; |
| 585 | /* This part ignores errors */ |
| 586 | for (e = _wenviron; *e != NULL; e++) { |
| 587 | PyObject *k; |
| 588 | PyObject *v; |
| 589 | wchar_t *p = wcschr(*e, L'='); |
| 590 | if (p == NULL) |
| 591 | continue; |
| 592 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 593 | if (k == NULL) { |
| 594 | PyErr_Clear(); |
| 595 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 596 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 597 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 598 | if (v == NULL) { |
| 599 | PyErr_Clear(); |
| 600 | Py_DECREF(k); |
| 601 | continue; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 602 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 603 | if (PyDict_GetItem(d, k) == NULL) { |
| 604 | if (PyDict_SetItem(d, k, v) != 0) |
| 605 | PyErr_Clear(); |
| 606 | } |
| 607 | Py_DECREF(k); |
| 608 | Py_DECREF(v); |
| 609 | } |
| 610 | #else |
| 611 | if (environ == NULL) |
| 612 | return d; |
| 613 | /* This part ignores errors */ |
| 614 | for (e = environ; *e != NULL; e++) { |
| 615 | PyObject *k; |
| 616 | PyObject *v; |
| 617 | char *p = strchr(*e, '='); |
| 618 | if (p == NULL) |
| 619 | continue; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 620 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 621 | if (k == NULL) { |
| 622 | PyErr_Clear(); |
| 623 | continue; |
| 624 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 625 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 626 | if (v == NULL) { |
| 627 | PyErr_Clear(); |
| 628 | Py_DECREF(k); |
| 629 | continue; |
| 630 | } |
| 631 | if (PyDict_GetItem(d, k) == NULL) { |
| 632 | if (PyDict_SetItem(d, k, v) != 0) |
| 633 | PyErr_Clear(); |
| 634 | } |
| 635 | Py_DECREF(k); |
| 636 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 637 | } |
| 638 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 639 | #if defined(PYOS_OS2) |
| 640 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); |
| 641 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ |
| 642 | PyObject *v = PyBytes_FromString(buffer); |
| 643 | PyDict_SetItemString(d, "BEGINLIBPATH", v); |
| 644 | Py_DECREF(v); |
| 645 | } |
| 646 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); |
| 647 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ |
| 648 | PyObject *v = PyBytes_FromString(buffer); |
| 649 | PyDict_SetItemString(d, "ENDLIBPATH", v); |
| 650 | Py_DECREF(v); |
| 651 | } |
| 652 | #endif |
| 653 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 657 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 658 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 659 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 660 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 661 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 662 | } |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 664 | posix_error_with_filename(char* name) |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 666 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 669 | |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 670 | static PyObject * |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 671 | posix_error_with_allocated_filename(PyObject* name) |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 672 | { |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 673 | PyObject *name_str, *rc; |
| 674 | name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name), |
| 675 | PyBytes_GET_SIZE(name)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 676 | Py_DECREF(name); |
Victor Stinner | 77ccd6d | 2010-05-08 00:36:42 +0000 | [diff] [blame] | 677 | rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, |
| 678 | name_str); |
| 679 | Py_XDECREF(name_str); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 680 | return rc; |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 683 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 684 | static PyObject * |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 685 | win32_error(char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 687 | /* XXX We should pass the function name along in the future. |
| 688 | (winreg.c also wants to pass the function name.) |
| 689 | This would however require an additional param to the |
| 690 | Windows error object, which is non-trivial. |
| 691 | */ |
| 692 | errno = GetLastError(); |
| 693 | if (filename) |
| 694 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 695 | else |
| 696 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 697 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 698 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 699 | static PyObject * |
| 700 | win32_error_unicode(char* function, Py_UNICODE* filename) |
| 701 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 702 | /* XXX - see win32_error for comments on 'function' */ |
| 703 | errno = GetLastError(); |
| 704 | if (filename) |
| 705 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); |
| 706 | else |
| 707 | return PyErr_SetFromWindowsErr(errno); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 710 | static int |
Hirokazu Yamamoto | d7e4c08 | 2008-08-17 09:30:15 +0000 | [diff] [blame] | 711 | convert_to_unicode(PyObject **param) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 712 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 713 | if (PyUnicode_CheckExact(*param)) |
| 714 | Py_INCREF(*param); |
| 715 | else if (PyUnicode_Check(*param)) |
| 716 | /* For a Unicode subtype that's not a Unicode object, |
| 717 | return a true Unicode object with the same data. */ |
| 718 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), |
| 719 | PyUnicode_GET_SIZE(*param)); |
| 720 | else |
| 721 | *param = PyUnicode_FromEncodedObject(*param, |
| 722 | Py_FileSystemDefaultEncoding, |
| 723 | "strict"); |
| 724 | return (*param) != NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 727 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 729 | #if defined(PYOS_OS2) |
| 730 | /********************************************************************** |
| 731 | * Helper Function to Trim and Format OS/2 Messages |
| 732 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 733 | static void |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 734 | os2_formatmsg(char *msgbuf, int msglen, char *reason) |
| 735 | { |
| 736 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */ |
| 737 | |
| 738 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */ |
| 739 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ]; |
| 740 | |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 741 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc))) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 742 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */ |
| 743 | } |
| 744 | |
| 745 | /* Add Optional Reason Text */ |
| 746 | if (reason) { |
| 747 | strcat(msgbuf, " : "); |
| 748 | strcat(msgbuf, reason); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /********************************************************************** |
| 753 | * Decode an OS/2 Operating System Error Code |
| 754 | * |
| 755 | * A convenience function to lookup an OS/2 error code and return a |
| 756 | * text message we can use to raise a Python exception. |
| 757 | * |
| 758 | * Notes: |
| 759 | * The messages for errors returned from the OS/2 kernel reside in |
| 760 | * the file OSO001.MSG in the \OS2 directory hierarchy. |
| 761 | * |
| 762 | **********************************************************************/ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 763 | static char * |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 764 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) |
| 765 | { |
| 766 | APIRET rc; |
| 767 | ULONG msglen; |
| 768 | |
| 769 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */ |
| 770 | Py_BEGIN_ALLOW_THREADS |
| 771 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen, |
| 772 | errorcode, "oso001.msg", &msglen); |
| 773 | Py_END_ALLOW_THREADS |
| 774 | |
| 775 | if (rc == NO_ERROR) |
| 776 | os2_formatmsg(msgbuf, msglen, reason); |
| 777 | else |
Tim Peters | 1ceb5fb | 2001-11-28 20:32:57 +0000 | [diff] [blame] | 778 | PyOS_snprintf(msgbuf, msgbuflen, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 779 | "unknown OS error #%d", errorcode); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 780 | |
| 781 | return msgbuf; |
| 782 | } |
| 783 | |
| 784 | /* Set an OS/2-specific error and return NULL. OS/2 kernel |
| 785 | errors are not in a global variable e.g. 'errno' nor are |
| 786 | they congruent with posix error numbers. */ |
| 787 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 788 | static PyObject * |
| 789 | os2_error(int code) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 790 | { |
| 791 | char text[1024]; |
| 792 | PyObject *v; |
| 793 | |
| 794 | os2_strerror(text, sizeof(text), code, ""); |
| 795 | |
| 796 | v = Py_BuildValue("(is)", code, text); |
| 797 | if (v != NULL) { |
Barry Warsaw | ca74da4 | 1999-02-09 19:31:45 +0000 | [diff] [blame] | 798 | PyErr_SetObject(PyExc_OSError, v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 799 | Py_DECREF(v); |
| 800 | } |
| 801 | return NULL; /* Signal to Python that an Exception is Pending */ |
| 802 | } |
| 803 | |
| 804 | #endif /* OS2 */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 805 | |
| 806 | /* POSIX generic methods */ |
| 807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 808 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 809 | posix_fildes(PyObject *fdobj, int (*func)(int)) |
| 810 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 811 | int fd; |
| 812 | int res; |
| 813 | fd = PyObject_AsFileDescriptor(fdobj); |
| 814 | if (fd < 0) |
| 815 | return NULL; |
| 816 | if (!_PyVerify_fd(fd)) |
| 817 | return posix_error(); |
| 818 | Py_BEGIN_ALLOW_THREADS |
| 819 | res = (*func)(fd); |
| 820 | Py_END_ALLOW_THREADS |
| 821 | if (res < 0) |
| 822 | return posix_error(); |
| 823 | Py_INCREF(Py_None); |
| 824 | return Py_None; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 825 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 826 | |
| 827 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 828 | posix_1str(PyObject *args, char *format, int (*func)(const char*)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 829 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 830 | PyObject *opath1 = NULL; |
| 831 | char *path1; |
| 832 | int res; |
| 833 | if (!PyArg_ParseTuple(args, format, |
| 834 | PyUnicode_FSConverter, &opath1)) |
| 835 | return NULL; |
| 836 | path1 = PyBytes_AsString(opath1); |
| 837 | Py_BEGIN_ALLOW_THREADS |
| 838 | res = (*func)(path1); |
| 839 | Py_END_ALLOW_THREADS |
| 840 | if (res < 0) |
| 841 | return posix_error_with_allocated_filename(opath1); |
| 842 | Py_DECREF(opath1); |
| 843 | Py_INCREF(Py_None); |
| 844 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 847 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 848 | posix_2str(PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 849 | char *format, |
| 850 | int (*func)(const char *, const char *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 851 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 852 | PyObject *opath1 = NULL, *opath2 = NULL; |
| 853 | char *path1, *path2; |
| 854 | int res; |
| 855 | if (!PyArg_ParseTuple(args, format, |
| 856 | PyUnicode_FSConverter, &opath1, |
| 857 | PyUnicode_FSConverter, &opath2)) { |
| 858 | return NULL; |
| 859 | } |
| 860 | path1 = PyBytes_AsString(opath1); |
| 861 | path2 = PyBytes_AsString(opath2); |
| 862 | Py_BEGIN_ALLOW_THREADS |
| 863 | res = (*func)(path1, path2); |
| 864 | Py_END_ALLOW_THREADS |
| 865 | Py_DECREF(opath1); |
| 866 | Py_DECREF(opath2); |
| 867 | if (res != 0) |
| 868 | /* XXX how to report both path1 and path2??? */ |
| 869 | return posix_error(); |
| 870 | Py_INCREF(Py_None); |
| 871 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 874 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 875 | static PyObject* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 876 | win32_1str(PyObject* args, char* func, |
| 877 | char* format, BOOL (__stdcall *funcA)(LPCSTR), |
| 878 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 879 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 880 | PyObject *uni; |
| 881 | char *ansi; |
| 882 | BOOL result; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 883 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 884 | if (!PyArg_ParseTuple(args, wformat, &uni)) |
| 885 | PyErr_Clear(); |
| 886 | else { |
| 887 | Py_BEGIN_ALLOW_THREADS |
| 888 | result = funcW(PyUnicode_AsUnicode(uni)); |
| 889 | Py_END_ALLOW_THREADS |
| 890 | if (!result) |
| 891 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); |
| 892 | Py_INCREF(Py_None); |
| 893 | return Py_None; |
| 894 | } |
| 895 | if (!PyArg_ParseTuple(args, format, &ansi)) |
| 896 | return NULL; |
| 897 | Py_BEGIN_ALLOW_THREADS |
| 898 | result = funcA(ansi); |
| 899 | Py_END_ALLOW_THREADS |
| 900 | if (!result) |
| 901 | return win32_error(func, ansi); |
| 902 | Py_INCREF(Py_None); |
| 903 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 904 | |
| 905 | } |
| 906 | |
| 907 | /* This is a reimplementation of the C library's chdir function, |
| 908 | but one that produces Win32 errors instead of DOS error codes. |
| 909 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 910 | it also needs to set "magic" environment variables indicating |
| 911 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 912 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 913 | win32_chdir(LPCSTR path) |
| 914 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 915 | char new_path[MAX_PATH+1]; |
| 916 | int result; |
| 917 | char env[4] = "=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 918 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 919 | if(!SetCurrentDirectoryA(path)) |
| 920 | return FALSE; |
| 921 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path); |
| 922 | if (!result) |
| 923 | return FALSE; |
| 924 | /* In the ANSI API, there should not be any paths longer |
| 925 | than MAX_PATH. */ |
| 926 | assert(result <= MAX_PATH+1); |
| 927 | if (strncmp(new_path, "\\\\", 2) == 0 || |
| 928 | strncmp(new_path, "//", 2) == 0) |
| 929 | /* UNC path, nothing to do. */ |
| 930 | return TRUE; |
| 931 | env[1] = new_path[0]; |
| 932 | return SetEnvironmentVariableA(env, new_path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /* The Unicode version differs from the ANSI version |
| 936 | since the current directory might exceed MAX_PATH characters */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 937 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 938 | win32_wchdir(LPCWSTR path) |
| 939 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 940 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; |
| 941 | int result; |
| 942 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 943 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 944 | if(!SetCurrentDirectoryW(path)) |
| 945 | return FALSE; |
| 946 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path); |
| 947 | if (!result) |
| 948 | return FALSE; |
| 949 | if (result > MAX_PATH+1) { |
| 950 | new_path = malloc(result * sizeof(wchar_t)); |
| 951 | if (!new_path) { |
| 952 | SetLastError(ERROR_OUTOFMEMORY); |
| 953 | return FALSE; |
| 954 | } |
| 955 | result = GetCurrentDirectoryW(result, new_path); |
| 956 | if (!result) { |
| 957 | free(new_path); |
| 958 | return FALSE; |
| 959 | } |
| 960 | } |
| 961 | if (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 962 | wcsncmp(new_path, L"//", 2) == 0) |
| 963 | /* UNC path, nothing to do. */ |
| 964 | return TRUE; |
| 965 | env[1] = new_path[0]; |
| 966 | result = SetEnvironmentVariableW(env, new_path); |
| 967 | if (new_path != _new_path) |
| 968 | free(new_path); |
| 969 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 970 | } |
| 971 | #endif |
| 972 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 973 | #ifdef MS_WINDOWS |
| 974 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 975 | - time stamps are restricted to second resolution |
| 976 | - file modification times suffer from forth-and-back conversions between |
| 977 | UTC and local time |
| 978 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 979 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 980 | #define HAVE_STAT_NSEC 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 981 | |
| 982 | struct win32_stat{ |
| 983 | int st_dev; |
| 984 | __int64 st_ino; |
| 985 | unsigned short st_mode; |
| 986 | int st_nlink; |
| 987 | int st_uid; |
| 988 | int st_gid; |
| 989 | int st_rdev; |
| 990 | __int64 st_size; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 991 | time_t st_atime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 992 | int st_atime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 993 | time_t st_mtime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 994 | int st_mtime_nsec; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 995 | time_t st_ctime; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 996 | int st_ctime_nsec; |
| 997 | }; |
| 998 | |
| 999 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 1000 | |
| 1001 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1002 | 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] | 1003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1004 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 1005 | /* Cannot simply cast and dereference in_ptr, |
| 1006 | since it might not be aligned properly */ |
| 1007 | __int64 in; |
| 1008 | memcpy(&in, in_ptr, sizeof(in)); |
| 1009 | *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] | 1010 | *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] | 1011 | } |
| 1012 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1013 | static void |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 1014 | 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] | 1015 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1016 | /* XXX endianness */ |
| 1017 | __int64 out; |
| 1018 | out = time_in + secs_between_epochs; |
| 1019 | out = out * 10000000 + nsec_in / 100; |
| 1020 | memcpy(out_ptr, &out, sizeof(out)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1023 | /* Below, we *know* that ugo+r is 0444 */ |
| 1024 | #if _S_IREAD != 0400 |
| 1025 | #error Unsupported C library |
| 1026 | #endif |
| 1027 | static int |
| 1028 | attributes_to_mode(DWORD attr) |
| 1029 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1030 | int m = 0; |
| 1031 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1032 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 1033 | else |
| 1034 | m |= _S_IFREG; |
| 1035 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 1036 | m |= 0444; |
| 1037 | else |
| 1038 | m |= 0666; |
| 1039 | return m; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1043 | 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] | 1044 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1045 | memset(result, 0, sizeof(*result)); |
| 1046 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 1047 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 1048 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 1049 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 1050 | 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] | 1051 | result->st_nlink = info->nNumberOfLinks; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1052 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1053 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 1054 | /* first clear the S_IFMT bits */ |
| 1055 | result->st_mode ^= (result->st_mode & 0170000); |
| 1056 | /* now set the bits that make this a symlink */ |
| 1057 | result->st_mode |= 0120000; |
| 1058 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1060 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1063 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1064 | 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] | 1065 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1066 | HANDLE hFindFile; |
| 1067 | WIN32_FIND_DATAA FileData; |
| 1068 | hFindFile = FindFirstFileA(pszFile, &FileData); |
| 1069 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1070 | return FALSE; |
| 1071 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1072 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1073 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1074 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1075 | info->ftCreationTime = FileData.ftCreationTime; |
| 1076 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1077 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1078 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1079 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1080 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1081 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1082 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1083 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | static BOOL |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1087 | 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] | 1088 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1089 | HANDLE hFindFile; |
| 1090 | WIN32_FIND_DATAW FileData; |
| 1091 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1092 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1093 | return FALSE; |
| 1094 | FindClose(hFindFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1095 | memset(info, 0, sizeof(*info)); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1096 | *reparse_tag = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1097 | info->dwFileAttributes = FileData.dwFileAttributes; |
| 1098 | info->ftCreationTime = FileData.ftCreationTime; |
| 1099 | info->ftLastAccessTime = FileData.ftLastAccessTime; |
| 1100 | info->ftLastWriteTime = FileData.ftLastWriteTime; |
| 1101 | info->nFileSizeHigh = FileData.nFileSizeHigh; |
| 1102 | info->nFileSizeLow = FileData.nFileSizeLow; |
| 1103 | /* info->nNumberOfLinks = 1; */ |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1104 | if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1105 | *reparse_tag = FileData.dwReserved0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1106 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1109 | /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ |
| 1110 | static int has_GetFinalPathNameByHandle = 0; |
| 1111 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, |
| 1112 | DWORD); |
| 1113 | static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, |
| 1114 | DWORD); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1115 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1116 | check_GetFinalPathNameByHandle() |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1117 | { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1118 | HINSTANCE hKernel32; |
| 1119 | /* only recheck */ |
| 1120 | if (!has_GetFinalPathNameByHandle) |
| 1121 | { |
| 1122 | hKernel32 = GetModuleHandle("KERNEL32"); |
| 1123 | *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, |
| 1124 | "GetFinalPathNameByHandleA"); |
| 1125 | *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32, |
| 1126 | "GetFinalPathNameByHandleW"); |
| 1127 | has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA && |
| 1128 | Py_GetFinalPathNameByHandleW; |
| 1129 | } |
| 1130 | return has_GetFinalPathNameByHandle; |
| 1131 | } |
| 1132 | |
| 1133 | static BOOL |
| 1134 | get_target_path(HANDLE hdl, wchar_t **target_path) |
| 1135 | { |
| 1136 | int buf_size, result_length; |
| 1137 | wchar_t *buf; |
| 1138 | |
| 1139 | /* We have a good handle to the target, use it to determine |
| 1140 | the target path name (then we'll call lstat on it). */ |
| 1141 | buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0, |
| 1142 | VOLUME_NAME_DOS); |
| 1143 | if(!buf_size) |
| 1144 | return FALSE; |
| 1145 | |
| 1146 | buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1147 | if (!buf) { |
| 1148 | SetLastError(ERROR_OUTOFMEMORY); |
| 1149 | return FALSE; |
| 1150 | } |
| 1151 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1152 | result_length = Py_GetFinalPathNameByHandleW(hdl, |
| 1153 | buf, buf_size, VOLUME_NAME_DOS); |
| 1154 | |
| 1155 | if(!result_length) { |
| 1156 | free(buf); |
| 1157 | return FALSE; |
| 1158 | } |
| 1159 | |
| 1160 | if(!CloseHandle(hdl)) { |
| 1161 | free(buf); |
| 1162 | return FALSE; |
| 1163 | } |
| 1164 | |
| 1165 | buf[result_length] = 0; |
| 1166 | |
| 1167 | *target_path = buf; |
| 1168 | return TRUE; |
| 1169 | } |
| 1170 | |
| 1171 | static int |
| 1172 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1173 | BOOL traverse); |
| 1174 | static int |
| 1175 | win32_xstat_impl(const char *path, struct win32_stat *result, |
| 1176 | BOOL traverse) |
| 1177 | { |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1178 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1179 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1180 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1181 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1182 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1183 | const char *dot; |
| 1184 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1185 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1186 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1187 | traverse reparse point. */ |
| 1188 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1191 | hFile = CreateFileA( |
| 1192 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1193 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1194 | 0, /* share mode */ |
| 1195 | NULL, /* security attributes */ |
| 1196 | OPEN_EXISTING, |
| 1197 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1198 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1199 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1200 | the symlink path agin and not the actual final path. */ |
| 1201 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
| 1202 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1203 | NULL); |
| 1204 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1205 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1206 | /* Either the target doesn't exist, or we don't have access to |
| 1207 | get a handle to it. If the former, we need to return an error. |
| 1208 | If the latter, we can use attributes_from_dir. */ |
| 1209 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1210 | return -1; |
| 1211 | /* Could not get attributes on open file. Fall back to |
| 1212 | reading the directory. */ |
| 1213 | if (!attributes_from_dir(path, &info, &reparse_tag)) |
| 1214 | /* Very strange. This should not fail now */ |
| 1215 | return -1; |
| 1216 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1217 | if (traverse) { |
| 1218 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1219 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1220 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1221 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1222 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1223 | } else { |
| 1224 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1225 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1226 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1227 | } |
| 1228 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1229 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1230 | return -1; |
| 1231 | |
| 1232 | /* Close the outer open file handle now that we're about to |
| 1233 | reopen it with different flags. */ |
| 1234 | if (!CloseHandle(hFile)) |
| 1235 | return -1; |
| 1236 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1237 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1238 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1239 | the file without the reparse handling flag set. */ |
| 1240 | hFile2 = CreateFileA( |
| 1241 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1242 | NULL, OPEN_EXISTING, |
| 1243 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1244 | NULL); |
| 1245 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1246 | return -1; |
| 1247 | |
| 1248 | if (!get_target_path(hFile2, &target_path)) |
| 1249 | return -1; |
| 1250 | |
| 1251 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1252 | free(target_path); |
| 1253 | return code; |
| 1254 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1255 | } else |
| 1256 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1257 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1258 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1261 | dot = strrchr(path, '.'); |
| 1262 | if (dot) { |
| 1263 | if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 || |
| 1264 | stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0) |
| 1265 | result->st_mode |= 0111; |
| 1266 | } |
| 1267 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1271 | win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, |
| 1272 | BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1273 | { |
| 1274 | int code; |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1275 | HANDLE hFile, hFile2; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1276 | BY_HANDLE_FILE_INFORMATION info; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1277 | ULONG reparse_tag = 0; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1278 | wchar_t *target_path; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1279 | const wchar_t *dot; |
| 1280 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1281 | if(!check_GetFinalPathNameByHandle()) { |
Brian Curtin | c8be840 | 2011-06-14 09:52:50 -0500 | [diff] [blame] | 1282 | /* If the OS doesn't have GetFinalPathNameByHandle, don't |
| 1283 | traverse reparse point. */ |
| 1284 | traverse = FALSE; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1287 | hFile = CreateFileW( |
| 1288 | path, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1289 | FILE_READ_ATTRIBUTES, /* desired access */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1290 | 0, /* share mode */ |
| 1291 | NULL, /* security attributes */ |
| 1292 | OPEN_EXISTING, |
| 1293 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1294 | /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink. |
| 1295 | Because of this, calls like GetFinalPathNameByHandle will return |
| 1296 | the symlink path agin and not the actual final path. */ |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 1297 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS| |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1298 | FILE_FLAG_OPEN_REPARSE_POINT, |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1299 | NULL); |
| 1300 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1301 | if (hFile == INVALID_HANDLE_VALUE) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1302 | /* Either the target doesn't exist, or we don't have access to |
| 1303 | get a handle to it. If the former, we need to return an error. |
| 1304 | If the latter, we can use attributes_from_dir. */ |
| 1305 | if (GetLastError() != ERROR_SHARING_VIOLATION) |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1306 | return -1; |
| 1307 | /* Could not get attributes on open file. Fall back to |
| 1308 | reading the directory. */ |
| 1309 | if (!attributes_from_dir_w(path, &info, &reparse_tag)) |
| 1310 | /* Very strange. This should not fail now */ |
| 1311 | return -1; |
| 1312 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1313 | if (traverse) { |
| 1314 | /* Should traverse, but could not open reparse point handle */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1315 | SetLastError(ERROR_SHARING_VIOLATION); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1316 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1317 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1318 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1319 | } else { |
| 1320 | if (!GetFileInformationByHandle(hFile, &info)) { |
| 1321 | CloseHandle(hFile); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1322 | return -1; |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1323 | } |
| 1324 | if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1325 | if (!win32_get_reparse_tag(hFile, &reparse_tag)) |
| 1326 | return -1; |
| 1327 | |
| 1328 | /* Close the outer open file handle now that we're about to |
| 1329 | reopen it with different flags. */ |
| 1330 | if (!CloseHandle(hFile)) |
| 1331 | return -1; |
| 1332 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1333 | if (traverse) { |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1334 | /* In order to call GetFinalPathNameByHandle we need to open |
| 1335 | the file without the reparse handling flag set. */ |
| 1336 | hFile2 = CreateFileW( |
| 1337 | path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, |
| 1338 | NULL, OPEN_EXISTING, |
| 1339 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, |
| 1340 | NULL); |
| 1341 | if (hFile2 == INVALID_HANDLE_VALUE) |
| 1342 | return -1; |
| 1343 | |
| 1344 | if (!get_target_path(hFile2, &target_path)) |
| 1345 | return -1; |
| 1346 | |
| 1347 | code = win32_xstat_impl_w(target_path, result, FALSE); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1348 | free(target_path); |
| 1349 | return code; |
| 1350 | } |
Hirokazu Yamamoto | 7ed117a | 2010-12-07 10:24:37 +0000 | [diff] [blame] | 1351 | } else |
| 1352 | CloseHandle(hFile); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1353 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1354 | attribute_data_to_stat(&info, reparse_tag, result); |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1355 | |
| 1356 | /* Set S_IEXEC if it is an .exe, .bat, ... */ |
| 1357 | dot = wcsrchr(path, '.'); |
| 1358 | if (dot) { |
| 1359 | if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || |
| 1360 | _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) |
| 1361 | result->st_mode |= 0111; |
| 1362 | } |
| 1363 | return 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static int |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1367 | win32_xstat(const char *path, struct win32_stat *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1368 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1369 | /* Protocol violation: we explicitly clear errno, instead of |
| 1370 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1371 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1372 | errno = 0; |
| 1373 | return code; |
| 1374 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1375 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1376 | static int |
| 1377 | win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse) |
| 1378 | { |
| 1379 | /* Protocol violation: we explicitly clear errno, instead of |
| 1380 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1381 | int code = win32_xstat_impl_w(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1382 | errno = 0; |
| 1383 | return code; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1384 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1385 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1386 | |
| 1387 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1388 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1389 | default does not traverse symlinks and instead returns attributes for |
| 1390 | the symlink. |
| 1391 | |
| 1392 | Therefore, win32_lstat will get the attributes traditionally, and |
| 1393 | win32_stat will first explicitly resolve the symlink target and then will |
| 1394 | call win32_lstat on that result. |
| 1395 | |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 1396 | The _w represent Unicode equivalents of the aforementioned ANSI functions. */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1397 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1398 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1399 | win32_lstat(const char* path, struct win32_stat *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1400 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1401 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1404 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1405 | 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] | 1406 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1407 | return win32_xstat_w(path, result, FALSE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | static int |
| 1411 | win32_stat(const char* path, struct win32_stat *result) |
| 1412 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1413 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1416 | static int |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1417 | win32_stat_w(const wchar_t* path, struct win32_stat *result) |
| 1418 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1419 | return win32_xstat_w(path, result, TRUE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | static int |
| 1423 | win32_fstat(int file_number, struct win32_stat *result) |
| 1424 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1425 | BY_HANDLE_FILE_INFORMATION info; |
| 1426 | HANDLE h; |
| 1427 | int type; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1428 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | h = (HANDLE)_get_osfhandle(file_number); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1431 | /* Protocol violation: we explicitly clear errno, instead of |
| 1432 | setting it to a POSIX error. Callers should use GetLastError. */ |
| 1433 | errno = 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1434 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1435 | if (h == INVALID_HANDLE_VALUE) { |
| 1436 | /* This is really a C library error (invalid file handle). |
| 1437 | We set the Win32 error to the closes one matching. */ |
| 1438 | SetLastError(ERROR_INVALID_HANDLE); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | memset(result, 0, sizeof(*result)); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | type = GetFileType(h); |
| 1444 | if (type == FILE_TYPE_UNKNOWN) { |
| 1445 | DWORD error = GetLastError(); |
| 1446 | if (error != 0) { |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1447 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1448 | } |
| 1449 | /* else: valid but unknown file */ |
| 1450 | } |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1452 | if (type != FILE_TYPE_DISK) { |
| 1453 | if (type == FILE_TYPE_CHAR) |
| 1454 | result->st_mode = _S_IFCHR; |
| 1455 | else if (type == FILE_TYPE_PIPE) |
| 1456 | result->st_mode = _S_IFIFO; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | if (!GetFileInformationByHandle(h, &info)) { |
| 1461 | return -1; |
| 1462 | } |
| 1463 | |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1464 | attribute_data_to_stat(&info, 0, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1465 | /* specific to fstat() */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1466 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 1467 | return 0; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | #endif /* MS_WINDOWS */ |
| 1471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1472 | PyDoc_STRVAR(stat_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1473 | "stat_result: Result from stat or lstat.\n\n\ |
| 1474 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1475 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1476 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1477 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1478 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1479 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1480 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1481 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1482 | |
| 1483 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | {"st_mode", "protection bits"}, |
| 1485 | {"st_ino", "inode"}, |
| 1486 | {"st_dev", "device"}, |
| 1487 | {"st_nlink", "number of hard links"}, |
| 1488 | {"st_uid", "user ID of owner"}, |
| 1489 | {"st_gid", "group ID of owner"}, |
| 1490 | {"st_size", "total size, in bytes"}, |
| 1491 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1492 | {NULL, "integer time of last access"}, |
| 1493 | {NULL, "integer time of last modification"}, |
| 1494 | {NULL, "integer time of last change"}, |
| 1495 | {"st_atime", "time of last access"}, |
| 1496 | {"st_mtime", "time of last modification"}, |
| 1497 | {"st_ctime", "time of last change"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1498 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1499 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1500 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1501 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1503 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1504 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1505 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1506 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1507 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1508 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1509 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1510 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1511 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1512 | #endif |
| 1513 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1514 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1515 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1516 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1517 | }; |
| 1518 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1519 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1520 | #define ST_BLKSIZE_IDX 13 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1521 | #else |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1522 | #define ST_BLKSIZE_IDX 12 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1523 | #endif |
| 1524 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1525 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1526 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1527 | #else |
| 1528 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1529 | #endif |
| 1530 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1531 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1532 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1533 | #else |
| 1534 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1535 | #endif |
| 1536 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1537 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1538 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1539 | #else |
| 1540 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1541 | #endif |
| 1542 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1543 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1544 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1545 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1546 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1547 | #endif |
| 1548 | |
| 1549 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1550 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1551 | #else |
| 1552 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1553 | #endif |
| 1554 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1555 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1556 | "stat_result", /* name */ |
| 1557 | stat_result__doc__, /* doc */ |
| 1558 | stat_result_fields, |
| 1559 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1560 | }; |
| 1561 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1562 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1563 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 1564 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1565 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1566 | 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] | 1567 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1568 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1569 | |
| 1570 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1571 | {"f_bsize", }, |
| 1572 | {"f_frsize", }, |
| 1573 | {"f_blocks", }, |
| 1574 | {"f_bfree", }, |
| 1575 | {"f_bavail", }, |
| 1576 | {"f_files", }, |
| 1577 | {"f_ffree", }, |
| 1578 | {"f_favail", }, |
| 1579 | {"f_flag", }, |
| 1580 | {"f_namemax",}, |
| 1581 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1582 | }; |
| 1583 | |
| 1584 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1585 | "statvfs_result", /* name */ |
| 1586 | statvfs_result__doc__, /* doc */ |
| 1587 | statvfs_result_fields, |
| 1588 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1589 | }; |
| 1590 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 1591 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 1592 | PyDoc_STRVAR(waitid_result__doc__, |
| 1593 | "waitid_result: Result from waitid.\n\n\ |
| 1594 | This object may be accessed either as a tuple of\n\ |
| 1595 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 1596 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 1597 | \n\ |
| 1598 | See os.waitid for more information."); |
| 1599 | |
| 1600 | static PyStructSequence_Field waitid_result_fields[] = { |
| 1601 | {"si_pid", }, |
| 1602 | {"si_uid", }, |
| 1603 | {"si_signo", }, |
| 1604 | {"si_status", }, |
| 1605 | {"si_code", }, |
| 1606 | {0} |
| 1607 | }; |
| 1608 | |
| 1609 | static PyStructSequence_Desc waitid_result_desc = { |
| 1610 | "waitid_result", /* name */ |
| 1611 | waitid_result__doc__, /* doc */ |
| 1612 | waitid_result_fields, |
| 1613 | 5 |
| 1614 | }; |
| 1615 | static PyTypeObject WaitidResultType; |
| 1616 | #endif |
| 1617 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1618 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1619 | static PyTypeObject StatResultType; |
| 1620 | static PyTypeObject StatVFSResultType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1621 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 1622 | static PyTypeObject SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 1623 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1624 | static newfunc structseq_new; |
| 1625 | |
| 1626 | static PyObject * |
| 1627 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1628 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1629 | PyStructSequence *result; |
| 1630 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1631 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1632 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 1633 | if (!result) |
| 1634 | return NULL; |
| 1635 | /* If we have been initialized from a tuple, |
| 1636 | st_?time might be set to None. Initialize it |
| 1637 | from the int slots. */ |
| 1638 | for (i = 7; i <= 9; i++) { |
| 1639 | if (result->ob_item[i+3] == Py_None) { |
| 1640 | Py_DECREF(Py_None); |
| 1641 | Py_INCREF(result->ob_item[i]); |
| 1642 | result->ob_item[i+3] = result->ob_item[i]; |
| 1643 | } |
| 1644 | } |
| 1645 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | |
| 1650 | /* If true, st_?time is float. */ |
Martin v. Löwis | fe33d0b | 2005-01-16 08:57:39 +0000 | [diff] [blame] | 1651 | static int _stat_float_times = 1; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1652 | |
| 1653 | PyDoc_STRVAR(stat_float_times__doc__, |
| 1654 | "stat_float_times([newval]) -> oldval\n\n\ |
| 1655 | Determine whether os.[lf]stat represents time stamps as float objects.\n\ |
| 1656 | If newval is True, future calls to stat() return floats, if it is False,\n\ |
| 1657 | future calls return ints. \n\ |
| 1658 | If newval is omitted, return the current setting.\n"); |
| 1659 | |
| 1660 | static PyObject* |
| 1661 | stat_float_times(PyObject* self, PyObject *args) |
| 1662 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1663 | int newval = -1; |
| 1664 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) |
| 1665 | return NULL; |
| 1666 | if (newval == -1) |
| 1667 | /* Return old value */ |
| 1668 | return PyBool_FromLong(_stat_float_times); |
| 1669 | _stat_float_times = newval; |
| 1670 | Py_INCREF(Py_None); |
| 1671 | return Py_None; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1672 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1673 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1674 | static void |
| 1675 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
| 1676 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1677 | PyObject *fval,*ival; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1678 | #if SIZEOF_TIME_T > SIZEOF_LONG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1679 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1680 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1681 | ival = PyLong_FromLong((long)sec); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 1682 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1683 | if (!ival) |
| 1684 | return; |
| 1685 | if (_stat_float_times) { |
| 1686 | fval = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 1687 | } else { |
| 1688 | fval = ival; |
| 1689 | Py_INCREF(fval); |
| 1690 | } |
| 1691 | PyStructSequence_SET_ITEM(v, index, ival); |
| 1692 | PyStructSequence_SET_ITEM(v, index+3, fval); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 1695 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1696 | (used by posix_stat() and posix_fstat()) */ |
| 1697 | static PyObject* |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1698 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1700 | unsigned long ansec, mnsec, cnsec; |
| 1701 | PyObject *v = PyStructSequence_New(&StatResultType); |
| 1702 | if (v == NULL) |
| 1703 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1704 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1706 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1707 | PyStructSequence_SET_ITEM(v, 1, |
| 1708 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1709 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1710 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1711 | #endif |
| 1712 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1713 | PyStructSequence_SET_ITEM(v, 2, |
| 1714 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1715 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1716 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1717 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1718 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
| 1719 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid)); |
| 1720 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1721 | #ifdef HAVE_LARGEFILE_SUPPORT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1722 | PyStructSequence_SET_ITEM(v, 6, |
| 1723 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1724 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1725 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1726 | #endif |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 1727 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1728 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1729 | ansec = st->st_atim.tv_nsec; |
| 1730 | mnsec = st->st_mtim.tv_nsec; |
| 1731 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1732 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1733 | ansec = st->st_atimespec.tv_nsec; |
| 1734 | mnsec = st->st_mtimespec.tv_nsec; |
| 1735 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1736 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1737 | ansec = st->st_atime_nsec; |
| 1738 | mnsec = st->st_mtime_nsec; |
| 1739 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1740 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1741 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1742 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1743 | fill_time(v, 7, st->st_atime, ansec); |
| 1744 | fill_time(v, 8, st->st_mtime, mnsec); |
| 1745 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1746 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1747 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1748 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 1749 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1750 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1751 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1752 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 1753 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1754 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1755 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1756 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 1757 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1758 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1759 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1760 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 1761 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1762 | #endif |
| 1763 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1764 | { |
| 1765 | PyObject *val; |
| 1766 | unsigned long bsec,bnsec; |
| 1767 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1768 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1769 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1770 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1771 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1772 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1773 | if (_stat_float_times) { |
| 1774 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
| 1775 | } else { |
| 1776 | val = PyLong_FromLong((long)bsec); |
| 1777 | } |
| 1778 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 1779 | val); |
| 1780 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1781 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1782 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1783 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 1784 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1785 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1786 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1787 | if (PyErr_Occurred()) { |
| 1788 | Py_DECREF(v); |
| 1789 | return NULL; |
| 1790 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1791 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1792 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1795 | static PyObject * |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 1796 | posix_do_stat(PyObject *self, PyObject *args, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1797 | char *format, |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1798 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1799 | int (*statfunc)(const char *, STRUCT_STAT *, ...), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1800 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1801 | int (*statfunc)(const char *, STRUCT_STAT *), |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1802 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1803 | char *wformat, |
| 1804 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1805 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1806 | STRUCT_STAT st; |
| 1807 | PyObject *opath; |
| 1808 | char *path; |
| 1809 | int res; |
| 1810 | PyObject *result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1811 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1812 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1813 | PyUnicodeObject *po; |
| 1814 | if (PyArg_ParseTuple(args, wformat, &po)) { |
| 1815 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1816 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1817 | Py_BEGIN_ALLOW_THREADS |
| 1818 | /* PyUnicode_AS_UNICODE result OK without |
| 1819 | thread lock as it is a simple dereference. */ |
| 1820 | res = wstatfunc(wpath, &st); |
| 1821 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1823 | if (res != 0) |
| 1824 | return win32_error_unicode("stat", wpath); |
| 1825 | return _pystat_fromstructstat(&st); |
| 1826 | } |
| 1827 | /* Drop the argument parsing error as narrow strings |
| 1828 | are also valid. */ |
| 1829 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1830 | #endif |
| 1831 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1832 | if (!PyArg_ParseTuple(args, format, |
| 1833 | PyUnicode_FSConverter, &opath)) |
| 1834 | return NULL; |
| 1835 | path = PyBytes_AsString(opath); |
| 1836 | Py_BEGIN_ALLOW_THREADS |
| 1837 | res = (*statfunc)(path, &st); |
| 1838 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1839 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1840 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1841 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1842 | result = win32_error("stat", path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1843 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1844 | result = posix_error_with_filename(path); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1845 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | } |
| 1847 | else |
| 1848 | result = _pystat_fromstructstat(&st); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 1849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1850 | Py_DECREF(opath); |
| 1851 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1854 | /* POSIX methods */ |
| 1855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1856 | PyDoc_STRVAR(posix_access__doc__, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1857 | "access(path, mode) -> True if granted, False otherwise\n\n\ |
Guido van Rossum | a0b9075 | 2002-06-18 16:22:43 +0000 | [diff] [blame] | 1858 | Use the real uid/gid to test for access to a path. Note that most\n\ |
| 1859 | operations will use the effective uid/gid, therefore this routine can\n\ |
| 1860 | be used in a suid/sgid environment to test if the invoking user has the\n\ |
| 1861 | specified access to the path. The mode argument can be F_OK to test\n\ |
| 1862 | 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] | 1863 | |
| 1864 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1865 | posix_access(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | PyObject *opath; |
| 1868 | char *path; |
| 1869 | int mode; |
| 1870 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1871 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1872 | DWORD attr; |
| 1873 | PyUnicodeObject *po; |
| 1874 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { |
| 1875 | Py_BEGIN_ALLOW_THREADS |
| 1876 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 1877 | it is a simple dereference. */ |
| 1878 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 1879 | Py_END_ALLOW_THREADS |
| 1880 | goto finish; |
| 1881 | } |
| 1882 | /* Drop the argument parsing error as narrow strings |
| 1883 | are also valid. */ |
| 1884 | PyErr_Clear(); |
| 1885 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1886 | PyUnicode_FSConverter, &opath, &mode)) |
| 1887 | return NULL; |
| 1888 | path = PyBytes_AsString(opath); |
| 1889 | Py_BEGIN_ALLOW_THREADS |
| 1890 | attr = GetFileAttributesA(path); |
| 1891 | Py_END_ALLOW_THREADS |
| 1892 | Py_DECREF(opath); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1893 | finish: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | if (attr == 0xFFFFFFFF) |
| 1895 | /* File does not exist, or cannot read attributes */ |
| 1896 | return PyBool_FromLong(0); |
| 1897 | /* Access is possible if either write access wasn't requested, or |
| 1898 | the file isn't read-only, or if it's a directory, as there are |
| 1899 | no read-only directories on Windows. */ |
| 1900 | return PyBool_FromLong(!(mode & 2) |
| 1901 | || !(attr & FILE_ATTRIBUTE_READONLY) |
| 1902 | || (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1903 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1904 | int res; |
| 1905 | if (!PyArg_ParseTuple(args, "O&i:access", |
| 1906 | PyUnicode_FSConverter, &opath, &mode)) |
| 1907 | return NULL; |
| 1908 | path = PyBytes_AsString(opath); |
| 1909 | Py_BEGIN_ALLOW_THREADS |
| 1910 | res = access(path, mode); |
| 1911 | Py_END_ALLOW_THREADS |
| 1912 | Py_DECREF(opath); |
| 1913 | return PyBool_FromLong(res == 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1914 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1917 | #ifndef F_OK |
| 1918 | #define F_OK 0 |
| 1919 | #endif |
| 1920 | #ifndef R_OK |
| 1921 | #define R_OK 4 |
| 1922 | #endif |
| 1923 | #ifndef W_OK |
| 1924 | #define W_OK 2 |
| 1925 | #endif |
| 1926 | #ifndef X_OK |
| 1927 | #define X_OK 1 |
| 1928 | #endif |
| 1929 | |
| 1930 | #ifdef HAVE_TTYNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1931 | PyDoc_STRVAR(posix_ttyname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1932 | "ttyname(fd) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1933 | Return the name of the terminal device connected to 'fd'."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1934 | |
| 1935 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1936 | posix_ttyname(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1937 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | int id; |
| 1939 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | if (!PyArg_ParseTuple(args, "i:ttyname", &id)) |
| 1942 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1943 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1944 | #if defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | /* file descriptor 0 only, the default input device (stdin) */ |
| 1946 | if (id == 0) { |
| 1947 | ret = ttyname(); |
| 1948 | } |
| 1949 | else { |
| 1950 | ret = NULL; |
| 1951 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1952 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1953 | ret = ttyname(id); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1954 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1955 | if (ret == NULL) |
| 1956 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1957 | return PyUnicode_DecodeFSDefault(ret); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1958 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 1959 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 1960 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1961 | #ifdef HAVE_CTERMID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1962 | PyDoc_STRVAR(posix_ctermid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1963 | "ctermid() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1964 | Return the name of the controlling terminal for this process."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1965 | |
| 1966 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1967 | posix_ctermid(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1968 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1969 | char *ret; |
| 1970 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1971 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 1972 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1973 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1974 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1975 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1976 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1977 | if (ret == NULL) |
| 1978 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 1979 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 1980 | } |
| 1981 | #endif |
| 1982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1983 | PyDoc_STRVAR(posix_chdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1984 | "chdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1985 | Change the current working directory to the specified path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 1986 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1987 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 1988 | posix_chdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1989 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1990 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | 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] | 1992 | #elif defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1993 | return posix_1str(args, "O&:chdir", _chdir2); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 1994 | #elif defined(__VMS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1995 | return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1996 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | return posix_1str(args, "O&:chdir", chdir); |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 1998 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2001 | #ifdef HAVE_FCHDIR |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2002 | PyDoc_STRVAR(posix_fchdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2003 | "fchdir(fildes)\n\n\ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2004 | 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] | 2005 | opened on a directory, not a file."); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2006 | |
| 2007 | static PyObject * |
| 2008 | posix_fchdir(PyObject *self, PyObject *fdobj) |
| 2009 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2010 | return posix_fildes(fdobj, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2011 | } |
| 2012 | #endif /* HAVE_FCHDIR */ |
| 2013 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | PyDoc_STRVAR(posix_chmod__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2016 | "chmod(path, mode)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2017 | Change the access permissions of a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2018 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2019 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2020 | posix_chmod(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2022 | PyObject *opath = NULL; |
| 2023 | char *path = NULL; |
| 2024 | int i; |
| 2025 | int res; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2026 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2027 | DWORD attr; |
| 2028 | PyUnicodeObject *po; |
| 2029 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { |
| 2030 | Py_BEGIN_ALLOW_THREADS |
| 2031 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); |
| 2032 | if (attr != 0xFFFFFFFF) { |
| 2033 | if (i & _S_IWRITE) |
| 2034 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2035 | else |
| 2036 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2037 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); |
| 2038 | } |
| 2039 | else |
| 2040 | res = 0; |
| 2041 | Py_END_ALLOW_THREADS |
| 2042 | if (!res) |
| 2043 | return win32_error_unicode("chmod", |
| 2044 | PyUnicode_AS_UNICODE(po)); |
| 2045 | Py_INCREF(Py_None); |
| 2046 | return Py_None; |
| 2047 | } |
| 2048 | /* Drop the argument parsing error as narrow strings |
| 2049 | are also valid. */ |
| 2050 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2051 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2053 | &opath, &i)) |
| 2054 | return NULL; |
| 2055 | path = PyBytes_AsString(opath); |
| 2056 | Py_BEGIN_ALLOW_THREADS |
| 2057 | attr = GetFileAttributesA(path); |
| 2058 | if (attr != 0xFFFFFFFF) { |
| 2059 | if (i & _S_IWRITE) |
| 2060 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 2061 | else |
| 2062 | attr |= FILE_ATTRIBUTE_READONLY; |
| 2063 | res = SetFileAttributesA(path, attr); |
| 2064 | } |
| 2065 | else |
| 2066 | res = 0; |
| 2067 | Py_END_ALLOW_THREADS |
| 2068 | if (!res) { |
| 2069 | win32_error("chmod", path); |
| 2070 | Py_DECREF(opath); |
| 2071 | return NULL; |
| 2072 | } |
| 2073 | Py_DECREF(opath); |
| 2074 | Py_INCREF(Py_None); |
| 2075 | return Py_None; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2076 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter, |
| 2078 | &opath, &i)) |
| 2079 | return NULL; |
| 2080 | path = PyBytes_AsString(opath); |
| 2081 | Py_BEGIN_ALLOW_THREADS |
| 2082 | res = chmod(path, i); |
| 2083 | Py_END_ALLOW_THREADS |
| 2084 | if (res < 0) |
| 2085 | return posix_error_with_allocated_filename(opath); |
| 2086 | Py_DECREF(opath); |
| 2087 | Py_INCREF(Py_None); |
| 2088 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2092 | #ifdef HAVE_FCHMOD |
| 2093 | PyDoc_STRVAR(posix_fchmod__doc__, |
| 2094 | "fchmod(fd, mode)\n\n\ |
| 2095 | Change the access permissions of the file given by file\n\ |
| 2096 | descriptor fd."); |
| 2097 | |
| 2098 | static PyObject * |
| 2099 | posix_fchmod(PyObject *self, PyObject *args) |
| 2100 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2101 | int fd, mode, res; |
| 2102 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) |
| 2103 | return NULL; |
| 2104 | Py_BEGIN_ALLOW_THREADS |
| 2105 | res = fchmod(fd, mode); |
| 2106 | Py_END_ALLOW_THREADS |
| 2107 | if (res < 0) |
| 2108 | return posix_error(); |
| 2109 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2110 | } |
| 2111 | #endif /* HAVE_FCHMOD */ |
| 2112 | |
| 2113 | #ifdef HAVE_LCHMOD |
| 2114 | PyDoc_STRVAR(posix_lchmod__doc__, |
| 2115 | "lchmod(path, mode)\n\n\ |
| 2116 | Change the access permissions of a file. If path is a symlink, this\n\ |
| 2117 | affects the link itself rather than the target."); |
| 2118 | |
| 2119 | static PyObject * |
| 2120 | posix_lchmod(PyObject *self, PyObject *args) |
| 2121 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2122 | PyObject *opath; |
| 2123 | char *path; |
| 2124 | int i; |
| 2125 | int res; |
| 2126 | if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter, |
| 2127 | &opath, &i)) |
| 2128 | return NULL; |
| 2129 | path = PyBytes_AsString(opath); |
| 2130 | Py_BEGIN_ALLOW_THREADS |
| 2131 | res = lchmod(path, i); |
| 2132 | Py_END_ALLOW_THREADS |
| 2133 | if (res < 0) |
| 2134 | return posix_error_with_allocated_filename(opath); |
| 2135 | Py_DECREF(opath); |
| 2136 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2137 | } |
| 2138 | #endif /* HAVE_LCHMOD */ |
| 2139 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2140 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2141 | #ifdef HAVE_CHFLAGS |
| 2142 | PyDoc_STRVAR(posix_chflags__doc__, |
| 2143 | "chflags(path, flags)\n\n\ |
| 2144 | Set file flags."); |
| 2145 | |
| 2146 | static PyObject * |
| 2147 | posix_chflags(PyObject *self, PyObject *args) |
| 2148 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2149 | PyObject *opath; |
| 2150 | char *path; |
| 2151 | unsigned long flags; |
| 2152 | int res; |
| 2153 | if (!PyArg_ParseTuple(args, "O&k:chflags", |
| 2154 | PyUnicode_FSConverter, &opath, &flags)) |
| 2155 | return NULL; |
| 2156 | path = PyBytes_AsString(opath); |
| 2157 | Py_BEGIN_ALLOW_THREADS |
| 2158 | res = chflags(path, flags); |
| 2159 | Py_END_ALLOW_THREADS |
| 2160 | if (res < 0) |
| 2161 | return posix_error_with_allocated_filename(opath); |
| 2162 | Py_DECREF(opath); |
| 2163 | Py_INCREF(Py_None); |
| 2164 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2165 | } |
| 2166 | #endif /* HAVE_CHFLAGS */ |
| 2167 | |
| 2168 | #ifdef HAVE_LCHFLAGS |
| 2169 | PyDoc_STRVAR(posix_lchflags__doc__, |
| 2170 | "lchflags(path, flags)\n\n\ |
| 2171 | Set file flags.\n\ |
| 2172 | This function will not follow symbolic links."); |
| 2173 | |
| 2174 | static PyObject * |
| 2175 | posix_lchflags(PyObject *self, PyObject *args) |
| 2176 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2177 | PyObject *opath; |
| 2178 | char *path; |
| 2179 | unsigned long flags; |
| 2180 | int res; |
| 2181 | if (!PyArg_ParseTuple(args, "O&k:lchflags", |
| 2182 | PyUnicode_FSConverter, &opath, &flags)) |
| 2183 | return NULL; |
| 2184 | path = PyBytes_AsString(opath); |
| 2185 | Py_BEGIN_ALLOW_THREADS |
| 2186 | res = lchflags(path, flags); |
| 2187 | Py_END_ALLOW_THREADS |
| 2188 | if (res < 0) |
| 2189 | return posix_error_with_allocated_filename(opath); |
| 2190 | Py_DECREF(opath); |
| 2191 | Py_INCREF(Py_None); |
| 2192 | return Py_None; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2193 | } |
| 2194 | #endif /* HAVE_LCHFLAGS */ |
| 2195 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2196 | #ifdef HAVE_CHROOT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2197 | PyDoc_STRVAR(posix_chroot__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2198 | "chroot(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2199 | Change root directory to path."); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2200 | |
| 2201 | static PyObject * |
| 2202 | posix_chroot(PyObject *self, PyObject *args) |
| 2203 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2204 | return posix_1str(args, "O&:chroot", chroot); |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 2205 | } |
| 2206 | #endif |
| 2207 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2208 | #ifdef HAVE_FSYNC |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2209 | PyDoc_STRVAR(posix_fsync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2210 | "fsync(fildes)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2211 | force write of file with filedescriptor to disk."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2212 | |
| 2213 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2214 | posix_fsync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2215 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2216 | return posix_fildes(fdobj, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2217 | } |
| 2218 | #endif /* HAVE_FSYNC */ |
| 2219 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2220 | #ifdef HAVE_SYNC |
| 2221 | PyDoc_STRVAR(posix_sync__doc__, |
| 2222 | "sync()\n\n\ |
| 2223 | Force write of everything to disk."); |
| 2224 | |
| 2225 | static PyObject * |
| 2226 | posix_sync(PyObject *self, PyObject *noargs) |
| 2227 | { |
| 2228 | Py_BEGIN_ALLOW_THREADS |
| 2229 | sync(); |
| 2230 | Py_END_ALLOW_THREADS |
| 2231 | Py_RETURN_NONE; |
| 2232 | } |
| 2233 | #endif |
| 2234 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2235 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2236 | |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 2237 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 2238 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 2239 | #endif |
| 2240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2241 | PyDoc_STRVAR(posix_fdatasync__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2242 | "fdatasync(fildes)\n\n\ |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2243 | force write of file with filedescriptor to disk.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2244 | does not force update of metadata."); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2245 | |
| 2246 | static PyObject * |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2247 | posix_fdatasync(PyObject *self, PyObject *fdobj) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2248 | { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2249 | return posix_fildes(fdobj, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 2250 | } |
| 2251 | #endif /* HAVE_FDATASYNC */ |
| 2252 | |
| 2253 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 2254 | #ifdef HAVE_CHOWN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2255 | PyDoc_STRVAR(posix_chown__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2256 | "chown(path, uid, gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2257 | 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] | 2258 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2259 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2260 | posix_chown(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2261 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | PyObject *opath; |
| 2263 | char *path; |
| 2264 | long uid, gid; |
| 2265 | int res; |
| 2266 | if (!PyArg_ParseTuple(args, "O&ll:chown", |
| 2267 | PyUnicode_FSConverter, &opath, |
| 2268 | &uid, &gid)) |
| 2269 | return NULL; |
| 2270 | path = PyBytes_AsString(opath); |
| 2271 | Py_BEGIN_ALLOW_THREADS |
| 2272 | res = chown(path, (uid_t) uid, (gid_t) gid); |
| 2273 | Py_END_ALLOW_THREADS |
| 2274 | if (res < 0) |
| 2275 | return posix_error_with_allocated_filename(opath); |
| 2276 | Py_DECREF(opath); |
| 2277 | Py_INCREF(Py_None); |
| 2278 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2279 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2280 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2281 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2282 | #ifdef HAVE_FCHOWN |
| 2283 | PyDoc_STRVAR(posix_fchown__doc__, |
| 2284 | "fchown(fd, uid, gid)\n\n\ |
| 2285 | Change the owner and group id of the file given by file descriptor\n\ |
| 2286 | fd to the numeric uid and gid."); |
| 2287 | |
| 2288 | static PyObject * |
| 2289 | posix_fchown(PyObject *self, PyObject *args) |
| 2290 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2291 | int fd; |
| 2292 | long uid, gid; |
| 2293 | int res; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 2294 | if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2295 | return NULL; |
| 2296 | Py_BEGIN_ALLOW_THREADS |
| 2297 | res = fchown(fd, (uid_t) uid, (gid_t) gid); |
| 2298 | Py_END_ALLOW_THREADS |
| 2299 | if (res < 0) |
| 2300 | return posix_error(); |
| 2301 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 2302 | } |
| 2303 | #endif /* HAVE_FCHOWN */ |
| 2304 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2305 | #ifdef HAVE_LCHOWN |
| 2306 | PyDoc_STRVAR(posix_lchown__doc__, |
| 2307 | "lchown(path, uid, gid)\n\n\ |
| 2308 | Change the owner and group id of path to the numeric uid and gid.\n\ |
| 2309 | This function will not follow symbolic links."); |
| 2310 | |
| 2311 | static PyObject * |
| 2312 | posix_lchown(PyObject *self, PyObject *args) |
| 2313 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2314 | PyObject *opath; |
| 2315 | char *path; |
| 2316 | long uid, gid; |
| 2317 | int res; |
| 2318 | if (!PyArg_ParseTuple(args, "O&ll:lchown", |
| 2319 | PyUnicode_FSConverter, &opath, |
| 2320 | &uid, &gid)) |
| 2321 | return NULL; |
| 2322 | path = PyBytes_AsString(opath); |
| 2323 | Py_BEGIN_ALLOW_THREADS |
| 2324 | res = lchown(path, (uid_t) uid, (gid_t) gid); |
| 2325 | Py_END_ALLOW_THREADS |
| 2326 | if (res < 0) |
| 2327 | return posix_error_with_allocated_filename(opath); |
| 2328 | Py_DECREF(opath); |
| 2329 | Py_INCREF(Py_None); |
| 2330 | return Py_None; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 2331 | } |
| 2332 | #endif /* HAVE_LCHOWN */ |
| 2333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2334 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2335 | #ifdef HAVE_GETCWD |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2336 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2337 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2338 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2339 | char buf[1026]; |
| 2340 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2341 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2342 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2343 | if (!use_bytes) { |
| 2344 | wchar_t wbuf[1026]; |
| 2345 | wchar_t *wbuf2 = wbuf; |
| 2346 | PyObject *resobj; |
| 2347 | DWORD len; |
| 2348 | Py_BEGIN_ALLOW_THREADS |
| 2349 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); |
| 2350 | /* If the buffer is large enough, len does not include the |
| 2351 | terminating \0. If the buffer is too small, len includes |
| 2352 | the space needed for the terminator. */ |
| 2353 | if (len >= sizeof wbuf/ sizeof wbuf[0]) { |
| 2354 | wbuf2 = malloc(len * sizeof(wchar_t)); |
| 2355 | if (wbuf2) |
| 2356 | len = GetCurrentDirectoryW(len, wbuf2); |
| 2357 | } |
| 2358 | Py_END_ALLOW_THREADS |
| 2359 | if (!wbuf2) { |
| 2360 | PyErr_NoMemory(); |
| 2361 | return NULL; |
| 2362 | } |
| 2363 | if (!len) { |
| 2364 | if (wbuf2 != wbuf) free(wbuf2); |
| 2365 | return win32_error("getcwdu", NULL); |
| 2366 | } |
| 2367 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 2368 | if (wbuf2 != wbuf) free(wbuf2); |
| 2369 | return resobj; |
| 2370 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2371 | #endif |
| 2372 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2373 | Py_BEGIN_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2374 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2375 | res = _getcwd2(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2376 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2377 | res = getcwd(buf, sizeof buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2378 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2379 | Py_END_ALLOW_THREADS |
| 2380 | if (res == NULL) |
| 2381 | return posix_error(); |
| 2382 | if (use_bytes) |
| 2383 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 2384 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2385 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 2386 | |
| 2387 | PyDoc_STRVAR(posix_getcwd__doc__, |
| 2388 | "getcwd() -> path\n\n\ |
| 2389 | Return a unicode string representing the current working directory."); |
| 2390 | |
| 2391 | static PyObject * |
| 2392 | posix_getcwd_unicode(PyObject *self) |
| 2393 | { |
| 2394 | return posix_getcwd(0); |
| 2395 | } |
| 2396 | |
| 2397 | PyDoc_STRVAR(posix_getcwdb__doc__, |
| 2398 | "getcwdb() -> path\n\n\ |
| 2399 | Return a bytes string representing the current working directory."); |
| 2400 | |
| 2401 | static PyObject * |
| 2402 | posix_getcwd_bytes(PyObject *self) |
| 2403 | { |
| 2404 | return posix_getcwd(1); |
| 2405 | } |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 2406 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2407 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2408 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2409 | #ifdef HAVE_LINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2410 | PyDoc_STRVAR(posix_link__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2411 | "link(src, dst)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2412 | Create a hard link to a file."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2413 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2414 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2415 | posix_link(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2417 | return posix_2str(args, "O&O&:link", link); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2418 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2419 | #endif /* HAVE_LINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2420 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2421 | #ifdef MS_WINDOWS |
| 2422 | PyDoc_STRVAR(win32_link__doc__, |
| 2423 | "link(src, dst)\n\n\ |
| 2424 | Create a hard link to a file."); |
| 2425 | |
| 2426 | static PyObject * |
| 2427 | win32_link(PyObject *self, PyObject *args) |
| 2428 | { |
| 2429 | PyObject *osrc, *odst; |
| 2430 | char *src, *dst; |
| 2431 | BOOL rslt; |
| 2432 | |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2433 | PyUnicodeObject *usrc, *udst; |
| 2434 | if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) { |
| 2435 | Py_BEGIN_ALLOW_THREADS |
| 2436 | rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst), |
| 2437 | PyUnicode_AS_UNICODE(usrc), NULL); |
| 2438 | Py_END_ALLOW_THREADS |
| 2439 | |
| 2440 | if (rslt == 0) |
| 2441 | return win32_error("link", NULL); |
| 2442 | |
| 2443 | Py_RETURN_NONE; |
| 2444 | } |
| 2445 | |
| 2446 | /* Narrow strings also valid. */ |
| 2447 | PyErr_Clear(); |
| 2448 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2449 | if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, |
| 2450 | PyUnicode_FSConverter, &odst)) |
| 2451 | return NULL; |
| 2452 | |
| 2453 | src = PyBytes_AsString(osrc); |
| 2454 | dst = PyBytes_AsString(odst); |
| 2455 | |
| 2456 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2457 | rslt = CreateHardLinkA(dst, src, NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2458 | Py_END_ALLOW_THREADS |
| 2459 | |
Stefan Krah | 30b341f | 2010-11-27 11:44:18 +0000 | [diff] [blame] | 2460 | Py_DECREF(osrc); |
| 2461 | Py_DECREF(odst); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2462 | if (rslt == 0) |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 2463 | return win32_error("link", NULL); |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2464 | |
| 2465 | Py_RETURN_NONE; |
| 2466 | } |
| 2467 | #endif /* MS_WINDOWS */ |
| 2468 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2470 | PyDoc_STRVAR(posix_listdir__doc__, |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2471 | "listdir([path]) -> list_of_strings\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2472 | Return a list containing the names of the entries in the directory.\n\ |
| 2473 | \n\ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2474 | path: path of directory to list (default: '.')\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2475 | \n\ |
| 2476 | 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] | 2477 | entries '.' and '..' even if they are present in the directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2478 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2479 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 2480 | posix_listdir(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2482 | /* XXX Should redo this putting the (now four) versions of opendir |
| 2483 | in separate files instead of having them all here... */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2484 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2486 | PyObject *d, *v; |
| 2487 | HANDLE hFindFile; |
| 2488 | BOOL result; |
| 2489 | WIN32_FIND_DATA FileData; |
| 2490 | PyObject *opath; |
| 2491 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ |
| 2492 | char *bufptr = namebuf; |
| 2493 | 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] | 2494 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2495 | PyObject *po = NULL; |
| 2496 | if (PyArg_ParseTuple(args, "|U:listdir", &po)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2497 | WIN32_FIND_DATAW wFileData; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2498 | Py_UNICODE *wnamebuf, *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2500 | if (po == NULL) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2501 | po_wchars = L"."; |
| 2502 | len = 1; |
| 2503 | } else { |
| 2504 | po_wchars = PyUnicode_AS_UNICODE(po); |
| 2505 | len = PyUnicode_GET_SIZE(po); |
| 2506 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2507 | /* Overallocate for \\*.*\0 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2508 | wnamebuf = malloc((len + 5) * sizeof(wchar_t)); |
| 2509 | if (!wnamebuf) { |
| 2510 | PyErr_NoMemory(); |
| 2511 | return NULL; |
| 2512 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2513 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2514 | if (len > 0) { |
| 2515 | Py_UNICODE wch = wnamebuf[len-1]; |
| 2516 | if (wch != L'/' && wch != L'\\' && wch != L':') |
| 2517 | wnamebuf[len++] = L'\\'; |
| 2518 | wcscpy(wnamebuf + len, L"*.*"); |
| 2519 | } |
| 2520 | if ((d = PyList_New(0)) == NULL) { |
| 2521 | free(wnamebuf); |
| 2522 | return NULL; |
| 2523 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2524 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2525 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2526 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2527 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2528 | int error = GetLastError(); |
| 2529 | if (error == ERROR_FILE_NOT_FOUND) { |
| 2530 | free(wnamebuf); |
| 2531 | return d; |
| 2532 | } |
| 2533 | Py_DECREF(d); |
| 2534 | win32_error_unicode("FindFirstFileW", wnamebuf); |
| 2535 | free(wnamebuf); |
| 2536 | return NULL; |
| 2537 | } |
| 2538 | do { |
| 2539 | /* Skip over . and .. */ |
| 2540 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 2541 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 2542 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); |
| 2543 | if (v == NULL) { |
| 2544 | Py_DECREF(d); |
| 2545 | d = NULL; |
| 2546 | break; |
| 2547 | } |
| 2548 | if (PyList_Append(d, v) != 0) { |
| 2549 | Py_DECREF(v); |
| 2550 | Py_DECREF(d); |
| 2551 | d = NULL; |
| 2552 | break; |
| 2553 | } |
| 2554 | Py_DECREF(v); |
| 2555 | } |
| 2556 | Py_BEGIN_ALLOW_THREADS |
| 2557 | result = FindNextFileW(hFindFile, &wFileData); |
| 2558 | Py_END_ALLOW_THREADS |
| 2559 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2560 | it got to the end of the directory. */ |
| 2561 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2562 | Py_DECREF(d); |
| 2563 | win32_error_unicode("FindNextFileW", wnamebuf); |
| 2564 | FindClose(hFindFile); |
| 2565 | free(wnamebuf); |
| 2566 | return NULL; |
| 2567 | } |
| 2568 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2569 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2570 | if (FindClose(hFindFile) == FALSE) { |
| 2571 | Py_DECREF(d); |
| 2572 | win32_error_unicode("FindClose", wnamebuf); |
| 2573 | free(wnamebuf); |
| 2574 | return NULL; |
| 2575 | } |
| 2576 | free(wnamebuf); |
| 2577 | return d; |
| 2578 | } |
| 2579 | /* Drop the argument parsing error as narrow strings |
| 2580 | are also valid. */ |
| 2581 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2582 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2583 | if (!PyArg_ParseTuple(args, "O&:listdir", |
| 2584 | PyUnicode_FSConverter, &opath)) |
| 2585 | return NULL; |
| 2586 | if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { |
| 2587 | PyErr_SetString(PyExc_ValueError, "path too long"); |
| 2588 | Py_DECREF(opath); |
| 2589 | return NULL; |
| 2590 | } |
| 2591 | strcpy(namebuf, PyBytes_AsString(opath)); |
| 2592 | len = PyObject_Size(opath); |
Stefan Krah | 2a7feee | 2010-11-27 22:06:49 +0000 | [diff] [blame] | 2593 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2594 | if (len > 0) { |
| 2595 | char ch = namebuf[len-1]; |
| 2596 | if (ch != SEP && ch != ALTSEP && ch != ':') |
| 2597 | namebuf[len++] = '/'; |
| 2598 | strcpy(namebuf + len, "*.*"); |
| 2599 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2600 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2601 | if ((d = PyList_New(0)) == NULL) |
| 2602 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2604 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2605 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 2606 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2607 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 2608 | int error = GetLastError(); |
| 2609 | if (error == ERROR_FILE_NOT_FOUND) |
| 2610 | return d; |
| 2611 | Py_DECREF(d); |
| 2612 | return win32_error("FindFirstFile", namebuf); |
| 2613 | } |
| 2614 | do { |
| 2615 | /* Skip over . and .. */ |
| 2616 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 2617 | strcmp(FileData.cFileName, "..") != 0) { |
| 2618 | v = PyBytes_FromString(FileData.cFileName); |
| 2619 | if (v == NULL) { |
| 2620 | Py_DECREF(d); |
| 2621 | d = NULL; |
| 2622 | break; |
| 2623 | } |
| 2624 | if (PyList_Append(d, v) != 0) { |
| 2625 | Py_DECREF(v); |
| 2626 | Py_DECREF(d); |
| 2627 | d = NULL; |
| 2628 | break; |
| 2629 | } |
| 2630 | Py_DECREF(v); |
| 2631 | } |
| 2632 | Py_BEGIN_ALLOW_THREADS |
| 2633 | result = FindNextFile(hFindFile, &FileData); |
| 2634 | Py_END_ALLOW_THREADS |
| 2635 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 2636 | it got to the end of the directory. */ |
| 2637 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
| 2638 | Py_DECREF(d); |
| 2639 | win32_error("FindNextFile", namebuf); |
| 2640 | FindClose(hFindFile); |
| 2641 | return NULL; |
| 2642 | } |
| 2643 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2644 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2645 | if (FindClose(hFindFile) == FALSE) { |
| 2646 | Py_DECREF(d); |
| 2647 | return win32_error("FindClose", namebuf); |
| 2648 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2649 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2650 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2651 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2652 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2653 | |
| 2654 | #ifndef MAX_PATH |
| 2655 | #define MAX_PATH CCHMAXPATH |
| 2656 | #endif |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2657 | PyObject *oname; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2658 | char *name, *pt; |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 2659 | Py_ssize_t len; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2660 | PyObject *d, *v; |
| 2661 | char namebuf[MAX_PATH+5]; |
| 2662 | HDIR hdir = 1; |
| 2663 | ULONG srchcnt = 1; |
| 2664 | FILEFINDBUF3 ep; |
| 2665 | APIRET rc; |
| 2666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2667 | if (!PyArg_ParseTuple(args, "O&:listdir", |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2668 | PyUnicode_FSConverter, &oname)) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2669 | return NULL; |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2670 | name = PyBytes_AsString(oname); |
| 2671 | len = PyBytes_GET_SIZE(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2672 | if (len >= MAX_PATH) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2673 | Py_DECREF(oname); |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2674 | PyErr_SetString(PyExc_ValueError, "path too long"); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2675 | return NULL; |
| 2676 | } |
| 2677 | strcpy(namebuf, name); |
| 2678 | for (pt = namebuf; *pt; pt++) |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2679 | if (*pt == ALTSEP) |
| 2680 | *pt = SEP; |
| 2681 | if (namebuf[len-1] != SEP) |
| 2682 | namebuf[len++] = SEP; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2683 | strcpy(namebuf + len, "*.*"); |
| 2684 | |
Neal Norwitz | 6c91378 | 2007-10-14 03:23:09 +0000 | [diff] [blame] | 2685 | if ((d = PyList_New(0)) == NULL) { |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2686 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2687 | return NULL; |
Alexandre Vassalotti | 4167ebc | 2007-10-14 02:54:41 +0000 | [diff] [blame] | 2688 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2689 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2690 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */ |
| 2691 | &hdir, /* Handle to Use While Search Directory */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2692 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2693 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */ |
| 2694 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */ |
| 2695 | FIL_STANDARD); /* Format of Entry (EAs or Not) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (rc != NO_ERROR) { |
| 2698 | errno = ENOENT; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2699 | return posix_error_with_allocated_filename(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2702 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2703 | do { |
| 2704 | if (ep.achName[0] == '.' |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 2705 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0'))) |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2706 | continue; /* Skip Over "." and ".." Names */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2707 | |
| 2708 | strcpy(namebuf, ep.achName); |
| 2709 | |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 2710 | /* Leave Case of Name Alone -- In Native Form */ |
| 2711 | /* (Removed Forced Lowercasing Code) */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2712 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2713 | v = PyBytes_FromString(namebuf); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2714 | if (v == NULL) { |
| 2715 | Py_DECREF(d); |
| 2716 | d = NULL; |
| 2717 | break; |
| 2718 | } |
| 2719 | if (PyList_Append(d, v) != 0) { |
| 2720 | Py_DECREF(v); |
| 2721 | Py_DECREF(d); |
| 2722 | d = NULL; |
| 2723 | break; |
| 2724 | } |
| 2725 | Py_DECREF(v); |
| 2726 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0); |
| 2727 | } |
| 2728 | |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2729 | Py_DECREF(oname); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 2730 | return d; |
| 2731 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2732 | PyObject *oname; |
| 2733 | char *name; |
| 2734 | PyObject *d, *v; |
| 2735 | DIR *dirp; |
| 2736 | struct dirent *ep; |
| 2737 | int arg_is_unicode = 1; |
Just van Rossum | 96b1c90 | 2003-03-03 17:32:15 +0000 | [diff] [blame] | 2738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2739 | errno = 0; |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2740 | /* v is never read, so it does not need to be initialized yet. */ |
| 2741 | if (!PyArg_ParseTuple(args, "|U:listdir", &v)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2742 | arg_is_unicode = 0; |
| 2743 | PyErr_Clear(); |
| 2744 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2745 | oname = NULL; |
| 2746 | if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2747 | return NULL; |
Antoine Pitrou | 3d330ad | 2010-09-14 10:08:08 +0000 | [diff] [blame] | 2748 | if (oname == NULL) { /* Default arg: "." */ |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 2749 | oname = PyBytes_FromString("."); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 2750 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2751 | name = PyBytes_AsString(oname); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2752 | Py_BEGIN_ALLOW_THREADS |
| 2753 | dirp = opendir(name); |
| 2754 | Py_END_ALLOW_THREADS |
| 2755 | if (dirp == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2756 | return posix_error_with_allocated_filename(oname); |
| 2757 | } |
| 2758 | if ((d = PyList_New(0)) == NULL) { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2759 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2760 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2761 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2762 | Py_DECREF(oname); |
| 2763 | return NULL; |
| 2764 | } |
| 2765 | for (;;) { |
| 2766 | errno = 0; |
| 2767 | Py_BEGIN_ALLOW_THREADS |
| 2768 | ep = readdir(dirp); |
| 2769 | Py_END_ALLOW_THREADS |
| 2770 | if (ep == NULL) { |
| 2771 | if (errno == 0) { |
| 2772 | break; |
| 2773 | } else { |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2774 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2775 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2776 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2777 | Py_DECREF(d); |
| 2778 | return posix_error_with_allocated_filename(oname); |
| 2779 | } |
| 2780 | } |
| 2781 | if (ep->d_name[0] == '.' && |
| 2782 | (NAMLEN(ep) == 1 || |
| 2783 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2784 | continue; |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2785 | if (arg_is_unicode) |
| 2786 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2787 | else |
| 2788 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | if (v == NULL) { |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2790 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2791 | break; |
| 2792 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2793 | if (PyList_Append(d, v) != 0) { |
| 2794 | Py_DECREF(v); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 2795 | Py_CLEAR(d); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2796 | break; |
| 2797 | } |
| 2798 | Py_DECREF(v); |
| 2799 | } |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2800 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | closedir(dirp); |
Antoine Pitrou | d3ccde8 | 2010-09-04 17:21:57 +0000 | [diff] [blame] | 2802 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2803 | Py_DECREF(oname); |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 2804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | return d; |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2806 | |
Tim Peters | 0bb44a4 | 2000-09-15 07:44:49 +0000 | [diff] [blame] | 2807 | #endif /* which OS */ |
| 2808 | } /* end of posix_listdir */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2809 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 2810 | #ifdef HAVE_FDOPENDIR |
| 2811 | PyDoc_STRVAR(posix_fdlistdir__doc__, |
| 2812 | "fdlistdir(fd) -> list_of_strings\n\n\ |
| 2813 | Like listdir(), but uses a file descriptor instead.\n\ |
| 2814 | After succesful execution of this function, fd will be closed."); |
| 2815 | |
| 2816 | static PyObject * |
| 2817 | posix_fdlistdir(PyObject *self, PyObject *args) |
| 2818 | { |
| 2819 | PyObject *d, *v; |
| 2820 | DIR *dirp; |
| 2821 | struct dirent *ep; |
| 2822 | int fd; |
| 2823 | |
| 2824 | errno = 0; |
| 2825 | if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd)) |
| 2826 | return NULL; |
| 2827 | Py_BEGIN_ALLOW_THREADS |
| 2828 | dirp = fdopendir(fd); |
| 2829 | Py_END_ALLOW_THREADS |
| 2830 | if (dirp == NULL) { |
| 2831 | close(fd); |
| 2832 | return posix_error(); |
| 2833 | } |
| 2834 | if ((d = PyList_New(0)) == NULL) { |
| 2835 | Py_BEGIN_ALLOW_THREADS |
| 2836 | closedir(dirp); |
| 2837 | Py_END_ALLOW_THREADS |
| 2838 | return NULL; |
| 2839 | } |
| 2840 | for (;;) { |
| 2841 | errno = 0; |
| 2842 | Py_BEGIN_ALLOW_THREADS |
| 2843 | ep = readdir(dirp); |
| 2844 | Py_END_ALLOW_THREADS |
| 2845 | if (ep == NULL) { |
| 2846 | if (errno == 0) { |
| 2847 | break; |
| 2848 | } else { |
| 2849 | Py_BEGIN_ALLOW_THREADS |
| 2850 | closedir(dirp); |
| 2851 | Py_END_ALLOW_THREADS |
| 2852 | Py_DECREF(d); |
| 2853 | return posix_error(); |
| 2854 | } |
| 2855 | } |
| 2856 | if (ep->d_name[0] == '.' && |
| 2857 | (NAMLEN(ep) == 1 || |
| 2858 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 2859 | continue; |
| 2860 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 2861 | if (v == NULL) { |
| 2862 | Py_CLEAR(d); |
| 2863 | break; |
| 2864 | } |
| 2865 | if (PyList_Append(d, v) != 0) { |
| 2866 | Py_DECREF(v); |
| 2867 | Py_CLEAR(d); |
| 2868 | break; |
| 2869 | } |
| 2870 | Py_DECREF(v); |
| 2871 | } |
| 2872 | Py_BEGIN_ALLOW_THREADS |
| 2873 | closedir(dirp); |
| 2874 | Py_END_ALLOW_THREADS |
| 2875 | |
| 2876 | return d; |
| 2877 | } |
| 2878 | #endif |
| 2879 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2880 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2881 | /* A helper function for abspath on win32 */ |
| 2882 | static PyObject * |
| 2883 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 2884 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2885 | PyObject *opath; |
| 2886 | char *path; |
| 2887 | char outbuf[MAX_PATH*2]; |
| 2888 | char *temp; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2889 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2890 | PyUnicodeObject *po; |
| 2891 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { |
| 2892 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 2893 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; |
| 2894 | Py_UNICODE *wtemp; |
| 2895 | DWORD result; |
| 2896 | PyObject *v; |
| 2897 | result = GetFullPathNameW(wpath, |
| 2898 | sizeof(woutbuf)/sizeof(woutbuf[0]), |
| 2899 | woutbuf, &wtemp); |
| 2900 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { |
| 2901 | woutbufp = malloc(result * sizeof(Py_UNICODE)); |
| 2902 | if (!woutbufp) |
| 2903 | return PyErr_NoMemory(); |
| 2904 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 2905 | } |
| 2906 | if (result) |
| 2907 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); |
| 2908 | else |
| 2909 | v = win32_error_unicode("GetFullPathNameW", wpath); |
| 2910 | if (woutbufp != woutbuf) |
| 2911 | free(woutbufp); |
| 2912 | return v; |
| 2913 | } |
| 2914 | /* Drop the argument parsing error as narrow strings |
| 2915 | are also valid. */ |
| 2916 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2917 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2918 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2919 | if (!PyArg_ParseTuple (args, "O&:_getfullpathname", |
| 2920 | PyUnicode_FSConverter, &opath)) |
| 2921 | return NULL; |
| 2922 | path = PyBytes_AsString(opath); |
| 2923 | if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), |
| 2924 | outbuf, &temp)) { |
| 2925 | win32_error("GetFullPathName", path); |
| 2926 | Py_DECREF(opath); |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | Py_DECREF(opath); |
| 2930 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 2931 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 2932 | Py_FileSystemDefaultEncoding, NULL); |
| 2933 | } |
| 2934 | return PyBytes_FromString(outbuf); |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 2935 | } /* end of posix__getfullpathname */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2936 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2937 | |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2938 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2939 | /* A helper function for samepath on windows */ |
| 2940 | static PyObject * |
| 2941 | posix__getfinalpathname(PyObject *self, PyObject *args) |
| 2942 | { |
| 2943 | HANDLE hFile; |
| 2944 | int buf_size; |
| 2945 | wchar_t *target_path; |
| 2946 | int result_length; |
| 2947 | PyObject *result; |
| 2948 | wchar_t *path; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2949 | |
Brian Curtin | 94622b0 | 2010-09-24 00:03:39 +0000 | [diff] [blame] | 2950 | if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) { |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2951 | return NULL; |
| 2952 | } |
| 2953 | |
| 2954 | if(!check_GetFinalPathNameByHandle()) { |
| 2955 | /* If the OS doesn't have GetFinalPathNameByHandle, return a |
| 2956 | NotImplementedError. */ |
| 2957 | return PyErr_Format(PyExc_NotImplementedError, |
| 2958 | "GetFinalPathNameByHandle not available on this platform"); |
| 2959 | } |
| 2960 | |
| 2961 | hFile = CreateFileW( |
| 2962 | path, |
| 2963 | 0, /* desired access */ |
| 2964 | 0, /* share mode */ |
| 2965 | NULL, /* security attributes */ |
| 2966 | OPEN_EXISTING, |
| 2967 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 2968 | FILE_FLAG_BACKUP_SEMANTICS, |
| 2969 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2970 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2971 | if(hFile == INVALID_HANDLE_VALUE) { |
| 2972 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2973 | return PyErr_Format(PyExc_RuntimeError, |
| 2974 | "Could not get a handle to file."); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | /* We have a good handle to the target, use it to determine the |
| 2978 | target path name. */ |
| 2979 | buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
| 2980 | |
| 2981 | if(!buf_size) |
| 2982 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2983 | |
| 2984 | target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); |
| 2985 | if(!target_path) |
| 2986 | return PyErr_NoMemory(); |
| 2987 | |
| 2988 | result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, |
| 2989 | buf_size, VOLUME_NAME_DOS); |
| 2990 | if(!result_length) |
| 2991 | return win32_error_unicode("GetFinalPathNamyByHandle", path); |
| 2992 | |
| 2993 | if(!CloseHandle(hFile)) |
| 2994 | return win32_error_unicode("GetFinalPathNameByHandle", path); |
| 2995 | |
| 2996 | target_path[result_length] = 0; |
| 2997 | result = PyUnicode_FromUnicode(target_path, result_length); |
| 2998 | free(target_path); |
| 2999 | return result; |
| 3000 | |
| 3001 | } /* end of posix__getfinalpathname */ |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3002 | |
| 3003 | static PyObject * |
| 3004 | posix__getfileinformation(PyObject *self, PyObject *args) |
| 3005 | { |
| 3006 | HANDLE hFile; |
| 3007 | BY_HANDLE_FILE_INFORMATION info; |
| 3008 | int fd; |
| 3009 | |
| 3010 | if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) |
| 3011 | return NULL; |
| 3012 | |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3013 | if (!_PyVerify_fd(fd)) |
| 3014 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3015 | |
| 3016 | hFile = (HANDLE)_get_osfhandle(fd); |
| 3017 | if (hFile == INVALID_HANDLE_VALUE) |
Hirokazu Yamamoto | 26253bb | 2010-12-05 04:16:47 +0000 | [diff] [blame] | 3018 | return posix_error(); |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 3019 | |
| 3020 | if (!GetFileInformationByHandle(hFile, &info)) |
| 3021 | return win32_error("_getfileinformation", NULL); |
| 3022 | |
| 3023 | return Py_BuildValue("iii", info.dwVolumeSerialNumber, |
| 3024 | info.nFileIndexHigh, |
| 3025 | info.nFileIndexLow); |
| 3026 | } |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3027 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 3028 | PyDoc_STRVAR(posix__isdir__doc__, |
| 3029 | "Return true if the pathname refers to an existing directory."); |
| 3030 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 3031 | static PyObject * |
| 3032 | posix__isdir(PyObject *self, PyObject *args) |
| 3033 | { |
| 3034 | PyObject *opath; |
| 3035 | char *path; |
| 3036 | PyUnicodeObject *po; |
| 3037 | DWORD attributes; |
| 3038 | |
| 3039 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
| 3040 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); |
| 3041 | |
| 3042 | attributes = GetFileAttributesW(wpath); |
| 3043 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3044 | Py_RETURN_FALSE; |
| 3045 | goto check; |
| 3046 | } |
| 3047 | /* Drop the argument parsing error as narrow strings |
| 3048 | are also valid. */ |
| 3049 | PyErr_Clear(); |
| 3050 | |
| 3051 | if (!PyArg_ParseTuple(args, "O&:_isdir", |
| 3052 | PyUnicode_FSConverter, &opath)) |
| 3053 | return NULL; |
| 3054 | |
| 3055 | path = PyBytes_AsString(opath); |
| 3056 | attributes = GetFileAttributesA(path); |
| 3057 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 3058 | Py_RETURN_FALSE; |
| 3059 | |
| 3060 | check: |
| 3061 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 3062 | Py_RETURN_TRUE; |
| 3063 | else |
| 3064 | Py_RETURN_FALSE; |
| 3065 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3066 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3068 | PyDoc_STRVAR(posix_mkdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3069 | "mkdir(path [, mode=0777])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3070 | Create a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3071 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3072 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3073 | posix_mkdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3074 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3075 | int res; |
| 3076 | PyObject *opath; |
| 3077 | char *path; |
| 3078 | int mode = 0777; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3079 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3080 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3081 | PyUnicodeObject *po; |
| 3082 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { |
| 3083 | Py_BEGIN_ALLOW_THREADS |
| 3084 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3085 | it is a simple dereference. */ |
| 3086 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); |
| 3087 | Py_END_ALLOW_THREADS |
| 3088 | if (!res) |
| 3089 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); |
| 3090 | Py_INCREF(Py_None); |
| 3091 | return Py_None; |
| 3092 | } |
| 3093 | /* Drop the argument parsing error as narrow strings |
| 3094 | are also valid. */ |
| 3095 | PyErr_Clear(); |
| 3096 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3097 | PyUnicode_FSConverter, &opath, &mode)) |
| 3098 | return NULL; |
| 3099 | path = PyBytes_AsString(opath); |
| 3100 | Py_BEGIN_ALLOW_THREADS |
| 3101 | /* PyUnicode_AS_UNICODE OK without thread lock as |
| 3102 | it is a simple dereference. */ |
| 3103 | res = CreateDirectoryA(path, NULL); |
| 3104 | Py_END_ALLOW_THREADS |
| 3105 | if (!res) { |
| 3106 | win32_error("mkdir", path); |
| 3107 | Py_DECREF(opath); |
| 3108 | return NULL; |
| 3109 | } |
| 3110 | Py_DECREF(opath); |
| 3111 | Py_INCREF(Py_None); |
| 3112 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3113 | #else |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3114 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3115 | if (!PyArg_ParseTuple(args, "O&|i:mkdir", |
| 3116 | PyUnicode_FSConverter, &opath, &mode)) |
| 3117 | return NULL; |
| 3118 | path = PyBytes_AsString(opath); |
| 3119 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3120 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3121 | res = mkdir(path); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3122 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3123 | res = mkdir(path, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3124 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3125 | Py_END_ALLOW_THREADS |
| 3126 | if (res < 0) |
| 3127 | return posix_error_with_allocated_filename(opath); |
| 3128 | Py_DECREF(opath); |
| 3129 | Py_INCREF(Py_None); |
| 3130 | return Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3131 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3134 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3135 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 3136 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3137 | #include <sys/resource.h> |
| 3138 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3139 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3140 | |
| 3141 | #ifdef HAVE_NICE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3142 | PyDoc_STRVAR(posix_nice__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3143 | "nice(inc) -> new_priority\n\n\ |
| 3144 | 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] | 3145 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3146 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3147 | posix_nice(PyObject *self, PyObject *args) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3148 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3149 | int increment, value; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3151 | if (!PyArg_ParseTuple(args, "i:nice", &increment)) |
| 3152 | return NULL; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3153 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | /* There are two flavours of 'nice': one that returns the new |
| 3155 | priority (as required by almost all standards out there) and the |
| 3156 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 3157 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 3158 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3159 | If we are of the nice family that returns the new priority, we |
| 3160 | need to clear errno before the call, and check if errno is filled |
| 3161 | before calling posix_error() on a returnvalue of -1, because the |
| 3162 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3164 | errno = 0; |
| 3165 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 3166 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3167 | if (value == 0) |
| 3168 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 3169 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3170 | if (value == -1 && errno != 0) |
| 3171 | /* either nice() or getpriority() returned an error */ |
| 3172 | return posix_error(); |
| 3173 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 3174 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3175 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3176 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3177 | |
| 3178 | #ifdef HAVE_GETPRIORITY |
| 3179 | PyDoc_STRVAR(posix_getpriority__doc__, |
| 3180 | "getpriority(which, who) -> current_priority\n\n\ |
| 3181 | Get program scheduling priority."); |
| 3182 | |
| 3183 | static PyObject * |
| 3184 | posix_getpriority(PyObject *self, PyObject *args) |
| 3185 | { |
| 3186 | int which, who, retval; |
| 3187 | |
| 3188 | if (!PyArg_ParseTuple(args, "ii", &which, &who)) |
| 3189 | return NULL; |
| 3190 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3191 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3192 | if (errno != 0) |
| 3193 | return posix_error(); |
| 3194 | return PyLong_FromLong((long)retval); |
| 3195 | } |
| 3196 | #endif /* HAVE_GETPRIORITY */ |
| 3197 | |
| 3198 | |
| 3199 | #ifdef HAVE_SETPRIORITY |
| 3200 | PyDoc_STRVAR(posix_setpriority__doc__, |
| 3201 | "setpriority(which, who, prio) -> None\n\n\ |
| 3202 | Set program scheduling priority."); |
| 3203 | |
| 3204 | static PyObject * |
| 3205 | posix_setpriority(PyObject *self, PyObject *args) |
| 3206 | { |
| 3207 | int which, who, prio, retval; |
| 3208 | |
| 3209 | if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio)) |
| 3210 | return NULL; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3211 | retval = setpriority(which, who, prio); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 3212 | if (retval == -1) |
| 3213 | return posix_error(); |
| 3214 | Py_RETURN_NONE; |
| 3215 | } |
| 3216 | #endif /* HAVE_SETPRIORITY */ |
| 3217 | |
| 3218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3219 | PyDoc_STRVAR(posix_rename__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3220 | "rename(old, new)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3221 | Rename a file or directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3222 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3223 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3224 | posix_rename(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3225 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3226 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3227 | PyObject *o1, *o2; |
| 3228 | char *p1, *p2; |
| 3229 | BOOL result; |
| 3230 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) |
| 3231 | goto error; |
| 3232 | if (!convert_to_unicode(&o1)) |
| 3233 | goto error; |
| 3234 | if (!convert_to_unicode(&o2)) { |
| 3235 | Py_DECREF(o1); |
| 3236 | goto error; |
| 3237 | } |
| 3238 | Py_BEGIN_ALLOW_THREADS |
| 3239 | result = MoveFileW(PyUnicode_AsUnicode(o1), |
| 3240 | PyUnicode_AsUnicode(o2)); |
| 3241 | Py_END_ALLOW_THREADS |
| 3242 | Py_DECREF(o1); |
| 3243 | Py_DECREF(o2); |
| 3244 | if (!result) |
| 3245 | return win32_error("rename", NULL); |
| 3246 | Py_INCREF(Py_None); |
| 3247 | return Py_None; |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3248 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3249 | PyErr_Clear(); |
| 3250 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) |
| 3251 | return NULL; |
| 3252 | Py_BEGIN_ALLOW_THREADS |
| 3253 | result = MoveFileA(p1, p2); |
| 3254 | Py_END_ALLOW_THREADS |
| 3255 | if (!result) |
| 3256 | return win32_error("rename", NULL); |
| 3257 | Py_INCREF(Py_None); |
| 3258 | return Py_None; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3259 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3260 | return posix_2str(args, "O&O&:rename", rename); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3261 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3265 | PyDoc_STRVAR(posix_rmdir__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3266 | "rmdir(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3267 | Remove a directory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3268 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3269 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3270 | posix_rmdir(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3271 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3272 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3273 | return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3274 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3275 | return posix_1str(args, "O&:rmdir", rmdir); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3276 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3280 | PyDoc_STRVAR(posix_stat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3281 | "stat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3282 | Perform a stat system call on the given path."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3284 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3285 | posix_stat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3286 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3287 | #ifdef MS_WINDOWS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3288 | 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] | 3289 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3290 | return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3291 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3294 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3295 | #ifdef HAVE_SYSTEM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3296 | PyDoc_STRVAR(posix_system__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3297 | "system(command) -> exit_status\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3298 | Execute the command (a string) in a subshell."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3299 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3300 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3301 | posix_system(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3302 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3303 | long sts; |
Amaury Forgeot d'Arc | 90ebd3e | 2007-11-20 01:52:14 +0000 | [diff] [blame] | 3304 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3305 | wchar_t *command; |
| 3306 | if (!PyArg_ParseTuple(args, "u:system", &command)) |
| 3307 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3308 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3309 | Py_BEGIN_ALLOW_THREADS |
| 3310 | sts = _wsystem(command); |
| 3311 | Py_END_ALLOW_THREADS |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3312 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3313 | PyObject *command_obj; |
| 3314 | char *command; |
| 3315 | if (!PyArg_ParseTuple(args, "O&:system", |
| 3316 | PyUnicode_FSConverter, &command_obj)) |
| 3317 | return NULL; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3318 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3319 | command = PyBytes_AsString(command_obj); |
| 3320 | Py_BEGIN_ALLOW_THREADS |
| 3321 | sts = system(command); |
| 3322 | Py_END_ALLOW_THREADS |
| 3323 | Py_DECREF(command_obj); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 3324 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3325 | return PyLong_FromLong(sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3326 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 3327 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3330 | PyDoc_STRVAR(posix_umask__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3331 | "umask(new_mask) -> old_mask\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3332 | Set the current numeric umask and return the previous umask."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3333 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3334 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3335 | posix_umask(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3336 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3337 | int i; |
| 3338 | if (!PyArg_ParseTuple(args, "i:umask", &i)) |
| 3339 | return NULL; |
| 3340 | i = (int)umask(i); |
| 3341 | if (i < 0) |
| 3342 | return posix_error(); |
| 3343 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3346 | #ifdef MS_WINDOWS |
| 3347 | |
| 3348 | /* override the default DeleteFileW behavior so that directory |
| 3349 | symlinks can be removed with this function, the same as with |
| 3350 | Unix symlinks */ |
| 3351 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 3352 | { |
| 3353 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 3354 | WIN32_FIND_DATAW find_data; |
| 3355 | HANDLE find_data_handle; |
| 3356 | int is_directory = 0; |
| 3357 | int is_link = 0; |
| 3358 | |
| 3359 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 3360 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3361 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3362 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 3363 | it is a symlink */ |
| 3364 | if(is_directory && |
| 3365 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 3366 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 3367 | |
| 3368 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
| 3369 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; |
| 3370 | FindClose(find_data_handle); |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | if (is_directory && is_link) |
| 3376 | return RemoveDirectoryW(lpFileName); |
| 3377 | |
| 3378 | return DeleteFileW(lpFileName); |
| 3379 | } |
| 3380 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3382 | PyDoc_STRVAR(posix_unlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3383 | "unlink(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3384 | Remove a file (same as remove(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3386 | PyDoc_STRVAR(posix_remove__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3387 | "remove(path)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3388 | Remove a file (same as unlink(path))."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3389 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3390 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3391 | posix_unlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3392 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3393 | #ifdef MS_WINDOWS |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 3394 | return win32_1str(args, "remove", "y:remove", DeleteFileA, |
| 3395 | "U:remove", Py_DeleteFileW); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3396 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3397 | return posix_1str(args, "O&:remove", unlink); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3398 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3401 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3402 | #ifdef HAVE_UNAME |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3403 | PyDoc_STRVAR(posix_uname__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3404 | "uname() -> (sysname, nodename, release, version, machine)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3405 | Return a tuple identifying the current operating system."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3406 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3407 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3408 | posix_uname(PyObject *self, PyObject *noargs) |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3409 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3410 | struct utsname u; |
| 3411 | int res; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 3412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3413 | Py_BEGIN_ALLOW_THREADS |
| 3414 | res = uname(&u); |
| 3415 | Py_END_ALLOW_THREADS |
| 3416 | if (res < 0) |
| 3417 | return posix_error(); |
| 3418 | return Py_BuildValue("(sssss)", |
| 3419 | u.sysname, |
| 3420 | u.nodename, |
| 3421 | u.release, |
| 3422 | u.version, |
| 3423 | u.machine); |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 3424 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3425 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3426 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3427 | |
| 3428 | /* |
| 3429 | * Classic POSIX utime functions supported microseconds (1m/sec). |
| 3430 | * Newer POSIX functions support nanoseconds (1 billion per sec). |
| 3431 | * posixmodule now uses the new functions where possible. |
| 3432 | * This improves accuracy in many situations, for example shutil.copy2(). |
| 3433 | * |
| 3434 | * The implementation isn't currently sophisticated enough to handle |
| 3435 | * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false. |
| 3436 | * Specifically, posix_futimes() would break. |
| 3437 | * |
| 3438 | * Supporting such a platform wouldn't be impossible; you'd need two |
| 3439 | * extract_time() functions, or make its precision a parameter. |
| 3440 | * Since such a platform seems unlikely we haven't bothered. |
| 3441 | */ |
| 3442 | #if defined(HAVE_UTIMENSAT) |
| 3443 | #define EXTRACT_TIME_PRECISION (1e9) |
| 3444 | #if !defined(HAVE_FUTIMENS) |
| 3445 | #error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment. |
| 3446 | #endif |
| 3447 | #else |
| 3448 | #define EXTRACT_TIME_PRECISION (1e6) |
| 3449 | #endif |
| 3450 | |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3451 | static int |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3452 | extract_time(PyObject *t, time_t* sec, long* usec) |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3453 | { |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3454 | time_t intval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3455 | if (PyFloat_Check(t)) { |
| 3456 | double tval = PyFloat_AsDouble(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3457 | PyObject *intobj = PyNumber_Long(t); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3458 | if (!intobj) |
| 3459 | return -1; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3460 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3461 | intval = PyLong_AsUnsignedLongLongMask(intobj); |
| 3462 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3463 | intval = PyLong_AsLong(intobj); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3464 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3465 | Py_DECREF(intobj); |
| 3466 | if (intval == -1 && PyErr_Occurred()) |
| 3467 | return -1; |
| 3468 | *sec = intval; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3469 | |
| 3470 | *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3471 | if (*usec < 0) |
| 3472 | /* If rounding gave us a negative number, |
| 3473 | truncate. */ |
| 3474 | *usec = 0; |
Martin v. Löwis | 076b209 | 2002-09-10 15:04:41 +0000 | [diff] [blame] | 3475 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | } |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3477 | #if SIZEOF_TIME_T > SIZEOF_LONG |
| 3478 | intval = PyLong_AsUnsignedLongLongMask(t); |
| 3479 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3480 | intval = PyLong_AsLong(t); |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3481 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3482 | if (intval == -1 && PyErr_Occurred()) |
| 3483 | return -1; |
| 3484 | *sec = intval; |
| 3485 | *usec = 0; |
| 3486 | return 0; |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3487 | } |
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_utime__doc__, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3490 | "utime(path, (atime, mtime))\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3491 | utime(path, None)\n\n\ |
Barry Warsaw | 3cef856 | 2000-05-01 16:17:24 +0000 | [diff] [blame] | 3492 | Set the access and modified time of the file to the given values. If the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3493 | second form is used, set the access and modified times to the current time."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3494 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3495 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3496 | posix_utime(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3497 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3498 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | PyObject *arg; |
| 3500 | PyUnicodeObject *obwpath; |
| 3501 | wchar_t *wpath = NULL; |
| 3502 | PyObject *oapath; |
| 3503 | char *apath; |
| 3504 | HANDLE hFile; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3505 | time_t atimesec, mtimesec; |
| 3506 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3507 | FILETIME atime, mtime; |
| 3508 | PyObject *result = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3509 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3510 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { |
| 3511 | wpath = PyUnicode_AS_UNICODE(obwpath); |
| 3512 | Py_BEGIN_ALLOW_THREADS |
| 3513 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, |
| 3514 | NULL, OPEN_EXISTING, |
| 3515 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3516 | Py_END_ALLOW_THREADS |
| 3517 | if (hFile == INVALID_HANDLE_VALUE) |
| 3518 | return win32_error_unicode("utime", wpath); |
| 3519 | } else |
| 3520 | /* Drop the argument parsing error as narrow strings |
| 3521 | are also valid. */ |
| 3522 | PyErr_Clear(); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3523 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3524 | if (!wpath) { |
| 3525 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3526 | PyUnicode_FSConverter, &oapath, &arg)) |
| 3527 | return NULL; |
| 3528 | apath = PyBytes_AsString(oapath); |
| 3529 | Py_BEGIN_ALLOW_THREADS |
| 3530 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, |
| 3531 | NULL, OPEN_EXISTING, |
| 3532 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 3533 | Py_END_ALLOW_THREADS |
| 3534 | if (hFile == INVALID_HANDLE_VALUE) { |
| 3535 | win32_error("utime", apath); |
| 3536 | Py_DECREF(oapath); |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | Py_DECREF(oapath); |
| 3540 | } |
| 3541 | |
| 3542 | if (arg == Py_None) { |
| 3543 | SYSTEMTIME now; |
| 3544 | GetSystemTime(&now); |
| 3545 | if (!SystemTimeToFileTime(&now, &mtime) || |
| 3546 | !SystemTimeToFileTime(&now, &atime)) { |
| 3547 | win32_error("utime", NULL); |
| 3548 | goto done; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 3549 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3550 | } |
| 3551 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3552 | PyErr_SetString(PyExc_TypeError, |
| 3553 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3554 | goto done; |
| 3555 | } |
| 3556 | else { |
| 3557 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3558 | &atimesec, &ausec) == -1) |
| 3559 | goto done; |
| 3560 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); |
| 3561 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3562 | &mtimesec, &musec) == -1) |
| 3563 | goto done; |
| 3564 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); |
| 3565 | } |
| 3566 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 3567 | /* Avoid putting the file name into the error here, |
| 3568 | as that may confuse the user into believing that |
| 3569 | something is wrong with the file, when it also |
| 3570 | could be the time stamp that gives a problem. */ |
| 3571 | win32_error("utime", NULL); |
Antoine Pitrou | e85da7a | 2011-01-06 18:25:55 +0000 | [diff] [blame] | 3572 | goto done; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3573 | } |
| 3574 | Py_INCREF(Py_None); |
| 3575 | result = Py_None; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3576 | done: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3577 | CloseHandle(hFile); |
| 3578 | return result; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3579 | #else /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3581 | PyObject *opath; |
| 3582 | char *path; |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 3583 | time_t atime, mtime; |
| 3584 | long ausec, musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3585 | int res; |
| 3586 | PyObject* arg; |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 3587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3588 | if (!PyArg_ParseTuple(args, "O&O:utime", |
| 3589 | PyUnicode_FSConverter, &opath, &arg)) |
| 3590 | return NULL; |
| 3591 | path = PyBytes_AsString(opath); |
| 3592 | if (arg == Py_None) { |
| 3593 | /* optional time values not given */ |
| 3594 | Py_BEGIN_ALLOW_THREADS |
| 3595 | res = utime(path, NULL); |
| 3596 | Py_END_ALLOW_THREADS |
| 3597 | } |
| 3598 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3599 | PyErr_SetString(PyExc_TypeError, |
| 3600 | "utime() arg 2 must be a tuple (atime, mtime)"); |
| 3601 | Py_DECREF(opath); |
| 3602 | return NULL; |
| 3603 | } |
| 3604 | else { |
| 3605 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
| 3606 | &atime, &ausec) == -1) { |
| 3607 | Py_DECREF(opath); |
| 3608 | return NULL; |
| 3609 | } |
| 3610 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
| 3611 | &mtime, &musec) == -1) { |
| 3612 | Py_DECREF(opath); |
| 3613 | return NULL; |
| 3614 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3615 | |
| 3616 | Py_BEGIN_ALLOW_THREADS |
| 3617 | { |
| 3618 | #ifdef HAVE_UTIMENSAT |
| 3619 | struct timespec buf[2]; |
| 3620 | buf[0].tv_sec = atime; |
| 3621 | buf[0].tv_nsec = ausec; |
| 3622 | buf[1].tv_sec = mtime; |
| 3623 | buf[1].tv_nsec = musec; |
| 3624 | res = utimensat(AT_FDCWD, path, buf, 0); |
| 3625 | #elif defined(HAVE_UTIMES) |
| 3626 | struct timeval buf[2]; |
| 3627 | buf[0].tv_sec = atime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3628 | buf[0].tv_usec = ausec; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3629 | buf[1].tv_sec = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | buf[1].tv_usec = musec; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3631 | res = utimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3632 | #elif defined(HAVE_UTIME_H) |
| 3633 | /* XXX should define struct utimbuf instead, above */ |
| 3634 | struct utimbuf buf; |
| 3635 | buf.actime = atime; |
| 3636 | buf.modtime = mtime; |
| 3637 | res = utime(path, &buf); |
Martin v. Löwis | 6aa9fdb | 2002-09-10 09:16:13 +0000 | [diff] [blame] | 3638 | #else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3639 | time_t buf[2]; |
| 3640 | buf[0] = atime; |
| 3641 | buf[1] = mtime; |
| 3642 | res = utime(path, buf); |
| 3643 | #endif |
| 3644 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3645 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3646 | } |
| 3647 | if (res < 0) { |
| 3648 | return posix_error_with_allocated_filename(opath); |
| 3649 | } |
| 3650 | Py_DECREF(opath); |
| 3651 | Py_INCREF(Py_None); |
| 3652 | return Py_None; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3653 | #undef UTIME_EXTRACT |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3654 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3657 | #ifdef HAVE_FUTIMES |
| 3658 | PyDoc_STRVAR(posix_futimes__doc__, |
| 3659 | "futimes(fd, (atime, mtime))\n\ |
| 3660 | futimes(fd, None)\n\n\ |
| 3661 | Set the access and modified time of the file specified by the file\n\ |
| 3662 | descriptor fd to the given values. If the second form is used, set the\n\ |
| 3663 | access and modified times to the current time."); |
| 3664 | |
| 3665 | static PyObject * |
| 3666 | posix_futimes(PyObject *self, PyObject *args) |
| 3667 | { |
| 3668 | int res, fd; |
| 3669 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3670 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3671 | long ausec, musec; |
| 3672 | |
| 3673 | if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg)) |
| 3674 | return NULL; |
| 3675 | |
| 3676 | if (arg == Py_None) { |
| 3677 | /* optional time values not given */ |
| 3678 | Py_BEGIN_ALLOW_THREADS |
| 3679 | res = futimes(fd, NULL); |
| 3680 | Py_END_ALLOW_THREADS |
| 3681 | } |
| 3682 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3683 | PyErr_SetString(PyExc_TypeError, |
| 3684 | "futimes() arg 2 must be a tuple (atime, mtime)"); |
| 3685 | return NULL; |
| 3686 | } |
| 3687 | else { |
| 3688 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3689 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3690 | return NULL; |
| 3691 | } |
| 3692 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3693 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3694 | return NULL; |
| 3695 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3696 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3697 | { |
| 3698 | #ifdef HAVE_FUTIMENS |
| 3699 | struct timespec buf[2]; |
| 3700 | buf[0].tv_sec = atime; |
| 3701 | buf[0].tv_nsec = ausec; |
| 3702 | buf[1].tv_sec = mtime; |
| 3703 | buf[1].tv_nsec = musec; |
| 3704 | res = futimens(fd, buf); |
| 3705 | #else |
| 3706 | struct timeval buf[2]; |
| 3707 | buf[0].tv_sec = atime; |
| 3708 | buf[0].tv_usec = ausec; |
| 3709 | buf[1].tv_sec = mtime; |
| 3710 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3711 | res = futimes(fd, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3712 | #endif |
| 3713 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3714 | Py_END_ALLOW_THREADS |
| 3715 | } |
| 3716 | if (res < 0) |
| 3717 | return posix_error(); |
| 3718 | Py_RETURN_NONE; |
| 3719 | } |
| 3720 | #endif |
| 3721 | |
| 3722 | #ifdef HAVE_LUTIMES |
| 3723 | PyDoc_STRVAR(posix_lutimes__doc__, |
| 3724 | "lutimes(path, (atime, mtime))\n\ |
| 3725 | lutimes(path, None)\n\n\ |
| 3726 | Like utime(), but if path is a symbolic link, it is not dereferenced."); |
| 3727 | |
| 3728 | static PyObject * |
| 3729 | posix_lutimes(PyObject *self, PyObject *args) |
| 3730 | { |
| 3731 | PyObject *opath, *arg; |
| 3732 | const char *path; |
| 3733 | int res; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3734 | time_t atime, mtime; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3735 | long ausec, musec; |
| 3736 | |
| 3737 | if (!PyArg_ParseTuple(args, "O&O:lutimes", |
| 3738 | PyUnicode_FSConverter, &opath, &arg)) |
| 3739 | return NULL; |
| 3740 | path = PyBytes_AsString(opath); |
| 3741 | if (arg == Py_None) { |
| 3742 | /* optional time values not given */ |
| 3743 | Py_BEGIN_ALLOW_THREADS |
| 3744 | res = lutimes(path, NULL); |
| 3745 | Py_END_ALLOW_THREADS |
| 3746 | } |
| 3747 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 3748 | PyErr_SetString(PyExc_TypeError, |
| 3749 | "lutimes() arg 2 must be a tuple (atime, mtime)"); |
| 3750 | Py_DECREF(opath); |
| 3751 | return NULL; |
| 3752 | } |
| 3753 | else { |
| 3754 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3755 | &atime, &ausec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3756 | Py_DECREF(opath); |
| 3757 | return NULL; |
| 3758 | } |
| 3759 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3760 | &mtime, &musec) == -1) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3761 | Py_DECREF(opath); |
| 3762 | return NULL; |
| 3763 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3764 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3765 | { |
| 3766 | #ifdef HAVE_UTIMENSAT |
| 3767 | struct timespec buf[2]; |
| 3768 | buf[0].tv_sec = atime; |
| 3769 | buf[0].tv_nsec = ausec; |
| 3770 | buf[1].tv_sec = mtime; |
| 3771 | buf[1].tv_nsec = musec; |
| 3772 | res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); |
| 3773 | #else |
| 3774 | struct timeval buf[2]; |
| 3775 | buf[0].tv_sec = atime; |
| 3776 | buf[0].tv_usec = ausec; |
| 3777 | buf[1].tv_sec = mtime; |
| 3778 | buf[1].tv_usec = musec; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3779 | res = lutimes(path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 3780 | #endif |
| 3781 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3782 | Py_END_ALLOW_THREADS |
| 3783 | } |
| 3784 | Py_DECREF(opath); |
| 3785 | if (res < 0) |
| 3786 | return posix_error(); |
| 3787 | Py_RETURN_NONE; |
| 3788 | } |
| 3789 | #endif |
| 3790 | |
| 3791 | #ifdef HAVE_FUTIMENS |
| 3792 | PyDoc_STRVAR(posix_futimens__doc__, |
| 3793 | "futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\ |
| 3794 | futimens(fd, None, None)\n\n\ |
| 3795 | Updates the timestamps of a file specified by the file descriptor fd, with\n\ |
| 3796 | nanosecond precision.\n\ |
| 3797 | The second form sets atime and mtime to the current time.\n\ |
| 3798 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 3799 | current time.\n\ |
| 3800 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 3801 | |
| 3802 | static PyObject * |
| 3803 | posix_futimens(PyObject *self, PyObject *args) |
| 3804 | { |
| 3805 | int res, fd; |
| 3806 | PyObject *atime, *mtime; |
| 3807 | struct timespec buf[2]; |
| 3808 | |
| 3809 | if (!PyArg_ParseTuple(args, "iOO:futimens", |
| 3810 | &fd, &atime, &mtime)) |
| 3811 | return NULL; |
| 3812 | if (atime == Py_None && mtime == Py_None) { |
| 3813 | /* optional time values not given */ |
| 3814 | Py_BEGIN_ALLOW_THREADS |
| 3815 | res = futimens(fd, NULL); |
| 3816 | Py_END_ALLOW_THREADS |
| 3817 | } |
| 3818 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 3819 | PyErr_SetString(PyExc_TypeError, |
| 3820 | "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)"); |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 3824 | PyErr_SetString(PyExc_TypeError, |
| 3825 | "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)"); |
| 3826 | return NULL; |
| 3827 | } |
| 3828 | else { |
| 3829 | if (!PyArg_ParseTuple(atime, "ll:futimens", |
| 3830 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 3831 | return NULL; |
| 3832 | } |
| 3833 | if (!PyArg_ParseTuple(mtime, "ll:futimens", |
| 3834 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 3835 | return NULL; |
| 3836 | } |
| 3837 | Py_BEGIN_ALLOW_THREADS |
| 3838 | res = futimens(fd, buf); |
| 3839 | Py_END_ALLOW_THREADS |
| 3840 | } |
| 3841 | if (res < 0) |
| 3842 | return posix_error(); |
| 3843 | Py_RETURN_NONE; |
| 3844 | } |
| 3845 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3846 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 3847 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3849 | PyDoc_STRVAR(posix__exit__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 3850 | "_exit(status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3851 | Exit to the system with specified status, without normal exit processing."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3852 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3853 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 3854 | posix__exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3855 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3856 | int sts; |
| 3857 | if (!PyArg_ParseTuple(args, "i:_exit", &sts)) |
| 3858 | return NULL; |
| 3859 | _exit(sts); |
| 3860 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3863 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 3864 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3865 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3867 | Py_ssize_t i; |
| 3868 | for (i = 0; i < count; i++) |
| 3869 | PyMem_Free(array[i]); |
| 3870 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3871 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 3873 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3874 | int fsconvert_strdup(PyObject *o, char**out) |
| 3875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3876 | PyObject *bytes; |
| 3877 | Py_ssize_t size; |
| 3878 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 3879 | return 0; |
| 3880 | size = PyBytes_GET_SIZE(bytes); |
| 3881 | *out = PyMem_Malloc(size+1); |
| 3882 | if (!*out) |
| 3883 | return 0; |
| 3884 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 3885 | Py_DECREF(bytes); |
| 3886 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3887 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 3888 | #endif |
| 3889 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3890 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3891 | static char** |
| 3892 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 3893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3894 | char **envlist; |
| 3895 | Py_ssize_t i, pos, envc; |
| 3896 | PyObject *keys=NULL, *vals=NULL; |
| 3897 | PyObject *key, *val, *key2, *val2; |
| 3898 | char *p, *k, *v; |
| 3899 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3900 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3901 | i = PyMapping_Size(env); |
| 3902 | if (i < 0) |
| 3903 | return NULL; |
| 3904 | envlist = PyMem_NEW(char *, i + 1); |
| 3905 | if (envlist == NULL) { |
| 3906 | PyErr_NoMemory(); |
| 3907 | return NULL; |
| 3908 | } |
| 3909 | envc = 0; |
| 3910 | keys = PyMapping_Keys(env); |
| 3911 | vals = PyMapping_Values(env); |
| 3912 | if (!keys || !vals) |
| 3913 | goto error; |
| 3914 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 3915 | PyErr_Format(PyExc_TypeError, |
| 3916 | "env.keys() or env.values() is not a list"); |
| 3917 | goto error; |
| 3918 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3920 | for (pos = 0; pos < i; pos++) { |
| 3921 | key = PyList_GetItem(keys, pos); |
| 3922 | val = PyList_GetItem(vals, pos); |
| 3923 | if (!key || !val) |
| 3924 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3926 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 3927 | goto error; |
| 3928 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 3929 | Py_DECREF(key2); |
| 3930 | goto error; |
| 3931 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3932 | |
| 3933 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3934 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */ |
| 3935 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3936 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3937 | k = PyBytes_AsString(key2); |
| 3938 | v = PyBytes_AsString(val2); |
| 3939 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3941 | p = PyMem_NEW(char, len); |
| 3942 | if (p == NULL) { |
| 3943 | PyErr_NoMemory(); |
| 3944 | Py_DECREF(key2); |
| 3945 | Py_DECREF(val2); |
| 3946 | goto error; |
| 3947 | } |
| 3948 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 3949 | envlist[envc++] = p; |
| 3950 | Py_DECREF(key2); |
| 3951 | Py_DECREF(val2); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3952 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3953 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3954 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3955 | } |
| 3956 | Py_DECREF(vals); |
| 3957 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3958 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3959 | envlist[envc] = 0; |
| 3960 | *envc_ptr = envc; |
| 3961 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3962 | |
| 3963 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3964 | Py_XDECREF(keys); |
| 3965 | Py_XDECREF(vals); |
| 3966 | while (--envc >= 0) |
| 3967 | PyMem_DEL(envlist[envc]); |
| 3968 | PyMem_DEL(envlist); |
| 3969 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 3970 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3971 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3972 | static char** |
| 3973 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 3974 | { |
| 3975 | int i; |
| 3976 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 3977 | if (argvlist == NULL) { |
| 3978 | PyErr_NoMemory(); |
| 3979 | return NULL; |
| 3980 | } |
| 3981 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3982 | PyObject* item = PySequence_ITEM(argv, i); |
| 3983 | if (item == NULL) |
| 3984 | goto fail; |
| 3985 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 3986 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3987 | goto fail; |
| 3988 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3989 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3990 | } |
| 3991 | argvlist[*argc] = NULL; |
| 3992 | return argvlist; |
| 3993 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 3994 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3995 | free_string_array(argvlist, *argc); |
| 3996 | return NULL; |
| 3997 | } |
| 3998 | #endif |
| 3999 | |
| 4000 | #ifdef HAVE_EXECV |
| 4001 | PyDoc_STRVAR(posix_execv__doc__, |
| 4002 | "execv(path, args)\n\n\ |
| 4003 | Execute an executable path with arguments, replacing current process.\n\ |
| 4004 | \n\ |
| 4005 | path: path of executable file\n\ |
| 4006 | args: tuple or list of strings"); |
| 4007 | |
| 4008 | static PyObject * |
| 4009 | posix_execv(PyObject *self, PyObject *args) |
| 4010 | { |
| 4011 | PyObject *opath; |
| 4012 | char *path; |
| 4013 | PyObject *argv; |
| 4014 | char **argvlist; |
| 4015 | Py_ssize_t argc; |
| 4016 | |
| 4017 | /* execv has two arguments: (path, argv), where |
| 4018 | argv is a list or tuple of strings. */ |
| 4019 | |
| 4020 | if (!PyArg_ParseTuple(args, "O&O:execv", |
| 4021 | PyUnicode_FSConverter, |
| 4022 | &opath, &argv)) |
| 4023 | return NULL; |
| 4024 | path = PyBytes_AsString(opath); |
| 4025 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4026 | PyErr_SetString(PyExc_TypeError, |
| 4027 | "execv() arg 2 must be a tuple or list"); |
| 4028 | Py_DECREF(opath); |
| 4029 | return NULL; |
| 4030 | } |
| 4031 | argc = PySequence_Size(argv); |
| 4032 | if (argc < 1) { |
| 4033 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
| 4034 | Py_DECREF(opath); |
| 4035 | return NULL; |
| 4036 | } |
| 4037 | |
| 4038 | argvlist = parse_arglist(argv, &argc); |
| 4039 | if (argvlist == NULL) { |
| 4040 | Py_DECREF(opath); |
| 4041 | return NULL; |
| 4042 | } |
| 4043 | |
| 4044 | execv(path, argvlist); |
| 4045 | |
| 4046 | /* If we get here it's definitely an error */ |
| 4047 | |
| 4048 | free_string_array(argvlist, argc); |
| 4049 | Py_DECREF(opath); |
| 4050 | return posix_error(); |
| 4051 | } |
| 4052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4053 | PyDoc_STRVAR(posix_execve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4054 | "execve(path, args, env)\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4055 | Execute a path with arguments and environment, replacing current process.\n\ |
| 4056 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4057 | path: path of executable file\n\ |
| 4058 | args: tuple or list of arguments\n\ |
| 4059 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4061 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4062 | posix_execve(PyObject *self, PyObject *args) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4063 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4064 | PyObject *opath; |
| 4065 | char *path; |
| 4066 | PyObject *argv, *env; |
| 4067 | char **argvlist; |
| 4068 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4069 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4070 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4071 | /* execve has three arguments: (path, argv, env), where |
| 4072 | argv is a list or tuple of strings and env is a dictionary |
| 4073 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4074 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4075 | if (!PyArg_ParseTuple(args, "O&OO:execve", |
| 4076 | PyUnicode_FSConverter, |
| 4077 | &opath, &argv, &env)) |
| 4078 | return NULL; |
| 4079 | path = PyBytes_AsString(opath); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4080 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4081 | PyErr_SetString(PyExc_TypeError, |
| 4082 | "execve() arg 2 must be a tuple or list"); |
| 4083 | goto fail_0; |
| 4084 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4085 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4086 | if (!PyMapping_Check(env)) { |
| 4087 | PyErr_SetString(PyExc_TypeError, |
| 4088 | "execve() arg 3 must be a mapping object"); |
| 4089 | goto fail_0; |
| 4090 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4091 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4092 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4093 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4094 | goto fail_0; |
| 4095 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4097 | envlist = parse_envlist(env, &envc); |
| 4098 | if (envlist == NULL) |
| 4099 | goto fail_1; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4100 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4101 | execve(path, argvlist, envlist); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4102 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4103 | /* If we get here it's definitely an error */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4104 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4105 | (void) posix_error(); |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4107 | while (--envc >= 0) |
| 4108 | PyMem_DEL(envlist[envc]); |
| 4109 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4110 | fail_1: |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4111 | free_string_array(argvlist, argc); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4112 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4113 | Py_DECREF(opath); |
| 4114 | return NULL; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4115 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4116 | #endif /* HAVE_EXECV */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 4117 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 4118 | #ifdef HAVE_FEXECVE |
| 4119 | PyDoc_STRVAR(posix_fexecve__doc__, |
| 4120 | "fexecve(fd, args, env)\n\n\ |
| 4121 | Execute the program specified by a file descriptor with arguments and\n\ |
| 4122 | environment, replacing the current process.\n\ |
| 4123 | \n\ |
| 4124 | fd: file descriptor of executable\n\ |
| 4125 | args: tuple or list of arguments\n\ |
| 4126 | env: dictionary of strings mapping to strings"); |
| 4127 | |
| 4128 | static PyObject * |
| 4129 | posix_fexecve(PyObject *self, PyObject *args) |
| 4130 | { |
| 4131 | int fd; |
| 4132 | PyObject *argv, *env; |
| 4133 | char **argvlist; |
| 4134 | char **envlist; |
| 4135 | Py_ssize_t argc, envc; |
| 4136 | |
| 4137 | if (!PyArg_ParseTuple(args, "iOO:fexecve", |
| 4138 | &fd, &argv, &env)) |
| 4139 | return NULL; |
| 4140 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 4141 | PyErr_SetString(PyExc_TypeError, |
| 4142 | "fexecve() arg 2 must be a tuple or list"); |
| 4143 | return NULL; |
| 4144 | } |
| 4145 | argc = PySequence_Size(argv); |
| 4146 | if (!PyMapping_Check(env)) { |
| 4147 | PyErr_SetString(PyExc_TypeError, |
| 4148 | "fexecve() arg 3 must be a mapping object"); |
| 4149 | return NULL; |
| 4150 | } |
| 4151 | |
| 4152 | argvlist = parse_arglist(argv, &argc); |
| 4153 | if (argvlist == NULL) |
| 4154 | return NULL; |
| 4155 | |
| 4156 | envlist = parse_envlist(env, &envc); |
| 4157 | if (envlist == NULL) |
| 4158 | goto fail; |
| 4159 | |
| 4160 | fexecve(fd, argvlist, envlist); |
| 4161 | |
| 4162 | /* If we get here it's definitely an error */ |
| 4163 | |
| 4164 | (void) posix_error(); |
| 4165 | |
| 4166 | while (--envc >= 0) |
| 4167 | PyMem_DEL(envlist[envc]); |
| 4168 | PyMem_DEL(envlist); |
| 4169 | fail: |
| 4170 | free_string_array(argvlist, argc); |
| 4171 | return NULL; |
| 4172 | } |
| 4173 | #endif /* HAVE_FEXECVE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4174 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4175 | #ifdef HAVE_SPAWNV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4176 | PyDoc_STRVAR(posix_spawnv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4177 | "spawnv(mode, path, args)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4178 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4179 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | mode: mode of process creation\n\ |
| 4181 | path: path of executable file\n\ |
| 4182 | args: tuple or list of strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4183 | |
| 4184 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4185 | posix_spawnv(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4186 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4187 | PyObject *opath; |
| 4188 | char *path; |
| 4189 | PyObject *argv; |
| 4190 | char **argvlist; |
| 4191 | int mode, i; |
| 4192 | Py_ssize_t argc; |
| 4193 | Py_intptr_t spawnval; |
| 4194 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4195 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4196 | /* spawnv has three arguments: (mode, path, argv), where |
| 4197 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4198 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4199 | if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode, |
| 4200 | PyUnicode_FSConverter, |
| 4201 | &opath, &argv)) |
| 4202 | return NULL; |
| 4203 | path = PyBytes_AsString(opath); |
| 4204 | if (PyList_Check(argv)) { |
| 4205 | argc = PyList_Size(argv); |
| 4206 | getitem = PyList_GetItem; |
| 4207 | } |
| 4208 | else if (PyTuple_Check(argv)) { |
| 4209 | argc = PyTuple_Size(argv); |
| 4210 | getitem = PyTuple_GetItem; |
| 4211 | } |
| 4212 | else { |
| 4213 | PyErr_SetString(PyExc_TypeError, |
| 4214 | "spawnv() arg 2 must be a tuple or list"); |
| 4215 | Py_DECREF(opath); |
| 4216 | return NULL; |
| 4217 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4218 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4219 | argvlist = PyMem_NEW(char *, argc+1); |
| 4220 | if (argvlist == NULL) { |
| 4221 | Py_DECREF(opath); |
| 4222 | return PyErr_NoMemory(); |
| 4223 | } |
| 4224 | for (i = 0; i < argc; i++) { |
| 4225 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4226 | &argvlist[i])) { |
| 4227 | free_string_array(argvlist, i); |
| 4228 | PyErr_SetString( |
| 4229 | PyExc_TypeError, |
| 4230 | "spawnv() arg 2 must contain only strings"); |
| 4231 | Py_DECREF(opath); |
| 4232 | return NULL; |
| 4233 | } |
| 4234 | } |
| 4235 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4236 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4237 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4238 | Py_BEGIN_ALLOW_THREADS |
| 4239 | spawnval = spawnv(mode, path, argvlist); |
| 4240 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4241 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4242 | if (mode == _OLD_P_OVERLAY) |
| 4243 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4244 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4245 | Py_BEGIN_ALLOW_THREADS |
| 4246 | spawnval = _spawnv(mode, path, argvlist); |
| 4247 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4248 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4249 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | free_string_array(argvlist, argc); |
| 4251 | Py_DECREF(opath); |
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 | if (spawnval == -1) |
| 4254 | return posix_error(); |
| 4255 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4256 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4257 | return Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4258 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4259 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4260 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4264 | PyDoc_STRVAR(posix_spawnve__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4265 | "spawnve(mode, path, args, env)\n\n\ |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4266 | Execute the program 'path' in a new process.\n\ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4267 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4268 | mode: mode of process creation\n\ |
| 4269 | path: path of executable file\n\ |
| 4270 | args: tuple or list of arguments\n\ |
| 4271 | env: dictionary of strings mapping to strings"); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4272 | |
| 4273 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 4274 | posix_spawnve(PyObject *self, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4275 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4276 | PyObject *opath; |
| 4277 | char *path; |
| 4278 | PyObject *argv, *env; |
| 4279 | char **argvlist; |
| 4280 | char **envlist; |
| 4281 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4282 | int mode; |
| 4283 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4284 | Py_intptr_t spawnval; |
| 4285 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4286 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4287 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4288 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 4289 | argv is a list or tuple of strings and env is a dictionary |
| 4290 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4291 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4292 | if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode, |
| 4293 | PyUnicode_FSConverter, |
| 4294 | &opath, &argv, &env)) |
| 4295 | return NULL; |
| 4296 | path = PyBytes_AsString(opath); |
| 4297 | if (PyList_Check(argv)) { |
| 4298 | argc = PyList_Size(argv); |
| 4299 | getitem = PyList_GetItem; |
| 4300 | } |
| 4301 | else if (PyTuple_Check(argv)) { |
| 4302 | argc = PyTuple_Size(argv); |
| 4303 | getitem = PyTuple_GetItem; |
| 4304 | } |
| 4305 | else { |
| 4306 | PyErr_SetString(PyExc_TypeError, |
| 4307 | "spawnve() arg 2 must be a tuple or list"); |
| 4308 | goto fail_0; |
| 4309 | } |
| 4310 | if (!PyMapping_Check(env)) { |
| 4311 | PyErr_SetString(PyExc_TypeError, |
| 4312 | "spawnve() arg 3 must be a mapping object"); |
| 4313 | goto fail_0; |
| 4314 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4316 | argvlist = PyMem_NEW(char *, argc+1); |
| 4317 | if (argvlist == NULL) { |
| 4318 | PyErr_NoMemory(); |
| 4319 | goto fail_0; |
| 4320 | } |
| 4321 | for (i = 0; i < argc; i++) { |
| 4322 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4323 | &argvlist[i])) |
| 4324 | { |
| 4325 | lastarg = i; |
| 4326 | goto fail_1; |
| 4327 | } |
| 4328 | } |
| 4329 | lastarg = argc; |
| 4330 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4331 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4332 | envlist = parse_envlist(env, &envc); |
| 4333 | if (envlist == NULL) |
| 4334 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4335 | |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4336 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4337 | Py_BEGIN_ALLOW_THREADS |
| 4338 | spawnval = spawnve(mode, path, argvlist, envlist); |
| 4339 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4340 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4341 | if (mode == _OLD_P_OVERLAY) |
| 4342 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4343 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4344 | Py_BEGIN_ALLOW_THREADS |
| 4345 | spawnval = _spawnve(mode, path, argvlist, envlist); |
| 4346 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 4347 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 4348 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4349 | if (spawnval == -1) |
| 4350 | (void) posix_error(); |
| 4351 | else |
Fredrik Lundh | e25cfd8 | 2000-07-09 13:10:40 +0000 | [diff] [blame] | 4352 | #if SIZEOF_LONG == SIZEOF_VOID_P |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4353 | res = Py_BuildValue("l", (long) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4354 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4355 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 4356 | #endif |
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 | while (--envc >= 0) |
| 4359 | PyMem_DEL(envlist[envc]); |
| 4360 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 4361 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4362 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 4363 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4364 | Py_DECREF(opath); |
| 4365 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4366 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4367 | |
| 4368 | /* OS/2 supports spawnvp & spawnvpe natively */ |
| 4369 | #if defined(PYOS_OS2) |
| 4370 | PyDoc_STRVAR(posix_spawnvp__doc__, |
| 4371 | "spawnvp(mode, file, args)\n\n\ |
| 4372 | Execute the program 'file' in a new process, using the environment\n\ |
| 4373 | search path to find the file.\n\ |
| 4374 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4375 | mode: mode of process creation\n\ |
| 4376 | file: executable file name\n\ |
| 4377 | args: tuple or list of strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4378 | |
| 4379 | static PyObject * |
| 4380 | posix_spawnvp(PyObject *self, PyObject *args) |
| 4381 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4382 | PyObject *opath; |
| 4383 | char *path; |
| 4384 | PyObject *argv; |
| 4385 | char **argvlist; |
| 4386 | int mode, i, argc; |
| 4387 | Py_intptr_t spawnval; |
| 4388 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4389 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4390 | /* spawnvp has three arguments: (mode, path, argv), where |
| 4391 | argv is a list or tuple of strings. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4393 | if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode, |
| 4394 | PyUnicode_FSConverter, |
| 4395 | &opath, &argv)) |
| 4396 | return NULL; |
| 4397 | path = PyBytes_AsString(opath); |
| 4398 | if (PyList_Check(argv)) { |
| 4399 | argc = PyList_Size(argv); |
| 4400 | getitem = PyList_GetItem; |
| 4401 | } |
| 4402 | else if (PyTuple_Check(argv)) { |
| 4403 | argc = PyTuple_Size(argv); |
| 4404 | getitem = PyTuple_GetItem; |
| 4405 | } |
| 4406 | else { |
| 4407 | PyErr_SetString(PyExc_TypeError, |
| 4408 | "spawnvp() arg 2 must be a tuple or list"); |
| 4409 | Py_DECREF(opath); |
| 4410 | return NULL; |
| 4411 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4412 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4413 | argvlist = PyMem_NEW(char *, argc+1); |
| 4414 | if (argvlist == NULL) { |
| 4415 | Py_DECREF(opath); |
| 4416 | return PyErr_NoMemory(); |
| 4417 | } |
| 4418 | for (i = 0; i < argc; i++) { |
| 4419 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4420 | &argvlist[i])) { |
| 4421 | free_string_array(argvlist, i); |
| 4422 | PyErr_SetString( |
| 4423 | PyExc_TypeError, |
| 4424 | "spawnvp() arg 2 must contain only strings"); |
| 4425 | Py_DECREF(opath); |
| 4426 | return NULL; |
| 4427 | } |
| 4428 | } |
| 4429 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4430 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4431 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4432 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4433 | spawnval = spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4434 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4435 | spawnval = _spawnvp(mode, path, argvlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4436 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4437 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4438 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4439 | free_string_array(argvlist, argc); |
| 4440 | Py_DECREF(opath); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4441 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4442 | if (spawnval == -1) |
| 4443 | return posix_error(); |
| 4444 | else |
| 4445 | return Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | |
| 4449 | PyDoc_STRVAR(posix_spawnvpe__doc__, |
| 4450 | "spawnvpe(mode, file, args, env)\n\n\ |
| 4451 | Execute the program 'file' in a new process, using the environment\n\ |
| 4452 | search path to find the file.\n\ |
| 4453 | \n\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4454 | mode: mode of process creation\n\ |
| 4455 | file: executable file name\n\ |
| 4456 | args: tuple or list of arguments\n\ |
| 4457 | env: dictionary of strings mapping to strings"); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4458 | |
| 4459 | static PyObject * |
| 4460 | posix_spawnvpe(PyObject *self, PyObject *args) |
| 4461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4462 | PyObject *opath |
| 4463 | char *path; |
| 4464 | PyObject *argv, *env; |
| 4465 | char **argvlist; |
| 4466 | char **envlist; |
| 4467 | PyObject *res=NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 4468 | int mode; |
| 4469 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4470 | Py_intptr_t spawnval; |
| 4471 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 4472 | int lastarg = 0; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4473 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4474 | /* spawnvpe has four arguments: (mode, path, argv, env), where |
| 4475 | argv is a list or tuple of strings and env is a dictionary |
| 4476 | like posix.environ. */ |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4477 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4478 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, |
| 4479 | PyUnicode_FSConverter, |
| 4480 | &opath, &argv, &env)) |
| 4481 | return NULL; |
| 4482 | path = PyBytes_AsString(opath); |
| 4483 | if (PyList_Check(argv)) { |
| 4484 | argc = PyList_Size(argv); |
| 4485 | getitem = PyList_GetItem; |
| 4486 | } |
| 4487 | else if (PyTuple_Check(argv)) { |
| 4488 | argc = PyTuple_Size(argv); |
| 4489 | getitem = PyTuple_GetItem; |
| 4490 | } |
| 4491 | else { |
| 4492 | PyErr_SetString(PyExc_TypeError, |
| 4493 | "spawnvpe() arg 2 must be a tuple or list"); |
| 4494 | goto fail_0; |
| 4495 | } |
| 4496 | if (!PyMapping_Check(env)) { |
| 4497 | PyErr_SetString(PyExc_TypeError, |
| 4498 | "spawnvpe() arg 3 must be a mapping object"); |
| 4499 | goto fail_0; |
| 4500 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4502 | argvlist = PyMem_NEW(char *, argc+1); |
| 4503 | if (argvlist == NULL) { |
| 4504 | PyErr_NoMemory(); |
| 4505 | goto fail_0; |
| 4506 | } |
| 4507 | for (i = 0; i < argc; i++) { |
| 4508 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 4509 | &argvlist[i])) |
| 4510 | { |
| 4511 | lastarg = i; |
| 4512 | goto fail_1; |
| 4513 | } |
| 4514 | } |
| 4515 | lastarg = argc; |
| 4516 | argvlist[argc] = NULL; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4518 | envlist = parse_envlist(env, &envc); |
| 4519 | if (envlist == NULL) |
| 4520 | goto fail_1; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4521 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4522 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4523 | #if defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4524 | spawnval = spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4525 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4526 | spawnval = _spawnvpe(mode, path, argvlist, envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4527 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4528 | Py_END_ALLOW_THREADS |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4529 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4530 | if (spawnval == -1) |
| 4531 | (void) posix_error(); |
| 4532 | else |
| 4533 | res = Py_BuildValue("l", (long) spawnval); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4534 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4535 | while (--envc >= 0) |
| 4536 | PyMem_DEL(envlist[envc]); |
| 4537 | PyMem_DEL(envlist); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4538 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4539 | free_string_array(argvlist, lastarg); |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4540 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4541 | Py_DECREF(opath); |
| 4542 | return res; |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 4543 | } |
| 4544 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 4545 | #endif /* HAVE_SPAWNV */ |
| 4546 | |
| 4547 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4548 | #ifdef HAVE_FORK1 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4549 | PyDoc_STRVAR(posix_fork1__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4550 | "fork1() -> pid\n\n\ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4551 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\ |
| 4552 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4553 | 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] | 4554 | |
| 4555 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4556 | posix_fork1(PyObject *self, PyObject *noargs) |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4557 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4558 | pid_t pid; |
| 4559 | int result = 0; |
| 4560 | _PyImport_AcquireLock(); |
| 4561 | pid = fork1(); |
| 4562 | if (pid == 0) { |
| 4563 | /* child: this clobbers and resets the import lock. */ |
| 4564 | PyOS_AfterFork(); |
| 4565 | } else { |
| 4566 | /* parent: release the import lock. */ |
| 4567 | result = _PyImport_ReleaseLock(); |
| 4568 | } |
| 4569 | if (pid == -1) |
| 4570 | return posix_error(); |
| 4571 | if (result < 0) { |
| 4572 | /* Don't clobber the OSError if the fork failed. */ |
| 4573 | PyErr_SetString(PyExc_RuntimeError, |
| 4574 | "not holding the import lock"); |
| 4575 | return NULL; |
| 4576 | } |
| 4577 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 4578 | } |
| 4579 | #endif |
| 4580 | |
| 4581 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4582 | #ifdef HAVE_FORK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4583 | PyDoc_STRVAR(posix_fork__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 4584 | "fork() -> pid\n\n\ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4585 | Fork a child process.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4586 | 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] | 4587 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4588 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4589 | posix_fork(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4590 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4591 | pid_t pid; |
| 4592 | int result = 0; |
| 4593 | _PyImport_AcquireLock(); |
| 4594 | pid = fork(); |
| 4595 | if (pid == 0) { |
| 4596 | /* child: this clobbers and resets the import lock. */ |
| 4597 | PyOS_AfterFork(); |
| 4598 | } else { |
| 4599 | /* parent: release the import lock. */ |
| 4600 | result = _PyImport_ReleaseLock(); |
| 4601 | } |
| 4602 | if (pid == -1) |
| 4603 | return posix_error(); |
| 4604 | if (result < 0) { |
| 4605 | /* Don't clobber the OSError if the fork failed. */ |
| 4606 | PyErr_SetString(PyExc_RuntimeError, |
| 4607 | "not holding the import lock"); |
| 4608 | return NULL; |
| 4609 | } |
| 4610 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4611 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 4612 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4613 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4614 | #ifdef HAVE_SCHED_H |
| 4615 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4616 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
| 4617 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4618 | PyDoc_STRVAR(posix_sched_get_priority_max__doc__, |
| 4619 | "sched_get_priority_max(policy)\n\n\ |
| 4620 | Get the maximum scheduling priority for *policy*."); |
| 4621 | |
| 4622 | static PyObject * |
| 4623 | posix_sched_get_priority_max(PyObject *self, PyObject *args) |
| 4624 | { |
| 4625 | int policy, max; |
| 4626 | |
| 4627 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy)) |
| 4628 | return NULL; |
| 4629 | max = sched_get_priority_max(policy); |
| 4630 | if (max < 0) |
| 4631 | return posix_error(); |
| 4632 | return PyLong_FromLong(max); |
| 4633 | } |
| 4634 | |
| 4635 | PyDoc_STRVAR(posix_sched_get_priority_min__doc__, |
| 4636 | "sched_get_priority_min(policy)\n\n\ |
| 4637 | Get the minimum scheduling priority for *policy*."); |
| 4638 | |
| 4639 | static PyObject * |
| 4640 | posix_sched_get_priority_min(PyObject *self, PyObject *args) |
| 4641 | { |
| 4642 | int policy, min; |
| 4643 | |
| 4644 | if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy)) |
| 4645 | return NULL; |
| 4646 | min = sched_get_priority_min(policy); |
| 4647 | if (min < 0) |
| 4648 | return posix_error(); |
| 4649 | return PyLong_FromLong(min); |
| 4650 | } |
| 4651 | |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 4652 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 4653 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4654 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4655 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4656 | PyDoc_STRVAR(posix_sched_getscheduler__doc__, |
| 4657 | "sched_getscheduler(pid)\n\n\ |
| 4658 | Get the scheduling policy for the process with a PID of *pid*.\n\ |
| 4659 | Passing a PID of 0 returns the scheduling policy for the calling process."); |
| 4660 | |
| 4661 | static PyObject * |
| 4662 | posix_sched_getscheduler(PyObject *self, PyObject *args) |
| 4663 | { |
| 4664 | pid_t pid; |
| 4665 | int policy; |
| 4666 | |
| 4667 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid)) |
| 4668 | return NULL; |
| 4669 | policy = sched_getscheduler(pid); |
| 4670 | if (policy < 0) |
| 4671 | return posix_error(); |
| 4672 | return PyLong_FromLong(policy); |
| 4673 | } |
| 4674 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4675 | #endif |
| 4676 | |
| 4677 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
| 4678 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4679 | static PyObject * |
| 4680 | sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4681 | { |
| 4682 | PyObject *res, *priority; |
Benjamin Peterson | 4e36d5a | 2011-08-02 19:56:11 -0500 | [diff] [blame] | 4683 | static char *kwlist[] = {"sched_priority", NULL}; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4684 | |
| 4685 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority)) |
| 4686 | return NULL; |
| 4687 | res = PyStructSequence_New(type); |
| 4688 | if (!res) |
| 4689 | return NULL; |
| 4690 | Py_INCREF(priority); |
| 4691 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4692 | return res; |
| 4693 | } |
| 4694 | |
| 4695 | PyDoc_STRVAR(sched_param__doc__, |
| 4696 | "sched_param(sched_priority): A scheduling parameter.\n\n\ |
| 4697 | Current has only one field: sched_priority"); |
| 4698 | |
| 4699 | static PyStructSequence_Field sched_param_fields[] = { |
| 4700 | {"sched_priority", "the scheduling priority"}, |
| 4701 | {0} |
| 4702 | }; |
| 4703 | |
| 4704 | static PyStructSequence_Desc sched_param_desc = { |
| 4705 | "sched_param", /* name */ |
| 4706 | sched_param__doc__, /* doc */ |
| 4707 | sched_param_fields, |
| 4708 | 1 |
| 4709 | }; |
| 4710 | |
| 4711 | static int |
| 4712 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 4713 | { |
| 4714 | long priority; |
| 4715 | |
| 4716 | if (Py_TYPE(param) != &SchedParamType) { |
| 4717 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 4718 | return 0; |
| 4719 | } |
| 4720 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 4721 | if (priority == -1 && PyErr_Occurred()) |
| 4722 | return 0; |
| 4723 | if (priority > INT_MAX || priority < INT_MIN) { |
| 4724 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 4725 | return 0; |
| 4726 | } |
| 4727 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 4728 | return 1; |
| 4729 | } |
| 4730 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4731 | #endif |
| 4732 | |
| 4733 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 4734 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4735 | PyDoc_STRVAR(posix_sched_setscheduler__doc__, |
| 4736 | "sched_setscheduler(pid, policy, param)\n\n\ |
| 4737 | Set the scheduling policy, *policy*, for *pid*.\n\ |
| 4738 | If *pid* is 0, the calling process is changed.\n\ |
| 4739 | *param* is an instance of sched_param."); |
| 4740 | |
| 4741 | static PyObject * |
| 4742 | posix_sched_setscheduler(PyObject *self, PyObject *args) |
| 4743 | { |
| 4744 | pid_t pid; |
| 4745 | int policy; |
| 4746 | struct sched_param param; |
| 4747 | |
| 4748 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler", |
| 4749 | &pid, &policy, &convert_sched_param, ¶m)) |
| 4750 | return NULL; |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4751 | |
| 4752 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 4753 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 4754 | ** scheduling policy under Solaris/Illumos, and others. |
| 4755 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 4756 | */ |
| 4757 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4758 | return posix_error(); |
| 4759 | Py_RETURN_NONE; |
| 4760 | } |
| 4761 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4762 | #endif |
| 4763 | |
| 4764 | #ifdef HAVE_SCHED_SETPARAM |
| 4765 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4766 | PyDoc_STRVAR(posix_sched_getparam__doc__, |
| 4767 | "sched_getparam(pid) -> sched_param\n\n\ |
| 4768 | Returns scheduling parameters for the process with *pid* as an instance of the\n\ |
| 4769 | sched_param class. A PID of 0 means the calling process."); |
| 4770 | |
| 4771 | static PyObject * |
| 4772 | posix_sched_getparam(PyObject *self, PyObject *args) |
| 4773 | { |
| 4774 | pid_t pid; |
| 4775 | struct sched_param param; |
| 4776 | PyObject *res, *priority; |
| 4777 | |
| 4778 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid)) |
| 4779 | return NULL; |
| 4780 | if (sched_getparam(pid, ¶m)) |
| 4781 | return posix_error(); |
| 4782 | res = PyStructSequence_New(&SchedParamType); |
| 4783 | if (!res) |
| 4784 | return NULL; |
| 4785 | priority = PyLong_FromLong(param.sched_priority); |
| 4786 | if (!priority) { |
| 4787 | Py_DECREF(res); |
| 4788 | return NULL; |
| 4789 | } |
| 4790 | PyStructSequence_SET_ITEM(res, 0, priority); |
| 4791 | return res; |
| 4792 | } |
| 4793 | |
| 4794 | PyDoc_STRVAR(posix_sched_setparam__doc__, |
| 4795 | "sched_setparam(pid, param)\n\n\ |
| 4796 | Set scheduling parameters for a process with PID *pid*.\n\ |
| 4797 | A PID of 0 means the calling process."); |
| 4798 | |
| 4799 | static PyObject * |
| 4800 | posix_sched_setparam(PyObject *self, PyObject *args) |
| 4801 | { |
| 4802 | pid_t pid; |
| 4803 | struct sched_param param; |
| 4804 | |
| 4805 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam", |
| 4806 | &pid, &convert_sched_param, ¶m)) |
| 4807 | return NULL; |
| 4808 | if (sched_setparam(pid, ¶m)) |
| 4809 | return posix_error(); |
| 4810 | Py_RETURN_NONE; |
| 4811 | } |
| 4812 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4813 | #endif |
| 4814 | |
| 4815 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
| 4816 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4817 | PyDoc_STRVAR(posix_sched_rr_get_interval__doc__, |
| 4818 | "sched_rr_get_interval(pid) -> float\n\n\ |
| 4819 | Return the round-robin quantum for the process with PID *pid* in seconds."); |
| 4820 | |
| 4821 | static PyObject * |
| 4822 | posix_sched_rr_get_interval(PyObject *self, PyObject *args) |
| 4823 | { |
| 4824 | pid_t pid; |
| 4825 | struct timespec interval; |
| 4826 | |
| 4827 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid)) |
| 4828 | return NULL; |
| 4829 | if (sched_rr_get_interval(pid, &interval)) |
| 4830 | return posix_error(); |
| 4831 | return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec); |
| 4832 | } |
| 4833 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 4834 | #endif |
| 4835 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4836 | PyDoc_STRVAR(posix_sched_yield__doc__, |
| 4837 | "sched_yield()\n\n\ |
| 4838 | Voluntarily relinquish the CPU."); |
| 4839 | |
| 4840 | static PyObject * |
| 4841 | posix_sched_yield(PyObject *self, PyObject *noargs) |
| 4842 | { |
| 4843 | if (sched_yield()) |
| 4844 | return posix_error(); |
| 4845 | Py_RETURN_NONE; |
| 4846 | } |
| 4847 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 4848 | #ifdef HAVE_SCHED_SETAFFINITY |
| 4849 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 4850 | typedef struct { |
| 4851 | PyObject_HEAD; |
| 4852 | Py_ssize_t size; |
| 4853 | int ncpus; |
| 4854 | cpu_set_t *set; |
| 4855 | } Py_cpu_set; |
| 4856 | |
| 4857 | static PyTypeObject cpu_set_type; |
| 4858 | |
| 4859 | static void |
| 4860 | cpu_set_dealloc(Py_cpu_set *set) |
| 4861 | { |
| 4862 | assert(set->set); |
| 4863 | CPU_FREE(set->set); |
| 4864 | Py_TYPE(set)->tp_free(set); |
| 4865 | } |
| 4866 | |
| 4867 | static Py_cpu_set * |
| 4868 | make_new_cpu_set(PyTypeObject *type, Py_ssize_t size) |
| 4869 | { |
| 4870 | Py_cpu_set *set; |
| 4871 | |
| 4872 | if (size < 0) { |
| 4873 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | set = (Py_cpu_set *)type->tp_alloc(type, 0); |
| 4877 | if (!set) |
| 4878 | return NULL; |
| 4879 | set->ncpus = size; |
| 4880 | set->size = CPU_ALLOC_SIZE(size); |
| 4881 | set->set = CPU_ALLOC(size); |
| 4882 | if (!set->set) { |
| 4883 | type->tp_free(set); |
| 4884 | PyErr_NoMemory(); |
| 4885 | return NULL; |
| 4886 | } |
| 4887 | CPU_ZERO_S(set->size, set->set); |
| 4888 | return set; |
| 4889 | } |
| 4890 | |
| 4891 | static PyObject * |
| 4892 | cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4893 | { |
| 4894 | int size; |
| 4895 | |
| 4896 | if (!_PyArg_NoKeywords("cpu_set()", kwargs) || |
| 4897 | !PyArg_ParseTuple(args, "i:cpu_set", &size)) |
| 4898 | return NULL; |
| 4899 | return (PyObject *)make_new_cpu_set(type, size); |
| 4900 | } |
| 4901 | |
| 4902 | static PyObject * |
| 4903 | cpu_set_repr(Py_cpu_set *set) |
| 4904 | { |
| 4905 | return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus); |
| 4906 | } |
| 4907 | |
| 4908 | static Py_ssize_t |
| 4909 | cpu_set_len(Py_cpu_set *set) |
| 4910 | { |
| 4911 | return set->ncpus; |
| 4912 | } |
| 4913 | |
| 4914 | static int |
| 4915 | _get_cpu(Py_cpu_set *set, const char *requester, PyObject *args) |
| 4916 | { |
| 4917 | int cpu; |
| 4918 | if (!PyArg_ParseTuple(args, requester, &cpu)) |
| 4919 | return -1; |
| 4920 | if (cpu < 0) { |
| 4921 | PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid"); |
| 4922 | return -1; |
| 4923 | } |
| 4924 | if (cpu >= set->ncpus) { |
| 4925 | PyErr_SetString(PyExc_ValueError, "cpu too large for set"); |
| 4926 | return -1; |
| 4927 | } |
| 4928 | return cpu; |
| 4929 | } |
| 4930 | |
| 4931 | PyDoc_STRVAR(cpu_set_set_doc, |
| 4932 | "cpu_set.set(i)\n\n\ |
| 4933 | Add CPU *i* to the set."); |
| 4934 | |
| 4935 | static PyObject * |
| 4936 | cpu_set_set(Py_cpu_set *set, PyObject *args) |
| 4937 | { |
| 4938 | int cpu = _get_cpu(set, "i|set", args); |
| 4939 | if (cpu == -1) |
| 4940 | return NULL; |
| 4941 | CPU_SET_S(cpu, set->size, set->set); |
| 4942 | Py_RETURN_NONE; |
| 4943 | } |
| 4944 | |
| 4945 | PyDoc_STRVAR(cpu_set_count_doc, |
| 4946 | "cpu_set.count() -> int\n\n\ |
| 4947 | Return the number of CPUs active in the set."); |
| 4948 | |
| 4949 | static PyObject * |
| 4950 | cpu_set_count(Py_cpu_set *set, PyObject *noargs) |
| 4951 | { |
| 4952 | return PyLong_FromLong(CPU_COUNT_S(set->size, set->set)); |
| 4953 | } |
| 4954 | |
| 4955 | PyDoc_STRVAR(cpu_set_clear_doc, |
| 4956 | "cpu_set.clear(i)\n\n\ |
| 4957 | Remove CPU *i* from the set."); |
| 4958 | |
| 4959 | static PyObject * |
| 4960 | cpu_set_clear(Py_cpu_set *set, PyObject *args) |
| 4961 | { |
| 4962 | int cpu = _get_cpu(set, "i|clear", args); |
| 4963 | if (cpu == -1) |
| 4964 | return NULL; |
| 4965 | CPU_CLR_S(cpu, set->size, set->set); |
| 4966 | Py_RETURN_NONE; |
| 4967 | } |
| 4968 | |
| 4969 | PyDoc_STRVAR(cpu_set_isset_doc, |
| 4970 | "cpu_set.isset(i) -> bool\n\n\ |
| 4971 | Test if CPU *i* is in the set."); |
| 4972 | |
| 4973 | static PyObject * |
| 4974 | cpu_set_isset(Py_cpu_set *set, PyObject *args) |
| 4975 | { |
| 4976 | int cpu = _get_cpu(set, "i|isset", args); |
| 4977 | if (cpu == -1) |
| 4978 | return NULL; |
| 4979 | if (CPU_ISSET_S(cpu, set->size, set->set)) |
| 4980 | Py_RETURN_TRUE; |
| 4981 | Py_RETURN_FALSE; |
| 4982 | } |
| 4983 | |
| 4984 | PyDoc_STRVAR(cpu_set_zero_doc, |
| 4985 | "cpu_set.zero()\n\n\ |
| 4986 | Clear the cpu_set."); |
| 4987 | |
| 4988 | static PyObject * |
| 4989 | cpu_set_zero(Py_cpu_set *set, PyObject *noargs) |
| 4990 | { |
| 4991 | CPU_ZERO_S(set->size, set->set); |
| 4992 | Py_RETURN_NONE; |
| 4993 | } |
| 4994 | |
| 4995 | static PyObject * |
| 4996 | cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op) |
| 4997 | { |
| 4998 | int eq; |
| 4999 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5000 | if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) |
| 5001 | Py_RETURN_NOTIMPLEMENTED; |
| 5002 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5003 | eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set); |
| 5004 | if ((op == Py_EQ) ? eq : !eq) |
| 5005 | Py_RETURN_TRUE; |
| 5006 | else |
| 5007 | Py_RETURN_FALSE; |
| 5008 | } |
| 5009 | |
| 5010 | #define CPU_SET_BINOP(name, op) \ |
| 5011 | static PyObject * \ |
| 5012 | do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \ |
| 5013 | if (res) { \ |
| 5014 | Py_INCREF(res); \ |
| 5015 | } \ |
| 5016 | else { \ |
Benjamin Peterson | e870fe6 | 2011-08-02 17:44:26 -0500 | [diff] [blame] | 5017 | res = make_new_cpu_set(&cpu_set_type, left->ncpus); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5018 | if (!res) \ |
| 5019 | return NULL; \ |
| 5020 | } \ |
Benjamin Peterson | 9b374bf | 2011-08-02 18:22:30 -0500 | [diff] [blame] | 5021 | if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5022 | Py_DECREF(res); \ |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5023 | Py_RETURN_NOTIMPLEMENTED; \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5024 | } \ |
Benjamin Peterson | 8f7bdd3 | 2011-08-02 18:34:30 -0500 | [diff] [blame] | 5025 | assert(left->size == right->size && right->size == res->size); \ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5026 | op(res->size, res->set, left->set, right->set); \ |
| 5027 | return (PyObject *)res; \ |
| 5028 | } \ |
| 5029 | static PyObject * \ |
| 5030 | cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5031 | return do_cpu_set_##name(left, right, NULL); \ |
| 5032 | } \ |
| 5033 | static PyObject * \ |
| 5034 | cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \ |
| 5035 | return do_cpu_set_##name(left, right, left); \ |
| 5036 | } \ |
| 5037 | |
| 5038 | CPU_SET_BINOP(and, CPU_AND_S) |
| 5039 | CPU_SET_BINOP(or, CPU_OR_S) |
| 5040 | CPU_SET_BINOP(xor, CPU_XOR_S) |
| 5041 | #undef CPU_SET_BINOP |
| 5042 | |
| 5043 | PyDoc_STRVAR(cpu_set_doc, |
| 5044 | "cpu_set(size)\n\n\ |
| 5045 | Create an empty mask of CPUs."); |
| 5046 | |
| 5047 | static PyNumberMethods cpu_set_as_number = { |
| 5048 | 0, /*nb_add*/ |
| 5049 | 0, /*nb_subtract*/ |
| 5050 | 0, /*nb_multiply*/ |
| 5051 | 0, /*nb_remainder*/ |
| 5052 | 0, /*nb_divmod*/ |
| 5053 | 0, /*nb_power*/ |
| 5054 | 0, /*nb_negative*/ |
| 5055 | 0, /*nb_positive*/ |
| 5056 | 0, /*nb_absolute*/ |
| 5057 | 0, /*nb_bool*/ |
| 5058 | 0, /*nb_invert*/ |
| 5059 | 0, /*nb_lshift*/ |
| 5060 | 0, /*nb_rshift*/ |
| 5061 | (binaryfunc)cpu_set_and, /*nb_and*/ |
| 5062 | (binaryfunc)cpu_set_xor, /*nb_xor*/ |
| 5063 | (binaryfunc)cpu_set_or, /*nb_or*/ |
| 5064 | 0, /*nb_int*/ |
| 5065 | 0, /*nb_reserved*/ |
| 5066 | 0, /*nb_float*/ |
| 5067 | 0, /*nb_inplace_add*/ |
| 5068 | 0, /*nb_inplace_subtract*/ |
| 5069 | 0, /*nb_inplace_multiply*/ |
| 5070 | 0, /*nb_inplace_remainder*/ |
| 5071 | 0, /*nb_inplace_power*/ |
| 5072 | 0, /*nb_inplace_lshift*/ |
| 5073 | 0, /*nb_inplace_rshift*/ |
| 5074 | (binaryfunc)cpu_set_iand, /*nb_inplace_and*/ |
| 5075 | (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/ |
| 5076 | (binaryfunc)cpu_set_ior, /*nb_inplace_or*/ |
| 5077 | }; |
| 5078 | |
| 5079 | static PySequenceMethods cpu_set_as_sequence = { |
| 5080 | (lenfunc)cpu_set_len, /* sq_length */ |
| 5081 | }; |
| 5082 | |
| 5083 | static PyMethodDef cpu_set_methods[] = { |
| 5084 | {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc}, |
| 5085 | {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc}, |
| 5086 | {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc}, |
| 5087 | {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc}, |
| 5088 | {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc}, |
| 5089 | {NULL, NULL} /* sentinel */ |
| 5090 | }; |
| 5091 | |
| 5092 | static PyTypeObject cpu_set_type = { |
| 5093 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 5094 | "posix.cpu_set", /* tp_name */ |
| 5095 | sizeof(Py_cpu_set), /* tp_basicsize */ |
| 5096 | 0, /* tp_itemsize */ |
| 5097 | /* methods */ |
| 5098 | (destructor)cpu_set_dealloc, /* tp_dealloc */ |
| 5099 | 0, /* tp_print */ |
| 5100 | 0, /* tp_getattr */ |
| 5101 | 0, /* tp_setattr */ |
| 5102 | 0, /* tp_reserved */ |
| 5103 | (reprfunc)cpu_set_repr, /* tp_repr */ |
| 5104 | &cpu_set_as_number, /* tp_as_number */ |
| 5105 | &cpu_set_as_sequence, /* tp_as_sequence */ |
| 5106 | 0, /* tp_as_mapping */ |
| 5107 | PyObject_HashNotImplemented, /* tp_hash */ |
| 5108 | 0, /* tp_call */ |
| 5109 | 0, /* tp_str */ |
| 5110 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5111 | 0, /* tp_setattro */ |
| 5112 | 0, /* tp_as_buffer */ |
| 5113 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 5114 | cpu_set_doc, /* tp_doc */ |
| 5115 | 0, /* tp_traverse */ |
| 5116 | 0, /* tp_clear */ |
| 5117 | (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */ |
| 5118 | 0, /* tp_weaklistoffset */ |
| 5119 | 0, /* tp_iter */ |
| 5120 | 0, /* tp_iternext */ |
| 5121 | cpu_set_methods, /* tp_methods */ |
| 5122 | 0, /* tp_members */ |
| 5123 | 0, /* tp_getset */ |
| 5124 | 0, /* tp_base */ |
| 5125 | 0, /* tp_dict */ |
| 5126 | 0, /* tp_descr_get */ |
| 5127 | 0, /* tp_descr_set */ |
| 5128 | 0, /* tp_dictoffset */ |
| 5129 | 0, /* tp_init */ |
| 5130 | PyType_GenericAlloc, /* tp_alloc */ |
| 5131 | cpu_set_new, /* tp_new */ |
| 5132 | PyObject_Del, /* tp_free */ |
| 5133 | }; |
| 5134 | |
| 5135 | PyDoc_STRVAR(posix_sched_setaffinity__doc__, |
| 5136 | "sched_setaffinity(pid, cpu_set)\n\n\ |
| 5137 | Set the affinity of the process with PID *pid* to *cpu_set*."); |
| 5138 | |
| 5139 | static PyObject * |
| 5140 | posix_sched_setaffinity(PyObject *self, PyObject *args) |
| 5141 | { |
| 5142 | pid_t pid; |
| 5143 | Py_cpu_set *cpu_set; |
| 5144 | |
Benjamin Peterson | a17a5d6 | 2011-08-09 16:49:13 -0500 | [diff] [blame] | 5145 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5146 | &pid, &cpu_set_type, &cpu_set)) |
| 5147 | return NULL; |
| 5148 | if (sched_setaffinity(pid, cpu_set->size, cpu_set->set)) |
| 5149 | return posix_error(); |
| 5150 | Py_RETURN_NONE; |
| 5151 | } |
| 5152 | |
| 5153 | PyDoc_STRVAR(posix_sched_getaffinity__doc__, |
| 5154 | "sched_getaffinity(pid, ncpus) -> cpu_set\n\n\ |
| 5155 | Return the affinity of the process with PID *pid*.\n\ |
| 5156 | The returned cpu_set will be of size *ncpus*."); |
| 5157 | |
| 5158 | static PyObject * |
| 5159 | posix_sched_getaffinity(PyObject *self, PyObject *args) |
| 5160 | { |
| 5161 | pid_t pid; |
| 5162 | int ncpus; |
| 5163 | Py_cpu_set *res; |
| 5164 | |
Benjamin Peterson | 7ac9214 | 2011-08-03 08:54:26 -0500 | [diff] [blame] | 5165 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity", |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5166 | &pid, &ncpus)) |
| 5167 | return NULL; |
| 5168 | res = make_new_cpu_set(&cpu_set_type, ncpus); |
| 5169 | if (!res) |
| 5170 | return NULL; |
| 5171 | if (sched_getaffinity(pid, res->size, res->set)) { |
| 5172 | Py_DECREF(res); |
| 5173 | return posix_error(); |
| 5174 | } |
| 5175 | return (PyObject *)res; |
| 5176 | } |
| 5177 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 5178 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 5179 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 5180 | #endif /* HAVE_SCHED_H */ |
| 5181 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5182 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 5183 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 5184 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5185 | #define DEV_PTY_FILE "/dev/ptc" |
| 5186 | #define HAVE_DEV_PTMX |
| 5187 | #else |
| 5188 | #define DEV_PTY_FILE "/dev/ptmx" |
| 5189 | #endif |
| 5190 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5191 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5192 | #ifdef HAVE_PTY_H |
| 5193 | #include <pty.h> |
| 5194 | #else |
| 5195 | #ifdef HAVE_LIBUTIL_H |
| 5196 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 5197 | #else |
| 5198 | #ifdef HAVE_UTIL_H |
| 5199 | #include <util.h> |
| 5200 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5201 | #endif /* HAVE_LIBUTIL_H */ |
| 5202 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 5203 | #ifdef HAVE_STROPTS_H |
| 5204 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5205 | #endif |
| 5206 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5207 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5208 | #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] | 5209 | PyDoc_STRVAR(posix_openpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5210 | "openpty() -> (master_fd, slave_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5211 | 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] | 5212 | |
| 5213 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5214 | posix_openpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5215 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5216 | int master_fd, slave_fd; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5217 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5218 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5219 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5220 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5222 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5223 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5224 | #endif |
| 5225 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5226 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5227 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5228 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
| 5229 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5230 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5231 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 5232 | if (slave_name == NULL) |
| 5233 | return posix_error(); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5234 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5235 | slave_fd = open(slave_name, O_RDWR); |
| 5236 | if (slave_fd < 0) |
| 5237 | return posix_error(); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5238 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5239 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
| 5240 | if (master_fd < 0) |
| 5241 | return posix_error(); |
| 5242 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
| 5243 | /* change permission of slave */ |
| 5244 | if (grantpt(master_fd) < 0) { |
| 5245 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5246 | return posix_error(); |
| 5247 | } |
| 5248 | /* unlock slave */ |
| 5249 | if (unlockpt(master_fd) < 0) { |
| 5250 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5251 | return posix_error(); |
| 5252 | } |
| 5253 | PyOS_setsig(SIGCHLD, sig_saved); |
| 5254 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 5255 | if (slave_name == NULL) |
| 5256 | return posix_error(); |
| 5257 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
| 5258 | if (slave_fd < 0) |
| 5259 | return posix_error(); |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 5260 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5261 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 5262 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5263 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5264 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 5265 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5266 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 5267 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5268 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5269 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 5270 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5271 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 5272 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5273 | |
| 5274 | #ifdef HAVE_FORKPTY |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5275 | PyDoc_STRVAR(posix_forkpty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5276 | "forkpty() -> (pid, master_fd)\n\n\ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5277 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\ |
| 5278 | 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] | 5279 | To both, return fd of newly opened pseudo-terminal.\n"); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5280 | |
| 5281 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5282 | posix_forkpty(PyObject *self, PyObject *noargs) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5283 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5284 | int master_fd = -1, result = 0; |
| 5285 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5286 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5287 | _PyImport_AcquireLock(); |
| 5288 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 5289 | if (pid == 0) { |
| 5290 | /* child: this clobbers and resets the import lock. */ |
| 5291 | PyOS_AfterFork(); |
| 5292 | } else { |
| 5293 | /* parent: release the import lock. */ |
| 5294 | result = _PyImport_ReleaseLock(); |
| 5295 | } |
| 5296 | if (pid == -1) |
| 5297 | return posix_error(); |
| 5298 | if (result < 0) { |
| 5299 | /* Don't clobber the OSError if the fork failed. */ |
| 5300 | PyErr_SetString(PyExc_RuntimeError, |
| 5301 | "not holding the import lock"); |
| 5302 | return NULL; |
| 5303 | } |
| 5304 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 5305 | } |
| 5306 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5307 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5308 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5309 | #ifdef HAVE_GETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5310 | PyDoc_STRVAR(posix_getegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5311 | "getegid() -> egid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5312 | Return the current process's effective group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5313 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5314 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5315 | posix_getegid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5316 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5317 | return PyLong_FromLong((long)getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5318 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5319 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5320 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5321 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5322 | #ifdef HAVE_GETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5323 | PyDoc_STRVAR(posix_geteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5324 | "geteuid() -> euid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5325 | Return the current process's effective user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5326 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5327 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5328 | posix_geteuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5329 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5330 | return PyLong_FromLong((long)geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5331 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5332 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5334 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5335 | #ifdef HAVE_GETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5336 | PyDoc_STRVAR(posix_getgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5337 | "getgid() -> gid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5338 | Return the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5339 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5340 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5341 | posix_getgid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5342 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5343 | return PyLong_FromLong((long)getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5344 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5345 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5346 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5348 | PyDoc_STRVAR(posix_getpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5349 | "getpid() -> pid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5350 | Return the current process id"); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5351 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5352 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5353 | posix_getpid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5354 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5355 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 5358 | #ifdef HAVE_GETGROUPLIST |
| 5359 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 5360 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 5361 | Returns a list of groups to which a user belongs.\n\n\ |
| 5362 | user: username to lookup\n\ |
| 5363 | group: base group id of the user"); |
| 5364 | |
| 5365 | static PyObject * |
| 5366 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 5367 | { |
| 5368 | #ifdef NGROUPS_MAX |
| 5369 | #define MAX_GROUPS NGROUPS_MAX |
| 5370 | #else |
| 5371 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 5372 | #define MAX_GROUPS 64 |
| 5373 | #endif |
| 5374 | |
| 5375 | const char *user; |
| 5376 | int i, ngroups; |
| 5377 | PyObject *list; |
| 5378 | #ifdef __APPLE__ |
| 5379 | int *groups, basegid; |
| 5380 | #else |
| 5381 | gid_t *groups, basegid; |
| 5382 | #endif |
| 5383 | ngroups = MAX_GROUPS; |
| 5384 | |
| 5385 | if (!PyArg_ParseTuple(args, "si", &user, &basegid)) |
| 5386 | return NULL; |
| 5387 | |
| 5388 | #ifdef __APPLE__ |
| 5389 | groups = PyMem_Malloc(ngroups * sizeof(int)); |
| 5390 | #else |
| 5391 | groups = PyMem_Malloc(ngroups * sizeof(gid_t)); |
| 5392 | #endif |
| 5393 | if (groups == NULL) |
| 5394 | return PyErr_NoMemory(); |
| 5395 | |
| 5396 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 5397 | PyMem_Del(groups); |
| 5398 | return posix_error(); |
| 5399 | } |
| 5400 | |
| 5401 | list = PyList_New(ngroups); |
| 5402 | if (list == NULL) { |
| 5403 | PyMem_Del(groups); |
| 5404 | return NULL; |
| 5405 | } |
| 5406 | |
| 5407 | for (i = 0; i < ngroups; i++) { |
| 5408 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
| 5409 | if (o == NULL) { |
| 5410 | Py_DECREF(list); |
| 5411 | PyMem_Del(groups); |
| 5412 | return NULL; |
| 5413 | } |
| 5414 | PyList_SET_ITEM(list, i, o); |
| 5415 | } |
| 5416 | |
| 5417 | PyMem_Del(groups); |
| 5418 | |
| 5419 | return list; |
| 5420 | } |
| 5421 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5422 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5423 | #ifdef HAVE_GETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5424 | PyDoc_STRVAR(posix_getgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5425 | "getgroups() -> list of group IDs\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5426 | Return list of supplemental group IDs for the process."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5427 | |
| 5428 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5429 | posix_getgroups(PyObject *self, PyObject *noargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5430 | { |
| 5431 | PyObject *result = NULL; |
| 5432 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5433 | #ifdef NGROUPS_MAX |
| 5434 | #define MAX_GROUPS NGROUPS_MAX |
| 5435 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5436 | /* 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] | 5437 | #define MAX_GROUPS 64 |
| 5438 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5439 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5440 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5441 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5442 | * This is a helper variable to store the intermediate result when |
| 5443 | * that happens. |
| 5444 | * |
| 5445 | * To keep the code readable the OSX behaviour is unconditional, |
| 5446 | * according to the POSIX spec this should be safe on all unix-y |
| 5447 | * systems. |
| 5448 | */ |
| 5449 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5450 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5451 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5452 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5453 | if (n < 0) { |
| 5454 | if (errno == EINVAL) { |
| 5455 | n = getgroups(0, NULL); |
| 5456 | if (n == -1) { |
| 5457 | return posix_error(); |
| 5458 | } |
| 5459 | if (n == 0) { |
| 5460 | /* Avoid malloc(0) */ |
| 5461 | alt_grouplist = grouplist; |
| 5462 | } else { |
| 5463 | alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); |
| 5464 | if (alt_grouplist == NULL) { |
| 5465 | errno = EINVAL; |
| 5466 | return posix_error(); |
| 5467 | } |
| 5468 | n = getgroups(n, alt_grouplist); |
| 5469 | if (n == -1) { |
| 5470 | PyMem_Free(alt_grouplist); |
| 5471 | return posix_error(); |
| 5472 | } |
| 5473 | } |
| 5474 | } else { |
| 5475 | return posix_error(); |
| 5476 | } |
| 5477 | } |
| 5478 | result = PyList_New(n); |
| 5479 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5480 | int i; |
| 5481 | for (i = 0; i < n; ++i) { |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5482 | PyObject *o = PyLong_FromLong((long)alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5483 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 5484 | Py_DECREF(result); |
| 5485 | result = NULL; |
| 5486 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5487 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5488 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5489 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | if (alt_grouplist != grouplist) { |
| 5493 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5494 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5495 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 5496 | return result; |
| 5497 | } |
| 5498 | #endif |
| 5499 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5500 | #ifdef HAVE_INITGROUPS |
| 5501 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 5502 | "initgroups(username, gid) -> None\n\n\ |
| 5503 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 5504 | the groups of which the specified username is a member, plus the specified\n\ |
| 5505 | group id."); |
| 5506 | |
| 5507 | static PyObject * |
| 5508 | posix_initgroups(PyObject *self, PyObject *args) |
| 5509 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5510 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5511 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5512 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5513 | long gid; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5514 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5515 | if (!PyArg_ParseTuple(args, "O&l:initgroups", |
| 5516 | PyUnicode_FSConverter, &oname, &gid)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5517 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5518 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5519 | |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 5520 | res = initgroups(username, (gid_t) gid); |
| 5521 | Py_DECREF(oname); |
| 5522 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5523 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5524 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5525 | Py_INCREF(Py_None); |
| 5526 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 5527 | } |
| 5528 | #endif |
| 5529 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5530 | #ifdef HAVE_GETPGID |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5531 | PyDoc_STRVAR(posix_getpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5532 | "getpgid(pid) -> pgid\n\n\ |
Neal Norwitz | 0c2c17c | 2002-06-13 21:22:11 +0000 | [diff] [blame] | 5533 | Call the system call getpgid()."); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5534 | |
| 5535 | static PyObject * |
| 5536 | posix_getpgid(PyObject *self, PyObject *args) |
| 5537 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5538 | pid_t pid, pgid; |
| 5539 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid)) |
| 5540 | return NULL; |
| 5541 | pgid = getpgid(pid); |
| 5542 | if (pgid < 0) |
| 5543 | return posix_error(); |
| 5544 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 5545 | } |
| 5546 | #endif /* HAVE_GETPGID */ |
| 5547 | |
| 5548 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5549 | #ifdef HAVE_GETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5550 | PyDoc_STRVAR(posix_getpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5551 | "getpgrp() -> pgrp\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5552 | Return the current process group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5553 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5554 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5555 | posix_getpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5556 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5557 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5558 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5559 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5560 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5561 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5562 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5563 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 5564 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5565 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5566 | #ifdef HAVE_SETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5567 | PyDoc_STRVAR(posix_setpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5568 | "setpgrp()\n\n\ |
Senthil Kumaran | 684760a | 2010-06-17 16:48:06 +0000 | [diff] [blame] | 5569 | Make this process the process group leader."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5570 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5571 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5572 | posix_setpgrp(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5573 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5574 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5575 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5576 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5577 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 5578 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5579 | return posix_error(); |
| 5580 | Py_INCREF(Py_None); |
| 5581 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5584 | #endif /* HAVE_SETPGRP */ |
| 5585 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5586 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5587 | |
| 5588 | #ifdef MS_WINDOWS |
| 5589 | #include <tlhelp32.h> |
| 5590 | |
| 5591 | static PyObject* |
| 5592 | win32_getppid() |
| 5593 | { |
| 5594 | HANDLE snapshot; |
| 5595 | pid_t mypid; |
| 5596 | PyObject* result = NULL; |
| 5597 | BOOL have_record; |
| 5598 | PROCESSENTRY32 pe; |
| 5599 | |
| 5600 | mypid = getpid(); /* This function never fails */ |
| 5601 | |
| 5602 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 5603 | if (snapshot == INVALID_HANDLE_VALUE) |
| 5604 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 5605 | |
| 5606 | pe.dwSize = sizeof(pe); |
| 5607 | have_record = Process32First(snapshot, &pe); |
| 5608 | while (have_record) { |
| 5609 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 5610 | /* We could cache the ulong value in a static variable. */ |
| 5611 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 5612 | break; |
| 5613 | } |
| 5614 | |
| 5615 | have_record = Process32Next(snapshot, &pe); |
| 5616 | } |
| 5617 | |
| 5618 | /* If our loop exits and our pid was not found (result will be NULL) |
| 5619 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 5620 | * error anyway, so let's raise it. */ |
| 5621 | if (!result) |
| 5622 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5623 | |
| 5624 | CloseHandle(snapshot); |
| 5625 | |
| 5626 | return result; |
| 5627 | } |
| 5628 | #endif /*MS_WINDOWS*/ |
| 5629 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5630 | PyDoc_STRVAR(posix_getppid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5631 | "getppid() -> ppid\n\n\ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5632 | Return the parent's process id. If the parent process has already exited,\n\ |
| 5633 | Windows machines will still return its id; others systems will return the id\n\ |
| 5634 | of the 'init' process (1)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5635 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5636 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5637 | posix_getppid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5638 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5639 | #ifdef MS_WINDOWS |
| 5640 | return win32_getppid(); |
| 5641 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5642 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5643 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 5644 | } |
| 5645 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5646 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5647 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5648 | #ifdef HAVE_GETLOGIN |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5649 | PyDoc_STRVAR(posix_getlogin__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5650 | "getlogin() -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5651 | Return the actual login name."); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5652 | |
| 5653 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5654 | posix_getlogin(PyObject *self, PyObject *noargs) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5655 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5656 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5657 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5658 | wchar_t user_name[UNLEN + 1]; |
| 5659 | DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); |
| 5660 | |
| 5661 | if (GetUserNameW(user_name, &num_chars)) { |
| 5662 | /* num_chars is the number of unicode chars plus null terminator */ |
| 5663 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5664 | } |
| 5665 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5666 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 5667 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5668 | char *name; |
| 5669 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5670 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5671 | errno = 0; |
| 5672 | name = getlogin(); |
| 5673 | if (name == NULL) { |
| 5674 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5675 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5676 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5677 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5678 | } |
| 5679 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 5680 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5681 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5682 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5683 | return result; |
| 5684 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 5685 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 5686 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5687 | #ifdef HAVE_GETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5688 | PyDoc_STRVAR(posix_getuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5689 | "getuid() -> uid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5690 | Return the current process's user id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5691 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5692 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5693 | posix_getuid(PyObject *self, PyObject *noargs) |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5694 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5695 | return PyLong_FromLong((long)getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5696 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5697 | #endif |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 5698 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5699 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5700 | #ifdef HAVE_KILL |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5701 | PyDoc_STRVAR(posix_kill__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5702 | "kill(pid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5703 | Kill a process with a signal."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5704 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5705 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5706 | posix_kill(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5707 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5708 | pid_t pid; |
| 5709 | int sig; |
| 5710 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig)) |
| 5711 | return NULL; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5712 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5713 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { |
| 5714 | APIRET rc; |
| 5715 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5716 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5717 | |
| 5718 | } else if (sig == XCPT_SIGNAL_KILLPROC) { |
| 5719 | APIRET rc; |
| 5720 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 5721 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5722 | |
| 5723 | } else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 5724 | return NULL; /* Unrecognized Signal Requested */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5725 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5726 | if (kill(pid, sig) == -1) |
| 5727 | return posix_error(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5728 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5729 | Py_INCREF(Py_None); |
| 5730 | return Py_None; |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5731 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 5732 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5733 | |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5734 | #ifdef HAVE_KILLPG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5735 | PyDoc_STRVAR(posix_killpg__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5736 | "killpg(pgid, sig)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5737 | Kill a process group with a signal."); |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5738 | |
| 5739 | static PyObject * |
| 5740 | posix_killpg(PyObject *self, PyObject *args) |
| 5741 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5742 | int sig; |
| 5743 | pid_t pgid; |
| 5744 | /* XXX some man pages make the `pgid` parameter an int, others |
| 5745 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 5746 | take the same type. Moreover, pid_t is always at least as wide as |
| 5747 | int (else compilation of this module fails), which is safe. */ |
| 5748 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig)) |
| 5749 | return NULL; |
| 5750 | if (killpg(pgid, sig) == -1) |
| 5751 | return posix_error(); |
| 5752 | Py_INCREF(Py_None); |
| 5753 | return Py_None; |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 5754 | } |
| 5755 | #endif |
| 5756 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5757 | #ifdef MS_WINDOWS |
| 5758 | PyDoc_STRVAR(win32_kill__doc__, |
| 5759 | "kill(pid, sig)\n\n\ |
| 5760 | Kill a process with a signal."); |
| 5761 | |
| 5762 | static PyObject * |
| 5763 | win32_kill(PyObject *self, PyObject *args) |
| 5764 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 5765 | PyObject *result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5766 | DWORD pid, sig, err; |
| 5767 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5769 | if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) |
| 5770 | return NULL; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5771 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5772 | /* Console processes which share a common console can be sent CTRL+C or |
| 5773 | CTRL+BREAK events, provided they handle said events. */ |
| 5774 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
| 5775 | if (GenerateConsoleCtrlEvent(sig, pid) == 0) { |
| 5776 | err = GetLastError(); |
| 5777 | PyErr_SetFromWindowsErr(err); |
| 5778 | } |
| 5779 | else |
| 5780 | Py_RETURN_NONE; |
| 5781 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5782 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5783 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 5784 | attempt to open and terminate the process. */ |
| 5785 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); |
| 5786 | if (handle == NULL) { |
| 5787 | err = GetLastError(); |
| 5788 | return PyErr_SetFromWindowsErr(err); |
| 5789 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5791 | if (TerminateProcess(handle, sig) == 0) { |
| 5792 | err = GetLastError(); |
| 5793 | result = PyErr_SetFromWindowsErr(err); |
| 5794 | } else { |
| 5795 | Py_INCREF(Py_None); |
| 5796 | result = Py_None; |
| 5797 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5798 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5799 | CloseHandle(handle); |
| 5800 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 5801 | } |
| 5802 | #endif /* MS_WINDOWS */ |
| 5803 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5804 | #ifdef HAVE_PLOCK |
| 5805 | |
| 5806 | #ifdef HAVE_SYS_LOCK_H |
| 5807 | #include <sys/lock.h> |
| 5808 | #endif |
| 5809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5810 | PyDoc_STRVAR(posix_plock__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5811 | "plock(op)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5812 | Lock program segments into memory."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5813 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5814 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5815 | posix_plock(PyObject *self, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5816 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5817 | int op; |
| 5818 | if (!PyArg_ParseTuple(args, "i:plock", &op)) |
| 5819 | return NULL; |
| 5820 | if (plock(op) == -1) |
| 5821 | return posix_error(); |
| 5822 | Py_INCREF(Py_None); |
| 5823 | return Py_None; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 5824 | } |
| 5825 | #endif |
| 5826 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5827 | #ifdef HAVE_SETUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5828 | PyDoc_STRVAR(posix_setuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5829 | "setuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5830 | Set the current process's user id."); |
| 5831 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5832 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5833 | posix_setuid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5834 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5835 | long uid_arg; |
| 5836 | uid_t uid; |
| 5837 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) |
| 5838 | return NULL; |
| 5839 | uid = uid_arg; |
| 5840 | if (uid != uid_arg) { |
| 5841 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5842 | return NULL; |
| 5843 | } |
| 5844 | if (setuid(uid) < 0) |
| 5845 | return posix_error(); |
| 5846 | Py_INCREF(Py_None); |
| 5847 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5848 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5849 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5850 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5851 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5852 | #ifdef HAVE_SETEUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5853 | PyDoc_STRVAR(posix_seteuid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5854 | "seteuid(uid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5855 | Set the current process's effective user id."); |
| 5856 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5857 | static PyObject * |
| 5858 | posix_seteuid (PyObject *self, PyObject *args) |
| 5859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5860 | long euid_arg; |
| 5861 | uid_t euid; |
| 5862 | if (!PyArg_ParseTuple(args, "l", &euid_arg)) |
| 5863 | return NULL; |
| 5864 | euid = euid_arg; |
| 5865 | if (euid != euid_arg) { |
| 5866 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5867 | return NULL; |
| 5868 | } |
| 5869 | if (seteuid(euid) < 0) { |
| 5870 | return posix_error(); |
| 5871 | } else { |
| 5872 | Py_INCREF(Py_None); |
| 5873 | return Py_None; |
| 5874 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5875 | } |
| 5876 | #endif /* HAVE_SETEUID */ |
| 5877 | |
| 5878 | #ifdef HAVE_SETEGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5879 | PyDoc_STRVAR(posix_setegid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5880 | "setegid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5881 | Set the current process's effective group id."); |
| 5882 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5883 | static PyObject * |
| 5884 | posix_setegid (PyObject *self, PyObject *args) |
| 5885 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5886 | long egid_arg; |
| 5887 | gid_t egid; |
| 5888 | if (!PyArg_ParseTuple(args, "l", &egid_arg)) |
| 5889 | return NULL; |
| 5890 | egid = egid_arg; |
| 5891 | if (egid != egid_arg) { |
| 5892 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5893 | return NULL; |
| 5894 | } |
| 5895 | if (setegid(egid) < 0) { |
| 5896 | return posix_error(); |
| 5897 | } else { |
| 5898 | Py_INCREF(Py_None); |
| 5899 | return Py_None; |
| 5900 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5901 | } |
| 5902 | #endif /* HAVE_SETEGID */ |
| 5903 | |
| 5904 | #ifdef HAVE_SETREUID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5905 | PyDoc_STRVAR(posix_setreuid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5906 | "setreuid(ruid, euid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5907 | Set the current process's real and effective user ids."); |
| 5908 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5909 | static PyObject * |
| 5910 | posix_setreuid (PyObject *self, PyObject *args) |
| 5911 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5912 | long ruid_arg, euid_arg; |
| 5913 | uid_t ruid, euid; |
| 5914 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) |
| 5915 | return NULL; |
| 5916 | if (ruid_arg == -1) |
| 5917 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ |
| 5918 | else |
| 5919 | ruid = ruid_arg; /* otherwise, assign from our long */ |
| 5920 | if (euid_arg == -1) |
| 5921 | euid = (uid_t)-1; |
| 5922 | else |
| 5923 | euid = euid_arg; |
| 5924 | if ((euid_arg != -1 && euid != euid_arg) || |
| 5925 | (ruid_arg != -1 && ruid != ruid_arg)) { |
| 5926 | PyErr_SetString(PyExc_OverflowError, "user id too big"); |
| 5927 | return NULL; |
| 5928 | } |
| 5929 | if (setreuid(ruid, euid) < 0) { |
| 5930 | return posix_error(); |
| 5931 | } else { |
| 5932 | Py_INCREF(Py_None); |
| 5933 | return Py_None; |
| 5934 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5935 | } |
| 5936 | #endif /* HAVE_SETREUID */ |
| 5937 | |
| 5938 | #ifdef HAVE_SETREGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5939 | PyDoc_STRVAR(posix_setregid__doc__, |
Neal Norwitz | 94f1d71 | 2004-02-16 01:26:34 +0000 | [diff] [blame] | 5940 | "setregid(rgid, egid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5941 | Set the current process's real and effective group ids."); |
| 5942 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5943 | static PyObject * |
| 5944 | posix_setregid (PyObject *self, PyObject *args) |
| 5945 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5946 | long rgid_arg, egid_arg; |
| 5947 | gid_t rgid, egid; |
| 5948 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) |
| 5949 | return NULL; |
| 5950 | if (rgid_arg == -1) |
| 5951 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ |
| 5952 | else |
| 5953 | rgid = rgid_arg; /* otherwise, assign from our long */ |
| 5954 | if (egid_arg == -1) |
| 5955 | egid = (gid_t)-1; |
| 5956 | else |
| 5957 | egid = egid_arg; |
| 5958 | if ((egid_arg != -1 && egid != egid_arg) || |
| 5959 | (rgid_arg != -1 && rgid != rgid_arg)) { |
| 5960 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5961 | return NULL; |
| 5962 | } |
| 5963 | if (setregid(rgid, egid) < 0) { |
| 5964 | return posix_error(); |
| 5965 | } else { |
| 5966 | Py_INCREF(Py_None); |
| 5967 | return Py_None; |
| 5968 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 5969 | } |
| 5970 | #endif /* HAVE_SETREGID */ |
| 5971 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5972 | #ifdef HAVE_SETGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5973 | PyDoc_STRVAR(posix_setgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5974 | "setgid(gid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5975 | Set the current process's group id."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5976 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5977 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 5978 | posix_setgid(PyObject *self, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5980 | long gid_arg; |
| 5981 | gid_t gid; |
| 5982 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) |
| 5983 | return NULL; |
| 5984 | gid = gid_arg; |
| 5985 | if (gid != gid_arg) { |
| 5986 | PyErr_SetString(PyExc_OverflowError, "group id too big"); |
| 5987 | return NULL; |
| 5988 | } |
| 5989 | if (setgid(gid) < 0) |
| 5990 | return posix_error(); |
| 5991 | Py_INCREF(Py_None); |
| 5992 | return Py_None; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5993 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 5994 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 5995 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 5996 | #ifdef HAVE_SETGROUPS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5997 | PyDoc_STRVAR(posix_setgroups__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 5998 | "setgroups(list)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5999 | Set the groups of the current process to list."); |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6000 | |
| 6001 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 6002 | posix_setgroups(PyObject *self, PyObject *groups) |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6003 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | int i, len; |
| 6005 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6006 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6007 | if (!PySequence_Check(groups)) { |
| 6008 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 6009 | return NULL; |
| 6010 | } |
| 6011 | len = PySequence_Size(groups); |
| 6012 | if (len > MAX_GROUPS) { |
| 6013 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 6014 | return NULL; |
| 6015 | } |
| 6016 | for(i = 0; i < len; i++) { |
| 6017 | PyObject *elem; |
| 6018 | elem = PySequence_GetItem(groups, i); |
| 6019 | if (!elem) |
| 6020 | return NULL; |
| 6021 | if (!PyLong_Check(elem)) { |
| 6022 | PyErr_SetString(PyExc_TypeError, |
| 6023 | "groups must be integers"); |
| 6024 | Py_DECREF(elem); |
| 6025 | return NULL; |
| 6026 | } else { |
| 6027 | unsigned long x = PyLong_AsUnsignedLong(elem); |
| 6028 | if (PyErr_Occurred()) { |
| 6029 | PyErr_SetString(PyExc_TypeError, |
| 6030 | "group id too big"); |
| 6031 | Py_DECREF(elem); |
| 6032 | return NULL; |
| 6033 | } |
| 6034 | grouplist[i] = x; |
| 6035 | /* read back the value to see if it fitted in gid_t */ |
| 6036 | if (grouplist[i] != x) { |
| 6037 | PyErr_SetString(PyExc_TypeError, |
| 6038 | "group id too big"); |
| 6039 | Py_DECREF(elem); |
| 6040 | return NULL; |
| 6041 | } |
| 6042 | } |
| 6043 | Py_DECREF(elem); |
| 6044 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | if (setgroups(len, grouplist) < 0) |
| 6047 | return posix_error(); |
| 6048 | Py_INCREF(Py_None); |
| 6049 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 6050 | } |
| 6051 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6052 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6053 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 6054 | static PyObject * |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6055 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6056 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6057 | PyObject *result; |
| 6058 | static PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6059 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6060 | if (pid == -1) |
| 6061 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6062 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6063 | if (struct_rusage == NULL) { |
| 6064 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 6065 | if (m == NULL) |
| 6066 | return NULL; |
| 6067 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); |
| 6068 | Py_DECREF(m); |
| 6069 | if (struct_rusage == NULL) |
| 6070 | return NULL; |
| 6071 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6072 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6073 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 6074 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 6075 | if (!result) |
| 6076 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6077 | |
| 6078 | #ifndef doubletime |
| 6079 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 6080 | #endif |
| 6081 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6082 | PyStructSequence_SET_ITEM(result, 0, |
| 6083 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
| 6084 | PyStructSequence_SET_ITEM(result, 1, |
| 6085 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6086 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6087 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 6088 | SET_INT(result, 2, ru->ru_maxrss); |
| 6089 | SET_INT(result, 3, ru->ru_ixrss); |
| 6090 | SET_INT(result, 4, ru->ru_idrss); |
| 6091 | SET_INT(result, 5, ru->ru_isrss); |
| 6092 | SET_INT(result, 6, ru->ru_minflt); |
| 6093 | SET_INT(result, 7, ru->ru_majflt); |
| 6094 | SET_INT(result, 8, ru->ru_nswap); |
| 6095 | SET_INT(result, 9, ru->ru_inblock); |
| 6096 | SET_INT(result, 10, ru->ru_oublock); |
| 6097 | SET_INT(result, 11, ru->ru_msgsnd); |
| 6098 | SET_INT(result, 12, ru->ru_msgrcv); |
| 6099 | SET_INT(result, 13, ru->ru_nsignals); |
| 6100 | SET_INT(result, 14, ru->ru_nvcsw); |
| 6101 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6102 | #undef SET_INT |
| 6103 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6104 | if (PyErr_Occurred()) { |
| 6105 | Py_DECREF(result); |
| 6106 | return NULL; |
| 6107 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6108 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6109 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6110 | } |
| 6111 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 6112 | |
| 6113 | #ifdef HAVE_WAIT3 |
| 6114 | PyDoc_STRVAR(posix_wait3__doc__, |
| 6115 | "wait3(options) -> (pid, status, rusage)\n\n\ |
| 6116 | Wait for completion of a child process."); |
| 6117 | |
| 6118 | static PyObject * |
| 6119 | posix_wait3(PyObject *self, PyObject *args) |
| 6120 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6121 | pid_t pid; |
| 6122 | int options; |
| 6123 | struct rusage ru; |
| 6124 | WAIT_TYPE status; |
| 6125 | WAIT_STATUS_INT(status) = 0; |
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 (!PyArg_ParseTuple(args, "i:wait3", &options)) |
| 6128 | return NULL; |
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 | Py_BEGIN_ALLOW_THREADS |
| 6131 | pid = wait3(&status, options, &ru); |
| 6132 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6134 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6135 | } |
| 6136 | #endif /* HAVE_WAIT3 */ |
| 6137 | |
| 6138 | #ifdef HAVE_WAIT4 |
| 6139 | PyDoc_STRVAR(posix_wait4__doc__, |
| 6140 | "wait4(pid, options) -> (pid, status, rusage)\n\n\ |
| 6141 | Wait for completion of a given child process."); |
| 6142 | |
| 6143 | static PyObject * |
| 6144 | posix_wait4(PyObject *self, PyObject *args) |
| 6145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6146 | pid_t pid; |
| 6147 | int options; |
| 6148 | struct rusage ru; |
| 6149 | WAIT_TYPE status; |
| 6150 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6152 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options)) |
| 6153 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6154 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6155 | Py_BEGIN_ALLOW_THREADS |
| 6156 | pid = wait4(pid, &status, options, &ru); |
| 6157 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6158 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6159 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6160 | } |
| 6161 | #endif /* HAVE_WAIT4 */ |
| 6162 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6163 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 6164 | PyDoc_STRVAR(posix_waitid__doc__, |
| 6165 | "waitid(idtype, id, options) -> waitid_result\n\n\ |
| 6166 | Wait for the completion of one or more child processes.\n\n\ |
| 6167 | idtype can be P_PID, P_PGID or P_ALL.\n\ |
| 6168 | id specifies the pid to wait on.\n\ |
| 6169 | options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\ |
| 6170 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\ |
| 6171 | Returns either waitid_result or None if WNOHANG is specified and there are\n\ |
| 6172 | no children in a waitable state."); |
| 6173 | |
| 6174 | static PyObject * |
| 6175 | posix_waitid(PyObject *self, PyObject *args) |
| 6176 | { |
| 6177 | PyObject *result; |
| 6178 | idtype_t idtype; |
| 6179 | id_t id; |
| 6180 | int options, res; |
| 6181 | siginfo_t si; |
| 6182 | si.si_pid = 0; |
| 6183 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options)) |
| 6184 | return NULL; |
| 6185 | Py_BEGIN_ALLOW_THREADS |
| 6186 | res = waitid(idtype, id, &si, options); |
| 6187 | Py_END_ALLOW_THREADS |
| 6188 | if (res == -1) |
| 6189 | return posix_error(); |
| 6190 | |
| 6191 | if (si.si_pid == 0) |
| 6192 | Py_RETURN_NONE; |
| 6193 | |
| 6194 | result = PyStructSequence_New(&WaitidResultType); |
| 6195 | if (!result) |
| 6196 | return NULL; |
| 6197 | |
| 6198 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
| 6199 | PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid)); |
| 6200 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 6201 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 6202 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 6203 | if (PyErr_Occurred()) { |
| 6204 | Py_DECREF(result); |
| 6205 | return NULL; |
| 6206 | } |
| 6207 | |
| 6208 | return result; |
| 6209 | } |
| 6210 | #endif |
| 6211 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6212 | #ifdef HAVE_WAITPID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6213 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6214 | "waitpid(pid, options) -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6215 | Wait for completion of a given child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6216 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6217 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6218 | posix_waitpid(PyObject *self, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6219 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6220 | pid_t pid; |
| 6221 | int options; |
| 6222 | WAIT_TYPE status; |
| 6223 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 6224 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6225 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6226 | return NULL; |
| 6227 | Py_BEGIN_ALLOW_THREADS |
| 6228 | pid = waitpid(pid, &status, options); |
| 6229 | Py_END_ALLOW_THREADS |
| 6230 | if (pid == -1) |
| 6231 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6233 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6236 | #elif defined(HAVE_CWAIT) |
| 6237 | |
| 6238 | /* 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] | 6239 | PyDoc_STRVAR(posix_waitpid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6240 | "waitpid(pid, options) -> (pid, status << 8)\n\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6241 | "Wait for completion of a given process. options is ignored on Windows."); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6242 | |
| 6243 | static PyObject * |
| 6244 | posix_waitpid(PyObject *self, PyObject *args) |
| 6245 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6246 | Py_intptr_t pid; |
| 6247 | int status, options; |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6248 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6249 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options)) |
| 6250 | return NULL; |
| 6251 | Py_BEGIN_ALLOW_THREADS |
| 6252 | pid = _cwait(&status, pid, options); |
| 6253 | Py_END_ALLOW_THREADS |
| 6254 | if (pid == -1) |
| 6255 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6256 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6257 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
| 6258 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 6259 | } |
| 6260 | #endif /* HAVE_WAITPID || HAVE_CWAIT */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6261 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6262 | #ifdef HAVE_WAIT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6263 | PyDoc_STRVAR(posix_wait__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6264 | "wait() -> (pid, status)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6265 | Wait for completion of a child process."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6267 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6268 | posix_wait(PyObject *self, PyObject *noargs) |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 6269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6270 | pid_t pid; |
| 6271 | WAIT_TYPE status; |
| 6272 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6273 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | Py_BEGIN_ALLOW_THREADS |
| 6275 | pid = wait(&status); |
| 6276 | Py_END_ALLOW_THREADS |
| 6277 | if (pid == -1) |
| 6278 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6279 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6280 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6281 | } |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6282 | #endif |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6283 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6285 | PyDoc_STRVAR(posix_lstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6286 | "lstat(path) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6287 | Like stat(path), but do not follow symbolic links."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6288 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6289 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6290 | posix_lstat(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6291 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6292 | #ifdef HAVE_LSTAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6293 | return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6294 | #else /* !HAVE_LSTAT */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6295 | #ifdef MS_WINDOWS |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 6296 | return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6297 | win32_lstat_w); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6298 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6299 | return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6300 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6301 | #endif /* !HAVE_LSTAT */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6304 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6305 | #ifdef HAVE_READLINK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6306 | PyDoc_STRVAR(posix_readlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6307 | "readlink(path) -> path\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | 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] | 6309 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6310 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6311 | posix_readlink(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6312 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | PyObject* v; |
| 6314 | char buf[MAXPATHLEN]; |
| 6315 | PyObject *opath; |
| 6316 | char *path; |
| 6317 | int n; |
| 6318 | int arg_is_unicode = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6320 | if (!PyArg_ParseTuple(args, "O&:readlink", |
| 6321 | PyUnicode_FSConverter, &opath)) |
| 6322 | return NULL; |
| 6323 | path = PyBytes_AsString(opath); |
| 6324 | v = PySequence_GetItem(args, 0); |
| 6325 | if (v == NULL) { |
| 6326 | Py_DECREF(opath); |
| 6327 | return NULL; |
| 6328 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6330 | if (PyUnicode_Check(v)) { |
| 6331 | arg_is_unicode = 1; |
| 6332 | } |
| 6333 | Py_DECREF(v); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6334 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | Py_BEGIN_ALLOW_THREADS |
| 6336 | n = readlink(path, buf, (int) sizeof buf); |
| 6337 | Py_END_ALLOW_THREADS |
| 6338 | if (n < 0) |
| 6339 | return posix_error_with_allocated_filename(opath); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6341 | Py_DECREF(opath); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 6342 | if (arg_is_unicode) |
| 6343 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 6344 | else |
| 6345 | return PyBytes_FromStringAndSize(buf, n); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6346 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6347 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6348 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6349 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6350 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6351 | PyDoc_STRVAR(posix_symlink__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6352 | "symlink(src, dst)\n\n\ |
Brett Cannon | 807413d | 2003-06-11 00:18:09 +0000 | [diff] [blame] | 6353 | Create a symbolic link pointing to src named dst."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6354 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6355 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6356 | posix_symlink(PyObject *self, PyObject *args) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6357 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6358 | return posix_2str(args, "O&O&:symlink", symlink); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6359 | } |
| 6360 | #endif /* HAVE_SYMLINK */ |
| 6361 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6362 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 6363 | |
| 6364 | PyDoc_STRVAR(win_readlink__doc__, |
| 6365 | "readlink(path) -> path\n\n\ |
| 6366 | Return a string representing the path to which the symbolic link points."); |
| 6367 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6368 | /* Windows readlink implementation */ |
| 6369 | static PyObject * |
| 6370 | win_readlink(PyObject *self, PyObject *args) |
| 6371 | { |
| 6372 | wchar_t *path; |
| 6373 | DWORD n_bytes_returned; |
| 6374 | DWORD io_result; |
| 6375 | PyObject *result; |
| 6376 | HANDLE reparse_point_handle; |
| 6377 | |
| 6378 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 6379 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 6380 | wchar_t *print_name; |
| 6381 | |
| 6382 | if (!PyArg_ParseTuple(args, |
| 6383 | "u:readlink", |
| 6384 | &path)) |
| 6385 | return NULL; |
| 6386 | |
| 6387 | /* First get a handle to the reparse point */ |
| 6388 | Py_BEGIN_ALLOW_THREADS |
| 6389 | reparse_point_handle = CreateFileW( |
| 6390 | path, |
| 6391 | 0, |
| 6392 | 0, |
| 6393 | 0, |
| 6394 | OPEN_EXISTING, |
| 6395 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 6396 | 0); |
| 6397 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6398 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6399 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 6400 | { |
| 6401 | return win32_error_unicode("readlink", path); |
| 6402 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6403 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6404 | Py_BEGIN_ALLOW_THREADS |
| 6405 | /* New call DeviceIoControl to read the reparse point */ |
| 6406 | io_result = DeviceIoControl( |
| 6407 | reparse_point_handle, |
| 6408 | FSCTL_GET_REPARSE_POINT, |
| 6409 | 0, 0, /* in buffer */ |
| 6410 | target_buffer, sizeof(target_buffer), |
| 6411 | &n_bytes_returned, |
| 6412 | 0 /* we're not using OVERLAPPED_IO */ |
| 6413 | ); |
| 6414 | CloseHandle(reparse_point_handle); |
| 6415 | Py_END_ALLOW_THREADS |
| 6416 | |
| 6417 | if (io_result==0) |
| 6418 | { |
| 6419 | return win32_error_unicode("readlink", path); |
| 6420 | } |
| 6421 | |
| 6422 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 6423 | { |
| 6424 | PyErr_SetString(PyExc_ValueError, |
| 6425 | "not a symbolic link"); |
| 6426 | return NULL; |
| 6427 | } |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6428 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 6429 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 6430 | |
| 6431 | result = PyUnicode_FromWideChar(print_name, |
| 6432 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6433 | return result; |
| 6434 | } |
| 6435 | |
| 6436 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 6437 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6438 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6439 | |
| 6440 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 6441 | static int has_CreateSymbolicLinkW = 0; |
| 6442 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); |
| 6443 | static int |
| 6444 | check_CreateSymbolicLinkW() |
| 6445 | { |
| 6446 | HINSTANCE hKernel32; |
| 6447 | /* only recheck */ |
| 6448 | if (has_CreateSymbolicLinkW) |
| 6449 | return has_CreateSymbolicLinkW; |
| 6450 | hKernel32 = GetModuleHandle("KERNEL32"); |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 6451 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 6452 | "CreateSymbolicLinkW"); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6453 | if (Py_CreateSymbolicLinkW) |
| 6454 | has_CreateSymbolicLinkW = 1; |
| 6455 | return has_CreateSymbolicLinkW; |
| 6456 | } |
| 6457 | |
| 6458 | PyDoc_STRVAR(win_symlink__doc__, |
| 6459 | "symlink(src, dst, target_is_directory=False)\n\n\ |
| 6460 | Create a symbolic link pointing to src named dst.\n\ |
| 6461 | target_is_directory is required if the target is to be interpreted as\n\ |
| 6462 | a directory.\n\ |
| 6463 | This function requires Windows 6.0 or greater, and raises a\n\ |
| 6464 | NotImplementedError otherwise."); |
| 6465 | |
| 6466 | static PyObject * |
| 6467 | win_symlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6468 | { |
| 6469 | static char *kwlist[] = {"src", "dest", "target_is_directory", NULL}; |
| 6470 | PyObject *src, *dest; |
| 6471 | int target_is_directory = 0; |
| 6472 | DWORD res; |
| 6473 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6474 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6475 | if (!check_CreateSymbolicLinkW()) |
| 6476 | { |
| 6477 | /* raise NotImplementedError */ |
| 6478 | return PyErr_Format(PyExc_NotImplementedError, |
| 6479 | "CreateSymbolicLinkW not found"); |
| 6480 | } |
| 6481 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink", |
| 6482 | kwlist, &src, &dest, &target_is_directory)) |
| 6483 | return NULL; |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 6484 | |
| 6485 | if (win32_can_symlink == 0) |
| 6486 | return PyErr_Format(PyExc_OSError, "symbolic link privilege not held"); |
| 6487 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6488 | if (!convert_to_unicode(&src)) { return NULL; } |
| 6489 | if (!convert_to_unicode(&dest)) { |
| 6490 | Py_DECREF(src); |
| 6491 | return NULL; |
| 6492 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6493 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6494 | /* if src is a directory, ensure target_is_directory==1 */ |
| 6495 | if( |
| 6496 | GetFileAttributesExW( |
| 6497 | PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info |
| 6498 | )) |
| 6499 | { |
| 6500 | target_is_directory = target_is_directory || |
| 6501 | (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
| 6502 | } |
| 6503 | |
| 6504 | Py_BEGIN_ALLOW_THREADS |
| 6505 | res = Py_CreateSymbolicLinkW( |
| 6506 | PyUnicode_AsUnicode(dest), |
| 6507 | PyUnicode_AsUnicode(src), |
| 6508 | target_is_directory); |
| 6509 | Py_END_ALLOW_THREADS |
| 6510 | Py_DECREF(src); |
| 6511 | Py_DECREF(dest); |
| 6512 | if (!res) |
| 6513 | { |
| 6514 | return win32_error_unicode("symlink", PyUnicode_AsUnicode(src)); |
| 6515 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6516 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 6517 | Py_INCREF(Py_None); |
| 6518 | return Py_None; |
| 6519 | } |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 6520 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6521 | |
| 6522 | #ifdef HAVE_TIMES |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6523 | #if defined(PYCC_VACPP) && defined(PYOS_OS2) |
| 6524 | static long |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 6525 | system_uptime(void) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6526 | { |
| 6527 | ULONG value = 0; |
| 6528 | |
| 6529 | Py_BEGIN_ALLOW_THREADS |
| 6530 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value)); |
| 6531 | Py_END_ALLOW_THREADS |
| 6532 | |
| 6533 | return value; |
| 6534 | } |
| 6535 | |
| 6536 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6537 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6538 | { |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6539 | /* Currently Only Uptime is Provided -- Others Later */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6540 | return Py_BuildValue("ddddd", |
| 6541 | (double)0 /* t.tms_utime / HZ */, |
| 6542 | (double)0 /* t.tms_stime / HZ */, |
| 6543 | (double)0 /* t.tms_cutime / HZ */, |
| 6544 | (double)0 /* t.tms_cstime / HZ */, |
| 6545 | (double)system_uptime() / 1000); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 6546 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6547 | #else /* not OS2 */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 6548 | #define NEED_TICKS_PER_SECOND |
| 6549 | static long ticks_per_second = -1; |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6550 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6551 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6552 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6553 | struct tms t; |
| 6554 | clock_t c; |
| 6555 | errno = 0; |
| 6556 | c = times(&t); |
| 6557 | if (c == (clock_t) -1) |
| 6558 | return posix_error(); |
| 6559 | return Py_BuildValue("ddddd", |
| 6560 | (double)t.tms_utime / ticks_per_second, |
| 6561 | (double)t.tms_stime / ticks_per_second, |
| 6562 | (double)t.tms_cutime / ticks_per_second, |
| 6563 | (double)t.tms_cstime / ticks_per_second, |
| 6564 | (double)c / ticks_per_second); |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6565 | } |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6566 | #endif /* not OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6567 | #endif /* HAVE_TIMES */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6568 | |
| 6569 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6570 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6571 | #define HAVE_TIMES /* so the method table will pick it up */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6572 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6573 | posix_times(PyObject *self, PyObject *noargs) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6575 | FILETIME create, exit, kernel, user; |
| 6576 | HANDLE hProc; |
| 6577 | hProc = GetCurrentProcess(); |
| 6578 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 6579 | /* The fields of a FILETIME structure are the hi and lo part |
| 6580 | of a 64-bit value expressed in 100 nanosecond units. |
| 6581 | 1e7 is one second in such units; 1e-7 the inverse. |
| 6582 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 6583 | */ |
| 6584 | return Py_BuildValue( |
| 6585 | "ddddd", |
| 6586 | (double)(user.dwHighDateTime*429.4967296 + |
| 6587 | user.dwLowDateTime*1e-7), |
| 6588 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 6589 | kernel.dwLowDateTime*1e-7), |
| 6590 | (double)0, |
| 6591 | (double)0, |
| 6592 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 6593 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6594 | #endif /* MS_WINDOWS */ |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6595 | |
| 6596 | #ifdef HAVE_TIMES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6597 | PyDoc_STRVAR(posix_times__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6598 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6599 | Return a tuple of floating point numbers indicating process times."); |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 6600 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6601 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6602 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6603 | #ifdef HAVE_GETSID |
| 6604 | PyDoc_STRVAR(posix_getsid__doc__, |
| 6605 | "getsid(pid) -> sid\n\n\ |
| 6606 | Call the system call getsid()."); |
| 6607 | |
| 6608 | static PyObject * |
| 6609 | posix_getsid(PyObject *self, PyObject *args) |
| 6610 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6611 | pid_t pid; |
| 6612 | int sid; |
| 6613 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid)) |
| 6614 | return NULL; |
| 6615 | sid = getsid(pid); |
| 6616 | if (sid < 0) |
| 6617 | return posix_error(); |
| 6618 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 6619 | } |
| 6620 | #endif /* HAVE_GETSID */ |
| 6621 | |
| 6622 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6623 | #ifdef HAVE_SETSID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6624 | PyDoc_STRVAR(posix_setsid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6625 | "setsid()\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6626 | Call the system call setsid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6627 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6628 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 6629 | posix_setsid(PyObject *self, PyObject *noargs) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6630 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6631 | if (setsid() < 0) |
| 6632 | return posix_error(); |
| 6633 | Py_INCREF(Py_None); |
| 6634 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6635 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6636 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6637 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6638 | #ifdef HAVE_SETPGID |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6639 | PyDoc_STRVAR(posix_setpgid__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6640 | "setpgid(pid, pgrp)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6641 | Call the system call setpgid()."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6642 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6643 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6644 | posix_setpgid(PyObject *self, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6645 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6646 | pid_t pid; |
| 6647 | int pgrp; |
| 6648 | if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp)) |
| 6649 | return NULL; |
| 6650 | if (setpgid(pid, pgrp) < 0) |
| 6651 | return posix_error(); |
| 6652 | Py_INCREF(Py_None); |
| 6653 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6654 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6655 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 6656 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6657 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6658 | #ifdef HAVE_TCGETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6659 | PyDoc_STRVAR(posix_tcgetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6660 | "tcgetpgrp(fd) -> pgid\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6661 | 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] | 6662 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6663 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6664 | posix_tcgetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6665 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6666 | int fd; |
| 6667 | pid_t pgid; |
| 6668 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) |
| 6669 | return NULL; |
| 6670 | pgid = tcgetpgrp(fd); |
| 6671 | if (pgid < 0) |
| 6672 | return posix_error(); |
| 6673 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6674 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6675 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6676 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6677 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6678 | #ifdef HAVE_TCSETPGRP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | PyDoc_STRVAR(posix_tcsetpgrp__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6680 | "tcsetpgrp(fd, pgid)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6681 | 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] | 6682 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6683 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6684 | posix_tcsetpgrp(PyObject *self, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6685 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6686 | int fd; |
| 6687 | pid_t pgid; |
| 6688 | if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid)) |
| 6689 | return NULL; |
| 6690 | if (tcsetpgrp(fd, pgid) < 0) |
| 6691 | return posix_error(); |
| 6692 | Py_INCREF(Py_None); |
| 6693 | return Py_None; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 6694 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6695 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 6696 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6697 | /* Functions acting on file descriptors */ |
| 6698 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6699 | PyDoc_STRVAR(posix_open__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6700 | "open(filename, flag [, mode=0777]) -> fd\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6701 | Open a file (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6702 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6703 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6704 | posix_open(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6706 | PyObject *ofile; |
| 6707 | char *file; |
| 6708 | int flag; |
| 6709 | int mode = 0777; |
| 6710 | int fd; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6711 | |
| 6712 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6713 | PyUnicodeObject *po; |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6714 | if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6715 | Py_BEGIN_ALLOW_THREADS |
| 6716 | /* PyUnicode_AS_UNICODE OK without thread |
| 6717 | lock as it is a simple dereference. */ |
| 6718 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); |
| 6719 | Py_END_ALLOW_THREADS |
| 6720 | if (fd < 0) |
| 6721 | return posix_error(); |
| 6722 | return PyLong_FromLong((long)fd); |
| 6723 | } |
| 6724 | /* Drop the argument parsing error as narrow strings |
| 6725 | are also valid. */ |
| 6726 | PyErr_Clear(); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 6727 | #endif |
| 6728 | |
Victor Stinner | 26de69d | 2011-06-17 15:15:38 +0200 | [diff] [blame] | 6729 | if (!PyArg_ParseTuple(args, "O&i|i:open", |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6730 | PyUnicode_FSConverter, &ofile, |
| 6731 | &flag, &mode)) |
| 6732 | return NULL; |
| 6733 | file = PyBytes_AsString(ofile); |
| 6734 | Py_BEGIN_ALLOW_THREADS |
| 6735 | fd = open(file, flag, mode); |
| 6736 | Py_END_ALLOW_THREADS |
| 6737 | if (fd < 0) |
| 6738 | return posix_error_with_allocated_filename(ofile); |
| 6739 | Py_DECREF(ofile); |
| 6740 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6743 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6744 | PyDoc_STRVAR(posix_close__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6745 | "close(fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6746 | Close a file descriptor (for low level IO)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6747 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6748 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6749 | posix_close(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6750 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6751 | int fd, res; |
| 6752 | if (!PyArg_ParseTuple(args, "i:close", &fd)) |
| 6753 | return NULL; |
| 6754 | if (!_PyVerify_fd(fd)) |
| 6755 | return posix_error(); |
| 6756 | Py_BEGIN_ALLOW_THREADS |
| 6757 | res = close(fd); |
| 6758 | Py_END_ALLOW_THREADS |
| 6759 | if (res < 0) |
| 6760 | return posix_error(); |
| 6761 | Py_INCREF(Py_None); |
| 6762 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6765 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6766 | PyDoc_STRVAR(posix_closerange__doc__, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6767 | "closerange(fd_low, fd_high)\n\n\ |
| 6768 | Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 6769 | |
| 6770 | static PyObject * |
| 6771 | posix_closerange(PyObject *self, PyObject *args) |
| 6772 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | int fd_from, fd_to, i; |
| 6774 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) |
| 6775 | return NULL; |
| 6776 | Py_BEGIN_ALLOW_THREADS |
| 6777 | for (i = fd_from; i < fd_to; i++) |
| 6778 | if (_PyVerify_fd(i)) |
| 6779 | close(i); |
| 6780 | Py_END_ALLOW_THREADS |
| 6781 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
| 6784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6785 | PyDoc_STRVAR(posix_dup__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6786 | "dup(fd) -> fd2\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6787 | Return a duplicate of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6788 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6789 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6790 | posix_dup(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6792 | int fd; |
| 6793 | if (!PyArg_ParseTuple(args, "i:dup", &fd)) |
| 6794 | return NULL; |
| 6795 | if (!_PyVerify_fd(fd)) |
| 6796 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | fd = dup(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6798 | if (fd < 0) |
| 6799 | return posix_error(); |
| 6800 | return PyLong_FromLong((long)fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | PyDoc_STRVAR(posix_dup2__doc__, |
Andrew M. Kuchling | 8135fd5 | 2004-01-16 13:18:42 +0000 | [diff] [blame] | 6805 | "dup2(old_fd, new_fd)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6806 | Duplicate file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6807 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6808 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6809 | posix_dup2(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6810 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6811 | int fd, fd2, res; |
| 6812 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) |
| 6813 | return NULL; |
| 6814 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 6815 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6816 | res = dup2(fd, fd2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6817 | if (res < 0) |
| 6818 | return posix_error(); |
| 6819 | Py_INCREF(Py_None); |
| 6820 | return Py_None; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6823 | #ifdef HAVE_LOCKF |
| 6824 | PyDoc_STRVAR(posix_lockf__doc__, |
| 6825 | "lockf(fd, cmd, len)\n\n\ |
| 6826 | Apply, test or remove a POSIX lock on an open file descriptor.\n\n\ |
| 6827 | fd is an open file descriptor.\n\ |
| 6828 | cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\ |
| 6829 | F_TEST.\n\ |
| 6830 | len specifies the section of the file to lock."); |
| 6831 | |
| 6832 | static PyObject * |
| 6833 | posix_lockf(PyObject *self, PyObject *args) |
| 6834 | { |
| 6835 | int fd, cmd, res; |
| 6836 | off_t len; |
| 6837 | if (!PyArg_ParseTuple(args, "iiO&:lockf", |
| 6838 | &fd, &cmd, _parse_off_t, &len)) |
| 6839 | return NULL; |
| 6840 | |
| 6841 | Py_BEGIN_ALLOW_THREADS |
| 6842 | res = lockf(fd, cmd, len); |
| 6843 | Py_END_ALLOW_THREADS |
| 6844 | |
| 6845 | if (res < 0) |
| 6846 | return posix_error(); |
| 6847 | |
| 6848 | Py_RETURN_NONE; |
| 6849 | } |
| 6850 | #endif |
| 6851 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6852 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6853 | PyDoc_STRVAR(posix_lseek__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6854 | "lseek(fd, pos, how) -> newpos\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6855 | Set the current position of a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6856 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6857 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6858 | posix_lseek(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6859 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6860 | int fd, how; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6861 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6862 | PY_LONG_LONG pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6863 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6864 | off_t pos, res; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6865 | #endif |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6866 | PyObject *posobj; |
| 6867 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6868 | return NULL; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6869 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 6871 | switch (how) { |
| 6872 | case 0: how = SEEK_SET; break; |
| 6873 | case 1: how = SEEK_CUR; break; |
| 6874 | case 2: how = SEEK_END; break; |
| 6875 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 6876 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6877 | |
Ross Lagerwall | 8e74967 | 2011-03-17 21:54:07 +0200 | [diff] [blame] | 6878 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 6879 | pos = PyLong_AsLong(posobj); |
| 6880 | #else |
| 6881 | pos = PyLong_AsLongLong(posobj); |
| 6882 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6883 | if (PyErr_Occurred()) |
| 6884 | return NULL; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6885 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6886 | if (!_PyVerify_fd(fd)) |
| 6887 | return posix_error(); |
| 6888 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6889 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6890 | res = _lseeki64(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6891 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6892 | res = lseek(fd, pos, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 6893 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6894 | Py_END_ALLOW_THREADS |
| 6895 | if (res < 0) |
| 6896 | return posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6897 | |
| 6898 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6899 | return PyLong_FromLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6900 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6901 | return PyLong_FromLongLong(res); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 6902 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6903 | } |
| 6904 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6905 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6906 | PyDoc_STRVAR(posix_read__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 6907 | "read(fd, buffersize) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6908 | Read a file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6910 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 6911 | posix_read(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6912 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6913 | int fd, size; |
| 6914 | Py_ssize_t n; |
| 6915 | PyObject *buffer; |
| 6916 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) |
| 6917 | return NULL; |
| 6918 | if (size < 0) { |
| 6919 | errno = EINVAL; |
| 6920 | return posix_error(); |
| 6921 | } |
| 6922 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 6923 | if (buffer == NULL) |
| 6924 | return NULL; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6925 | if (!_PyVerify_fd(fd)) { |
| 6926 | Py_DECREF(buffer); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6927 | return posix_error(); |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 6928 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6929 | Py_BEGIN_ALLOW_THREADS |
| 6930 | n = read(fd, PyBytes_AS_STRING(buffer), size); |
| 6931 | Py_END_ALLOW_THREADS |
| 6932 | if (n < 0) { |
| 6933 | Py_DECREF(buffer); |
| 6934 | return posix_error(); |
| 6935 | } |
| 6936 | if (n != size) |
| 6937 | _PyBytes_Resize(&buffer, n); |
| 6938 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 6939 | } |
| 6940 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6941 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 6942 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6943 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6944 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 6945 | { |
| 6946 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6947 | Py_ssize_t blen, total = 0; |
| 6948 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6949 | *iov = PyMem_New(struct iovec, cnt); |
| 6950 | if (*iov == NULL) { |
| 6951 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6952 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6953 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6954 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6955 | *buf = PyMem_New(Py_buffer, cnt); |
| 6956 | if (*buf == NULL) { |
| 6957 | PyMem_Del(*iov); |
| 6958 | PyErr_NoMemory(); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6959 | return total; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6960 | } |
| 6961 | |
| 6962 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6963 | PyObject *item = PySequence_GetItem(seq, i); |
| 6964 | if (item == NULL) |
| 6965 | goto fail; |
| 6966 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 6967 | Py_DECREF(item); |
| 6968 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6969 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6970 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6971 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6972 | blen = (*buf)[i].len; |
| 6973 | (*iov)[i].iov_len = blen; |
| 6974 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6975 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 6976 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6977 | |
| 6978 | fail: |
| 6979 | PyMem_Del(*iov); |
| 6980 | for (j = 0; j < i; j++) { |
| 6981 | PyBuffer_Release(&(*buf)[j]); |
| 6982 | } |
| 6983 | PyMem_Del(*buf); |
| 6984 | return 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | static void |
| 6988 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 6989 | { |
| 6990 | int i; |
| 6991 | PyMem_Del(iov); |
| 6992 | for (i = 0; i < cnt; i++) { |
| 6993 | PyBuffer_Release(&buf[i]); |
| 6994 | } |
| 6995 | PyMem_Del(buf); |
| 6996 | } |
| 6997 | #endif |
| 6998 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6999 | #ifdef HAVE_READV |
| 7000 | PyDoc_STRVAR(posix_readv__doc__, |
| 7001 | "readv(fd, buffers) -> bytesread\n\n\ |
| 7002 | Read from a file descriptor into a number of writable buffers. buffers\n\ |
| 7003 | is an arbitrary sequence of writable buffers.\n\ |
| 7004 | Returns the total number of bytes read."); |
| 7005 | |
| 7006 | static PyObject * |
| 7007 | posix_readv(PyObject *self, PyObject *args) |
| 7008 | { |
| 7009 | int fd, cnt; |
| 7010 | Py_ssize_t n; |
| 7011 | PyObject *seq; |
| 7012 | struct iovec *iov; |
| 7013 | Py_buffer *buf; |
| 7014 | |
| 7015 | if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq)) |
| 7016 | return NULL; |
| 7017 | if (!PySequence_Check(seq)) { |
| 7018 | PyErr_SetString(PyExc_TypeError, |
| 7019 | "readv() arg 2 must be a sequence"); |
| 7020 | return NULL; |
| 7021 | } |
| 7022 | cnt = PySequence_Size(seq); |
| 7023 | |
| 7024 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE)) |
| 7025 | return NULL; |
| 7026 | |
| 7027 | Py_BEGIN_ALLOW_THREADS |
| 7028 | n = readv(fd, iov, cnt); |
| 7029 | Py_END_ALLOW_THREADS |
| 7030 | |
| 7031 | iov_cleanup(iov, buf, cnt); |
| 7032 | return PyLong_FromSsize_t(n); |
| 7033 | } |
| 7034 | #endif |
| 7035 | |
| 7036 | #ifdef HAVE_PREAD |
| 7037 | PyDoc_STRVAR(posix_pread__doc__, |
| 7038 | "pread(fd, buffersize, offset) -> string\n\n\ |
| 7039 | Read from a file descriptor, fd, at a position of offset. It will read up\n\ |
| 7040 | to buffersize number of bytes. The file offset remains unchanged."); |
| 7041 | |
| 7042 | static PyObject * |
| 7043 | posix_pread(PyObject *self, PyObject *args) |
| 7044 | { |
| 7045 | int fd, size; |
| 7046 | off_t offset; |
| 7047 | Py_ssize_t n; |
| 7048 | PyObject *buffer; |
| 7049 | if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset)) |
| 7050 | return NULL; |
| 7051 | |
| 7052 | if (size < 0) { |
| 7053 | errno = EINVAL; |
| 7054 | return posix_error(); |
| 7055 | } |
| 7056 | buffer = PyBytes_FromStringAndSize((char *)NULL, size); |
| 7057 | if (buffer == NULL) |
| 7058 | return NULL; |
| 7059 | if (!_PyVerify_fd(fd)) { |
| 7060 | Py_DECREF(buffer); |
| 7061 | return posix_error(); |
| 7062 | } |
| 7063 | Py_BEGIN_ALLOW_THREADS |
| 7064 | n = pread(fd, PyBytes_AS_STRING(buffer), size, offset); |
| 7065 | Py_END_ALLOW_THREADS |
| 7066 | if (n < 0) { |
| 7067 | Py_DECREF(buffer); |
| 7068 | return posix_error(); |
| 7069 | } |
| 7070 | if (n != size) |
| 7071 | _PyBytes_Resize(&buffer, n); |
| 7072 | return buffer; |
| 7073 | } |
| 7074 | #endif |
| 7075 | |
| 7076 | PyDoc_STRVAR(posix_write__doc__, |
| 7077 | "write(fd, string) -> byteswritten\n\n\ |
| 7078 | Write a string to a file descriptor."); |
| 7079 | |
| 7080 | static PyObject * |
| 7081 | posix_write(PyObject *self, PyObject *args) |
| 7082 | { |
| 7083 | Py_buffer pbuf; |
| 7084 | int fd; |
| 7085 | Py_ssize_t size, len; |
| 7086 | |
| 7087 | if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf)) |
| 7088 | return NULL; |
| 7089 | if (!_PyVerify_fd(fd)) { |
| 7090 | PyBuffer_Release(&pbuf); |
| 7091 | return posix_error(); |
| 7092 | } |
| 7093 | len = pbuf.len; |
| 7094 | Py_BEGIN_ALLOW_THREADS |
| 7095 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 7096 | if (len > INT_MAX) |
| 7097 | len = INT_MAX; |
| 7098 | size = write(fd, pbuf.buf, (int)len); |
| 7099 | #else |
| 7100 | size = write(fd, pbuf.buf, len); |
| 7101 | #endif |
| 7102 | Py_END_ALLOW_THREADS |
| 7103 | PyBuffer_Release(&pbuf); |
| 7104 | if (size < 0) |
| 7105 | return posix_error(); |
| 7106 | return PyLong_FromSsize_t(size); |
| 7107 | } |
| 7108 | |
| 7109 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7110 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 7111 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 7112 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 7113 | -> byteswritten\n\ |
| 7114 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 7115 | |
| 7116 | static PyObject * |
| 7117 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 7118 | { |
| 7119 | int in, out; |
| 7120 | Py_ssize_t ret; |
| 7121 | off_t offset; |
| 7122 | |
| 7123 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 7124 | #ifndef __APPLE__ |
| 7125 | Py_ssize_t len; |
| 7126 | #endif |
| 7127 | PyObject *headers = NULL, *trailers = NULL; |
| 7128 | Py_buffer *hbuf, *tbuf; |
| 7129 | off_t sbytes; |
| 7130 | struct sf_hdtr sf; |
| 7131 | int flags = 0; |
| 7132 | sf.headers = NULL; |
| 7133 | sf.trailers = NULL; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 7134 | static char *keywords[] = {"out", "in", |
| 7135 | "offset", "count", |
| 7136 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7137 | |
| 7138 | #ifdef __APPLE__ |
| 7139 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7140 | keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7141 | #else |
| 7142 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Georg Brandl | a391b11 | 2011-02-25 15:23:18 +0000 | [diff] [blame] | 7143 | keywords, &out, &in, _parse_off_t, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7144 | #endif |
| 7145 | &headers, &trailers, &flags)) |
| 7146 | return NULL; |
| 7147 | if (headers != NULL) { |
| 7148 | if (!PySequence_Check(headers)) { |
| 7149 | PyErr_SetString(PyExc_TypeError, |
| 7150 | "sendfile() headers must be a sequence or None"); |
| 7151 | return NULL; |
| 7152 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7153 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7154 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7155 | if (sf.hdr_cnt > 0 && |
| 7156 | !(i = iov_setup(&(sf.headers), &hbuf, |
| 7157 | headers, sf.hdr_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7158 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7159 | #ifdef __APPLE__ |
| 7160 | sbytes += i; |
| 7161 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | if (trailers != NULL) { |
| 7165 | if (!PySequence_Check(trailers)) { |
| 7166 | PyErr_SetString(PyExc_TypeError, |
| 7167 | "sendfile() trailers must be a sequence or None"); |
| 7168 | return NULL; |
| 7169 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7170 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7171 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7172 | if (sf.trl_cnt > 0 && |
| 7173 | !(i = iov_setup(&(sf.trailers), &tbuf, |
| 7174 | trailers, sf.trl_cnt, PyBUF_SIMPLE))) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7175 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 7176 | #ifdef __APPLE__ |
| 7177 | sbytes += i; |
| 7178 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7179 | } |
| 7180 | } |
| 7181 | |
| 7182 | Py_BEGIN_ALLOW_THREADS |
| 7183 | #ifdef __APPLE__ |
| 7184 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
| 7185 | #else |
| 7186 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
| 7187 | #endif |
| 7188 | Py_END_ALLOW_THREADS |
| 7189 | |
| 7190 | if (sf.headers != NULL) |
| 7191 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 7192 | if (sf.trailers != NULL) |
| 7193 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 7194 | |
| 7195 | if (ret < 0) { |
| 7196 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 7197 | if (sbytes != 0) { |
| 7198 | // some data has been sent |
| 7199 | goto done; |
| 7200 | } |
| 7201 | else { |
| 7202 | // no data has been sent; upper application is supposed |
| 7203 | // to retry on EAGAIN or EBUSY |
| 7204 | return posix_error(); |
| 7205 | } |
| 7206 | } |
| 7207 | return posix_error(); |
| 7208 | } |
| 7209 | goto done; |
| 7210 | |
| 7211 | done: |
| 7212 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 7213 | return Py_BuildValue("l", sbytes); |
| 7214 | #else |
| 7215 | return Py_BuildValue("L", sbytes); |
| 7216 | #endif |
| 7217 | |
| 7218 | #else |
| 7219 | Py_ssize_t count; |
| 7220 | PyObject *offobj; |
| 7221 | static char *keywords[] = {"out", "in", |
| 7222 | "offset", "count", NULL}; |
| 7223 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 7224 | keywords, &out, &in, &offobj, &count)) |
| 7225 | return NULL; |
| 7226 | #ifdef linux |
| 7227 | if (offobj == Py_None) { |
| 7228 | Py_BEGIN_ALLOW_THREADS |
| 7229 | ret = sendfile(out, in, NULL, count); |
| 7230 | Py_END_ALLOW_THREADS |
| 7231 | if (ret < 0) |
| 7232 | return posix_error(); |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 7233 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7234 | } |
| 7235 | #endif |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 7236 | if (!_parse_off_t(offobj, &offset)) |
| 7237 | return NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7238 | Py_BEGIN_ALLOW_THREADS |
| 7239 | ret = sendfile(out, in, &offset, count); |
| 7240 | Py_END_ALLOW_THREADS |
| 7241 | if (ret < 0) |
| 7242 | return posix_error(); |
| 7243 | return Py_BuildValue("n", ret); |
| 7244 | #endif |
| 7245 | } |
| 7246 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7248 | PyDoc_STRVAR(posix_fstat__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7249 | "fstat(fd) -> stat result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7250 | Like stat(), but for an open file descriptor."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7251 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7252 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7253 | posix_fstat(PyObject *self, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7254 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7255 | int fd; |
| 7256 | STRUCT_STAT st; |
| 7257 | int res; |
| 7258 | if (!PyArg_ParseTuple(args, "i:fstat", &fd)) |
| 7259 | return NULL; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7260 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7261 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 7262 | fsync(fd); |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 7263 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7264 | if (!_PyVerify_fd(fd)) |
| 7265 | return posix_error(); |
| 7266 | Py_BEGIN_ALLOW_THREADS |
| 7267 | res = FSTAT(fd, &st); |
| 7268 | Py_END_ALLOW_THREADS |
| 7269 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7270 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | return win32_error("fstat", NULL); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7272 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7273 | return posix_error(); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 7274 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7275 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7276 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7277 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7278 | } |
| 7279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7280 | PyDoc_STRVAR(posix_isatty__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7281 | "isatty(fd) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7282 | 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] | 7283 | connected to the slave end of a terminal."); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7284 | |
| 7285 | static PyObject * |
Thomas Wouters | 616607a | 2000-07-19 14:45:40 +0000 | [diff] [blame] | 7286 | posix_isatty(PyObject *self, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7287 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7288 | int fd; |
| 7289 | if (!PyArg_ParseTuple(args, "i:isatty", &fd)) |
| 7290 | return NULL; |
| 7291 | if (!_PyVerify_fd(fd)) |
| 7292 | return PyBool_FromLong(0); |
| 7293 | return PyBool_FromLong(isatty(fd)); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 7294 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7295 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7296 | #ifdef HAVE_PIPE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7297 | PyDoc_STRVAR(posix_pipe__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7298 | "pipe() -> (read_end, write_end)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7299 | Create a pipe."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7300 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7301 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7302 | posix_pipe(PyObject *self, PyObject *noargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7303 | { |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7304 | #if defined(PYOS_OS2) |
| 7305 | HFILE read, write; |
| 7306 | APIRET rc; |
| 7307 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7308 | rc = DosCreatePipe( &read, &write, 4096); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7309 | if (rc != NO_ERROR) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7310 | return os2_error(rc); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7311 | |
| 7312 | return Py_BuildValue("(ii)", read, write); |
| 7313 | #else |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7314 | #if !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7315 | int fds[2]; |
| 7316 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7317 | res = pipe(fds); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7318 | if (res != 0) |
| 7319 | return posix_error(); |
| 7320 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7321 | #else /* MS_WINDOWS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7322 | HANDLE read, write; |
| 7323 | int read_fd, write_fd; |
| 7324 | BOOL ok; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7325 | ok = CreatePipe(&read, &write, NULL, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7326 | if (!ok) |
| 7327 | return win32_error("CreatePipe", NULL); |
| 7328 | read_fd = _open_osfhandle((Py_intptr_t)read, 0); |
| 7329 | write_fd = _open_osfhandle((Py_intptr_t)write, 1); |
| 7330 | return Py_BuildValue("(ii)", read_fd, write_fd); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7331 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 7332 | #endif |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 7333 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7334 | #endif /* HAVE_PIPE */ |
| 7335 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7336 | #ifdef HAVE_PIPE2 |
| 7337 | PyDoc_STRVAR(posix_pipe2__doc__, |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7338 | "pipe2(flags) -> (read_end, write_end)\n\n\ |
| 7339 | Create a pipe with flags set atomically.\n\ |
| 7340 | flags can be constructed by ORing together one or more of these values:\n\ |
| 7341 | O_NONBLOCK, O_CLOEXEC.\n\ |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7342 | "); |
| 7343 | |
| 7344 | static PyObject * |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7345 | posix_pipe2(PyObject *self, PyObject *arg) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7346 | { |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7347 | int flags; |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7348 | int fds[2]; |
| 7349 | int res; |
| 7350 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 7351 | flags = PyLong_AsLong(arg); |
| 7352 | if (flags == -1 && PyErr_Occurred()) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 7353 | return NULL; |
| 7354 | |
| 7355 | res = pipe2(fds, flags); |
| 7356 | if (res != 0) |
| 7357 | return posix_error(); |
| 7358 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 7359 | } |
| 7360 | #endif /* HAVE_PIPE2 */ |
| 7361 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7362 | #ifdef HAVE_WRITEV |
| 7363 | PyDoc_STRVAR(posix_writev__doc__, |
| 7364 | "writev(fd, buffers) -> byteswritten\n\n\ |
| 7365 | Write the contents of buffers to a file descriptor, where buffers is an\n\ |
| 7366 | arbitrary sequence of buffers.\n\ |
| 7367 | Returns the total bytes written."); |
| 7368 | |
| 7369 | static PyObject * |
| 7370 | posix_writev(PyObject *self, PyObject *args) |
| 7371 | { |
| 7372 | int fd, cnt; |
| 7373 | Py_ssize_t res; |
| 7374 | PyObject *seq; |
| 7375 | struct iovec *iov; |
| 7376 | Py_buffer *buf; |
| 7377 | if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq)) |
| 7378 | return NULL; |
| 7379 | if (!PySequence_Check(seq)) { |
| 7380 | PyErr_SetString(PyExc_TypeError, |
| 7381 | "writev() arg 2 must be a sequence"); |
| 7382 | return NULL; |
| 7383 | } |
| 7384 | cnt = PySequence_Size(seq); |
| 7385 | |
| 7386 | if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) { |
| 7387 | return NULL; |
| 7388 | } |
| 7389 | |
| 7390 | Py_BEGIN_ALLOW_THREADS |
| 7391 | res = writev(fd, iov, cnt); |
| 7392 | Py_END_ALLOW_THREADS |
| 7393 | |
| 7394 | iov_cleanup(iov, buf, cnt); |
| 7395 | return PyLong_FromSsize_t(res); |
| 7396 | } |
| 7397 | #endif |
| 7398 | |
| 7399 | #ifdef HAVE_PWRITE |
| 7400 | PyDoc_STRVAR(posix_pwrite__doc__, |
| 7401 | "pwrite(fd, string, offset) -> byteswritten\n\n\ |
| 7402 | Write string to a file descriptor, fd, from offset, leaving the file\n\ |
| 7403 | offset unchanged."); |
| 7404 | |
| 7405 | static PyObject * |
| 7406 | posix_pwrite(PyObject *self, PyObject *args) |
| 7407 | { |
| 7408 | Py_buffer pbuf; |
| 7409 | int fd; |
| 7410 | off_t offset; |
| 7411 | Py_ssize_t size; |
| 7412 | |
| 7413 | if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset)) |
| 7414 | return NULL; |
| 7415 | |
| 7416 | if (!_PyVerify_fd(fd)) { |
| 7417 | PyBuffer_Release(&pbuf); |
| 7418 | return posix_error(); |
| 7419 | } |
| 7420 | Py_BEGIN_ALLOW_THREADS |
| 7421 | size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset); |
| 7422 | Py_END_ALLOW_THREADS |
| 7423 | PyBuffer_Release(&pbuf); |
| 7424 | if (size < 0) |
| 7425 | return posix_error(); |
| 7426 | return PyLong_FromSsize_t(size); |
| 7427 | } |
| 7428 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7429 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7430 | #ifdef HAVE_MKFIFO |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7431 | PyDoc_STRVAR(posix_mkfifo__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7432 | "mkfifo(filename [, mode=0666])\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7433 | Create a FIFO (a POSIX named pipe)."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7434 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7435 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7436 | posix_mkfifo(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7437 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7438 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | char *filename; |
| 7440 | int mode = 0666; |
| 7441 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7442 | if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath, |
| 7443 | &mode)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7444 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7445 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7446 | Py_BEGIN_ALLOW_THREADS |
| 7447 | res = mkfifo(filename, mode); |
| 7448 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7449 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7450 | if (res < 0) |
| 7451 | return posix_error(); |
| 7452 | Py_INCREF(Py_None); |
| 7453 | return Py_None; |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7454 | } |
| 7455 | #endif |
| 7456 | |
| 7457 | |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 7458 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7459 | PyDoc_STRVAR(posix_mknod__doc__, |
Neal Norwitz | c18b308 | 2002-10-11 22:19:42 +0000 | [diff] [blame] | 7460 | "mknod(filename [, mode=0600, device])\n\n\ |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7461 | Create a filesystem node (file, device special file or named pipe)\n\ |
| 7462 | named filename. mode specifies both the permissions to use and the\n\ |
| 7463 | type of node to be created, being combined (bitwise OR) with one of\n\ |
| 7464 | 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] | 7465 | device defines the newly created device special file (probably using\n\ |
| 7466 | os.makedev()), otherwise it is ignored."); |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 7467 | |
| 7468 | |
| 7469 | static PyObject * |
| 7470 | posix_mknod(PyObject *self, PyObject *args) |
| 7471 | { |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7472 | PyObject *opath; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7473 | char *filename; |
| 7474 | int mode = 0600; |
| 7475 | int device = 0; |
| 7476 | int res; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7477 | if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath, |
| 7478 | &mode, &device)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | return NULL; |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7480 | filename = PyBytes_AS_STRING(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7481 | Py_BEGIN_ALLOW_THREADS |
| 7482 | res = mknod(filename, mode, device); |
| 7483 | Py_END_ALLOW_THREADS |
Benjamin Peterson | d4efbf9 | 2010-08-11 19:20:42 +0000 | [diff] [blame] | 7484 | Py_DECREF(opath); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7485 | if (res < 0) |
| 7486 | return posix_error(); |
| 7487 | Py_INCREF(Py_None); |
| 7488 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7489 | } |
| 7490 | #endif |
| 7491 | |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7492 | #ifdef HAVE_DEVICE_MACROS |
| 7493 | PyDoc_STRVAR(posix_major__doc__, |
| 7494 | "major(device) -> major number\n\ |
| 7495 | Extracts a device major number from a raw device number."); |
| 7496 | |
| 7497 | static PyObject * |
| 7498 | posix_major(PyObject *self, PyObject *args) |
| 7499 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7500 | int device; |
| 7501 | if (!PyArg_ParseTuple(args, "i:major", &device)) |
| 7502 | return NULL; |
| 7503 | return PyLong_FromLong((long)major(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7504 | } |
| 7505 | |
| 7506 | PyDoc_STRVAR(posix_minor__doc__, |
| 7507 | "minor(device) -> minor number\n\ |
| 7508 | Extracts a device minor number from a raw device number."); |
| 7509 | |
| 7510 | static PyObject * |
| 7511 | posix_minor(PyObject *self, PyObject *args) |
| 7512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7513 | int device; |
| 7514 | if (!PyArg_ParseTuple(args, "i:minor", &device)) |
| 7515 | return NULL; |
| 7516 | return PyLong_FromLong((long)minor(device)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | PyDoc_STRVAR(posix_makedev__doc__, |
| 7520 | "makedev(major, minor) -> device number\n\ |
| 7521 | Composes a raw device number from the major and minor device numbers."); |
| 7522 | |
| 7523 | static PyObject * |
| 7524 | posix_makedev(PyObject *self, PyObject *args) |
| 7525 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7526 | int major, minor; |
| 7527 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) |
| 7528 | return NULL; |
| 7529 | return PyLong_FromLong((long)makedev(major, minor)); |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 7530 | } |
| 7531 | #endif /* device macros */ |
| 7532 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7533 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7534 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7535 | PyDoc_STRVAR(posix_ftruncate__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7536 | "ftruncate(fd, length)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7537 | Truncate a file to a specified length."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7538 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7539 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7540 | posix_ftruncate(PyObject *self, PyObject *args) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7541 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7542 | int fd; |
| 7543 | off_t length; |
| 7544 | int res; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7545 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7546 | if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7547 | return NULL; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7548 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | Py_BEGIN_ALLOW_THREADS |
| 7550 | res = ftruncate(fd, length); |
| 7551 | Py_END_ALLOW_THREADS |
| 7552 | if (res < 0) |
| 7553 | return posix_error(); |
| 7554 | Py_INCREF(Py_None); |
| 7555 | return Py_None; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 7556 | } |
| 7557 | #endif |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 7558 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7559 | #ifdef HAVE_TRUNCATE |
| 7560 | PyDoc_STRVAR(posix_truncate__doc__, |
| 7561 | "truncate(path, length)\n\n\ |
| 7562 | Truncate the file given by path to length bytes."); |
| 7563 | |
| 7564 | static PyObject * |
| 7565 | posix_truncate(PyObject *self, PyObject *args) |
| 7566 | { |
| 7567 | PyObject *opath; |
| 7568 | const char *path; |
| 7569 | off_t length; |
| 7570 | int res; |
| 7571 | |
| 7572 | if (!PyArg_ParseTuple(args, "O&O&:truncate", |
| 7573 | PyUnicode_FSConverter, &opath, _parse_off_t, &length)) |
| 7574 | return NULL; |
| 7575 | path = PyBytes_AsString(opath); |
| 7576 | |
| 7577 | Py_BEGIN_ALLOW_THREADS |
| 7578 | res = truncate(path, length); |
| 7579 | Py_END_ALLOW_THREADS |
| 7580 | Py_DECREF(opath); |
| 7581 | if (res < 0) |
| 7582 | return posix_error(); |
| 7583 | Py_RETURN_NONE; |
| 7584 | } |
| 7585 | #endif |
| 7586 | |
| 7587 | #ifdef HAVE_POSIX_FALLOCATE |
| 7588 | PyDoc_STRVAR(posix_posix_fallocate__doc__, |
| 7589 | "posix_fallocate(fd, offset, len)\n\n\ |
| 7590 | Ensures that enough disk space is allocated for the file specified by fd\n\ |
| 7591 | starting from offset and continuing for len bytes."); |
| 7592 | |
| 7593 | static PyObject * |
| 7594 | posix_posix_fallocate(PyObject *self, PyObject *args) |
| 7595 | { |
| 7596 | off_t len, offset; |
| 7597 | int res, fd; |
| 7598 | |
| 7599 | if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate", |
| 7600 | &fd, _parse_off_t, &offset, _parse_off_t, &len)) |
| 7601 | return NULL; |
| 7602 | |
| 7603 | Py_BEGIN_ALLOW_THREADS |
| 7604 | res = posix_fallocate(fd, offset, len); |
| 7605 | Py_END_ALLOW_THREADS |
| 7606 | if (res != 0) { |
| 7607 | errno = res; |
| 7608 | return posix_error(); |
| 7609 | } |
| 7610 | Py_RETURN_NONE; |
| 7611 | } |
| 7612 | #endif |
| 7613 | |
| 7614 | #ifdef HAVE_POSIX_FADVISE |
| 7615 | PyDoc_STRVAR(posix_posix_fadvise__doc__, |
| 7616 | "posix_fadvise(fd, offset, len, advice)\n\n\ |
| 7617 | Announces an intention to access data in a specific pattern thus allowing\n\ |
| 7618 | the kernel to make optimizations.\n\ |
| 7619 | The advice applies to the region of the file specified by fd starting at\n\ |
| 7620 | offset and continuing for len bytes.\n\ |
| 7621 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\ |
| 7622 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\ |
| 7623 | POSIX_FADV_DONTNEED."); |
| 7624 | |
| 7625 | static PyObject * |
| 7626 | posix_posix_fadvise(PyObject *self, PyObject *args) |
| 7627 | { |
| 7628 | off_t len, offset; |
| 7629 | int res, fd, advice; |
| 7630 | |
| 7631 | if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise", |
| 7632 | &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice)) |
| 7633 | return NULL; |
| 7634 | |
| 7635 | Py_BEGIN_ALLOW_THREADS |
| 7636 | res = posix_fadvise(fd, offset, len, advice); |
| 7637 | Py_END_ALLOW_THREADS |
| 7638 | if (res != 0) { |
| 7639 | errno = res; |
| 7640 | return posix_error(); |
| 7641 | } |
| 7642 | Py_RETURN_NONE; |
| 7643 | } |
| 7644 | #endif |
| 7645 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7646 | #ifdef HAVE_PUTENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7647 | PyDoc_STRVAR(posix_putenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7648 | "putenv(key, value)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7649 | Change or add an environment variable."); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7650 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 7651 | /* Save putenv() parameters as values here, so we can collect them when they |
| 7652 | * get re-set with another call for the same key. */ |
| 7653 | static PyObject *posix_putenv_garbage; |
| 7654 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7655 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7656 | posix_putenv(PyObject *self, PyObject *args) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7657 | { |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7658 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7659 | wchar_t *s1, *s2; |
| 7660 | wchar_t *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7661 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | PyObject *os1, *os2; |
| 7663 | char *s1, *s2; |
| 7664 | char *newenv; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7665 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7666 | PyObject *newstr = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7667 | size_t len; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7668 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7669 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7670 | if (!PyArg_ParseTuple(args, |
| 7671 | "uu:putenv", |
| 7672 | &s1, &s2)) |
| 7673 | return NULL; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7674 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7675 | if (!PyArg_ParseTuple(args, |
| 7676 | "O&O&:putenv", |
| 7677 | PyUnicode_FSConverter, &os1, |
| 7678 | PyUnicode_FSConverter, &os2)) |
| 7679 | return NULL; |
| 7680 | s1 = PyBytes_AsString(os1); |
| 7681 | s2 = PyBytes_AsString(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7682 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7683 | |
| 7684 | #if defined(PYOS_OS2) |
| 7685 | if (stricmp(s1, "BEGINLIBPATH") == 0) { |
| 7686 | APIRET rc; |
| 7687 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7688 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7689 | if (rc != NO_ERROR) { |
| 7690 | os2_error(rc); |
| 7691 | goto error; |
| 7692 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7693 | |
| 7694 | } else if (stricmp(s1, "ENDLIBPATH") == 0) { |
| 7695 | APIRET rc; |
| 7696 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7697 | rc = DosSetExtLIBPATH(s2, END_LIBPATH); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7698 | if (rc != NO_ERROR) { |
| 7699 | os2_error(rc); |
| 7700 | goto error; |
| 7701 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7702 | } else { |
| 7703 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7704 | /* XXX This can leak memory -- not easy to fix :-( */ |
| 7705 | /* len includes space for a trailing \0; the size arg to |
| 7706 | PyBytes_FromStringAndSize does not count that */ |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7707 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7708 | len = wcslen(s1) + wcslen(s2) + 2; |
| 7709 | newstr = PyUnicode_FromUnicode(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7710 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7711 | len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7712 | newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1); |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7713 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7714 | if (newstr == NULL) { |
| 7715 | PyErr_NoMemory(); |
| 7716 | goto error; |
| 7717 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7718 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7719 | newenv = PyUnicode_AsUnicode(newstr); |
| 7720 | _snwprintf(newenv, len, L"%s=%s", s1, s2); |
| 7721 | if (_wputenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7722 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7723 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7724 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7725 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7726 | newenv = PyBytes_AS_STRING(newstr); |
| 7727 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2); |
| 7728 | if (putenv(newenv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7729 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7730 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7731 | } |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 7732 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7733 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7734 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 7735 | * this will cause previous value to be collected. This has to |
| 7736 | * happen after the real putenv() call because the old value |
| 7737 | * was still accessible until then. */ |
| 7738 | if (PyDict_SetItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7739 | #ifdef MS_WINDOWS |
| 7740 | PyTuple_GET_ITEM(args, 0), |
| 7741 | #else |
| 7742 | os1, |
| 7743 | #endif |
| 7744 | newstr)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7745 | /* really not much we can do; just leak */ |
| 7746 | PyErr_Clear(); |
| 7747 | } |
| 7748 | else { |
| 7749 | Py_DECREF(newstr); |
| 7750 | } |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 7751 | |
| 7752 | #if defined(PYOS_OS2) |
| 7753 | } |
| 7754 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7755 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7756 | #ifndef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | Py_DECREF(os1); |
| 7758 | Py_DECREF(os2); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7759 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7760 | Py_RETURN_NONE; |
| 7761 | |
| 7762 | error: |
| 7763 | #ifndef MS_WINDOWS |
| 7764 | Py_DECREF(os1); |
| 7765 | Py_DECREF(os2); |
| 7766 | #endif |
| 7767 | Py_XDECREF(newstr); |
| 7768 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7769 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7770 | #endif /* putenv */ |
| 7771 | |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7772 | #ifdef HAVE_UNSETENV |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7773 | PyDoc_STRVAR(posix_unsetenv__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7774 | "unsetenv(key)\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7775 | Delete an environment variable."); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7776 | |
| 7777 | static PyObject * |
| 7778 | posix_unsetenv(PyObject *self, PyObject *args) |
| 7779 | { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7780 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | char *s1; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7782 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) |
| 7784 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7785 | #else |
| 7786 | PyObject *os1; |
| 7787 | char *s1; |
| 7788 | |
| 7789 | if (!PyArg_ParseTuple(args, "O&:unsetenv", |
| 7790 | PyUnicode_FSConverter, &os1)) |
| 7791 | return NULL; |
| 7792 | s1 = PyBytes_AsString(os1); |
| 7793 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7795 | unsetenv(s1); |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7796 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7797 | /* Remove the key from posix_putenv_garbage; |
| 7798 | * this will cause it to be collected. This has to |
| 7799 | * happen after the real unsetenv() call because the |
| 7800 | * old value was still accessible until then. |
| 7801 | */ |
| 7802 | if (PyDict_DelItem(posix_putenv_garbage, |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7803 | #ifdef MS_WINDOWS |
| 7804 | PyTuple_GET_ITEM(args, 0) |
| 7805 | #else |
| 7806 | os1 |
| 7807 | #endif |
| 7808 | )) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7809 | /* really not much we can do; just leak */ |
| 7810 | PyErr_Clear(); |
| 7811 | } |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7812 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 7813 | #ifndef MS_WINDOWS |
| 7814 | Py_DECREF(os1); |
| 7815 | #endif |
| 7816 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 7817 | } |
| 7818 | #endif /* unsetenv */ |
| 7819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7820 | PyDoc_STRVAR(posix_strerror__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7821 | "strerror(code) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7822 | Translate an error code to a message string."); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7823 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 7824 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7825 | posix_strerror(PyObject *self, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7826 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | int code; |
| 7828 | char *message; |
| 7829 | if (!PyArg_ParseTuple(args, "i:strerror", &code)) |
| 7830 | return NULL; |
| 7831 | message = strerror(code); |
| 7832 | if (message == NULL) { |
| 7833 | PyErr_SetString(PyExc_ValueError, |
| 7834 | "strerror() argument out of range"); |
| 7835 | return NULL; |
| 7836 | } |
| 7837 | return PyUnicode_FromString(message); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7838 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 7839 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 7840 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7841 | #ifdef HAVE_SYS_WAIT_H |
| 7842 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7843 | #ifdef WCOREDUMP |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7844 | PyDoc_STRVAR(posix_WCOREDUMP__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7845 | "WCOREDUMP(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7846 | 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] | 7847 | |
| 7848 | static PyObject * |
| 7849 | posix_WCOREDUMP(PyObject *self, PyObject *args) |
| 7850 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7851 | WAIT_TYPE status; |
| 7852 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7853 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7854 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) |
| 7855 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | return PyBool_FromLong(WCOREDUMP(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7858 | } |
| 7859 | #endif /* WCOREDUMP */ |
| 7860 | |
| 7861 | #ifdef WIFCONTINUED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7862 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7863 | "WIFCONTINUED(status) -> bool\n\n\ |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7864 | 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] | 7865 | job control stop."); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7866 | |
| 7867 | static PyObject * |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 7868 | posix_WIFCONTINUED(PyObject *self, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7869 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7870 | WAIT_TYPE status; |
| 7871 | WAIT_STATUS_INT(status) = 0; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7872 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7873 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) |
| 7874 | return NULL; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7875 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7876 | return PyBool_FromLong(WIFCONTINUED(status)); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 7877 | } |
| 7878 | #endif /* WIFCONTINUED */ |
| 7879 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7880 | #ifdef WIFSTOPPED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7881 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7882 | "WIFSTOPPED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7883 | Return True if the process returning 'status' was stopped."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7884 | |
| 7885 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7886 | posix_WIFSTOPPED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7887 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7888 | WAIT_TYPE status; |
| 7889 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7890 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7891 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) |
| 7892 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7894 | return PyBool_FromLong(WIFSTOPPED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7895 | } |
| 7896 | #endif /* WIFSTOPPED */ |
| 7897 | |
| 7898 | #ifdef WIFSIGNALED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7899 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7900 | "WIFSIGNALED(status) -> bool\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7901 | 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] | 7902 | |
| 7903 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7904 | posix_WIFSIGNALED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7905 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7906 | WAIT_TYPE status; |
| 7907 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7908 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7909 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) |
| 7910 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7912 | return PyBool_FromLong(WIFSIGNALED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7913 | } |
| 7914 | #endif /* WIFSIGNALED */ |
| 7915 | |
| 7916 | #ifdef WIFEXITED |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7917 | PyDoc_STRVAR(posix_WIFEXITED__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7918 | "WIFEXITED(status) -> bool\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7919 | 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] | 7920 | system call."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7921 | |
| 7922 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7923 | posix_WIFEXITED(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7924 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7925 | WAIT_TYPE status; |
| 7926 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7928 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) |
| 7929 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7930 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7931 | return PyBool_FromLong(WIFEXITED(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7932 | } |
| 7933 | #endif /* WIFEXITED */ |
| 7934 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7935 | #ifdef WEXITSTATUS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7936 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7937 | "WEXITSTATUS(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7938 | Return the process return code from 'status'."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7939 | |
| 7940 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7941 | posix_WEXITSTATUS(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7942 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7943 | WAIT_TYPE status; |
| 7944 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7945 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7946 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) |
| 7947 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7949 | return Py_BuildValue("i", WEXITSTATUS(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7950 | } |
| 7951 | #endif /* WEXITSTATUS */ |
| 7952 | |
| 7953 | #ifdef WTERMSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7954 | PyDoc_STRVAR(posix_WTERMSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7955 | "WTERMSIG(status) -> integer\n\n\ |
Fred Drake | 7e3535c | 1999-02-02 16:37:11 +0000 | [diff] [blame] | 7956 | 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] | 7957 | value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7958 | |
| 7959 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7960 | posix_WTERMSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7962 | WAIT_TYPE status; |
| 7963 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7965 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) |
| 7966 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7967 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7968 | return Py_BuildValue("i", WTERMSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7969 | } |
| 7970 | #endif /* WTERMSIG */ |
| 7971 | |
| 7972 | #ifdef WSTOPSIG |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7973 | PyDoc_STRVAR(posix_WSTOPSIG__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 7974 | "WSTOPSIG(status) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7975 | Return the signal that stopped the process that provided\n\ |
| 7976 | the 'status' value."); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7977 | |
| 7978 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 7979 | posix_WSTOPSIG(PyObject *self, PyObject *args) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7980 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7981 | WAIT_TYPE status; |
| 7982 | WAIT_STATUS_INT(status) = 0; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7983 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7984 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) |
| 7985 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7986 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7987 | return Py_BuildValue("i", WSTOPSIG(status)); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 7988 | } |
| 7989 | #endif /* WSTOPSIG */ |
| 7990 | |
| 7991 | #endif /* HAVE_SYS_WAIT_H */ |
| 7992 | |
| 7993 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7994 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 7995 | #ifdef _SCO_DS |
| 7996 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 7997 | needed definitions in sys/statvfs.h */ |
| 7998 | #define _SVID3 |
| 7999 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8000 | #include <sys/statvfs.h> |
| 8001 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8002 | static PyObject* |
| 8003 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8004 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 8005 | if (v == NULL) |
| 8006 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8007 | |
| 8008 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8009 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8010 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8011 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 8012 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 8013 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 8014 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 8015 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 8016 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 8017 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8018 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8019 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8020 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 8021 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 8022 | PyStructSequence_SET_ITEM(v, 2, |
| 8023 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 8024 | PyStructSequence_SET_ITEM(v, 3, |
| 8025 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 8026 | PyStructSequence_SET_ITEM(v, 4, |
| 8027 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 8028 | PyStructSequence_SET_ITEM(v, 5, |
| 8029 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 8030 | PyStructSequence_SET_ITEM(v, 6, |
| 8031 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 8032 | PyStructSequence_SET_ITEM(v, 7, |
| 8033 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 8034 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 8035 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8036 | #endif |
| 8037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8038 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8039 | } |
| 8040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8041 | PyDoc_STRVAR(posix_fstatvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8042 | "fstatvfs(fd) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8043 | Perform an fstatvfs system call on the given fd."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8044 | |
| 8045 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8046 | posix_fstatvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8047 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8048 | int fd, res; |
| 8049 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8050 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8051 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) |
| 8052 | return NULL; |
| 8053 | Py_BEGIN_ALLOW_THREADS |
| 8054 | res = fstatvfs(fd, &st); |
| 8055 | Py_END_ALLOW_THREADS |
| 8056 | if (res != 0) |
| 8057 | return posix_error(); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8058 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8059 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8060 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8061 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8062 | |
| 8063 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8064 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8065 | #include <sys/statvfs.h> |
| 8066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8067 | PyDoc_STRVAR(posix_statvfs__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8068 | "statvfs(path) -> statvfs result\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8069 | Perform a statvfs system call on the given path."); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8070 | |
| 8071 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8072 | posix_statvfs(PyObject *self, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8073 | { |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8074 | PyObject *path; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8075 | int res; |
| 8076 | struct statvfs st; |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8077 | if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8078 | return NULL; |
| 8079 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8080 | res = statvfs(PyBytes_AS_STRING(path), &st); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8081 | Py_END_ALLOW_THREADS |
Victor Stinner | 6fa6777 | 2011-09-20 04:04:33 +0200 | [diff] [blame] | 8082 | if (res != 0) { |
| 8083 | posix_error_with_filename(PyBytes_AS_STRING(path)); |
| 8084 | Py_DECREF(path); |
| 8085 | return NULL; |
| 8086 | } |
| 8087 | Py_DECREF(path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 8088 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8089 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8090 | } |
| 8091 | #endif /* HAVE_STATVFS */ |
| 8092 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 8093 | #ifdef MS_WINDOWS |
| 8094 | PyDoc_STRVAR(win32__getdiskusage__doc__, |
| 8095 | "_getdiskusage(path) -> (total, free)\n\n\ |
| 8096 | Return disk usage statistics about the given path as (total, free) tuple."); |
| 8097 | |
| 8098 | static PyObject * |
| 8099 | win32__getdiskusage(PyObject *self, PyObject *args) |
| 8100 | { |
| 8101 | BOOL retval; |
| 8102 | ULARGE_INTEGER _, total, free; |
| 8103 | LPCTSTR path; |
| 8104 | |
| 8105 | if (! PyArg_ParseTuple(args, "s", &path)) |
| 8106 | return NULL; |
| 8107 | |
| 8108 | Py_BEGIN_ALLOW_THREADS |
| 8109 | retval = GetDiskFreeSpaceEx(path, &_, &total, &free); |
| 8110 | Py_END_ALLOW_THREADS |
| 8111 | if (retval == 0) |
| 8112 | return PyErr_SetFromWindowsErr(0); |
| 8113 | |
| 8114 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 8115 | } |
| 8116 | #endif |
| 8117 | |
| 8118 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8119 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 8120 | * It maps strings representing configuration variable names to |
| 8121 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 8122 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8123 | * rarely-used constants. There are three separate tables that use |
| 8124 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 8125 | * |
| 8126 | * This code is always included, even if none of the interfaces that |
| 8127 | * need it are included. The #if hackery needed to avoid it would be |
| 8128 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8129 | */ |
| 8130 | struct constdef { |
| 8131 | char *name; |
| 8132 | long value; |
| 8133 | }; |
| 8134 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8135 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8136 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 8137 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8138 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8139 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8140 | *valuep = PyLong_AS_LONG(arg); |
| 8141 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8142 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 8143 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8144 | /* look up the value in the table using a binary search */ |
| 8145 | size_t lo = 0; |
| 8146 | size_t mid; |
| 8147 | size_t hi = tablesize; |
| 8148 | int cmp; |
| 8149 | const char *confname; |
| 8150 | if (!PyUnicode_Check(arg)) { |
| 8151 | PyErr_SetString(PyExc_TypeError, |
| 8152 | "configuration names must be strings or integers"); |
| 8153 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8154 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8155 | confname = _PyUnicode_AsString(arg); |
| 8156 | if (confname == NULL) |
| 8157 | return 0; |
| 8158 | while (lo < hi) { |
| 8159 | mid = (lo + hi) / 2; |
| 8160 | cmp = strcmp(confname, table[mid].name); |
| 8161 | if (cmp < 0) |
| 8162 | hi = mid; |
| 8163 | else if (cmp > 0) |
| 8164 | lo = mid + 1; |
| 8165 | else { |
| 8166 | *valuep = table[mid].value; |
| 8167 | return 1; |
| 8168 | } |
| 8169 | } |
| 8170 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 8171 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8172 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8173 | } |
| 8174 | |
| 8175 | |
| 8176 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 8177 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8178 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8179 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8180 | #endif |
| 8181 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8182 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8183 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8184 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8185 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8186 | #endif |
| 8187 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8188 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8189 | #endif |
| 8190 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8191 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8192 | #endif |
| 8193 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8194 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8195 | #endif |
| 8196 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8197 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8198 | #endif |
| 8199 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8200 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8201 | #endif |
| 8202 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8203 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8204 | #endif |
| 8205 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8206 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8207 | #endif |
| 8208 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8209 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8210 | #endif |
| 8211 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8212 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8213 | #endif |
| 8214 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8215 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8216 | #endif |
| 8217 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8218 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8219 | #endif |
| 8220 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8221 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8222 | #endif |
| 8223 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8224 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8225 | #endif |
| 8226 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8227 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8228 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 8229 | #ifdef _PC_ACL_ENABLED |
| 8230 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 8231 | #endif |
| 8232 | #ifdef _PC_MIN_HOLE_SIZE |
| 8233 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 8234 | #endif |
| 8235 | #ifdef _PC_ALLOC_SIZE_MIN |
| 8236 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 8237 | #endif |
| 8238 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 8239 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 8240 | #endif |
| 8241 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 8242 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 8243 | #endif |
| 8244 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 8245 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 8246 | #endif |
| 8247 | #ifdef _PC_REC_XFER_ALIGN |
| 8248 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 8249 | #endif |
| 8250 | #ifdef _PC_SYMLINK_MAX |
| 8251 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 8252 | #endif |
| 8253 | #ifdef _PC_XATTR_ENABLED |
| 8254 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 8255 | #endif |
| 8256 | #ifdef _PC_XATTR_EXISTS |
| 8257 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 8258 | #endif |
| 8259 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 8260 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 8261 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8262 | }; |
| 8263 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8264 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8265 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8266 | { |
| 8267 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 8268 | sizeof(posix_constants_pathconf) |
| 8269 | / sizeof(struct constdef)); |
| 8270 | } |
| 8271 | #endif |
| 8272 | |
| 8273 | #ifdef HAVE_FPATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8274 | PyDoc_STRVAR(posix_fpathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8275 | "fpathconf(fd, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8276 | 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] | 8277 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8278 | |
| 8279 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8280 | posix_fpathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8281 | { |
| 8282 | PyObject *result = NULL; |
| 8283 | int name, fd; |
| 8284 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8285 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd, |
| 8286 | conv_path_confname, &name)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8287 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8288 | |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8289 | errno = 0; |
| 8290 | limit = fpathconf(fd, name); |
| 8291 | if (limit == -1 && errno != 0) |
| 8292 | posix_error(); |
| 8293 | else |
| 8294 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8295 | } |
| 8296 | return result; |
| 8297 | } |
| 8298 | #endif |
| 8299 | |
| 8300 | |
| 8301 | #ifdef HAVE_PATHCONF |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8302 | PyDoc_STRVAR(posix_pathconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8303 | "pathconf(path, name) -> integer\n\n\ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8304 | 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] | 8305 | If there is no limit, return -1."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8306 | |
| 8307 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8308 | posix_pathconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8309 | { |
| 8310 | PyObject *result = NULL; |
| 8311 | int name; |
| 8312 | char *path; |
| 8313 | |
| 8314 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path, |
| 8315 | conv_path_confname, &name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8316 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8317 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8318 | errno = 0; |
| 8319 | limit = pathconf(path, name); |
| 8320 | if (limit == -1 && errno != 0) { |
| 8321 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8322 | /* could be a path or name problem */ |
| 8323 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8324 | else |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8325 | posix_error_with_filename(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | } |
| 8327 | else |
| 8328 | result = PyLong_FromLong(limit); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8329 | } |
| 8330 | return result; |
| 8331 | } |
| 8332 | #endif |
| 8333 | |
| 8334 | #ifdef HAVE_CONFSTR |
| 8335 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8336 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8337 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8338 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8339 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8340 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8341 | #endif |
| 8342 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8343 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 8344 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8345 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8346 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8347 | #endif |
| 8348 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8349 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8350 | #endif |
| 8351 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8352 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8353 | #endif |
| 8354 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8355 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8356 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8357 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8358 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8359 | #endif |
| 8360 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8361 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8362 | #endif |
| 8363 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8364 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8365 | #endif |
| 8366 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8367 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8368 | #endif |
| 8369 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8370 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8371 | #endif |
| 8372 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8373 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8374 | #endif |
| 8375 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8376 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8377 | #endif |
| 8378 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8379 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8380 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8381 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8382 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8383 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8384 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8385 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8386 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8387 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8388 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8389 | #endif |
| 8390 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8391 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8392 | #endif |
| 8393 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8394 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8395 | #endif |
| 8396 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8397 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8398 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8399 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8400 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8401 | #endif |
| 8402 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8403 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8404 | #endif |
| 8405 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8406 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8407 | #endif |
| 8408 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8409 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8410 | #endif |
| 8411 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8413 | #endif |
| 8414 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8415 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8416 | #endif |
| 8417 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8418 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8419 | #endif |
| 8420 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8421 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8422 | #endif |
| 8423 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8424 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8425 | #endif |
| 8426 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8427 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8428 | #endif |
| 8429 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8431 | #endif |
| 8432 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8433 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8434 | #endif |
| 8435 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8436 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8437 | #endif |
| 8438 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8440 | #endif |
| 8441 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8442 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8443 | #endif |
| 8444 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8445 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8446 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8447 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8448 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8449 | #endif |
| 8450 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8451 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8452 | #endif |
| 8453 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8454 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8455 | #endif |
| 8456 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8457 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8458 | #endif |
| 8459 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8460 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8461 | #endif |
| 8462 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8463 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8464 | #endif |
| 8465 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8467 | #endif |
| 8468 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8469 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8470 | #endif |
| 8471 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8473 | #endif |
| 8474 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8475 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8476 | #endif |
| 8477 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8478 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8479 | #endif |
| 8480 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8482 | #endif |
| 8483 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8484 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8485 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8486 | }; |
| 8487 | |
| 8488 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8489 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8490 | { |
| 8491 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 8492 | sizeof(posix_constants_confstr) |
| 8493 | / sizeof(struct constdef)); |
| 8494 | } |
| 8495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8496 | PyDoc_STRVAR(posix_confstr__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 8497 | "confstr(name) -> string\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8498 | Return a string-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8499 | |
| 8500 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 8501 | posix_confstr(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8502 | { |
| 8503 | PyObject *result = NULL; |
| 8504 | int name; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8505 | char buffer[255]; |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 8506 | int len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8507 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8508 | if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) |
| 8509 | return NULL; |
| 8510 | |
| 8511 | errno = 0; |
| 8512 | len = confstr(name, buffer, sizeof(buffer)); |
| 8513 | if (len == 0) { |
| 8514 | if (errno) { |
| 8515 | posix_error(); |
| 8516 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8517 | } |
| 8518 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8519 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8520 | } |
| 8521 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 8522 | |
| 8523 | if ((unsigned int)len >= sizeof(buffer)) { |
| 8524 | char *buf = PyMem_Malloc(len); |
| 8525 | if (buf == NULL) |
| 8526 | return PyErr_NoMemory(); |
| 8527 | confstr(name, buf, len); |
| 8528 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 8529 | PyMem_Free(buf); |
| 8530 | } |
| 8531 | else |
| 8532 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8533 | return result; |
| 8534 | } |
| 8535 | #endif |
| 8536 | |
| 8537 | |
| 8538 | #ifdef HAVE_SYSCONF |
| 8539 | static struct constdef posix_constants_sysconf[] = { |
| 8540 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8541 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8542 | #endif |
| 8543 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8545 | #endif |
| 8546 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8547 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8548 | #endif |
| 8549 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8550 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8551 | #endif |
| 8552 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8553 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8554 | #endif |
| 8555 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8556 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8557 | #endif |
| 8558 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8559 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8560 | #endif |
| 8561 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8563 | #endif |
| 8564 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8565 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8566 | #endif |
| 8567 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8568 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8569 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8570 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8571 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8572 | #endif |
| 8573 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8574 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8575 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8576 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8578 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8579 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8581 | #endif |
| 8582 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8583 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8584 | #endif |
| 8585 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8586 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8587 | #endif |
| 8588 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8589 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8590 | #endif |
| 8591 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8592 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8593 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8594 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8595 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8596 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8597 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8598 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8599 | #endif |
| 8600 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8601 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8602 | #endif |
| 8603 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8604 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8605 | #endif |
| 8606 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8607 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8611 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8612 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8613 | {"SC_CAP", _SC_CAP}, |
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_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8616 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8617 | #endif |
| 8618 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8619 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8620 | #endif |
| 8621 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8622 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8623 | #endif |
| 8624 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8625 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8626 | #endif |
| 8627 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8629 | #endif |
| 8630 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8632 | #endif |
| 8633 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8634 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8635 | #endif |
| 8636 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8638 | #endif |
| 8639 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8640 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8641 | #endif |
| 8642 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8643 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8644 | #endif |
| 8645 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8646 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8647 | #endif |
| 8648 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8649 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8650 | #endif |
| 8651 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8653 | #endif |
| 8654 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8655 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8656 | #endif |
| 8657 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8659 | #endif |
| 8660 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8661 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8662 | #endif |
| 8663 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8664 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8665 | #endif |
| 8666 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8667 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8668 | #endif |
| 8669 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8670 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8671 | #endif |
| 8672 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8673 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8674 | #endif |
| 8675 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8676 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8679 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8682 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8683 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8684 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8685 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8686 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8687 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8689 | #endif |
| 8690 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8691 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8692 | #endif |
| 8693 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8694 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8695 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8696 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8697 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8698 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8699 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8700 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8701 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8702 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8703 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8704 | #endif |
| 8705 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8706 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8707 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8708 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8709 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8710 | #endif |
| 8711 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8716 | #endif |
| 8717 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8718 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8719 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8720 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8721 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8722 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8723 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8724 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8725 | #endif |
| 8726 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8727 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8728 | #endif |
| 8729 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8734 | #endif |
| 8735 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8736 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8737 | #endif |
| 8738 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8739 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8740 | #endif |
| 8741 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8742 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8743 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8744 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8745 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
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_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8748 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8749 | #endif |
| 8750 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8751 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8752 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8753 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8754 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8755 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8756 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8758 | #endif |
| 8759 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8761 | #endif |
| 8762 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8764 | #endif |
| 8765 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8767 | #endif |
| 8768 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8770 | #endif |
| 8771 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8772 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8773 | #endif |
| 8774 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8775 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8776 | #endif |
| 8777 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8778 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8779 | #endif |
| 8780 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8781 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
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_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8785 | #endif |
| 8786 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8788 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8789 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8791 | #endif |
| 8792 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8793 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8794 | #endif |
| 8795 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8797 | #endif |
| 8798 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8799 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8800 | #endif |
| 8801 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8803 | #endif |
| 8804 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8805 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8806 | #endif |
| 8807 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8809 | #endif |
| 8810 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8811 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8812 | #endif |
| 8813 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8814 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8815 | #endif |
| 8816 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8818 | #endif |
| 8819 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8820 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8821 | #endif |
| 8822 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8824 | #endif |
| 8825 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8826 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8830 | #endif |
| 8831 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8832 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8833 | #endif |
| 8834 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8836 | #endif |
| 8837 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8841 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8842 | #endif |
| 8843 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8844 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8845 | #endif |
| 8846 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8847 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8848 | #endif |
| 8849 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8850 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8851 | #endif |
| 8852 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8853 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8854 | #endif |
| 8855 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8857 | #endif |
| 8858 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8859 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8862 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8865 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8866 | #endif |
| 8867 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8869 | #endif |
| 8870 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8871 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8872 | #endif |
| 8873 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8874 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8875 | #endif |
| 8876 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8877 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8878 | #endif |
| 8879 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8880 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8881 | #endif |
| 8882 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8883 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8884 | #endif |
| 8885 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8886 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8887 | #endif |
| 8888 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8889 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8890 | #endif |
| 8891 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8892 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8893 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8894 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8895 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 8896 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8897 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8898 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8899 | #endif |
| 8900 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8901 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8902 | #endif |
| 8903 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8904 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8905 | #endif |
| 8906 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8907 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8908 | #endif |
| 8909 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8911 | #endif |
| 8912 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8913 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8914 | #endif |
| 8915 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8916 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8917 | #endif |
| 8918 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8919 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8920 | #endif |
| 8921 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8922 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8923 | #endif |
| 8924 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8925 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8928 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8929 | #endif |
| 8930 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8932 | #endif |
| 8933 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8934 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8935 | #endif |
| 8936 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8937 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8938 | #endif |
| 8939 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8941 | #endif |
| 8942 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8947 | #endif |
| 8948 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8950 | #endif |
| 8951 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8953 | #endif |
| 8954 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8955 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8956 | #endif |
| 8957 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8958 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8959 | #endif |
| 8960 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8961 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8962 | #endif |
| 8963 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8964 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8965 | #endif |
| 8966 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8971 | #endif |
| 8972 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8973 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8974 | #endif |
| 8975 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8977 | #endif |
| 8978 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8980 | #endif |
| 8981 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8983 | #endif |
| 8984 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8985 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8986 | #endif |
| 8987 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8991 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8992 | #endif |
| 8993 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8994 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8995 | #endif |
| 8996 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8998 | #endif |
| 8999 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9001 | #endif |
| 9002 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9006 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9007 | #endif |
| 9008 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9009 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9012 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9013 | #endif |
| 9014 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9016 | #endif |
| 9017 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9018 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9019 | #endif |
| 9020 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9022 | #endif |
| 9023 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9027 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9030 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9031 | #endif |
| 9032 | }; |
| 9033 | |
| 9034 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9035 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9036 | { |
| 9037 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 9038 | sizeof(posix_constants_sysconf) |
| 9039 | / sizeof(struct constdef)); |
| 9040 | } |
| 9041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9042 | PyDoc_STRVAR(posix_sysconf__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9043 | "sysconf(name) -> integer\n\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9044 | Return an integer-valued system configuration variable."); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9045 | |
| 9046 | static PyObject * |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9047 | posix_sysconf(PyObject *self, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9048 | { |
| 9049 | PyObject *result = NULL; |
| 9050 | int name; |
| 9051 | |
| 9052 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) { |
| 9053 | int value; |
| 9054 | |
| 9055 | errno = 0; |
| 9056 | value = sysconf(name); |
| 9057 | if (value == -1 && errno != 0) |
| 9058 | posix_error(); |
| 9059 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9060 | result = PyLong_FromLong(value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 9061 | } |
| 9062 | return result; |
| 9063 | } |
| 9064 | #endif |
| 9065 | |
| 9066 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9067 | /* This code is used to ensure that the tables of configuration value names |
| 9068 | * are in sorted order as required by conv_confname(), and also to build the |
| 9069 | * the exported dictionaries that are used to publish information about the |
| 9070 | * names available on the host platform. |
| 9071 | * |
| 9072 | * Sorting the table at runtime ensures that the table is properly ordered |
| 9073 | * when used, even for platforms we're not able to test on. It also makes |
| 9074 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9075 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9076 | |
| 9077 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9078 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9079 | { |
| 9080 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9081 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9082 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9083 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9084 | |
| 9085 | return strcmp(c1->name, c2->name); |
| 9086 | } |
| 9087 | |
| 9088 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 9089 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9090 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9091 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9092 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9093 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9094 | |
| 9095 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 9096 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9097 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9098 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9099 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 9100 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9101 | PyObject *o = PyLong_FromLong(table[i].value); |
| 9102 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 9103 | Py_XDECREF(o); |
| 9104 | Py_DECREF(d); |
| 9105 | return -1; |
| 9106 | } |
| 9107 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9108 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9109 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9112 | /* Return -1 on failure, 0 on success. */ |
| 9113 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9114 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9115 | { |
| 9116 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9117 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9118 | sizeof(posix_constants_pathconf) |
| 9119 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9120 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9121 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9122 | #endif |
| 9123 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9124 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9125 | sizeof(posix_constants_confstr) |
| 9126 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9127 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9128 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9129 | #endif |
| 9130 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9131 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9132 | sizeof(posix_constants_sysconf) |
| 9133 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 9134 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9135 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9136 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9137 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9138 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 9139 | |
| 9140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9141 | PyDoc_STRVAR(posix_abort__doc__, |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 9142 | "abort() -> does not return!\n\n\ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9143 | 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] | 9144 | in the hardest way possible on the hosting operating system."); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9145 | |
| 9146 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9147 | posix_abort(PyObject *self, PyObject *noargs) |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9148 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9149 | abort(); |
| 9150 | /*NOTREACHED*/ |
| 9151 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 9152 | return NULL; |
| 9153 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 9154 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9155 | #ifdef MS_WINDOWS |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9156 | PyDoc_STRVAR(win32_startfile__doc__, |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9157 | "startfile(filepath [, operation]) - Start a file with its associated\n\ |
| 9158 | application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9159 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 9160 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 9161 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 9162 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 9163 | application (if any) its extension is associated.\n\ |
| 9164 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 9165 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9166 | \n\ |
| 9167 | startfile returns as soon as the associated application is launched.\n\ |
| 9168 | There is no option to wait for the application to close, and no way\n\ |
| 9169 | to retrieve the application's exit status.\n\ |
| 9170 | \n\ |
| 9171 | The filepath is relative to the current directory. If you want to use\n\ |
| 9172 | 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] | 9173 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9174 | |
| 9175 | static PyObject * |
| 9176 | win32_startfile(PyObject *self, PyObject *args) |
| 9177 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9178 | PyObject *ofilepath; |
| 9179 | char *filepath; |
| 9180 | char *operation = NULL; |
| 9181 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 9182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9183 | PyObject *unipath, *woperation = NULL; |
| 9184 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 9185 | &unipath, &operation)) { |
| 9186 | PyErr_Clear(); |
| 9187 | goto normal; |
| 9188 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9189 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9190 | if (operation) { |
| 9191 | woperation = PyUnicode_DecodeASCII(operation, |
| 9192 | strlen(operation), NULL); |
| 9193 | if (!woperation) { |
| 9194 | PyErr_Clear(); |
| 9195 | operation = NULL; |
| 9196 | goto normal; |
| 9197 | } |
| 9198 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 9199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9200 | Py_BEGIN_ALLOW_THREADS |
| 9201 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, |
| 9202 | PyUnicode_AS_UNICODE(unipath), |
| 9203 | NULL, NULL, SW_SHOWNORMAL); |
| 9204 | Py_END_ALLOW_THREADS |
| 9205 | |
| 9206 | Py_XDECREF(woperation); |
| 9207 | if (rc <= (HINSTANCE)32) { |
| 9208 | PyObject *errval = win32_error_unicode("startfile", |
| 9209 | PyUnicode_AS_UNICODE(unipath)); |
| 9210 | return errval; |
| 9211 | } |
| 9212 | Py_INCREF(Py_None); |
| 9213 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9214 | |
| 9215 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9216 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 9217 | PyUnicode_FSConverter, &ofilepath, |
| 9218 | &operation)) |
| 9219 | return NULL; |
| 9220 | filepath = PyBytes_AsString(ofilepath); |
| 9221 | Py_BEGIN_ALLOW_THREADS |
| 9222 | rc = ShellExecute((HWND)0, operation, filepath, |
| 9223 | NULL, NULL, SW_SHOWNORMAL); |
| 9224 | Py_END_ALLOW_THREADS |
| 9225 | if (rc <= (HINSTANCE)32) { |
| 9226 | PyObject *errval = win32_error("startfile", filepath); |
| 9227 | Py_DECREF(ofilepath); |
| 9228 | return errval; |
| 9229 | } |
| 9230 | Py_DECREF(ofilepath); |
| 9231 | Py_INCREF(Py_None); |
| 9232 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 9233 | } |
| 9234 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 9235 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9236 | #ifdef HAVE_GETLOADAVG |
| 9237 | PyDoc_STRVAR(posix_getloadavg__doc__, |
| 9238 | "getloadavg() -> (float, float, float)\n\n\ |
| 9239 | Return the number of processes in the system run queue averaged over\n\ |
| 9240 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\ |
| 9241 | was unobtainable"); |
| 9242 | |
| 9243 | static PyObject * |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9244 | posix_getloadavg(PyObject *self, PyObject *noargs) |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9245 | { |
| 9246 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9247 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9248 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 9249 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9250 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 9251 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9252 | } |
| 9253 | #endif |
| 9254 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9255 | #ifdef MS_WINDOWS |
| 9256 | |
| 9257 | PyDoc_STRVAR(win32_urandom__doc__, |
| 9258 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9259 | Return n random bytes suitable for cryptographic use."); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9260 | |
| 9261 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 9262 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 9263 | DWORD dwFlags ); |
| 9264 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 9265 | BYTE *pbBuffer ); |
| 9266 | |
| 9267 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 9268 | /* This handle is never explicitly released. Instead, the operating |
| 9269 | system will release it when the process terminates. */ |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9270 | static HCRYPTPROV hCryptProv = 0; |
| 9271 | |
Tim Peters | 4ad8217 | 2004-08-30 17:02:04 +0000 | [diff] [blame] | 9272 | static PyObject* |
| 9273 | win32_urandom(PyObject *self, PyObject *args) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9274 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9275 | int howMany; |
| 9276 | PyObject* result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9277 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9278 | /* Read arguments */ |
| 9279 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9280 | return NULL; |
| 9281 | if (howMany < 0) |
| 9282 | return PyErr_Format(PyExc_ValueError, |
| 9283 | "negative argument not allowed"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9284 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9285 | if (hCryptProv == 0) { |
| 9286 | HINSTANCE hAdvAPI32 = NULL; |
| 9287 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9288 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9289 | /* Obtain handle to the DLL containing CryptoAPI |
| 9290 | This should not fail */ |
| 9291 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 9292 | if(hAdvAPI32 == NULL) |
| 9293 | return win32_error("GetModuleHandle", NULL); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | /* Obtain pointers to the CryptoAPI functions |
| 9296 | This will fail on some early versions of Win95 */ |
| 9297 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 9298 | hAdvAPI32, |
| 9299 | "CryptAcquireContextA"); |
| 9300 | if (pCryptAcquireContext == NULL) |
| 9301 | return PyErr_Format(PyExc_NotImplementedError, |
| 9302 | "CryptAcquireContextA not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9303 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9304 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( |
| 9305 | hAdvAPI32, "CryptGenRandom"); |
| 9306 | if (pCryptGenRandom == NULL) |
| 9307 | return PyErr_Format(PyExc_NotImplementedError, |
| 9308 | "CryptGenRandom not found"); |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9310 | /* Acquire context */ |
| 9311 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 9312 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 9313 | return win32_error("CryptAcquireContext", NULL); |
| 9314 | } |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | /* Allocate bytes */ |
| 9317 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9318 | if (result != NULL) { |
| 9319 | /* Get random data */ |
| 9320 | memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */ |
| 9321 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) |
| 9322 | PyBytes_AS_STRING(result))) { |
| 9323 | Py_DECREF(result); |
| 9324 | return win32_error("CryptGenRandom", NULL); |
| 9325 | } |
| 9326 | } |
| 9327 | return result; |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 9328 | } |
| 9329 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 9330 | |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9331 | PyDoc_STRVAR(device_encoding__doc__, |
| 9332 | "device_encoding(fd) -> str\n\n\ |
| 9333 | Return a string describing the encoding of the device\n\ |
| 9334 | if the output is a terminal; else return None."); |
| 9335 | |
| 9336 | static PyObject * |
| 9337 | device_encoding(PyObject *self, PyObject *args) |
| 9338 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9339 | int fd; |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9340 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
| 9341 | UINT cp; |
| 9342 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9343 | if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) |
| 9344 | return NULL; |
| 9345 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 9346 | Py_INCREF(Py_None); |
| 9347 | return Py_None; |
| 9348 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9349 | #if defined(MS_WINDOWS) || defined(MS_WIN64) |
Victor Stinner | 7870bdf | 2011-05-23 18:12:52 +0200 | [diff] [blame] | 9350 | if (fd == 0) |
| 9351 | cp = GetConsoleCP(); |
| 9352 | else if (fd == 1 || fd == 2) |
| 9353 | cp = GetConsoleOutputCP(); |
| 9354 | else |
| 9355 | cp = 0; |
| 9356 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 9357 | has no console */ |
| 9358 | if (cp != 0) |
| 9359 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9360 | #elif defined(CODESET) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9361 | { |
| 9362 | char *codeset = nl_langinfo(CODESET); |
| 9363 | if (codeset != NULL && codeset[0] != 0) |
| 9364 | return PyUnicode_FromString(codeset); |
| 9365 | } |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9366 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | Py_INCREF(Py_None); |
| 9368 | return Py_None; |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9371 | #ifdef __VMS |
| 9372 | /* Use openssl random routine */ |
| 9373 | #include <openssl/rand.h> |
| 9374 | PyDoc_STRVAR(vms_urandom__doc__, |
| 9375 | "urandom(n) -> str\n\n\ |
Neal Norwitz | 93c5682 | 2007-08-26 07:10:06 +0000 | [diff] [blame] | 9376 | Return n random bytes suitable for cryptographic use."); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9377 | |
| 9378 | static PyObject* |
| 9379 | vms_urandom(PyObject *self, PyObject *args) |
| 9380 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | int howMany; |
| 9382 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9384 | /* Read arguments */ |
| 9385 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) |
| 9386 | return NULL; |
| 9387 | if (howMany < 0) |
| 9388 | return PyErr_Format(PyExc_ValueError, |
| 9389 | "negative argument not allowed"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9390 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9391 | /* Allocate bytes */ |
| 9392 | result = PyBytes_FromStringAndSize(NULL, howMany); |
| 9393 | if (result != NULL) { |
| 9394 | /* Get random data */ |
| 9395 | if (RAND_pseudo_bytes((unsigned char*) |
| 9396 | PyBytes_AS_STRING(result), |
| 9397 | howMany) < 0) { |
| 9398 | Py_DECREF(result); |
| 9399 | return PyErr_Format(PyExc_ValueError, |
| 9400 | "RAND_pseudo_bytes"); |
| 9401 | } |
| 9402 | } |
| 9403 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9404 | } |
| 9405 | #endif |
| 9406 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9407 | #ifdef HAVE_SETRESUID |
| 9408 | PyDoc_STRVAR(posix_setresuid__doc__, |
| 9409 | "setresuid(ruid, euid, suid)\n\n\ |
| 9410 | Set the current process's real, effective, and saved user ids."); |
| 9411 | |
| 9412 | static PyObject* |
| 9413 | posix_setresuid (PyObject *self, PyObject *args) |
| 9414 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9415 | /* We assume uid_t is no larger than a long. */ |
| 9416 | long ruid, euid, suid; |
| 9417 | if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid)) |
| 9418 | return NULL; |
| 9419 | if (setresuid(ruid, euid, suid) < 0) |
| 9420 | return posix_error(); |
| 9421 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9422 | } |
| 9423 | #endif |
| 9424 | |
| 9425 | #ifdef HAVE_SETRESGID |
| 9426 | PyDoc_STRVAR(posix_setresgid__doc__, |
| 9427 | "setresgid(rgid, egid, sgid)\n\n\ |
| 9428 | Set the current process's real, effective, and saved group ids."); |
| 9429 | |
| 9430 | static PyObject* |
| 9431 | posix_setresgid (PyObject *self, PyObject *args) |
| 9432 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9433 | /* We assume uid_t is no larger than a long. */ |
| 9434 | long rgid, egid, sgid; |
| 9435 | if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid)) |
| 9436 | return NULL; |
| 9437 | if (setresgid(rgid, egid, sgid) < 0) |
| 9438 | return posix_error(); |
| 9439 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9440 | } |
| 9441 | #endif |
| 9442 | |
| 9443 | #ifdef HAVE_GETRESUID |
| 9444 | PyDoc_STRVAR(posix_getresuid__doc__, |
| 9445 | "getresuid() -> (ruid, euid, suid)\n\n\ |
| 9446 | Get tuple of the current process's real, effective, and saved user ids."); |
| 9447 | |
| 9448 | static PyObject* |
| 9449 | posix_getresuid (PyObject *self, PyObject *noargs) |
| 9450 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9451 | uid_t ruid, euid, suid; |
| 9452 | long l_ruid, l_euid, l_suid; |
| 9453 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 9454 | return posix_error(); |
| 9455 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9456 | l_ruid = ruid; |
| 9457 | l_euid = euid; |
| 9458 | l_suid = suid; |
| 9459 | return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9460 | } |
| 9461 | #endif |
| 9462 | |
| 9463 | #ifdef HAVE_GETRESGID |
| 9464 | PyDoc_STRVAR(posix_getresgid__doc__, |
| 9465 | "getresgid() -> (rgid, egid, sgid)\n\n\ |
Georg Brandl | a9b51d2 | 2010-09-05 17:07:12 +0000 | [diff] [blame] | 9466 | 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] | 9467 | |
| 9468 | static PyObject* |
| 9469 | posix_getresgid (PyObject *self, PyObject *noargs) |
| 9470 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9471 | uid_t rgid, egid, sgid; |
| 9472 | long l_rgid, l_egid, l_sgid; |
| 9473 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 9474 | return posix_error(); |
| 9475 | /* Force the values into long's as we don't know the size of uid_t. */ |
| 9476 | l_rgid = rgid; |
| 9477 | l_egid = egid; |
| 9478 | l_sgid = sgid; |
| 9479 | return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 9480 | } |
| 9481 | #endif |
| 9482 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9483 | /* Posix *at family of functions: |
| 9484 | faccessat, fchmodat, fchownat, fstatat, futimesat, |
| 9485 | linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat, |
| 9486 | unlinkat, utimensat, mkfifoat */ |
| 9487 | |
| 9488 | #ifdef HAVE_FACCESSAT |
| 9489 | PyDoc_STRVAR(posix_faccessat__doc__, |
| 9490 | "faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\ |
| 9491 | Like access() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9492 | flags is optional and can be constructed by ORing together zero or more\n\ |
| 9493 | of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\ |
| 9494 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9495 | is interpreted relative to the current working directory."); |
| 9496 | |
| 9497 | static PyObject * |
| 9498 | posix_faccessat(PyObject *self, PyObject *args) |
| 9499 | { |
| 9500 | PyObject *opath; |
| 9501 | char *path; |
| 9502 | int mode; |
| 9503 | int res; |
| 9504 | int dirfd, flags = 0; |
| 9505 | if (!PyArg_ParseTuple(args, "iO&i|i:faccessat", |
| 9506 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9507 | return NULL; |
| 9508 | path = PyBytes_AsString(opath); |
| 9509 | Py_BEGIN_ALLOW_THREADS |
| 9510 | res = faccessat(dirfd, path, mode, flags); |
| 9511 | Py_END_ALLOW_THREADS |
| 9512 | Py_DECREF(opath); |
| 9513 | return PyBool_FromLong(res == 0); |
| 9514 | } |
| 9515 | #endif |
| 9516 | |
| 9517 | #ifdef HAVE_FCHMODAT |
| 9518 | PyDoc_STRVAR(posix_fchmodat__doc__, |
| 9519 | "fchmodat(dirfd, path, mode, flags=0)\n\n\ |
| 9520 | Like chmod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9521 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9522 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9523 | is interpreted relative to the current working directory."); |
| 9524 | |
| 9525 | static PyObject * |
| 9526 | posix_fchmodat(PyObject *self, PyObject *args) |
| 9527 | { |
| 9528 | int dirfd, mode, res; |
| 9529 | int flags = 0; |
| 9530 | PyObject *opath; |
| 9531 | char *path; |
| 9532 | |
| 9533 | if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat", |
| 9534 | &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags)) |
| 9535 | return NULL; |
| 9536 | |
| 9537 | path = PyBytes_AsString(opath); |
| 9538 | |
| 9539 | Py_BEGIN_ALLOW_THREADS |
| 9540 | res = fchmodat(dirfd, path, mode, flags); |
| 9541 | Py_END_ALLOW_THREADS |
| 9542 | Py_DECREF(opath); |
| 9543 | if (res < 0) |
| 9544 | return posix_error(); |
| 9545 | Py_RETURN_NONE; |
| 9546 | } |
| 9547 | #endif /* HAVE_FCHMODAT */ |
| 9548 | |
| 9549 | #ifdef HAVE_FCHOWNAT |
| 9550 | PyDoc_STRVAR(posix_fchownat__doc__, |
| 9551 | "fchownat(dirfd, path, uid, gid, flags=0)\n\n\ |
| 9552 | Like chown() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9553 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9554 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9555 | is interpreted relative to the current working directory."); |
| 9556 | |
| 9557 | static PyObject * |
| 9558 | posix_fchownat(PyObject *self, PyObject *args) |
| 9559 | { |
| 9560 | PyObject *opath; |
| 9561 | int dirfd, res; |
| 9562 | long uid, gid; |
| 9563 | int flags = 0; |
| 9564 | char *path; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9565 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9566 | if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat", |
| 9567 | &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags)) |
| 9568 | return NULL; |
| 9569 | |
| 9570 | path = PyBytes_AsString(opath); |
| 9571 | |
| 9572 | Py_BEGIN_ALLOW_THREADS |
| 9573 | res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags); |
| 9574 | Py_END_ALLOW_THREADS |
| 9575 | Py_DECREF(opath); |
| 9576 | if (res < 0) |
| 9577 | return posix_error(); |
| 9578 | Py_RETURN_NONE; |
| 9579 | } |
| 9580 | #endif /* HAVE_FCHOWNAT */ |
| 9581 | |
| 9582 | #ifdef HAVE_FSTATAT |
| 9583 | PyDoc_STRVAR(posix_fstatat__doc__, |
| 9584 | "fstatat(dirfd, path, flags=0) -> stat result\n\n\ |
| 9585 | Like stat() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9586 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9587 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9588 | is interpreted relative to the current working directory."); |
| 9589 | |
| 9590 | static PyObject * |
| 9591 | posix_fstatat(PyObject *self, PyObject *args) |
| 9592 | { |
| 9593 | PyObject *opath; |
| 9594 | char *path; |
| 9595 | STRUCT_STAT st; |
| 9596 | int dirfd, res, flags = 0; |
| 9597 | |
| 9598 | if (!PyArg_ParseTuple(args, "iO&|i:fstatat", |
| 9599 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9600 | return NULL; |
| 9601 | path = PyBytes_AsString(opath); |
| 9602 | |
| 9603 | Py_BEGIN_ALLOW_THREADS |
| 9604 | res = fstatat(dirfd, path, &st, flags); |
| 9605 | Py_END_ALLOW_THREADS |
| 9606 | Py_DECREF(opath); |
| 9607 | if (res != 0) |
| 9608 | return posix_error(); |
| 9609 | |
| 9610 | return _pystat_fromstructstat(&st); |
| 9611 | } |
| 9612 | #endif |
| 9613 | |
| 9614 | #ifdef HAVE_FUTIMESAT |
| 9615 | PyDoc_STRVAR(posix_futimesat__doc__, |
| 9616 | "futimesat(dirfd, path, (atime, mtime))\n\ |
| 9617 | futimesat(dirfd, path, None)\n\n\ |
| 9618 | Like utime() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9619 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9620 | is interpreted relative to the current working directory."); |
| 9621 | |
| 9622 | static PyObject * |
| 9623 | posix_futimesat(PyObject *self, PyObject *args) |
| 9624 | { |
| 9625 | PyObject *opath; |
| 9626 | char *path; |
| 9627 | int res, dirfd; |
| 9628 | PyObject* arg; |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9629 | time_t atime, mtime; |
| 9630 | long ausec, musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9631 | |
| 9632 | if (!PyArg_ParseTuple(args, "iO&O:futimesat", |
| 9633 | &dirfd, PyUnicode_FSConverter, &opath, &arg)) |
| 9634 | return NULL; |
| 9635 | path = PyBytes_AsString(opath); |
| 9636 | if (arg == Py_None) { |
| 9637 | /* optional time values not given */ |
| 9638 | Py_BEGIN_ALLOW_THREADS |
| 9639 | res = futimesat(dirfd, path, NULL); |
| 9640 | Py_END_ALLOW_THREADS |
| 9641 | } |
| 9642 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { |
| 9643 | PyErr_SetString(PyExc_TypeError, |
| 9644 | "futimesat() arg 3 must be a tuple (atime, mtime)"); |
| 9645 | Py_DECREF(opath); |
| 9646 | return NULL; |
| 9647 | } |
| 9648 | else { |
| 9649 | if (extract_time(PyTuple_GET_ITEM(arg, 0), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9650 | &atime, &ausec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9651 | Py_DECREF(opath); |
| 9652 | return NULL; |
| 9653 | } |
| 9654 | if (extract_time(PyTuple_GET_ITEM(arg, 1), |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9655 | &mtime, &musec) == -1) { |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9656 | Py_DECREF(opath); |
| 9657 | return NULL; |
| 9658 | } |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9659 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9660 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9661 | { |
| 9662 | #ifdef HAVE_UTIMENSAT |
| 9663 | struct timespec buf[2]; |
| 9664 | buf[0].tv_sec = atime; |
| 9665 | buf[0].tv_nsec = ausec; |
| 9666 | buf[1].tv_sec = mtime; |
| 9667 | buf[1].tv_nsec = musec; |
| 9668 | res = utimensat(dirfd, path, buf, 0); |
| 9669 | #else |
| 9670 | struct timeval buf[2]; |
| 9671 | buf[0].tv_sec = atime; |
| 9672 | buf[0].tv_usec = ausec; |
| 9673 | buf[1].tv_sec = mtime; |
| 9674 | buf[1].tv_usec = musec; |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9675 | res = futimesat(dirfd, path, buf); |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 9676 | #endif |
| 9677 | } |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 9678 | Py_END_ALLOW_THREADS |
| 9679 | } |
| 9680 | Py_DECREF(opath); |
| 9681 | if (res < 0) { |
| 9682 | return posix_error(); |
| 9683 | } |
| 9684 | Py_RETURN_NONE; |
| 9685 | } |
| 9686 | #endif |
| 9687 | |
| 9688 | #ifdef HAVE_LINKAT |
| 9689 | PyDoc_STRVAR(posix_linkat__doc__, |
| 9690 | "linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\ |
| 9691 | Like link() but if srcpath is relative, it is taken as relative to srcfd\n\ |
| 9692 | and if dstpath is relative, it is taken as relative to dstfd.\n\ |
| 9693 | flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\ |
| 9694 | If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\ |
| 9695 | srcpath is interpreted relative to the current working directory. This\n\ |
| 9696 | also applies for dstpath."); |
| 9697 | |
| 9698 | static PyObject * |
| 9699 | posix_linkat(PyObject *self, PyObject *args) |
| 9700 | { |
| 9701 | PyObject *osrc, *odst; |
| 9702 | char *src, *dst; |
| 9703 | int res, srcfd, dstfd; |
| 9704 | int flags = 0; |
| 9705 | |
| 9706 | if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat", |
| 9707 | &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags)) |
| 9708 | return NULL; |
| 9709 | src = PyBytes_AsString(osrc); |
| 9710 | dst = PyBytes_AsString(odst); |
| 9711 | Py_BEGIN_ALLOW_THREADS |
| 9712 | res = linkat(srcfd, src, dstfd, dst, flags); |
| 9713 | Py_END_ALLOW_THREADS |
| 9714 | Py_DECREF(osrc); |
| 9715 | Py_DECREF(odst); |
| 9716 | if (res < 0) |
| 9717 | return posix_error(); |
| 9718 | Py_RETURN_NONE; |
| 9719 | } |
| 9720 | #endif /* HAVE_LINKAT */ |
| 9721 | |
| 9722 | #ifdef HAVE_MKDIRAT |
| 9723 | PyDoc_STRVAR(posix_mkdirat__doc__, |
| 9724 | "mkdirat(dirfd, path, mode=0o777)\n\n\ |
| 9725 | Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9726 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9727 | is interpreted relative to the current working directory."); |
| 9728 | |
| 9729 | static PyObject * |
| 9730 | posix_mkdirat(PyObject *self, PyObject *args) |
| 9731 | { |
| 9732 | int res, dirfd; |
| 9733 | PyObject *opath; |
| 9734 | char *path; |
| 9735 | int mode = 0777; |
| 9736 | |
| 9737 | if (!PyArg_ParseTuple(args, "iO&|i:mkdirat", |
| 9738 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 9739 | return NULL; |
| 9740 | path = PyBytes_AsString(opath); |
| 9741 | Py_BEGIN_ALLOW_THREADS |
| 9742 | res = mkdirat(dirfd, path, mode); |
| 9743 | Py_END_ALLOW_THREADS |
| 9744 | Py_DECREF(opath); |
| 9745 | if (res < 0) |
| 9746 | return posix_error(); |
| 9747 | Py_RETURN_NONE; |
| 9748 | } |
| 9749 | #endif |
| 9750 | |
| 9751 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 9752 | PyDoc_STRVAR(posix_mknodat__doc__, |
| 9753 | "mknodat(dirfd, path, mode=0o600, device=0)\n\n\ |
| 9754 | Like mknod() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9755 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9756 | is interpreted relative to the current working directory."); |
| 9757 | |
| 9758 | static PyObject * |
| 9759 | posix_mknodat(PyObject *self, PyObject *args) |
| 9760 | { |
| 9761 | PyObject *opath; |
| 9762 | char *filename; |
| 9763 | int mode = 0600; |
| 9764 | int device = 0; |
| 9765 | int res, dirfd; |
| 9766 | if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd, |
| 9767 | PyUnicode_FSConverter, &opath, &mode, &device)) |
| 9768 | return NULL; |
| 9769 | filename = PyBytes_AS_STRING(opath); |
| 9770 | Py_BEGIN_ALLOW_THREADS |
| 9771 | res = mknodat(dirfd, filename, mode, device); |
| 9772 | Py_END_ALLOW_THREADS |
| 9773 | Py_DECREF(opath); |
| 9774 | if (res < 0) |
| 9775 | return posix_error(); |
| 9776 | Py_RETURN_NONE; |
| 9777 | } |
| 9778 | #endif |
| 9779 | |
| 9780 | #ifdef HAVE_OPENAT |
| 9781 | PyDoc_STRVAR(posix_openat__doc__, |
| 9782 | "openat(dirfd, path, flag, mode=0o777) -> fd\n\n\ |
| 9783 | Like open() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9784 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9785 | is interpreted relative to the current working directory."); |
| 9786 | |
| 9787 | static PyObject * |
| 9788 | posix_openat(PyObject *self, PyObject *args) |
| 9789 | { |
| 9790 | PyObject *ofile; |
| 9791 | char *file; |
| 9792 | int flag, dirfd, fd; |
| 9793 | int mode = 0777; |
| 9794 | |
| 9795 | if (!PyArg_ParseTuple(args, "iO&i|i:openat", |
| 9796 | &dirfd, PyUnicode_FSConverter, &ofile, |
| 9797 | &flag, &mode)) |
| 9798 | return NULL; |
| 9799 | file = PyBytes_AsString(ofile); |
| 9800 | Py_BEGIN_ALLOW_THREADS |
| 9801 | fd = openat(dirfd, file, flag, mode); |
| 9802 | Py_END_ALLOW_THREADS |
| 9803 | Py_DECREF(ofile); |
| 9804 | if (fd < 0) |
| 9805 | return posix_error(); |
| 9806 | return PyLong_FromLong((long)fd); |
| 9807 | } |
| 9808 | #endif |
| 9809 | |
| 9810 | #ifdef HAVE_READLINKAT |
| 9811 | PyDoc_STRVAR(posix_readlinkat__doc__, |
| 9812 | "readlinkat(dirfd, path) -> path\n\n\ |
| 9813 | Like readlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9814 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9815 | is interpreted relative to the current working directory."); |
| 9816 | |
| 9817 | static PyObject * |
| 9818 | posix_readlinkat(PyObject *self, PyObject *args) |
| 9819 | { |
| 9820 | PyObject *v, *opath; |
| 9821 | char buf[MAXPATHLEN]; |
| 9822 | char *path; |
| 9823 | int n, dirfd; |
| 9824 | int arg_is_unicode = 0; |
| 9825 | |
| 9826 | if (!PyArg_ParseTuple(args, "iO&:readlinkat", |
| 9827 | &dirfd, PyUnicode_FSConverter, &opath)) |
| 9828 | return NULL; |
| 9829 | path = PyBytes_AsString(opath); |
| 9830 | v = PySequence_GetItem(args, 1); |
| 9831 | if (v == NULL) { |
| 9832 | Py_DECREF(opath); |
| 9833 | return NULL; |
| 9834 | } |
| 9835 | |
| 9836 | if (PyUnicode_Check(v)) { |
| 9837 | arg_is_unicode = 1; |
| 9838 | } |
| 9839 | Py_DECREF(v); |
| 9840 | |
| 9841 | Py_BEGIN_ALLOW_THREADS |
| 9842 | n = readlinkat(dirfd, path, buf, (int) sizeof buf); |
| 9843 | Py_END_ALLOW_THREADS |
| 9844 | Py_DECREF(opath); |
| 9845 | if (n < 0) |
| 9846 | return posix_error(); |
| 9847 | |
| 9848 | if (arg_is_unicode) |
| 9849 | return PyUnicode_DecodeFSDefaultAndSize(buf, n); |
| 9850 | else |
| 9851 | return PyBytes_FromStringAndSize(buf, n); |
| 9852 | } |
| 9853 | #endif /* HAVE_READLINKAT */ |
| 9854 | |
| 9855 | #ifdef HAVE_RENAMEAT |
| 9856 | PyDoc_STRVAR(posix_renameat__doc__, |
| 9857 | "renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\ |
| 9858 | Like rename() but if oldpath is relative, it is taken as relative to\n\ |
| 9859 | olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\ |
| 9860 | If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\ |
| 9861 | oldpath is interpreted relative to the current working directory. This\n\ |
| 9862 | also applies for newpath."); |
| 9863 | |
| 9864 | static PyObject * |
| 9865 | posix_renameat(PyObject *self, PyObject *args) |
| 9866 | { |
| 9867 | int res; |
| 9868 | PyObject *opathold, *opathnew; |
| 9869 | char *opath, *npath; |
| 9870 | int oldfd, newfd; |
| 9871 | |
| 9872 | if (!PyArg_ParseTuple(args, "iO&iO&:renameat", |
| 9873 | &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew)) |
| 9874 | return NULL; |
| 9875 | opath = PyBytes_AsString(opathold); |
| 9876 | npath = PyBytes_AsString(opathnew); |
| 9877 | Py_BEGIN_ALLOW_THREADS |
| 9878 | res = renameat(oldfd, opath, newfd, npath); |
| 9879 | Py_END_ALLOW_THREADS |
| 9880 | Py_DECREF(opathold); |
| 9881 | Py_DECREF(opathnew); |
| 9882 | if (res < 0) |
| 9883 | return posix_error(); |
| 9884 | Py_RETURN_NONE; |
| 9885 | } |
| 9886 | #endif |
| 9887 | |
| 9888 | #if HAVE_SYMLINKAT |
| 9889 | PyDoc_STRVAR(posix_symlinkat__doc__, |
| 9890 | "symlinkat(src, dstfd, dst)\n\n\ |
| 9891 | Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\ |
| 9892 | If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\ |
| 9893 | is interpreted relative to the current working directory."); |
| 9894 | |
| 9895 | static PyObject * |
| 9896 | posix_symlinkat(PyObject *self, PyObject *args) |
| 9897 | { |
| 9898 | int res, dstfd; |
| 9899 | PyObject *osrc, *odst; |
| 9900 | char *src, *dst; |
| 9901 | |
| 9902 | if (!PyArg_ParseTuple(args, "O&iO&:symlinkat", |
| 9903 | PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst)) |
| 9904 | return NULL; |
| 9905 | src = PyBytes_AsString(osrc); |
| 9906 | dst = PyBytes_AsString(odst); |
| 9907 | Py_BEGIN_ALLOW_THREADS |
| 9908 | res = symlinkat(src, dstfd, dst); |
| 9909 | Py_END_ALLOW_THREADS |
| 9910 | Py_DECREF(osrc); |
| 9911 | Py_DECREF(odst); |
| 9912 | if (res < 0) |
| 9913 | return posix_error(); |
| 9914 | Py_RETURN_NONE; |
| 9915 | } |
| 9916 | #endif /* HAVE_SYMLINKAT */ |
| 9917 | |
| 9918 | #ifdef HAVE_UNLINKAT |
| 9919 | PyDoc_STRVAR(posix_unlinkat__doc__, |
| 9920 | "unlinkat(dirfd, path, flags=0)\n\n\ |
| 9921 | Like unlink() but if path is relative, it is taken as relative to dirfd.\n\ |
| 9922 | flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\ |
| 9923 | specified, unlinkat() behaves like rmdir().\n\ |
| 9924 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9925 | is interpreted relative to the current working directory."); |
| 9926 | |
| 9927 | static PyObject * |
| 9928 | posix_unlinkat(PyObject *self, PyObject *args) |
| 9929 | { |
| 9930 | int dirfd, res, flags = 0; |
| 9931 | PyObject *opath; |
| 9932 | char *path; |
| 9933 | |
| 9934 | if (!PyArg_ParseTuple(args, "iO&|i:unlinkat", |
| 9935 | &dirfd, PyUnicode_FSConverter, &opath, &flags)) |
| 9936 | return NULL; |
| 9937 | path = PyBytes_AsString(opath); |
| 9938 | Py_BEGIN_ALLOW_THREADS |
| 9939 | res = unlinkat(dirfd, path, flags); |
| 9940 | Py_END_ALLOW_THREADS |
| 9941 | Py_DECREF(opath); |
| 9942 | if (res < 0) |
| 9943 | return posix_error(); |
| 9944 | Py_RETURN_NONE; |
| 9945 | } |
| 9946 | #endif |
| 9947 | |
| 9948 | #ifdef HAVE_UTIMENSAT |
| 9949 | PyDoc_STRVAR(posix_utimensat__doc__, |
| 9950 | "utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ |
| 9951 | (mtime_sec, mtime_nsec), flags)\n\ |
| 9952 | utimensat(dirfd, path, None, None, flags)\n\n\ |
| 9953 | Updates the timestamps of a file with nanosecond precision. If path is\n\ |
| 9954 | relative, it is taken as relative to dirfd.\n\ |
| 9955 | The second form sets atime and mtime to the current time.\n\ |
| 9956 | flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ |
| 9957 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 9958 | is interpreted relative to the current working directory.\n\ |
| 9959 | If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\ |
| 9960 | current time.\n\ |
| 9961 | If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); |
| 9962 | |
| 9963 | static PyObject * |
| 9964 | posix_utimensat(PyObject *self, PyObject *args) |
| 9965 | { |
| 9966 | PyObject *opath; |
| 9967 | char *path; |
| 9968 | int res, dirfd, flags = 0; |
| 9969 | PyObject *atime, *mtime; |
| 9970 | |
| 9971 | struct timespec buf[2]; |
| 9972 | |
| 9973 | if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", |
| 9974 | &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) |
| 9975 | return NULL; |
| 9976 | path = PyBytes_AsString(opath); |
| 9977 | if (atime == Py_None && mtime == Py_None) { |
| 9978 | /* optional time values not given */ |
| 9979 | Py_BEGIN_ALLOW_THREADS |
| 9980 | res = utimensat(dirfd, path, NULL, flags); |
| 9981 | Py_END_ALLOW_THREADS |
| 9982 | } |
| 9983 | else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) { |
| 9984 | PyErr_SetString(PyExc_TypeError, |
| 9985 | "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)"); |
| 9986 | Py_DECREF(opath); |
| 9987 | return NULL; |
| 9988 | } |
| 9989 | else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) { |
| 9990 | PyErr_SetString(PyExc_TypeError, |
| 9991 | "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)"); |
| 9992 | Py_DECREF(opath); |
| 9993 | return NULL; |
| 9994 | } |
| 9995 | else { |
| 9996 | if (!PyArg_ParseTuple(atime, "ll:utimensat", |
| 9997 | &(buf[0].tv_sec), &(buf[0].tv_nsec))) { |
| 9998 | Py_DECREF(opath); |
| 9999 | return NULL; |
| 10000 | } |
| 10001 | if (!PyArg_ParseTuple(mtime, "ll:utimensat", |
| 10002 | &(buf[1].tv_sec), &(buf[1].tv_nsec))) { |
| 10003 | Py_DECREF(opath); |
| 10004 | return NULL; |
| 10005 | } |
| 10006 | Py_BEGIN_ALLOW_THREADS |
| 10007 | res = utimensat(dirfd, path, buf, flags); |
| 10008 | Py_END_ALLOW_THREADS |
| 10009 | } |
| 10010 | Py_DECREF(opath); |
| 10011 | if (res < 0) { |
| 10012 | return posix_error(); |
| 10013 | } |
| 10014 | Py_RETURN_NONE; |
| 10015 | } |
| 10016 | #endif |
| 10017 | |
| 10018 | #ifdef HAVE_MKFIFOAT |
| 10019 | PyDoc_STRVAR(posix_mkfifoat__doc__, |
| 10020 | "mkfifoat(dirfd, path, mode=0o666)\n\n\ |
| 10021 | Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\ |
| 10022 | If path is relative and dirfd is the special value AT_FDCWD, then path\n\ |
| 10023 | is interpreted relative to the current working directory."); |
| 10024 | |
| 10025 | static PyObject * |
| 10026 | posix_mkfifoat(PyObject *self, PyObject *args) |
| 10027 | { |
| 10028 | PyObject *opath; |
| 10029 | char *filename; |
| 10030 | int mode = 0666; |
| 10031 | int res, dirfd; |
| 10032 | if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat", |
| 10033 | &dirfd, PyUnicode_FSConverter, &opath, &mode)) |
| 10034 | return NULL; |
| 10035 | filename = PyBytes_AS_STRING(opath); |
| 10036 | Py_BEGIN_ALLOW_THREADS |
| 10037 | res = mkfifoat(dirfd, filename, mode); |
| 10038 | Py_END_ALLOW_THREADS |
| 10039 | Py_DECREF(opath); |
| 10040 | if (res < 0) |
| 10041 | return posix_error(); |
| 10042 | Py_RETURN_NONE; |
| 10043 | } |
| 10044 | #endif |
| 10045 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10046 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10047 | |
| 10048 | static int |
| 10049 | try_getxattr(const char *path, const char *name, |
| 10050 | ssize_t (*get)(const char *, const char *, void *, size_t), |
| 10051 | Py_ssize_t buf_size, PyObject **res) |
| 10052 | { |
| 10053 | PyObject *value; |
| 10054 | Py_ssize_t len; |
| 10055 | |
| 10056 | assert(buf_size <= XATTR_SIZE_MAX); |
| 10057 | value = PyBytes_FromStringAndSize(NULL, buf_size); |
| 10058 | if (!value) |
| 10059 | return 0; |
| 10060 | Py_BEGIN_ALLOW_THREADS; |
| 10061 | len = get(path, name, PyBytes_AS_STRING(value), buf_size); |
| 10062 | Py_END_ALLOW_THREADS; |
| 10063 | if (len < 0) { |
| 10064 | Py_DECREF(value); |
| 10065 | if (errno == ERANGE) { |
| 10066 | value = NULL; |
| 10067 | } |
| 10068 | else { |
| 10069 | posix_error(); |
| 10070 | return 0; |
| 10071 | } |
| 10072 | } |
| 10073 | else if (len != buf_size) { |
| 10074 | /* Can only shrink. */ |
| 10075 | _PyBytes_Resize(&value, len); |
| 10076 | } |
| 10077 | *res = value; |
| 10078 | return 1; |
| 10079 | } |
| 10080 | |
| 10081 | static PyObject * |
| 10082 | getxattr_common(const char *path, PyObject *name_obj, |
| 10083 | ssize_t (*get)(const char *, const char *, void *, size_t)) |
| 10084 | { |
| 10085 | PyObject *value; |
| 10086 | const char *name = PyBytes_AS_STRING(name_obj); |
| 10087 | |
| 10088 | /* Try a small value first. */ |
| 10089 | if (!try_getxattr(path, name, get, 128, &value)) |
| 10090 | return NULL; |
| 10091 | if (value) |
| 10092 | return value; |
| 10093 | /* Now the maximum possible one. */ |
| 10094 | if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value)) |
| 10095 | return NULL; |
| 10096 | assert(value); |
| 10097 | return value; |
| 10098 | } |
| 10099 | |
| 10100 | PyDoc_STRVAR(posix_getxattr__doc__, |
| 10101 | "getxattr(path, attr) -> value\n\n\ |
| 10102 | Return the value of extended attribute *name* on *path*."); |
| 10103 | |
| 10104 | static PyObject * |
| 10105 | posix_getxattr(PyObject *self, PyObject *args) |
| 10106 | { |
| 10107 | PyObject *path, *res, *name; |
| 10108 | |
| 10109 | if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path, |
| 10110 | PyUnicode_FSConverter, &name)) |
| 10111 | return NULL; |
| 10112 | res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr); |
| 10113 | Py_DECREF(path); |
| 10114 | Py_DECREF(name); |
| 10115 | return res; |
| 10116 | } |
| 10117 | |
| 10118 | PyDoc_STRVAR(posix_lgetxattr__doc__, |
| 10119 | "lgetxattr(path, attr) -> value\n\n\ |
| 10120 | Like getxattr but don't follow symlinks."); |
| 10121 | |
| 10122 | static PyObject * |
| 10123 | posix_lgetxattr(PyObject *self, PyObject *args) |
| 10124 | { |
| 10125 | PyObject *path, *res, *name; |
| 10126 | |
| 10127 | if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path, |
| 10128 | PyUnicode_FSConverter, &name)) |
| 10129 | return NULL; |
| 10130 | res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr); |
| 10131 | Py_DECREF(path); |
| 10132 | Py_DECREF(name); |
| 10133 | return res; |
| 10134 | } |
| 10135 | |
| 10136 | static ssize_t |
| 10137 | wrap_fgetxattr(const char *path, const char *name, void *value, size_t size) |
| 10138 | { |
| 10139 | /* Hack to share code. */ |
| 10140 | return fgetxattr((int)(Py_uintptr_t)path, name, value, size); |
| 10141 | } |
| 10142 | |
| 10143 | PyDoc_STRVAR(posix_fgetxattr__doc__, |
| 10144 | "fgetxattr(fd, attr) -> value\n\n\ |
| 10145 | Like getxattr but operate on a fd instead of a path."); |
| 10146 | |
| 10147 | static PyObject * |
| 10148 | posix_fgetxattr(PyObject *self, PyObject *args) |
| 10149 | { |
| 10150 | PyObject *res, *name; |
| 10151 | int fd; |
| 10152 | |
| 10153 | if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name)) |
| 10154 | return NULL; |
| 10155 | res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr); |
| 10156 | Py_DECREF(name); |
| 10157 | return res; |
| 10158 | } |
| 10159 | |
| 10160 | PyDoc_STRVAR(posix_setxattr__doc__, |
| 10161 | "setxattr(path, attr, value, flags=0)\n\n\ |
| 10162 | Set extended attribute *attr* on *path* to *value*."); |
| 10163 | |
| 10164 | static PyObject * |
| 10165 | posix_setxattr(PyObject *self, PyObject *args) |
| 10166 | { |
| 10167 | PyObject *path, *name; |
| 10168 | Py_buffer data; |
| 10169 | int flags = 0, err; |
| 10170 | |
| 10171 | if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter, |
| 10172 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10173 | return NULL; |
| 10174 | Py_BEGIN_ALLOW_THREADS; |
| 10175 | err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10176 | data.buf, data.len, flags); |
| 10177 | Py_END_ALLOW_THREADS; |
| 10178 | Py_DECREF(path); |
| 10179 | Py_DECREF(name); |
| 10180 | PyBuffer_Release(&data); |
| 10181 | if (err) |
| 10182 | return posix_error(); |
| 10183 | Py_RETURN_NONE; |
| 10184 | } |
| 10185 | |
| 10186 | PyDoc_STRVAR(posix_lsetxattr__doc__, |
| 10187 | "lsetxattr(path, attr, value, flags=0)\n\n\ |
| 10188 | Like setxattr but don't follow symlinks."); |
| 10189 | |
| 10190 | static PyObject * |
| 10191 | posix_lsetxattr(PyObject *self, PyObject *args) |
| 10192 | { |
| 10193 | PyObject *path, *name; |
| 10194 | Py_buffer data; |
| 10195 | int flags = 0, err; |
| 10196 | |
| 10197 | if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter, |
| 10198 | &path, PyUnicode_FSConverter, &name, &data, &flags)) |
| 10199 | return NULL; |
| 10200 | Py_BEGIN_ALLOW_THREADS; |
| 10201 | err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name), |
| 10202 | data.buf, data.len, flags); |
| 10203 | Py_END_ALLOW_THREADS; |
| 10204 | Py_DECREF(path); |
| 10205 | Py_DECREF(name); |
| 10206 | PyBuffer_Release(&data); |
| 10207 | if (err) |
| 10208 | return posix_error(); |
| 10209 | Py_RETURN_NONE; |
| 10210 | } |
| 10211 | |
| 10212 | PyDoc_STRVAR(posix_fsetxattr__doc__, |
| 10213 | "fsetxattr(fd, attr, value, flags=0)\n\n\ |
| 10214 | Like setxattr but operates on *fd* instead of a path."); |
| 10215 | |
| 10216 | static PyObject * |
| 10217 | posix_fsetxattr(PyObject *self, PyObject *args) |
| 10218 | { |
| 10219 | Py_buffer data; |
| 10220 | const char *name; |
| 10221 | int fd, flags = 0, err; |
| 10222 | |
| 10223 | if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter, |
| 10224 | &name, &data, &flags)) |
| 10225 | return NULL; |
| 10226 | Py_BEGIN_ALLOW_THREADS; |
| 10227 | err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags); |
| 10228 | Py_END_ALLOW_THREADS; |
| 10229 | Py_DECREF(name); |
| 10230 | PyBuffer_Release(&data); |
| 10231 | if (err) |
| 10232 | return posix_error(); |
| 10233 | Py_RETURN_NONE; |
| 10234 | } |
| 10235 | |
| 10236 | PyDoc_STRVAR(posix_removexattr__doc__, |
| 10237 | "removexattr(path, attr)\n\n\ |
| 10238 | Remove extended attribute *attr* on *path*."); |
| 10239 | |
| 10240 | static PyObject * |
| 10241 | posix_removexattr(PyObject *self, PyObject *args) |
| 10242 | { |
| 10243 | PyObject *path, *name; |
| 10244 | int err; |
| 10245 | |
| 10246 | if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path, |
| 10247 | PyUnicode_FSConverter, &name)) |
| 10248 | return NULL; |
| 10249 | Py_BEGIN_ALLOW_THREADS; |
| 10250 | err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10251 | Py_END_ALLOW_THREADS; |
| 10252 | Py_DECREF(path); |
| 10253 | Py_DECREF(name); |
| 10254 | if (err) |
| 10255 | return posix_error(); |
| 10256 | Py_RETURN_NONE; |
| 10257 | } |
| 10258 | |
| 10259 | PyDoc_STRVAR(posix_lremovexattr__doc__, |
| 10260 | "lremovexattr(path, attr)\n\n\ |
| 10261 | Like removexattr but don't follow symlinks."); |
| 10262 | |
| 10263 | static PyObject * |
| 10264 | posix_lremovexattr(PyObject *self, PyObject *args) |
| 10265 | { |
| 10266 | PyObject *path, *name; |
| 10267 | int err; |
| 10268 | |
| 10269 | if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path, |
| 10270 | PyUnicode_FSConverter, &name)) |
| 10271 | return NULL; |
| 10272 | Py_BEGIN_ALLOW_THREADS; |
| 10273 | err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name)); |
| 10274 | Py_END_ALLOW_THREADS; |
| 10275 | Py_DECREF(path); |
| 10276 | Py_DECREF(name); |
| 10277 | if (err) |
| 10278 | return posix_error(); |
| 10279 | Py_RETURN_NONE; |
| 10280 | } |
| 10281 | |
| 10282 | PyDoc_STRVAR(posix_fremovexattr__doc__, |
| 10283 | "fremovexattr(fd, attr)\n\n\ |
| 10284 | Like removexattr but operates on a file descriptor."); |
| 10285 | |
| 10286 | static PyObject * |
| 10287 | posix_fremovexattr(PyObject *self, PyObject *args) |
| 10288 | { |
| 10289 | PyObject *name; |
| 10290 | int fd, err; |
| 10291 | |
| 10292 | if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd, |
| 10293 | PyUnicode_FSConverter, &name)) |
| 10294 | return NULL; |
| 10295 | Py_BEGIN_ALLOW_THREADS; |
| 10296 | err = fremovexattr(fd, PyBytes_AS_STRING(name)); |
| 10297 | Py_END_ALLOW_THREADS; |
| 10298 | Py_DECREF(name); |
| 10299 | if (err) |
| 10300 | return posix_error(); |
| 10301 | Py_RETURN_NONE; |
| 10302 | } |
| 10303 | |
| 10304 | static Py_ssize_t |
| 10305 | try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t), |
| 10306 | Py_ssize_t buf_size, char **buf) |
| 10307 | { |
| 10308 | Py_ssize_t len; |
| 10309 | |
| 10310 | *buf = PyMem_MALLOC(buf_size); |
| 10311 | if (!*buf) { |
| 10312 | PyErr_NoMemory(); |
| 10313 | return -1; |
| 10314 | } |
| 10315 | Py_BEGIN_ALLOW_THREADS; |
| 10316 | len = list(path, *buf, buf_size); |
| 10317 | Py_END_ALLOW_THREADS; |
| 10318 | if (len < 0) { |
| 10319 | PyMem_FREE(*buf); |
| 10320 | if (errno != ERANGE) |
| 10321 | posix_error(); |
| 10322 | return -1; |
| 10323 | } |
| 10324 | return len; |
| 10325 | } |
| 10326 | |
| 10327 | static PyObject * |
| 10328 | listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t)) |
| 10329 | { |
| 10330 | PyObject *res, *attr; |
| 10331 | Py_ssize_t len, err, start, i; |
| 10332 | char *buf; |
| 10333 | |
| 10334 | len = try_listxattr(path, list, 256, &buf); |
| 10335 | if (len < 0) { |
| 10336 | if (PyErr_Occurred()) |
| 10337 | return NULL; |
| 10338 | len = try_listxattr(path, list, XATTR_LIST_MAX, &buf); |
| 10339 | if (len < 0) |
| 10340 | return NULL; |
| 10341 | } |
| 10342 | res = PyList_New(0); |
| 10343 | if (!res) { |
| 10344 | PyMem_FREE(buf); |
| 10345 | return NULL; |
| 10346 | } |
| 10347 | for (start = i = 0; i < len; i++) { |
| 10348 | if (!buf[i]) { |
| 10349 | attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start); |
| 10350 | if (!attr) { |
| 10351 | Py_DECREF(res); |
| 10352 | PyMem_FREE(buf); |
| 10353 | return NULL; |
| 10354 | } |
| 10355 | err = PyList_Append(res, attr); |
| 10356 | Py_DECREF(attr); |
| 10357 | if (err) { |
| 10358 | Py_DECREF(res); |
| 10359 | PyMem_FREE(buf); |
| 10360 | return NULL; |
| 10361 | } |
| 10362 | start = i + 1; |
| 10363 | } |
| 10364 | } |
| 10365 | PyMem_FREE(buf); |
| 10366 | return res; |
| 10367 | } |
| 10368 | |
| 10369 | PyDoc_STRVAR(posix_listxattr__doc__, |
| 10370 | "listxattr(path)\n\n\ |
| 10371 | Return a list of extended attributes on *path*."); |
| 10372 | |
| 10373 | static PyObject * |
| 10374 | posix_listxattr(PyObject *self, PyObject *args) |
| 10375 | { |
| 10376 | PyObject *path, *res; |
| 10377 | |
| 10378 | if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path)) |
| 10379 | return NULL; |
| 10380 | res = listxattr_common(PyBytes_AS_STRING(path), listxattr); |
| 10381 | Py_DECREF(path); |
| 10382 | return res; |
| 10383 | } |
| 10384 | |
| 10385 | PyDoc_STRVAR(posix_llistxattr__doc__, |
| 10386 | "llistxattr(path)\n\n\ |
| 10387 | Like listxattr but don't follow symlinks.."); |
| 10388 | |
| 10389 | static PyObject * |
| 10390 | posix_llistxattr(PyObject *self, PyObject *args) |
| 10391 | { |
| 10392 | PyObject *path, *res; |
| 10393 | |
| 10394 | if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path)) |
| 10395 | return NULL; |
| 10396 | res = listxattr_common(PyBytes_AS_STRING(path), llistxattr); |
| 10397 | Py_DECREF(path); |
| 10398 | return res; |
| 10399 | } |
| 10400 | |
| 10401 | static ssize_t |
| 10402 | wrap_flistxattr(const char *path, char *buf, size_t len) |
| 10403 | { |
| 10404 | /* Hack to share code. */ |
| 10405 | return flistxattr((int)(Py_uintptr_t)path, buf, len); |
| 10406 | } |
| 10407 | |
| 10408 | PyDoc_STRVAR(posix_flistxattr__doc__, |
| 10409 | "flistxattr(path)\n\n\ |
| 10410 | Like flistxattr but operates on a file descriptor."); |
| 10411 | |
| 10412 | static PyObject * |
| 10413 | posix_flistxattr(PyObject *self, PyObject *args) |
| 10414 | { |
| 10415 | long fd; |
| 10416 | |
| 10417 | if (!PyArg_ParseTuple(args, "i:flistxattr", &fd)) |
| 10418 | return NULL; |
| 10419 | return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr); |
| 10420 | } |
| 10421 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10422 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10423 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10424 | static PyMethodDef posix_methods[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10425 | {"access", posix_access, METH_VARARGS, posix_access__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10426 | #ifdef HAVE_TTYNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10427 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10428 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10429 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10430 | #ifdef HAVE_CHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10431 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10432 | #endif /* HAVE_CHFLAGS */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10433 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10434 | #ifdef HAVE_FCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10435 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10436 | #endif /* HAVE_FCHMOD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10437 | #ifdef HAVE_CHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10438 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10439 | #endif /* HAVE_CHOWN */ |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10440 | #ifdef HAVE_LCHMOD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10441 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10442 | #endif /* HAVE_LCHMOD */ |
| 10443 | #ifdef HAVE_FCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10444 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 10445 | #endif /* HAVE_FCHOWN */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10446 | #ifdef HAVE_LCHFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10447 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 10448 | #endif /* HAVE_LCHFLAGS */ |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10449 | #ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10450 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 10451 | #endif /* HAVE_LCHOWN */ |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10452 | #ifdef HAVE_CHROOT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10453 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 10454 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10455 | #ifdef HAVE_CTERMID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10456 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10457 | #endif |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10458 | #ifdef HAVE_GETCWD |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10459 | {"getcwd", (PyCFunction)posix_getcwd_unicode, |
| 10460 | METH_NOARGS, posix_getcwd__doc__}, |
| 10461 | {"getcwdb", (PyCFunction)posix_getcwd_bytes, |
| 10462 | METH_NOARGS, posix_getcwdb__doc__}, |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 10463 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10464 | #ifdef HAVE_LINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10465 | {"link", posix_link, METH_VARARGS, posix_link__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10466 | #endif /* HAVE_LINK */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10467 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 10468 | #ifdef HAVE_FDOPENDIR |
| 10469 | {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__}, |
| 10470 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10471 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, |
| 10472 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10473 | #ifdef HAVE_NICE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10474 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10475 | #endif /* HAVE_NICE */ |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 10476 | #ifdef HAVE_GETPRIORITY |
| 10477 | {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__}, |
| 10478 | #endif /* HAVE_GETPRIORITY */ |
| 10479 | #ifdef HAVE_SETPRIORITY |
| 10480 | {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__}, |
| 10481 | #endif /* HAVE_SETPRIORITY */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10482 | #ifdef HAVE_READLINK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10483 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10484 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10485 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10486 | {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10487 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 10488 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, |
| 10489 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, |
| 10490 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10491 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10492 | #if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10493 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10494 | #endif /* HAVE_SYMLINK */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10495 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10496 | {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS, |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10497 | win_symlink__doc__}, |
| 10498 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10499 | #ifdef HAVE_SYSTEM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10500 | {"system", posix_system, METH_VARARGS, posix_system__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10501 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10503 | #ifdef HAVE_UNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10504 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10505 | #endif /* HAVE_UNAME */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10506 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, |
| 10507 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, |
| 10508 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10509 | #ifdef HAVE_FUTIMES |
| 10510 | {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__}, |
| 10511 | #endif |
| 10512 | #ifdef HAVE_LUTIMES |
| 10513 | {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__}, |
| 10514 | #endif |
| 10515 | #ifdef HAVE_FUTIMENS |
| 10516 | {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__}, |
| 10517 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10518 | #ifdef HAVE_TIMES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10519 | {"times", posix_times, METH_NOARGS, posix_times__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10520 | #endif /* HAVE_TIMES */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10521 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10522 | #ifdef HAVE_EXECV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, |
| 10524 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10525 | #endif /* HAVE_EXECV */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10526 | #ifdef HAVE_FEXECVE |
| 10527 | {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__}, |
| 10528 | #endif |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10529 | #ifdef HAVE_SPAWNV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10530 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, |
| 10531 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10532 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10533 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, |
| 10534 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 10535 | #endif /* PYOS_OS2 */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 10536 | #endif /* HAVE_SPAWNV */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10537 | #ifdef HAVE_FORK1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10538 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 10539 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10540 | #ifdef HAVE_FORK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10541 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10542 | #endif /* HAVE_FORK */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10543 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10544 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10545 | {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__}, |
| 10546 | {"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] | 10547 | #endif |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10548 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10549 | {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10550 | #endif |
| 10551 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10552 | {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10553 | #endif |
| 10554 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10555 | {"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] | 10556 | #endif |
| 10557 | #ifdef HAVE_SCHED_SETPARAM |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10558 | {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10559 | #endif |
| 10560 | #ifdef HAVE_SCHED_SETSCHEDULER |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10561 | {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__}, |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 10562 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10563 | {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__}, |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 10564 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 10565 | {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__}, |
| 10566 | {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__}, |
| 10567 | #endif |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 10568 | #endif /* HAVE_SCHED_H */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10569 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10570 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 10571 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10572 | #ifdef HAVE_FORKPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 10574 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10575 | #ifdef HAVE_GETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10576 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10577 | #endif /* HAVE_GETEGID */ |
| 10578 | #ifdef HAVE_GETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10579 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10580 | #endif /* HAVE_GETEUID */ |
| 10581 | #ifdef HAVE_GETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10582 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10583 | #endif /* HAVE_GETGID */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 10584 | #ifdef HAVE_GETGROUPLIST |
| 10585 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 10586 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10587 | #ifdef HAVE_GETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10588 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10589 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10590 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10591 | #ifdef HAVE_GETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10592 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10593 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10594 | #ifdef HAVE_GETPPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10595 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10596 | #endif /* HAVE_GETPPID */ |
| 10597 | #ifdef HAVE_GETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10598 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10599 | #endif /* HAVE_GETUID */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10600 | #ifdef HAVE_GETLOGIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10601 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10602 | #endif |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10603 | #ifdef HAVE_KILL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10604 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10605 | #endif /* HAVE_KILL */ |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10606 | #ifdef HAVE_KILLPG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10607 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, |
Martin v. Löwis | b2c92f4 | 2002-02-16 23:35:41 +0000 | [diff] [blame] | 10608 | #endif /* HAVE_KILLPG */ |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10609 | #ifdef HAVE_PLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10610 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 10611 | #endif /* HAVE_PLOCK */ |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10612 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10613 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
| 10614 | {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 10615 | {"link", win32_link, METH_VARARGS, win32_link__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 10616 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10617 | #ifdef HAVE_SETUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10618 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10619 | #endif /* HAVE_SETUID */ |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10620 | #ifdef HAVE_SETEUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10621 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10622 | #endif /* HAVE_SETEUID */ |
| 10623 | #ifdef HAVE_SETEGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10624 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10625 | #endif /* HAVE_SETEGID */ |
| 10626 | #ifdef HAVE_SETREUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10628 | #endif /* HAVE_SETREUID */ |
| 10629 | #ifdef HAVE_SETREGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10630 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 10631 | #endif /* HAVE_SETREGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10632 | #ifdef HAVE_SETGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10633 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10634 | #endif /* HAVE_SETGID */ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10635 | #ifdef HAVE_SETGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10636 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 10637 | #endif /* HAVE_SETGROUPS */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10638 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10639 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 10640 | #endif /* HAVE_INITGROUPS */ |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10641 | #ifdef HAVE_GETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10642 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 10643 | #endif /* HAVE_GETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10644 | #ifdef HAVE_SETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10645 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10646 | #endif /* HAVE_SETPGRP */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10647 | #ifdef HAVE_WAIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10648 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10649 | #endif /* HAVE_WAIT */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10650 | #ifdef HAVE_WAIT3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10651 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10652 | #endif /* HAVE_WAIT3 */ |
| 10653 | #ifdef HAVE_WAIT4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10655 | #endif /* HAVE_WAIT4 */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10656 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 10657 | {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__}, |
| 10658 | #endif |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 10659 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10661 | #endif /* HAVE_WAITPID */ |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10662 | #ifdef HAVE_GETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10664 | #endif /* HAVE_GETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10665 | #ifdef HAVE_SETSID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10667 | #endif /* HAVE_SETSID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10668 | #ifdef HAVE_SETPGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10670 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10671 | #ifdef HAVE_TCGETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10673 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10674 | #ifdef HAVE_TCSETPGRP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 10676 | #endif /* HAVE_TCSETPGRP */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | {"open", posix_open, METH_VARARGS, posix_open__doc__}, |
| 10678 | {"close", posix_close, METH_VARARGS, posix_close__doc__}, |
| 10679 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, |
| 10680 | {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__}, |
| 10681 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, |
| 10682 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10683 | #ifdef HAVE_LOCKF |
| 10684 | {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__}, |
| 10685 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10686 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, |
| 10687 | {"read", posix_read, METH_VARARGS, posix_read__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10688 | #ifdef HAVE_READV |
| 10689 | {"readv", posix_readv, METH_VARARGS, posix_readv__doc__}, |
| 10690 | #endif |
| 10691 | #ifdef HAVE_PREAD |
| 10692 | {"pread", posix_pread, METH_VARARGS, posix_pread__doc__}, |
| 10693 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10694 | {"write", posix_write, METH_VARARGS, posix_write__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10695 | #ifdef HAVE_WRITEV |
| 10696 | {"writev", posix_writev, METH_VARARGS, posix_writev__doc__}, |
| 10697 | #endif |
| 10698 | #ifdef HAVE_PWRITE |
| 10699 | {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__}, |
| 10700 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 10701 | #ifdef HAVE_SENDFILE |
| 10702 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 10703 | posix_sendfile__doc__}, |
| 10704 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, |
| 10706 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10707 | #ifdef HAVE_PIPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10709 | #endif |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10710 | #ifdef HAVE_PIPE2 |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 10711 | {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10712 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10713 | #ifdef HAVE_MKFIFO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10714 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10715 | #endif |
Neal Norwitz | 1169011 | 2002-07-30 01:08:28 +0000 | [diff] [blame] | 10716 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, |
Martin v. Löwis | 06a83e9 | 2002-04-14 10:19:44 +0000 | [diff] [blame] | 10718 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10719 | #ifdef HAVE_DEVICE_MACROS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"major", posix_major, METH_VARARGS, posix_major__doc__}, |
| 10721 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, |
| 10722 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 10723 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10724 | #ifdef HAVE_FTRUNCATE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10726 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10727 | #ifdef HAVE_TRUNCATE |
| 10728 | {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__}, |
| 10729 | #endif |
| 10730 | #ifdef HAVE_POSIX_FALLOCATE |
| 10731 | {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__}, |
| 10732 | #endif |
| 10733 | #ifdef HAVE_POSIX_FADVISE |
| 10734 | {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__}, |
| 10735 | #endif |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10736 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10737 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10738 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10739 | #ifdef HAVE_UNSETENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10740 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10741 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10743 | #ifdef HAVE_FCHDIR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10744 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10745 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10746 | #ifdef HAVE_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10747 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10748 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10749 | #ifdef HAVE_SYNC |
| 10750 | {"sync", posix_sync, METH_NOARGS, posix_sync__doc__}, |
| 10751 | #endif |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10752 | #ifdef HAVE_FDATASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10753 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 10754 | #endif |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10755 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10756 | #ifdef WCOREDUMP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10757 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10758 | #endif /* WCOREDUMP */ |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10759 | #ifdef WIFCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10760 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, |
Martin v. Löwis | 2b41b0d | 2002-05-04 13:13:41 +0000 | [diff] [blame] | 10761 | #endif /* WIFCONTINUED */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10762 | #ifdef WIFSTOPPED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10764 | #endif /* WIFSTOPPED */ |
| 10765 | #ifdef WIFSIGNALED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10766 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10767 | #endif /* WIFSIGNALED */ |
| 10768 | #ifdef WIFEXITED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10770 | #endif /* WIFEXITED */ |
| 10771 | #ifdef WEXITSTATUS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10772 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10773 | #endif /* WEXITSTATUS */ |
| 10774 | #ifdef WTERMSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10775 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10776 | #endif /* WTERMSIG */ |
| 10777 | #ifdef WSTOPSIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10778 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10779 | #endif /* WSTOPSIG */ |
| 10780 | #endif /* HAVE_SYS_WAIT_H */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10781 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10782 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10783 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10784 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10785 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10786 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10787 | #ifdef HAVE_CONFSTR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10788 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10789 | #endif |
| 10790 | #ifdef HAVE_SYSCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10791 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10792 | #endif |
| 10793 | #ifdef HAVE_FPATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10795 | #endif |
| 10796 | #ifdef HAVE_PATHCONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10798 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10799 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 10800 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10801 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10802 | {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 10803 | {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 10804 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10805 | {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10806 | #endif |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10807 | #ifdef HAVE_GETLOADAVG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10808 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 10809 | #endif |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10810 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10811 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 10812 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10813 | #ifdef __VMS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10814 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10815 | #endif |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10816 | #ifdef HAVE_SETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10817 | {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10818 | #endif |
| 10819 | #ifdef HAVE_SETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10820 | {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10821 | #endif |
| 10822 | #ifdef HAVE_GETRESUID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10823 | {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10824 | #endif |
| 10825 | #ifdef HAVE_GETRESGID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10826 | {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 10827 | #endif |
| 10828 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 10829 | /* posix *at family of functions */ |
| 10830 | #ifdef HAVE_FACCESSAT |
| 10831 | {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__}, |
| 10832 | #endif |
| 10833 | #ifdef HAVE_FCHMODAT |
| 10834 | {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__}, |
| 10835 | #endif /* HAVE_FCHMODAT */ |
| 10836 | #ifdef HAVE_FCHOWNAT |
| 10837 | {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__}, |
| 10838 | #endif /* HAVE_FCHOWNAT */ |
| 10839 | #ifdef HAVE_FSTATAT |
| 10840 | {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__}, |
| 10841 | #endif |
| 10842 | #ifdef HAVE_FUTIMESAT |
| 10843 | {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__}, |
| 10844 | #endif |
| 10845 | #ifdef HAVE_LINKAT |
| 10846 | {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__}, |
| 10847 | #endif /* HAVE_LINKAT */ |
| 10848 | #ifdef HAVE_MKDIRAT |
| 10849 | {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__}, |
| 10850 | #endif |
| 10851 | #if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV) |
| 10852 | {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__}, |
| 10853 | #endif |
| 10854 | #ifdef HAVE_OPENAT |
| 10855 | {"openat", posix_openat, METH_VARARGS, posix_openat__doc__}, |
| 10856 | #endif |
| 10857 | #ifdef HAVE_READLINKAT |
| 10858 | {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__}, |
| 10859 | #endif /* HAVE_READLINKAT */ |
| 10860 | #ifdef HAVE_RENAMEAT |
| 10861 | {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__}, |
| 10862 | #endif |
| 10863 | #if HAVE_SYMLINKAT |
| 10864 | {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__}, |
| 10865 | #endif /* HAVE_SYMLINKAT */ |
| 10866 | #ifdef HAVE_UNLINKAT |
| 10867 | {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, |
| 10868 | #endif |
| 10869 | #ifdef HAVE_UTIMENSAT |
| 10870 | {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, |
| 10871 | #endif |
| 10872 | #ifdef HAVE_MKFIFOAT |
| 10873 | {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, |
| 10874 | #endif |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 10875 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 10876 | {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__}, |
| 10877 | {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__}, |
| 10878 | {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__}, |
| 10879 | {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__}, |
| 10880 | {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__}, |
| 10881 | {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__}, |
| 10882 | {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__}, |
| 10883 | {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__}, |
| 10884 | {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__}, |
| 10885 | {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__}, |
| 10886 | {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__}, |
| 10887 | {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__}, |
| 10888 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10889 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10890 | }; |
| 10891 | |
| 10892 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10893 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10894 | ins(PyObject *module, char *symbol, long value) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10895 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10896 | return PyModule_AddIntConstant(module, symbol, value); |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10897 | } |
| 10898 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10899 | #if defined(PYOS_OS2) |
| 10900 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10901 | static int insertvalues(PyObject *module) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10902 | { |
| 10903 | APIRET rc; |
| 10904 | ULONG values[QSV_MAX+1]; |
| 10905 | PyObject *v; |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 10906 | char *ver, tmp[50]; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10907 | |
| 10908 | Py_BEGIN_ALLOW_THREADS |
Andrew MacIntyre | 75e0145 | 2003-04-21 14:19:51 +0000 | [diff] [blame] | 10909 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10910 | Py_END_ALLOW_THREADS |
| 10911 | |
| 10912 | if (rc != NO_ERROR) { |
| 10913 | os2_error(rc); |
| 10914 | return -1; |
| 10915 | } |
| 10916 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10917 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1; |
| 10918 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1; |
| 10919 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1; |
| 10920 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1; |
| 10921 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1; |
| 10922 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1; |
| 10923 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10924 | |
| 10925 | switch (values[QSV_VERSION_MINOR]) { |
| 10926 | case 0: ver = "2.00"; break; |
| 10927 | case 10: ver = "2.10"; break; |
| 10928 | case 11: ver = "2.11"; break; |
| 10929 | case 30: ver = "3.00"; break; |
| 10930 | case 40: ver = "4.00"; break; |
| 10931 | case 50: ver = "5.00"; break; |
| 10932 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10933 | PyOS_snprintf(tmp, sizeof(tmp), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10934 | "%d-%d", values[QSV_VERSION_MAJOR], |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 10935 | values[QSV_VERSION_MINOR]); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10936 | ver = &tmp[0]; |
| 10937 | } |
| 10938 | |
| 10939 | /* Add Indicator of the Version of the Operating System */ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10940 | if (PyModule_AddStringConstant(module, "version", tmp) < 0) |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10941 | return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10942 | |
| 10943 | /* Add Indicator of Which Drive was Used to Boot the System */ |
| 10944 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1; |
| 10945 | tmp[1] = ':'; |
| 10946 | tmp[2] = '\0'; |
| 10947 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 10948 | return PyModule_AddStringConstant(module, "bootdrive", tmp); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 10949 | } |
| 10950 | #endif |
| 10951 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10952 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10953 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10954 | enable_symlink() |
| 10955 | { |
| 10956 | HANDLE tok; |
| 10957 | TOKEN_PRIVILEGES tok_priv; |
| 10958 | LUID luid; |
| 10959 | int meth_idx = 0; |
| 10960 | |
| 10961 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10962 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10963 | |
| 10964 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10965 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10966 | |
| 10967 | tok_priv.PrivilegeCount = 1; |
| 10968 | tok_priv.Privileges[0].Luid = luid; |
| 10969 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 10970 | |
| 10971 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 10972 | sizeof(TOKEN_PRIVILEGES), |
| 10973 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10974 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10975 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 10976 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 10977 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 10978 | } |
| 10979 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 10980 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10981 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 10982 | all_ins(PyObject *d) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 10983 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10984 | #ifdef F_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10985 | if (ins(d, "F_OK", (long)F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10986 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10987 | #ifdef R_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10988 | if (ins(d, "R_OK", (long)R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10989 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10990 | #ifdef W_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10991 | if (ins(d, "W_OK", (long)W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10992 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10993 | #ifdef X_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10994 | if (ins(d, "X_OK", (long)X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 10995 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10996 | #ifdef NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10997 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10998 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 10999 | #ifdef TMP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11000 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11001 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11002 | #ifdef WCONTINUED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11003 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11004 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11005 | #ifdef WNOHANG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11006 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11007 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11008 | #ifdef WUNTRACED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11009 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 11010 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11011 | #ifdef O_RDONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11012 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11013 | #endif |
| 11014 | #ifdef O_WRONLY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11015 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11016 | #endif |
| 11017 | #ifdef O_RDWR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11018 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11019 | #endif |
| 11020 | #ifdef O_NDELAY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11021 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11022 | #endif |
| 11023 | #ifdef O_NONBLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11024 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11025 | #endif |
| 11026 | #ifdef O_APPEND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11027 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11028 | #endif |
| 11029 | #ifdef O_DSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11030 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11031 | #endif |
| 11032 | #ifdef O_RSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11033 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11034 | #endif |
| 11035 | #ifdef O_SYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11036 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11037 | #endif |
| 11038 | #ifdef O_NOCTTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11039 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11040 | #endif |
| 11041 | #ifdef O_CREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11042 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11043 | #endif |
| 11044 | #ifdef O_EXCL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11045 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11046 | #endif |
| 11047 | #ifdef O_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11048 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11049 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11050 | #ifdef O_BINARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11051 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11052 | #endif |
| 11053 | #ifdef O_TEXT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11054 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 11055 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11056 | #ifdef O_LARGEFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11057 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11058 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11059 | #ifdef O_SHLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11060 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11061 | #endif |
| 11062 | #ifdef O_EXLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11063 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 11064 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11065 | #ifdef PRIO_PROCESS |
| 11066 | if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1; |
| 11067 | #endif |
| 11068 | #ifdef PRIO_PGRP |
| 11069 | if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1; |
| 11070 | #endif |
| 11071 | #ifdef PRIO_USER |
| 11072 | if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1; |
| 11073 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 11074 | #ifdef O_CLOEXEC |
| 11075 | if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1; |
| 11076 | #endif |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 11077 | /* posix - constants for *at functions */ |
| 11078 | #ifdef AT_SYMLINK_NOFOLLOW |
| 11079 | if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1; |
| 11080 | #endif |
| 11081 | #ifdef AT_EACCESS |
| 11082 | if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1; |
| 11083 | #endif |
| 11084 | #ifdef AT_FDCWD |
| 11085 | if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1; |
| 11086 | #endif |
| 11087 | #ifdef AT_REMOVEDIR |
| 11088 | if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1; |
| 11089 | #endif |
| 11090 | #ifdef AT_SYMLINK_FOLLOW |
| 11091 | if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1; |
| 11092 | #endif |
| 11093 | #ifdef UTIME_NOW |
| 11094 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11095 | #endif |
| 11096 | #ifdef UTIME_OMIT |
| 11097 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11098 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 11099 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11100 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11101 | /* MS Windows */ |
| 11102 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | /* Don't inherit in child processes. */ |
| 11104 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | /* Optimize for short life (keep in memory). */ |
| 11108 | /* MS forgot to define this one with a non-underscore form too. */ |
| 11109 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | /* Automatically delete when last handle is closed. */ |
| 11113 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | /* Optimize for random access. */ |
| 11117 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11118 | #endif |
| 11119 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11120 | /* Optimize for sequential access. */ |
| 11121 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11124 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11125 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11126 | /* Send a SIGIO signal whenever input or output |
| 11127 | becomes available on file descriptor */ |
| 11128 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 11129 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11130 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | /* Direct disk access. */ |
| 11132 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11133 | #endif |
| 11134 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11135 | /* Must be a directory. */ |
| 11136 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11137 | #endif |
| 11138 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | /* Do not follow links. */ |
| 11140 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 11141 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11142 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | /* Do not update the access time. */ |
| 11144 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 11145 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11147 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11148 | #ifdef EX_OK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | if (ins(d, "EX_OK", (long)EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11150 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11151 | #ifdef EX_USAGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11153 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11154 | #ifdef EX_DATAERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11156 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11157 | #ifdef EX_NOINPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11159 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11160 | #ifdef EX_NOUSER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11162 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11163 | #ifdef EX_NOHOST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11165 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11166 | #ifdef EX_UNAVAILABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11168 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11169 | #ifdef EX_SOFTWARE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11171 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11172 | #ifdef EX_OSERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11174 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11175 | #ifdef EX_OSFILE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11177 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11178 | #ifdef EX_CANTCREAT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11180 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11181 | #ifdef EX_IOERR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11183 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11184 | #ifdef EX_TEMPFAIL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11186 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11187 | #ifdef EX_PROTOCOL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11189 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11190 | #ifdef EX_NOPERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11192 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11193 | #ifdef EX_CONFIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11195 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11196 | #ifdef EX_NOTFOUND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 11198 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 11199 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11200 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11201 | #ifdef ST_RDONLY |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11202 | if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11203 | #endif /* ST_RDONLY */ |
| 11204 | #ifdef ST_NOSUID |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 11205 | if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 11206 | #endif /* ST_NOSUID */ |
| 11207 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11208 | /* FreeBSD sendfile() constants */ |
| 11209 | #ifdef SF_NODISKIO |
| 11210 | if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1; |
| 11211 | #endif |
| 11212 | #ifdef SF_MNOWAIT |
| 11213 | if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1; |
| 11214 | #endif |
| 11215 | #ifdef SF_SYNC |
| 11216 | if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1; |
| 11217 | #endif |
| 11218 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11219 | /* constants for posix_fadvise */ |
| 11220 | #ifdef POSIX_FADV_NORMAL |
| 11221 | if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1; |
| 11222 | #endif |
| 11223 | #ifdef POSIX_FADV_SEQUENTIAL |
| 11224 | if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1; |
| 11225 | #endif |
| 11226 | #ifdef POSIX_FADV_RANDOM |
| 11227 | if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1; |
| 11228 | #endif |
| 11229 | #ifdef POSIX_FADV_NOREUSE |
| 11230 | if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1; |
| 11231 | #endif |
| 11232 | #ifdef POSIX_FADV_WILLNEED |
| 11233 | if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1; |
| 11234 | #endif |
| 11235 | #ifdef POSIX_FADV_DONTNEED |
| 11236 | if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1; |
| 11237 | #endif |
| 11238 | |
| 11239 | /* constants for waitid */ |
| 11240 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
| 11241 | if (ins(d, "P_PID", (long)P_PID)) return -1; |
| 11242 | if (ins(d, "P_PGID", (long)P_PGID)) return -1; |
| 11243 | if (ins(d, "P_ALL", (long)P_ALL)) return -1; |
| 11244 | #endif |
| 11245 | #ifdef WEXITED |
| 11246 | if (ins(d, "WEXITED", (long)WEXITED)) return -1; |
| 11247 | #endif |
| 11248 | #ifdef WNOWAIT |
| 11249 | if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1; |
| 11250 | #endif |
| 11251 | #ifdef WSTOPPED |
| 11252 | if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1; |
| 11253 | #endif |
| 11254 | #ifdef CLD_EXITED |
| 11255 | if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1; |
| 11256 | #endif |
| 11257 | #ifdef CLD_DUMPED |
| 11258 | if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1; |
| 11259 | #endif |
| 11260 | #ifdef CLD_TRAPPED |
| 11261 | if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1; |
| 11262 | #endif |
| 11263 | #ifdef CLD_CONTINUED |
| 11264 | if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1; |
| 11265 | #endif |
| 11266 | |
| 11267 | /* constants for lockf */ |
| 11268 | #ifdef F_LOCK |
| 11269 | if (ins(d, "F_LOCK", (long)F_LOCK)) return -1; |
| 11270 | #endif |
| 11271 | #ifdef F_TLOCK |
| 11272 | if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1; |
| 11273 | #endif |
| 11274 | #ifdef F_ULOCK |
| 11275 | if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1; |
| 11276 | #endif |
| 11277 | #ifdef F_TEST |
| 11278 | if (ins(d, "F_TEST", (long)F_TEST)) return -1; |
| 11279 | #endif |
| 11280 | |
| 11281 | /* constants for futimens */ |
| 11282 | #ifdef UTIME_NOW |
| 11283 | if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1; |
| 11284 | #endif |
| 11285 | #ifdef UTIME_OMIT |
| 11286 | if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1; |
| 11287 | #endif |
| 11288 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11289 | #ifdef HAVE_SPAWNV |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11290 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11291 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; |
| 11292 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; |
| 11293 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; |
| 11294 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; |
| 11295 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; |
| 11296 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; |
| 11297 | if (ins(d, "P_PM", (long)P_PM)) return -1; |
| 11298 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; |
| 11299 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; |
| 11300 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; |
| 11301 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; |
| 11302 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; |
| 11303 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; |
| 11304 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; |
| 11305 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; |
| 11306 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; |
| 11307 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; |
| 11308 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; |
| 11309 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; |
| 11310 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11311 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11312 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; |
| 11313 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; |
| 11314 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; |
| 11315 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; |
| 11316 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11317 | #endif |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 11318 | #endif |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 11319 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11320 | #ifdef HAVE_SCHED_H |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11321 | if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11322 | if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1; |
| 11323 | if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1; |
| 11324 | #ifdef SCHED_SPORADIC |
| 11325 | if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1; |
| 11326 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11327 | #ifdef SCHED_BATCH |
| 11328 | if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1; |
| 11329 | #endif |
| 11330 | #ifdef SCHED_IDLE |
| 11331 | if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1; |
| 11332 | #endif |
| 11333 | #ifdef SCHED_RESET_ON_FORK |
| 11334 | if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1; |
| 11335 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 11336 | #ifdef SCHED_SYS |
| 11337 | if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1; |
| 11338 | #endif |
| 11339 | #ifdef SCHED_IA |
| 11340 | if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1; |
| 11341 | #endif |
| 11342 | #ifdef SCHED_FSS |
| 11343 | if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1; |
| 11344 | #endif |
| 11345 | #ifdef SCHED_FX |
| 11346 | if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1; |
| 11347 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11348 | #endif |
| 11349 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11350 | #ifdef USE_XATTRS |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11351 | if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1; |
| 11352 | if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1; |
| 11353 | if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1; |
| 11354 | #endif |
| 11355 | |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11356 | #if defined(PYOS_OS2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11357 | if (insertvalues(d)) return -1; |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 11358 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11360 | } |
| 11361 | |
| 11362 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11363 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11364 | #define INITFUNC PyInit_nt |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11365 | #define MODNAME "nt" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11366 | |
| 11367 | #elif defined(PYOS_OS2) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11368 | #define INITFUNC PyInit_os2 |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11369 | #define MODNAME "os2" |
Tim Peters | 58e0a8c | 2001-05-14 22:32:33 +0000 | [diff] [blame] | 11370 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 11371 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11372 | #define INITFUNC PyInit_posix |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 11373 | #define MODNAME "posix" |
| 11374 | #endif |
| 11375 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11376 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | PyModuleDef_HEAD_INIT, |
| 11378 | MODNAME, |
| 11379 | posix__doc__, |
| 11380 | -1, |
| 11381 | posix_methods, |
| 11382 | NULL, |
| 11383 | NULL, |
| 11384 | NULL, |
| 11385 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11386 | }; |
| 11387 | |
| 11388 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 11389 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 11390 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11391 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | PyObject *m, *v; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11393 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11394 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 11395 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | m = PyModule_Create(&posixmodule); |
| 11399 | if (m == NULL) |
| 11400 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11402 | /* Initialize environ dictionary */ |
| 11403 | v = convertenviron(); |
| 11404 | Py_XINCREF(v); |
| 11405 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 11406 | return NULL; |
| 11407 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | if (all_ins(m)) |
| 11410 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 11411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | if (setup_confname_tables(m)) |
| 11413 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | Py_INCREF(PyExc_OSError); |
| 11416 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 11417 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11418 | #ifdef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11419 | if (PyType_Ready(&cpu_set_type) < 0) |
| 11420 | return NULL; |
| 11421 | Py_INCREF(&cpu_set_type); |
| 11422 | PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type); |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 11423 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11424 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11425 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11426 | if (posix_putenv_garbage == NULL) |
| 11427 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 11428 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11429 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11431 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11432 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 11433 | PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc); |
| 11434 | #endif |
| 11435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | stat_result_desc.name = MODNAME ".stat_result"; |
| 11437 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 11438 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 11439 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 11440 | PyStructSequence_InitType(&StatResultType, &stat_result_desc); |
| 11441 | structseq_new = StatResultType.tp_new; |
| 11442 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11443 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | statvfs_result_desc.name = MODNAME ".statvfs_result"; |
| 11445 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11446 | #ifdef NEED_TICKS_PER_SECOND |
| 11447 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11449 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11450 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11451 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 11453 | # endif |
| 11454 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11455 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 11456 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11457 | sched_param_desc.name = MODNAME ".sched_param"; |
| 11458 | PyStructSequence_InitType(&SchedParamType, &sched_param_desc); |
| 11459 | SchedParamType.tp_new = sched_param_new; |
| 11460 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11462 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 11463 | Py_INCREF((PyObject*) &WaitidResultType); |
| 11464 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 11465 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | Py_INCREF((PyObject*) &StatResultType); |
| 11467 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 11468 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 11469 | PyModule_AddObject(m, "statvfs_result", |
| 11470 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11471 | |
| 11472 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 11473 | Py_INCREF(&SchedParamType); |
| 11474 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 11475 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | initialized = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11477 | |
| 11478 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | /* |
| 11480 | * Step 2 of weak-linking support on Mac OS X. |
| 11481 | * |
| 11482 | * The code below removes functions that are not available on the |
| 11483 | * currently active platform. |
| 11484 | * |
| 11485 | * This block allow one to use a python binary that was build on |
| 11486 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on |
| 11487 | * OSX 10.4. |
| 11488 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11489 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | if (fstatvfs == NULL) { |
| 11491 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 11492 | return NULL; |
| 11493 | } |
| 11494 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11495 | #endif /* HAVE_FSTATVFS */ |
| 11496 | |
| 11497 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | if (statvfs == NULL) { |
| 11499 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 11500 | return NULL; |
| 11501 | } |
| 11502 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11503 | #endif /* HAVE_STATVFS */ |
| 11504 | |
| 11505 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | if (lchown == NULL) { |
| 11507 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 11508 | return NULL; |
| 11509 | } |
| 11510 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11511 | #endif /* HAVE_LCHOWN */ |
| 11512 | |
| 11513 | |
| 11514 | #endif /* __APPLE__ */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11516 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11517 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11518 | |
| 11519 | #ifdef __cplusplus |
| 11520 | } |
| 11521 | #endif |